Cov.h

Go to the documentation of this file.
00001 #ifndef COV_H_
00002 #define COV_H_
00003 
00004 #include "VecMat.h"
00005 
00006 /** \addtogroup statistics Statistical methods */
00007 /* @{ */
00008 
00009 //! Calculate the NxN covariance matrix for a NxM matrix of observations with 0 mean
00010 template <typename UblasMatrix>
00011 UblasMatrix Cov(const UblasMatrix &observations)
00012 {
00013         const size_t nobservations = observations.size1();
00014         const size_t nsamples = observations.size2();
00015         UblasMatrix result(nobservations,nobservations);
00016         
00017         for (size_t i = 0; i < nobservations; ++i)
00018         {
00019                 for (size_t j = 0; j <= i; ++j)
00020                 {
00021                         result(i,j) = 0;
00022                         for (size_t k = 0; k < nsamples; ++k)
00023                         {
00024                                 result(i,j) += observations(i,k) * observations(j,k);
00025                         }
00026                         result(j,i) = result(i,j);
00027                 }
00028         }
00029         result /= (nsamples-1);
00030         return result;
00031 }
00032 /* @} */
00033 #endif /*COV_H_*/

Generated on Fri Jul 4 15:30:20 2008 for GPLIB++ by  doxygen 1.5.5