#include #include "arraystats.h" /* global external variables */ extern int N, M, *m; extern float **X, **Y, *muX, *muY; extern double *cov; extern int gene_index; extern int N1, N2, *m1, *m2; extern float **X1, **X2, **Y1, **Y2; extern double *cov1, *cov2; extern int gene_index_1, gene_index_2; /* global definitions */ double Sex, Sey, Re, Sdx, Sdy, Rd; double Vex, Vey, Vdx, Vdy; double C11, C12, C22, detC ; double B11, B12, B22; #define PI 3.1415926 /********************************************************************************************/ /** **/ /** Z.c - Objective function (variables beta) **/ /** **/ /** April 2000 Copyright (c) Vesteinn Thorsson and Trey Ideker **/ /********************************************************************************************/ /* X and Y must be shifted my mus */ double Z( double *beta ){ int i,j, pisum; double logsum, quadsum; Sex = beta[1]; Sey = beta[2]; Re = beta[3]; Sdx = beta[4]; Sdy = beta[5]; #ifdef USERHOD Rd = beta[6]; #else Rd = 0; #endif Vex = Sex*Sex; Vey = Sey*Sey; Vdx = Sdx*Sdx; Vdy = Sdy*Sdy; pisum = 0; logsum = 0.; quadsum = 0.; for (i=0 ; i= 0 ){ s1 = 1 ; } else { s1 = -1; } if ( mju[2] >= 0 ){ s2 = 1 ; } else { s2 = -1; } mju[1] = fabs( mju[1] ); mju[2] = fabs( mju[2] ); #endif /* construct C, the covariance matrix */ C11 = Vex* mju[1]*mju[1] + Vdx; C12 = Sex*Sey*Re* mju[1]*mju[2] + Sdx*Sdy*Rd; C22 = Vey* mju[2]*mju[2] + Vdy; detC = C11*C22 - C12*C12; /* Compute gradient of detC for s = {mux, muy} : d(detC)/ds = d(C11)/ds * C22 + d(C22)/ds * C11 - 2 C12 * d(C12)/ds */ ddetC_dg1 = 2.*Vex*mju[1] * C22 - 2.* C12 *Sex*Sey*Re *mju[2] ; ddetC_dg2 = 2.*Vey*mju[2] * C11 - 2.* C12 *Sex*Sey*Re *mju[1] ; /* construct B, the inverse of C */ B11 = C22 / detC; B12 = -C12 / detC; B22 = C11 / detC; /* Compute gradient of objective function 1/(2 detC) * ( d(detC)/ds ( 1 - [x y]B[xy]) + x^2 d(C22)/ds + y^2 d(C11)/ds - 2 xy d(C12)/ds ) + 1/2 * ( B11*d(x^2)/ds+ B22 d(y^2)/ds + 2 B12 d(xy)/ds ) */ grad[1] = m[gene_index] * ddetC_dg1/detC/((double)2.); grad[2] = m[gene_index] * ddetC_dg2/detC/((double)2.); for (j=0 ; j= 0 && mju[1] <= 1e-8 && grad[1] > 0.) || ( mju[1] <= 0 && mju[1] >= -1e-8 && grad[1] < 0.) ) grad[1] = 0 ; if ( (mju[2] >= 0 && mju[2] <= 1e-8 && grad[2] > 0.) || (mju[2] <= 0 && mju[2] >= -1e-8 && grad[2] < 0.) ) grad[2] = 0 ; /* threshold was set emperically */ /* For the conjugate gradient method, this is not an ultimate solution, as the search */ /* direction is not always the gradient. Re-initializing might solve this. */ #endif } /********************************************************************************************/ /** **/ /** Z_gene_constrained.c - Single gene objective function **/ /** (constrained variables mju=mu_x=mu_y) **/ /** **/ /** April 2000 Copyright (c) Vesteinn Thorsson and Trey Ideker **/ /********************************************************************************************/ /* X (Y) must not have muX (muY) subtraction */ double Z_gene_constrained( double mju ){ int j; double logsum, quadsum, x, y; Sex = cov[1]; Sey = cov[2]; Re = cov[3]; Sdx = cov[4]; Sdy = cov[5]; #ifdef USERHOD Rd = cov[6]; #else Rd =0; #endif Vex = Sex*Sex; Vey = Sey*Sey; Vdx = Sdx*Sdx; Vdy = Sdy*Sdy; #ifdef MUNONNEG /* Take absolute value of mu for redefinition */ mju = fabs( mju ); #endif /* Covariance matrix C and determinant */ C11 = Vex* mju * mju + Vdx; C12 = Sex*Sey*Re * mju*mju + Sdx*Sdy*Rd; C22 = Vey* mju * mju + Vdy; detC = C11*C22 - C12*C12; if ( detC <= 0 ){ printf("Error: Determinant of correlation matrix is not positive\n"); exit(1); } logsum = m[gene_index] * log(detC); /* one contribution from each sample */ /* construct B, the inverse of C */ B11 = C22 / detC; B12 = -C12 / detC; B22 = C11 / detC; quadsum=0.; for (j=0 ; j