template<class ArrayT>
Covariance matrix. Compute the covariance matrix of the observed data Output: [nc x nc] covariance matrix and (optionally) a vector of mean-values for each dimension. NOTE : this version is an approximation, only valid when the number of rows in m is large. Definition at line 27 of file MatCovariance.h. References MatDiv(), MatE(), MatNrCol(), MatNrRow(), MatSet(), and VecE(). Referenced by Impala::Application::Im::DoMatrix(), and MatKLM(). 00028 { 00029 typedef typename ArrayT::ArithType ArithT; 00030 typedef typename ArrayT::StorType StorT; 00031 00032 int nr = MatNrRow(m); 00033 int nc = MatNrCol(m); 00034 ArrayT* cov = MatCreate<ArrayT>(nc, nc); 00035 MatSet(cov, 0); 00036 ArrayT* mean = VecCreate<ArrayT>(nc) ; //means 00037 MatSet(mean, 0); 00038 00039 for (int k=0; k<nr; k++) 00040 { 00041 StorT* meanPtr = VecE(mean, 0); 00042 StorT* mkPtr = MatE(m, k, 0); 00043 for (int i=0; i<nc; i++) 00044 { 00045 ArithT mkiVal = mkPtr[i]; 00046 meanPtr[i] += mkiVal; 00047 StorT* coviPtr = MatE(cov, i, 0); 00048 for (int j=i; j<nc; j++) 00049 { 00050 coviPtr[j] += mkiVal * mkPtr[j]; 00051 } 00052 } 00053 } 00054 00055 MatDiv(mean, nr); 00056 00057 for (int i=0; i<nc; i++) 00058 { 00059 StorT* coviPtr = MatE(cov, i, 0); 00060 StorT* meanPtr = VecE(mean, 0); 00061 ArithT meani = meanPtr[i]; 00062 for (int j=i; j<nc; j++) 00063 { 00064 ArithT res = coviPtr[j] / nr - meani * meanPtr[j]; 00065 coviPtr[j] = res; 00066 *MatE(cov, j, i) = res; 00067 } 00068 } 00069 00070 if (meanOut != 0) 00071 MatSet(meanOut, mean); 00072 delete mean; 00073 00074 return cov; 00075 }
Here is the call graph for this function:
|