From c18588b6d00650b9ce742229fdf1eca7133f58fc Mon Sep 17 00:00:00 2001 From: Peter Carbonetto Date: Thu, 4 May 2017 14:41:32 -0500 Subject: Local updates made by Xiang---shared via email on May 4, 2017, subject: gemma on expression data. --- src/gemma.cpp | 241 +++++++++++++++++++++++++++++++++++++++----- src/io.cpp | 156 +++++++++++++++++++++++++++-- src/io.h | 3 + src/lmm.cpp | 24 +++++ src/mathfunc.cpp | 27 +++++ src/mathfunc.h | 1 + src/param.cpp | 84 ++++++++++++++-- src/param.h | 11 +- src/vc.cpp | 300 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/vc.h | 1 + 10 files changed, 797 insertions(+), 51 deletions(-) diff --git a/src/gemma.cpp b/src/gemma.cpp index 682835f..ca9c4aa 100644 --- a/src/gemma.cpp +++ b/src/gemma.cpp @@ -39,9 +39,12 @@ #include "vc_float.h" #include "lm_float.h" //for LM class #include "bslmm_float.h" //for BSLMM class +#include "bslmmdap_float.h" //for BSLMMDAP class +#include "ldr_float.h" //for LDR class #include "lmm_float.h" //for LMM class, and functions CalcLambda, CalcPve, CalcVgVe #include "mvlmm_float.h" //for MVLMM class #include "prdt_float.h" //for PRDT class +#include "varcov_float.h" //for MVLMM class #include "mathfunc_float.h" //for a few functions #else #include "io.h" @@ -49,9 +52,12 @@ #include "vc.h" #include "lm.h" #include "bslmm.h" +#include "bslmmdap.h" +#include "ldr.h" #include "lmm.h" #include "mvlmm.h" #include "prdt.h" +#include "varcov.h" #include "mathfunc.h" #endif @@ -365,6 +371,8 @@ void GEMMA::PrintHelp(size_t option) cout<<" options: 1: BSLMM"<size, W->size2); gsl_vector *Uty=gsl_vector_alloc (y->size); - //read relatedness matrix G if (!(cPar.file_kin).empty()) { cPar.ReadGenotypes (UtX, G, false); @@ -2373,17 +2411,20 @@ void GEMMA::BatchRun (PARAM &cPar) CalcUtX (U, UtX); cPar.time_UtX=(clock()-time_start)/(double(CLOCKS_PER_SEC)*60.0); - //perform BSLMM analysis - BSLMM cBslmm; - cBslmm.CopyFromParam(cPar); - time_start=clock(); - if (cPar.a_mode==12) { //ridge regression - cBslmm.RidgeR(U, UtX, Uty, eval, cPar.l_remle_null); - } else { //Run MCMC - cBslmm.MCMC(U, UtX, Uty, eval, y); + //perform BSLMM or BSLMMDAP analysis + if (cPar.a_mode==11 || cPar.a_mode==12 || cPar.a_mode==13) { + BSLMM cBslmm; + cBslmm.CopyFromParam(cPar); + time_start=clock(); + if (cPar.a_mode==12) { //ridge regression + cBslmm.RidgeR(U, UtX, Uty, eval, cPar.l_remle_null); + } else { //Run MCMC + cBslmm.MCMC(U, UtX, Uty, eval, y); + } + cPar.time_opt=(clock()-time_start)/(double(CLOCKS_PER_SEC)*60.0); + cBslmm.CopyToParam(cPar); + } else { } - cPar.time_opt=(clock()-time_start)/(double(CLOCKS_PER_SEC)*60.0); - cBslmm.CopyToParam(cPar); //release all matrices and vectors gsl_matrix_free (G); @@ -2399,8 +2440,157 @@ void GEMMA::BatchRun (PARAM &cPar) } + + //BSLMM-DAP + if (cPar.a_mode==14 || cPar.a_mode==15 || cPar.a_mode==16) { + if (cPar.a_mode==14) { + gsl_vector *y=gsl_vector_alloc (cPar.ni_test); + gsl_matrix *W=gsl_matrix_alloc (y->size, cPar.n_cvt); + gsl_matrix *G=gsl_matrix_alloc (y->size, y->size); + gsl_matrix *UtX=gsl_matrix_alloc (y->size, cPar.ns_test); + + //set covariates matrix W and phenotype vector y + //an intercept should be included in W, + cPar.CopyCvtPhen (W, y, 0); + + //center y, even for case/control data + cPar.pheno_mean=CenterVector(y); + + //run bvsr if rho==1 + if (cPar.rho_min==1 && cPar.rho_max==1) { + //read genotypes X (not UtX) + cPar.ReadGenotypes (UtX, G, false); + + //perform BSLMM analysis + BSLMM cBslmm; + cBslmm.CopyFromParam(cPar); + time_start=clock(); + cBslmm.MCMC(UtX, y); + cPar.time_opt=(clock()-time_start)/(double(CLOCKS_PER_SEC)*60.0); + cBslmm.CopyToParam(cPar); + //else, if rho!=1 + } else { + gsl_matrix *U=gsl_matrix_alloc (y->size, y->size); + gsl_vector *eval=gsl_vector_alloc (y->size); + gsl_matrix *UtW=gsl_matrix_alloc (y->size, W->size2); + gsl_vector *Uty=gsl_vector_alloc (y->size); + + //read relatedness matrix G + if (!(cPar.file_kin).empty()) { + cPar.ReadGenotypes (UtX, G, false); + + //read relatedness matrix G + ReadFile_kin (cPar.file_kin, cPar.indicator_idv, cPar.mapID2num, cPar.k_mode, cPar.error, G); + if (cPar.error==true) {cout<<"error! fail to read kinship/relatedness file. "<size; i++) { + if (gsl_vector_get (eval, i)<1e-10) {gsl_vector_set (eval, i, 0);} + cPar.trace_G+=gsl_vector_get (eval, i); + } + cPar.trace_G/=(double)eval->size; + cPar.time_eigen=(clock()-time_start)/(double(CLOCKS_PER_SEC)*60.0); + + //calculate UtW and Uty + CalcUtX (U, W, UtW); + CalcUtX (U, y, Uty); + + //calculate REMLE/MLE estimate and pve + CalcLambda ('L', eval, UtW, Uty, cPar.l_min, cPar.l_max, cPar.n_region, cPar.l_mle_null, cPar.logl_mle_H0); + CalcLambda ('R', eval, UtW, Uty, cPar.l_min, cPar.l_max, cPar.n_region, cPar.l_remle_null, cPar.logl_remle_H0); + CalcPve (eval, UtW, Uty, cPar.l_remle_null, cPar.trace_G, cPar.pve_null, cPar.pve_se_null); + + cPar.PrintSummary(); + + //Creat and calcualte UtX, use a large memory + cout<<"Calculating UtX..."< vec_rs; + vector vec_sa2, vec_sb2, wab; + vector > > BF; + + //read hyp and bf files (functions defined in BSLMMDAP) + ReadFile_hyb (cPar.file_hyp, vec_sa2, vec_sb2, wab); + ReadFile_bf (cPar.file_bf, vec_rs, BF); + + cPar.ns_test=vec_rs.size(); + if (wab.size()!=BF[0][0].size()) {cout<<"error! hyp and bf files dimension do not match"< &setSnps) //read header HEADER header; !safeGetline(infile, line).eof(); - ReadHeader_io (line, header); + ReadHeader (line, header); if (header.rs_col==0 && (header.chr_col==0 || header.pos_col==0) ) { cout<<"missing rs id in the hearder"< &indicator_snp, const int k //read header to determine which column contains which item -bool ReadHeader_io (const string &line, HEADER &header) +bool ReadHeader (const string &line, HEADER &header) { string rs_ptr[]={"rs","RS","snp","SNP","snps","SNPS","snpid","SNPID","rsid","RSID","MarkerName"}; set rs_set(rs_ptr, rs_ptr+11); @@ -2594,8 +2595,17 @@ bool ReadHeader_io (const string &line, HEADER &header) if (header.af_col==0) {header.af_col=header.coln+1;} else {cout<<"error! more than two af columns in the file."< &mapRS2cat, size_ //read header HEADER header; !safeGetline(infile, line).eof(); - ReadHeader_io (line, header); + ReadHeader (line, header); //use the header to count the number of categories n_vc=header.coln; @@ -2715,6 +2725,118 @@ bool ReadFile_mcat (const string &file_mcat, map &mapRS2cat, siz } + + + +/* +//read the continuous category file, record mapR2catc +bool ReadFile_catc (const string &file_cat, map > &mapRS2catc, size_t &n_cat) +{ + mapRS2catc.clear(); + + igzstream infile (file_cat.c_str(), igzstream::in); + if (!infile) {cout<<"error! fail to open category file: "< catc; + for (size_t i=0; i0) {n_vc++;} + + infile.clear(); + infile.close(); + + return true; +} + + + + +bool ReadFile_mcatc (const string &file_mcat, map > &mapRS2catc, size_t &n_cat) +{ + mapRS2catc.clear(); + + igzstream infile (file_mcat.c_str(), igzstream::in); + if (!infile) {cout<<"error! fail to open mcategory file: "< > mapRS2catc_tmp; + size_t n_cat_tmp, t=0; + + while (!safeGetline(infile, file_name).eof()) { + mapRS2catc_tmp.clear(); + ReadFile_catc (file_name, mapRS2catc_tmp, n_cat_tmp); + mapRS2catc.insert(mapRS2catc_tmp.begin(), mapRS2catc_tmp.end()); + if (t==0) {n_cat=n_cat_tmp;} + if (n_cat!=n_cat_tmp) {cout<<"number of category differs in different mcatc files."< &indicator_idv, const vector &indicator_snp, const map &mapRS2weight, const map &mapRS2cat, const vector &snpInfo, const gsl_matrix *W, gsl_matrix *matrix_kin, gsl_vector *vector_ns) { @@ -3208,7 +3330,7 @@ bool ReadFile_wsnp (const string &file_wcat, const size_t n_vc, map &mapRS2ca //read header HEADER header; !safeGetline(infile, line).eof(); - ReadHeader_io (line, header); + ReadHeader (line, header); if (header.n_col==0 ) { if ( (header.nobs_col==0 && header.nmis_col==0) && (header.ncase_col==0 && header.ncontrol_col==0) ) { @@ -3412,7 +3534,7 @@ void ReadFile_beta (const string &file_beta, const map &mapRS2wA //read header HEADER header; !safeGetline(infile, line).eof(); - ReadHeader_io (line, header); + ReadHeader (line, header); if (header.n_col==0 ) { if ( (header.nobs_col==0 && header.nmis_col==0) && (header.ncase_col==0 && header.ncontrol_col==0) ) { @@ -3503,7 +3625,6 @@ void ReadFile_beta (const string &file_beta, const map &mapRS2wA - void Calcq (const size_t n_block, const vector &vec_cat, const vector &vec_ni, const vector &vec_weight, const vector &vec_z2, gsl_matrix *Vq, gsl_vector *q, gsl_vector *s) { gsl_matrix_set_zero (Vq); @@ -3845,6 +3966,21 @@ void ReadFile_mstudy (const string &file_mstudy, gsl_matrix *Vq_mat, gsl_vector return; } + +//copied from lmm.cpp; is used in the following function compKtoV +//map a number 1-(n_cvt+2) to an index between 0 and [(n_c+2)^2+(n_c+2)]/2-1 +size_t GetabIndex (const size_t a, const size_t b, const size_t n_cvt) { + if (a>n_cvt+2 || b>n_cvt+2 || a<=0 || b<=0) {cout<<"error in GetabIndex."<a) {l=a; h=b;} else {l=b; h=a;} + + size_t n=n_cvt+2; + index=(2*n-l+2)*(l-1)/2+h-l; + + return index; +} + //read reference file void ReadFile_mref (const string &file_mref, gsl_matrix *S_mat, gsl_matrix *Svar_mat, gsl_vector *s_vec, size_t &ni) { diff --git a/src/io.h b/src/io.h index 14dfcc9..c1b762d 100644 --- a/src/io.h +++ b/src/io.h @@ -83,6 +83,9 @@ bool ReadHeader (const string &line, HEADER &header); bool ReadFile_cat (const string &file_cat, map &mapRS2cat, size_t &n_vc); bool ReadFile_mcat (const string &file_mcat, map &mapRS2cat, size_t &n_vc); +bool ReadFile_catc (const string &file_cat, map > &mapRS2catc, size_t &n_cat); +bool ReadFile_mcatc (const string &file_mcat, map > &mapRS2catc, size_t &n_cat); + bool BimbamKin (const string &file_geno, const int display_pace, const vector &indicator_idv, const vector &indicator_snp, const map &mapRS2weight, const map &mapRS2cat, const vector &snpInfo, const gsl_matrix *W, gsl_matrix *matrix_kin, gsl_vector *vector_ns); bool PlinkKin (const string &file_bed, const int display_pace, const vector &indicator_idv, const vector &indicator_snp, const map &mapRS2weight, const map &mapRS2cat, const vector &snpInfo, const gsl_matrix *W, gsl_matrix *matrix_kin, gsl_vector *vector_ns); bool MFILEKin (const size_t mfile_mode, const string &file_mfile, const int display_pace, const vector &indicator_idv, const vector > &mindicator_snp, const map &mapRS2weight, const map &mapRS2cat, const vector > &msnpInfo, const gsl_matrix *W, gsl_matrix *matrix_kin, gsl_vector *vector_ns); diff --git a/src/lmm.cpp b/src/lmm.cpp index a707534..044f33c 100644 --- a/src/lmm.cpp +++ b/src/lmm.cpp @@ -183,6 +183,30 @@ void LMM::WriteFiles () return; } + + + + + + + + + + +//map a number 1-(n_cvt+2) to an index between 0 and [(n_c+2)^2+(n_c+2)]/2-1 +size_t GetabIndex (const size_t a, const size_t b, const size_t n_cvt) { + if (a>n_cvt+2 || b>n_cvt+2 || a<=0 || b<=0) {cout<<"error in GetabIndex."<a) {l=a; h=b;} else {l=b; h=a;} + + size_t n=n_cvt+2; + index=(2*n-l+2)*(l-1)/2+h-l; + + return index; +} + + void CalcPab (const size_t n_cvt, const size_t e_mode, const gsl_vector *Hi_eval, const gsl_matrix *Uab, const gsl_vector *ab, gsl_matrix *Pab) { size_t index_ab, index_aw, index_bw, index_ww; diff --git a/src/mathfunc.cpp b/src/mathfunc.cpp index 915245b..4417f8a 100644 --- a/src/mathfunc.cpp +++ b/src/mathfunc.cpp @@ -165,6 +165,33 @@ void CenterMatrix (gsl_matrix *G, const gsl_matrix *W) } +//standardize the matrix G such that all diagonal elements = 1 +void StandardizeMatrix (gsl_matrix *G) +{ + double d=0.0; + vector vec_d; + + for (size_t i=0; isize1; ++i) { + vec_d.push_back(gsl_matrix_get(G, i, i)); + } + for (size_t i=0; isize1; ++i) { + for (size_t j=i; jsize2; ++j) { + if (j==i) { + gsl_matrix_set(G, i, j, 1); + } else { + d=gsl_matrix_get(G, i, j); + d/=sqrt(vec_d[i]*vec_d[j]); + gsl_matrix_set(G, i, j, d); + gsl_matrix_set(G, j, i, d); + } + } + } + + return; +} + + + //scale the matrix G such that the mean diagonal = 1 double ScaleMatrix (gsl_matrix *G) { diff --git a/src/mathfunc.h b/src/mathfunc.h index 98c0e35..84f3186 100644 --- a/src/mathfunc.h +++ b/src/mathfunc.h @@ -30,6 +30,7 @@ double VectorVar (const gsl_vector *v); void CenterMatrix (gsl_matrix *G); void CenterMatrix (gsl_matrix *G, const gsl_vector *w); void CenterMatrix (gsl_matrix *G, const gsl_matrix *W); +void StandardizeMatrix (gsl_matrix *G); double ScaleMatrix (gsl_matrix *G); double CenterVector (gsl_vector *y); void CenterVector (gsl_vector *y, const gsl_matrix *W); diff --git a/src/param.cpp b/src/param.cpp index 0a63a16..4b8c3a4 100644 --- a/src/param.cpp +++ b/src/param.cpp @@ -57,6 +57,7 @@ pheno_mean(0), noconstrain (false), h_min(-1), h_max(-1), h_scale(-1), rho_min(0.0), rho_max(1.0), rho_scale(-1), logp_min(0.0), logp_max(0.0), logp_scale(-1), +h_ngrid(10), rho_ngrid(10), s_min(0), s_max(300), w_step(100000), s_step(1000000), r_pace(10), w_pace(1000), @@ -66,7 +67,7 @@ geo_mean(2000.0), randseed(-1), window_cm(0), window_bp(0), window_ns(0), n_block(200), error(false), -ni_subsample(0), n_cvt(1), n_vc(1), +ni_subsample(0), n_cvt(1), n_vc(1), n_cat(0), time_total(0.0), time_G(0.0), time_eigen(0.0), time_UtX(0.0), time_UtZ(0.0), time_opt(0.0), time_Omega(0.0) {} @@ -76,7 +77,14 @@ time_total(0.0), time_G(0.0), time_eigen(0.0), time_UtX(0.0), time_UtZ(0.0), tim void PARAM::ReadFiles (void) { string file_str; - + /* + //read continuous cat file + if (!file_mcatc.empty()) { + if (ReadFile_mcatc (file_mcatc, mapRS2catc, n_cat)==false) {error=true;} + } else if (!file_catc.empty()) { + if (ReadFile_catc (file_catc, mapRS2catc, n_cat)==false) {error=true;} + } + */ //read cat file if (!file_mcat.empty()) { if (ReadFile_mcat (file_mcat, mapRS2cat, n_vc)==false) {error=true;} @@ -398,7 +406,7 @@ void PARAM::CheckParam (void) //check parameters if (k_mode!=1 && k_mode!=2) {cout<<"error! unknown kinship/relatedness input mode: "<1) {cout<<"error! missing level needs to be between 0 and 1. current value = "<0.5) {cout<<"error! maf level needs to be between 0 and 0.5. current value = "< flag_vec; + vector catc; + + for (size_t j=0; j::size_type i=0; i catc_col; + set catd_col; }; @@ -129,6 +131,7 @@ public: string file_gxe; //optional string file_cvt; //optional string file_cat, file_mcat; + string file_catc, file_mcatc; string file_var; string file_beta; string file_cor; @@ -138,6 +141,7 @@ public: string file_ref, file_mref; string file_weight, file_wsnp, file_wcat; string file_out; + string file_bf, file_hyp; string path_out; @@ -194,6 +198,7 @@ public: double h_min, h_max, h_scale; //priors for h double rho_min, rho_max, rho_scale; //priors for rho double logp_min, logp_max, logp_scale; //priors for log(pi) + size_t h_ngrid, rho_ngrid; size_t s_min, s_max; //minimum and maximum number of gammas size_t w_step; //number of warm up/burn in iterations size_t s_step; //number of sampling iterations @@ -225,6 +230,7 @@ public: size_t ni_subsample; //number of subsampled individuals //size_t ni_total_ref, ns_total_ref, ns_pair;//max number of individuals, number of snps and number of snp pairs in the reference panel size_t n_cvt; //number of covariates + size_t n_cat; //number of continuous categories size_t n_ph; //number of phenotypes size_t n_vc; //number of variance components (including the diagonal matrix) double time_total; //record total time @@ -262,6 +268,7 @@ public: map mapRS2cM; //map rs# to cM map mapRS2est; //map rs# to parameters map mapRS2cat; //map rs# to category number + map > mapRS2catc; //map rs# to continuous categories map mapRS2wsnp; //map rs# to snp weights map > mapRS2wcat; //map rs# to snp cat weights @@ -281,6 +288,7 @@ public: void ReadGenotypes (vector > &Xt, gsl_matrix *K, const bool calc_K); void CheckCvt (); void CopyCvt (gsl_matrix *W); + void CopyA (size_t flag, gsl_matrix *A); void CopyGxe (gsl_vector *gxe); void CopyWeight (gsl_vector *w); void ProcessCvtPhen(); @@ -299,7 +307,6 @@ public: void UpdateSNP (const map &mapRS2wA); }; -size_t GetabIndex (const size_t a, const size_t b, const size_t n_cvt); - + #endif diff --git a/src/vc.cpp b/src/vc.cpp index f17a9e9..c0aa40d 100644 --- a/src/vc.cpp +++ b/src/vc.cpp @@ -453,7 +453,7 @@ int LogRL_dev12 (const gsl_vector *log_sigma2, void *params, gsl_vector *dev1, g //read header to determine which column contains which item -bool ReadHeader_vc (const string &line, HEADER &header) +bool ReadHeader (const string &line, HEADER &header) { string rs_ptr[]={"rs","RS","snp","SNP","snps","SNPS","snpid","SNPID","rsid","RSID"}; set rs_set(rs_ptr, rs_ptr+10); @@ -586,7 +586,7 @@ void ReadFile_cor (const string &file_cor, const set &setSnps, vector &vec_rs, const v HEADER header; !safeGetline(infile, line).eof(); - ReadHeader_vc (line, header); + ReadHeader (line, header); while (!safeGetline(infile, line).eof()) { //do not read cor values this time; upto col_n-1 @@ -1059,6 +1059,25 @@ void ReadFile_cor (const string &file_cor, const vector &vec_rs, const v return; } + + + + +//copied from lmm.cpp; is used in the following function VCss +//map a number 1-(n_cvt+2) to an index between 0 and [(n_c+2)^2+(n_c+2)]/2-1 +size_t GetabIndex (const size_t a, const size_t b, const size_t n_cvt) { + if (a>n_cvt+2 || b>n_cvt+2 || a<=0 || b<=0) {cout<<"error in GetabIndex."<a) {l=a; h=b;} else {l=b; h=a;} + + size_t n=n_cvt+2; + index=(2*n-l+2)*(l-1)/2+h-l; + + return index; +} + + //use the new method to calculate variance components with summary statistics //first, use a function CalcS to compute S matrix (where the diagonal elements are part of V(q) ), and then use bootstrap to compute the variance for S, use a set of genotypes, phenotypes, and individual ids, and snp category label void CalcVCss(const gsl_matrix *Vq, const gsl_matrix *S_mat, const gsl_matrix *Svar_mat, const gsl_vector *q_vec, const gsl_vector *s_vec, const double df, vector &v_pve, vector &v_se_pve, double &pve_total, double &se_pve_total, vector &v_sigma2, vector &v_se_sigma2, vector &v_enrich, vector &v_se_enrich) { @@ -1336,7 +1355,7 @@ void VC::CalcVChe (const gsl_matrix *K, const gsl_matrix *W, const gsl_vector *y gsl_vector_set(q_vec, i, d); } - //compuate yKrKKry, which is used later for confidence interval + //compute yKrKKry, which is used later for confidence interval for (size_t i=0; isize1, n2=K->size2; + size_t n_vc=n2/n1; + + double d, y2_sum, tau_inv, se_tau_inv; + + //new matrices/vectors + gsl_matrix *K_scale=gsl_matrix_alloc (n1, n2); + gsl_vector *y_scale=gsl_vector_alloc (n1); + gsl_vector *y2=gsl_vector_alloc (n1); + gsl_vector *n1_vec=gsl_vector_alloc (n1); + gsl_matrix *Ay=gsl_matrix_alloc (n1, n_vc); + gsl_matrix *K2=gsl_matrix_alloc (n1, n_vc*n_vc); + gsl_matrix *K_tmp=gsl_matrix_alloc (n1, n1); + gsl_matrix *V_mat=gsl_matrix_alloc (n1, n1); + + //old matrices/vectors + gsl_vector *pve=gsl_vector_alloc (n_vc); + gsl_vector *se_pve=gsl_vector_alloc (n_vc); + gsl_vector *q_vec=gsl_vector_alloc (n_vc); + gsl_matrix *S1=gsl_matrix_alloc (n_vc, n_vc); + gsl_matrix *S2=gsl_matrix_alloc (n_vc, n_vc); + gsl_matrix *S_mat=gsl_matrix_alloc (n_vc, n_vc); + gsl_matrix *Si_mat=gsl_matrix_alloc (n_vc, n_vc); + gsl_matrix *J_mat=gsl_matrix_alloc (n_vc, n_vc); + gsl_matrix *Var_mat=gsl_matrix_alloc (n_vc, n_vc); + + int sig; + gsl_permutation * pmt=gsl_permutation_alloc (n_vc); + + //center and scale K by W + //and standardize K further so that all diagonal elements are 1 + for (size_t i=0; isize; i++) { + y2_sum+=gsl_vector_get(y2, i); + } + + //compute the n_vc size q vector + for (size_t i=0; isize1; t++) { + gsl_vector_view Ktmp_col=gsl_matrix_column (K_tmp, t); + gsl_vector_add (n1_vec, &Ktmp_col.vector); + } + gsl_vector_add_constant (n1_vec, -1.0); + + //compute S1 + gsl_blas_ddot (n1_vec, y2, &d); + gsl_matrix_set (S1, i, j, 2*d); + if (i!=j) {gsl_matrix_set (S1, j, i, 2*d);} + + //compute S2 + d=0; + for (size_t t=0; tsize; t++) { + d+=gsl_vector_get (n1_vec, t); + } + gsl_matrix_set (S2, i, j, d); + if (i!=j) {gsl_matrix_set (S2, j, i, d);} + + //save information to compute J + gsl_vector_view K2col1=gsl_matrix_column (K2, n_vc*i+j); + gsl_vector_view K2col2=gsl_matrix_column (K2, n_vc*j+i); + + gsl_vector_memcpy(&K2col1.vector, n1_vec); + if (i!=j) {gsl_vector_memcpy(&K2col2.vector, n1_vec);} + } + } + + //iterate to solve tau and h's + size_t it=0; + double s=1; + while (abs(s)>1e-3 && it<100) { + //update tau_inv + gsl_blas_ddot (q_vec, pve, &d); + if (it>0) {s=y2_sum/(double)n1-d/((double)n1*((double)n1-1))-tau_inv;} + tau_inv=y2_sum/(double)n1-d/((double)n1*((double)n1-1)); + if (it>0) {s/=tau_inv;} + + //update S + gsl_matrix_memcpy (S_mat, S2); + gsl_matrix_scale (S_mat, -1*tau_inv); + gsl_matrix_add (S_mat, S1); + + //update h=S^{-1}q + int sig; + gsl_permutation * pmt=gsl_permutation_alloc (n_vc); + LUDecomp (S_mat, pmt, &sig); + LUInvert (S_mat, pmt, Si_mat); + gsl_blas_dgemv (CblasNoTrans, 1.0, Si_mat, q_vec, 0.0, pve); + + //cout<1) { + cout<<"total pve = "<