
Ublas : 
From: Jerry Swan (pmxjs_at_[hidden])
Date: 20060818 14:03:00
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?
for( icase=0; icase<ncases; ++icase )
{
// Cumulate covariances
for( var=0 ; var<nvars; ++var )
{
diff1 = data( icase, var )  means[var];
for( var2=var; var2<nvars; ++var2 )
{
diff2 = data( icase, var2 )  means[var2];
covar( var2, var ) += diff1 * diff2;
}
}
}
for( var=0; var<nvars; ++var )
{
// Divide sums by n
for( var2=var; var2<nvars; ++var2 )
{
covar( var2, var ) /= ncases;
// symmetrically duplicate
if( var != var2 )
covar( var, var2 ) = covar( var2, var );
}
}
Regards,
Jerry.
This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.