Cov.h

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

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8