GPLIB++
Cov.h
Go to the documentation of this file.
1 #ifndef COV_H_
2 #define COV_H_
3 
4 #include "VecMat.h"
5 
6 namespace gplib
7  {
8 
9  /** \addtogroup statistics Statistical methods */
10  /* @{ */
11 
12  //! Calculate the NxN covariance matrix for a NxM matrix of observations with 0 mean
13  template<typename UblasMatrix>
14  UblasMatrix Cov(const UblasMatrix &observations)
15  {
16  const size_t nobservations = observations.size1();
17  const size_t nsamples = observations.size2();
18  UblasMatrix result(nobservations, nobservations);
19 
20  for (size_t i = 0; i < nobservations; ++i)
21  {
22  for (size_t j = 0; j <= i; ++j)
23  {
24  result(i, j) = 0;
25  for (size_t k = 0; k < nsamples; ++k)
26  {
27  result(i, j) += observations(i, k) * observations(j, k);
28  }
29  result(j, i) = result(i, j);
30  }
31  }
32  result /= (nsamples - 1);
33  return result;
34  }
35  /* @} */
36  }
37 #endif /*COV_H_*/
UblasMatrix Cov(const UblasMatrix &observations)
Calculate the NxN covariance matrix for a NxM matrix of observations with 0 mean. ...
Definition: Cov.h:14