#include "debug.h"
Go to the source code of this file.
Classes | |
| class | matrix |
Functions | |
| matrix | covar2 (matrix const &X, matrix const &Y) |
| calculates empirical covariance (columnwise) of two matrices. | |
Definition in file linalg.h.
|
||||||||||||
|
calculates empirical covariance (columnwise) of two matrices. If matrix 2 is a (column)-vector. Then the covariance between matrix 2 and each of the columns in matrix 1 is returned.
Definition at line 377 of file linalg.cpp.
00377 {
00378 assert(X.row()==Y.row() && (X.column()==Y.column() || Y.column()==1));
00379
00380 safevector <double> mx = X.mean();
00381 safevector <double> my = Y.mean();
00382 matrix cv(X.column());
00383
00384
00385 // If Y is a column-vector we calculate [cov(X_1,Y),...,cov(X_n,Y)].
00386 if (Y.column()==1) {
00387 for (int s=0; s<X.column(); s++) {
00388 double sum=0;
00389 for (int i=0; i<X.row(); i++) {
00390 sum += (X.get(i,s)-mx[s])*(Y.get(i)-my[0]);
00391 // std::cerr << "sum+=" << sum << std::endl;
00392 }
00393 cv.set(sum/(double)(X.row()-1),s);
00394 }
00395 return cv;
00396 }
00397
00398
00399 // Else we calculate [cov(X_1,Y_1),....,cov(X_n,Y_n)].
00400 for (int s=0; s<X.column(); s++) {
00401 double sum=0;
00402 for (int i=0; i<X.row(); i++) {
00403 sum += (X.get(i,s)-mx[s])*(Y.get(i,s)-my[s]);
00404 }
00405 cv.set(sum/(double)(X.row()-1),s);
00406 }
00407 return cv;
00408 }
|
1.3.6