From ff19b2bd8f3f5c708c3156e9adf26fa94b4d4ea1 Mon Sep 17 00:00:00 2001 From: Peter Carbonetto Date: Fri, 7 Jul 2017 09:50:23 -0500 Subject: Removed FORCE_FLOAT from mvlmm.cpp. --- src/mvlmm.cpp | 840 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 411 insertions(+), 429 deletions(-) (limited to 'src') diff --git a/src/mvlmm.cpp b/src/mvlmm.cpp index 0049972..1c65dd1 100644 --- a/src/mvlmm.cpp +++ b/src/mvlmm.cpp @@ -1,6 +1,6 @@ /* Genome-wide Efficient Mixed Model Association (GEMMA) - Copyright (C) 2011 Xiang Zhou + Copyright (C) 2011-2017, Xiang Zhou This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,10 +13,8 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program. If not, see . - */ - - + along with this program. If not, see . +*/ #include #include @@ -44,25 +42,13 @@ #include "lapack.h" #include "eigenlib.h" #include "gzstream.h" - -#ifdef FORCE_FLOAT -#include "lmm_float.h" -#include "mvlmm_float.h" -#else #include "lmm.h" #include "mvlmm.h" -#endif - - using namespace std; - -//in this file, X, Y are already transformed (i.e. UtX and UtY) - - -void MVLMM::CopyFromParam (PARAM &cPar) -{ +// In this file, X, Y are already transformed (i.e. UtX and UtY). +void MVLMM::CopyFromParam (PARAM &cPar) { a_mode=cPar.a_mode; d_pace=cPar.d_pace; @@ -105,9 +91,7 @@ void MVLMM::CopyFromParam (PARAM &cPar) return; } - -void MVLMM::CopyToParam (PARAM &cPar) -{ +void MVLMM::CopyToParam (PARAM &cPar) { cPar.time_UtX=time_UtX; cPar.time_opt=time_opt; @@ -131,17 +115,19 @@ void MVLMM::CopyToParam (PARAM &cPar) return; } - -void MVLMM::WriteFiles () -{ +void MVLMM::WriteFiles () { string file_str; file_str=path_out+"/"+file_out; file_str+=".assoc.txt"; ofstream outfile (file_str.c_str(), ofstream::out); - if (!outfile) {cout<<"error writing file: "<size1; double d, logdet_Ve=0.0; - //eigen decomposition of V_e + // Eigen decomposition of V_e. gsl_matrix *Lambda=gsl_matrix_alloc (d_size, d_size); gsl_matrix *V_e_temp=gsl_matrix_alloc (d_size, d_size); gsl_matrix *V_e_h=gsl_matrix_alloc (d_size, d_size); @@ -228,7 +209,7 @@ double EigenProc (const gsl_matrix *V_g, const gsl_matrix *V_e, gsl_vector *D_l, gsl_matrix_memcpy(V_e_temp, V_e); EigenDecomp(V_e_temp, U_l, D_l, 0); - //calculate V_e_h and V_e_hi + // Calculate V_e_h and V_e_hi. gsl_matrix_set_zero(V_e_h); gsl_matrix_set_zero(V_e_hi); for (size_t i=0; isize, d_size=D_l->size, dc_size=Qi->size1; size_t c_size=dc_size/d_size; @@ -323,14 +276,14 @@ double CalcQi (const gsl_vector *eval, const gsl_vector *D_l, const gsl_matrix * dl=gsl_vector_get(D_l, l); if (jsize, c_size=X->size1, d_size=D_l->size; gsl_vector_set_zero (xHiy); @@ -374,20 +328,15 @@ void CalcXHiY(const gsl_vector *eval, const gsl_vector *D_l, const gsl_matrix *X gsl_vector_set(xHiy, j*d_size+i, d); } } - /* - cout<<"xHiy: "<size, d_size=D_l->size; double delta, dl, d_u, d_e; @@ -407,9 +356,8 @@ void CalcOmega (const gsl_vector *eval, const gsl_vector *D_l, gsl_matrix *Omega return; } - -void UpdateU (const gsl_matrix *OmegaE, const gsl_matrix *UltVehiY, const gsl_matrix *UltVehiBX, gsl_matrix *UltVehiU) -{ +void UpdateU (const gsl_matrix *OmegaE, const gsl_matrix *UltVehiY, + const gsl_matrix *UltVehiBX, gsl_matrix *UltVehiU) { gsl_matrix_memcpy (UltVehiU, UltVehiY); gsl_matrix_sub (UltVehiU, UltVehiBX); @@ -417,9 +365,8 @@ void UpdateU (const gsl_matrix *OmegaE, const gsl_matrix *UltVehiY, const gsl_ma return; } - -void UpdateE (const gsl_matrix *UltVehiY, const gsl_matrix *UltVehiBX, const gsl_matrix *UltVehiU, gsl_matrix *UltVehiE) -{ +void UpdateE (const gsl_matrix *UltVehiY, const gsl_matrix *UltVehiBX, + const gsl_matrix *UltVehiU, gsl_matrix *UltVehiE) { gsl_matrix_memcpy (UltVehiE, UltVehiY); gsl_matrix_sub (UltVehiE, UltVehiBX); gsl_matrix_sub (UltVehiE, UltVehiU); @@ -427,10 +374,9 @@ void UpdateE (const gsl_matrix *UltVehiY, const gsl_matrix *UltVehiBX, const gsl return; } - - -void UpdateL_B (const gsl_matrix *X, const gsl_matrix *XXti, const gsl_matrix *UltVehiY, const gsl_matrix *UltVehiU, gsl_matrix *UltVehiBX, gsl_matrix *UltVehiB) -{ +void UpdateL_B (const gsl_matrix *X, const gsl_matrix *XXti, + const gsl_matrix *UltVehiY, const gsl_matrix *UltVehiU, + gsl_matrix *UltVehiBX, gsl_matrix *UltVehiB) { size_t c_size=X->size1, d_size=UltVehiY->size1; gsl_matrix *YUX=gsl_matrix_alloc (d_size, c_size); @@ -438,27 +384,29 @@ void UpdateL_B (const gsl_matrix *X, const gsl_matrix *XXti, const gsl_matrix *U gsl_matrix_memcpy (UltVehiBX, UltVehiY); gsl_matrix_sub (UltVehiBX, UltVehiU); - gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, UltVehiBX, X, 0.0, YUX); - gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, YUX, XXti, 0.0, UltVehiB); + gsl_blas_dgemm(CblasNoTrans,CblasTrans,1.0,UltVehiBX,X,0.0,YUX); + gsl_blas_dgemm(CblasNoTrans,CblasNoTrans,1.0,YUX,XXti,0.0,UltVehiB); gsl_matrix_free(YUX); return; } -void UpdateRL_B (const gsl_vector *xHiy, const gsl_matrix *Qi, gsl_matrix *UltVehiB) -{ - size_t d_size=UltVehiB->size1, c_size=UltVehiB->size2, dc_size=Qi->size1; +void UpdateRL_B (const gsl_vector *xHiy, const gsl_matrix *Qi, + gsl_matrix *UltVehiB) { + size_t d_size=UltVehiB->size1, c_size=UltVehiB->size2, + dc_size=Qi->size1; gsl_vector *b=gsl_vector_alloc (dc_size); - //calculate b=Qiv + // Calculate b=Qiv. gsl_blas_dgemv(CblasNoTrans, 1.0, Qi, xHiy, 0.0, b); - //copy b to UltVehiB + // Copy b to UltVehiB. for (size_t i=0; isize, d_size=U->size1; gsl_matrix_set_zero (V_g); @@ -478,7 +425,7 @@ void UpdateV (const gsl_vector *eval, const gsl_matrix *U, const gsl_matrix *E, double delta; - //calculate the first part: UD^{-1}U^T and EE^T + // Calculate the first part: UD^{-1}U^T and EE^T. for (size_t k=0; ksize, c_size=X->size1, d_size=D_l->size, dc_size=Qi->size1; + size_t n_size=eval->size, c_size=X->size1; + size_t d_size=D_l->size, dc_size=Qi->size1; gsl_matrix_set_zero(Sigma_uu); gsl_matrix_set_zero(Sigma_ee); double delta, dl, x, d; - //calculate the first diagonal term + // Calculate the first diagonal term. gsl_vector_view Suu_diag=gsl_matrix_diagonal (Sigma_uu); gsl_vector_view See_diag=gsl_matrix_diagonal (Sigma_ee); for (size_t k=0; ksize, d_size=D_l->size, dc_size=Qi->size1; double logl=0.0, delta, dl, y, d; - //calculate yHiy+log|H_k| + // Calculate yHiy+log|H_k|. for (size_t k=0; ksize, c_size=X->size1, d_size=Y->size1; size_t dc_size=d_size*c_size; @@ -637,94 +600,88 @@ double MphEM (const char func_name, const size_t max_iter, const double max_prec gsl_vector *xHiy=gsl_vector_alloc (dc_size); gsl_permutation * pmt=gsl_permutation_alloc (c_size); - double logl_const=0.0, logl_old=0.0, logl_new=0.0, logdet_Q, logdet_Ve; + double logl_const=0.0, logl_old=0.0, logl_new=0.0; + double logdet_Q, logdet_Ve; int sig; - //calculate |XXt| and (XXt)^{-1} + // Calculate |XXt| and (XXt)^{-1}. gsl_blas_dsyrk (CblasUpper, CblasNoTrans, 1.0, X, 0.0, XXt); for (size_t i=0; isize, c_size=W->size1, d_size=V_g->size1; size_t dc_size=d_size*c_size; double delta, dl, d, d1, d2, dy, dx, dw, logdet_Ve, logdet_Q, p_value; @@ -764,7 +717,6 @@ double MphCalcP (const gsl_vector *eval, const gsl_vector *x_vec, const gsl_matr gsl_matrix *xPx=gsl_matrix_alloc (d_size, d_size); gsl_vector *xPy=gsl_vector_alloc (d_size); - //gsl_vector *UltVehiy=gsl_vector_alloc (d_size); gsl_vector *WHiy=gsl_vector_alloc (dc_size); gsl_matrix_set_zero (xPx); @@ -772,16 +724,17 @@ double MphCalcP (const gsl_vector *eval, const gsl_vector *x_vec, const gsl_matr gsl_vector_set_zero (xPy); gsl_vector_set_zero (WHiy); - //eigen decomposition and calculate log|Ve| + // Eigen decomposition and calculate log|Ve|. logdet_Ve=EigenProc (V_g, V_e, D_l, UltVeh, UltVehi); - //calculate Qi and log|Q| + // Calculate Qi and log|Q|. logdet_Q=CalcQi (eval, D_l, W, Qi); - //calculate UltVehiY - gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, UltVehi, Y, 0.0, UltVehiY); + // Calculate UltVehiY. + gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, UltVehi, Y, + 0.0, UltVehiY); - //calculate WHix, WHiy, xHiy, xHix + // Calculate WHix, WHiy, xHiy, xHix. for (size_t i=0; isize, c_size=W->size1, d_size=V_g->size1; size_t dc_size=d_size*c_size; double delta, dl, d, dy, dw, logdet_Ve, logdet_Q; @@ -867,7 +819,6 @@ void MphCalcBeta (const gsl_vector *eval, const gsl_matrix *W, const gsl_matrix gsl_matrix *UltVehi=gsl_matrix_alloc (d_size, d_size); gsl_matrix *Qi=gsl_matrix_alloc (dc_size, dc_size); gsl_matrix *Qi_temp=gsl_matrix_alloc (dc_size, dc_size); - //gsl_vector *UltVehiy=gsl_vector_alloc (d_size); gsl_vector *WHiy=gsl_vector_alloc (dc_size); gsl_vector *QiWHiy=gsl_vector_alloc (dc_size); gsl_vector *beta=gsl_vector_alloc (dc_size); @@ -875,16 +826,17 @@ void MphCalcBeta (const gsl_vector *eval, const gsl_matrix *W, const gsl_matrix gsl_vector_set_zero (WHiy); - //eigen decomposition and calculate log|Ve| + // Eigen decomposition and calculate log|Ve|. logdet_Ve=EigenProc (V_g, V_e, D_l, UltVeh, UltVehi); - //calculate Qi and log|Q| + // Calculate Qi and log|Q|. logdet_Q=CalcQi (eval, D_l, W, Qi); - //calculate UltVehiY - gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, UltVehi, Y, 0.0, UltVehiY); + // Calculate UltVehiY. + gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, UltVehi, Y, + 0.0, UltVehiY); - //calculate WHiy + // Calculate WHiy. for (size_t i=0; isize2; j++) { - for (size_t i=0; isize1; i++) { - gsl_matrix_set(B, i, j, gsl_vector_get(beta, j*d_size+i)); - gsl_matrix_set(se_B, i, j, sqrt(gsl_matrix_get(Vbeta, j*d_size+i, j*d_size+i))); - } - } - - //free matrices + for (size_t i=0; isize1; i++) { + gsl_matrix_set(B, i, j, gsl_vector_get(beta, j*d_size+i)); + gsl_matrix_set(se_B, i, j, + sqrt(gsl_matrix_get(Vbeta,j*d_size+i,j*d_size+i))); + } + } + + // Free matrices. gsl_vector_free(D_l); gsl_matrix_free(UltVeh); gsl_matrix_free(UltVehi); @@ -947,19 +907,15 @@ void MphCalcBeta (const gsl_vector *eval, const gsl_matrix *W, const gsl_matrix return; } +// Below are functions for Newton-Raphson's algorithm. - -//below are functions for Newton-Raphson's algorithm - - - - - -//calculate all Hi and return logdet_H=\sum_{k=1}^{n}log|H_k| -//and calculate Qi and return logdet_Q -//and calculate yPy -void CalcHiQi (const gsl_vector *eval, const gsl_matrix *X, const gsl_matrix *V_g, const gsl_matrix *V_e, gsl_matrix *Hi_all, gsl_matrix *Qi, double &logdet_H, double &logdet_Q) -{ +// Calculate all Hi and return logdet_H=\sum_{k=1}^{n}log|H_k| +// and calculate Qi and return logdet_Q +// and calculate yPy. +void CalcHiQi (const gsl_vector *eval, const gsl_matrix *X, + const gsl_matrix *V_g, const gsl_matrix *V_e, + gsl_matrix *Hi_all, gsl_matrix *Qi, double &logdet_H, + double &logdet_Q) { gsl_matrix_set_zero (Hi_all); gsl_matrix_set_zero (Qi); logdet_H=0.0; logdet_Q=0.0; @@ -972,10 +928,10 @@ void CalcHiQi (const gsl_vector *eval, const gsl_matrix *X, const gsl_matrix *V_ gsl_matrix *UltVehi=gsl_matrix_alloc (d_size, d_size); gsl_vector *D_l=gsl_vector_alloc (d_size); - //calculate D_l, UltVeh and UltVehi + // Calculate D_l, UltVeh and UltVehi. logdet_Ve=EigenProc (V_g, V_e, D_l, UltVeh, UltVehi); - //calculate each Hi and log|H_k| + // Calculate each Hi and log|H_k|. logdet_H=(double)n_size*logdet_Ve; for (size_t k=0; ksize2, d_size=Y->size1; for (size_t k=0; ksize2, c_size=X->size1, d_size=Hi_all->size1; @@ -1053,11 +1013,14 @@ void Calc_xHi_all (const gsl_matrix *X, const gsl_matrix *Hi_all, gsl_matrix *xH double d; for (size_t k=0; ksize2; for (size_t k=0; ksize2, d_size=Y->size1, dc_size=xHi->size1; for (size_t k=0; k=d_size || j>=d_size) {cout<<"error in GetIndex."<=d_size || j>=d_size) { + cout<<"error in GetIndex."<size, d_size=Hiy->size1; - double delta, d_Hiy_i1, d_Hiy_j1, d_Hiy_i2, d_Hiy_j2, d_Hi_i1i2, d_Hi_i1j2, d_Hi_j1i2, d_Hi_j1j2; + double delta, d_Hiy_i1, d_Hiy_j1, d_Hiy_i2, d_Hiy_j2; + double d_Hi_i1i2, d_Hi_i1j2, d_Hi_j1i2, d_Hi_j1j2; for (size_t k=0; k