
Ublas : 
From: Michael Stevens (mail_at_[hidden])
Date: 20060829 00:08:46
On Friday, 18. August 2006 20:03, Jerry Swan wrote:
> Given a matrix D with dimensions nvars by ncases and a vector m containing
> the mean values of each row of D, I wish to calculate the standardized
> covariance of D, i.e. to form a covariance matrix with zero mean.
>
> If I were merely concerned with the unstandardized covariance matrix, I
> understand that I could efficiently calculate it as follows:
>
> matrix<real> covar = prod(D,trans(D))/ncases;
>
> The code for explicitly calculating the standardized variant appears below.
> Is there some variant of the above that would allow this to be more
> efficiently calculated using ublas?
Hi Jerry,
You should use the function outer_product to calculate covariances
Converting from my own code (see Bayes++ in my signature).
My S is your data matrix.
typedef matrix,real> Matrix;
typedef matrix_column<Matrix> Column;
X.clear(); // Covariance
const Matrix::size_type nSamples = S.size2();
for (Matrix::size_type i = 0; i != nSamples; ++i) {
Column Si(S,i);
X.plus_assign (outer_prod(Six, Six));
}
X.plus_assign (trans(X));
X /= real(nSamples*2);
Hopefully this compiles, I just hand converted it!
Regards,
Michael
 ___________________________________ Michael Stevens Systems Engineering 34128 Kassel, Germany Phone/Fax: +49 561 5218038 Navigation Systems, Estimation and Bayesian Filtering http://bayesclasses.sf.net ___________________________________