GPLIB++
PCA.h
Go to the documentation of this file.
1 #ifndef PCA_H_
2 #define PCA_H_
3 
4 #include "VecMat.h"
5 #include "statutils.h"
6 #include "Cov.h"
7 
8 namespace ublas = boost::numeric::ublas;
9 
10 namespace gplib
11  {
12 
13  /** \addtogroup statistics Statistical methods */
14  /* @{ */
15 
16  /*! /file
17  * This file contains function connected to Principal Component Analysis
18  */
19 
20  //! This template function calculates the principal component rotation matrix from a matrix of observations
21  /*! The input matrix observations has the different channels (or datasets) as rows and corresponding samples as columns
22  * the parameter pcatrans will contain the matrix of principal component vectors, at the moment in no particular order
23  */
24  template<typename UblasMatrix>
25  void PCA(const UblasMatrix &observations, gplib::cmat &evectors,
26  gplib::cvec &evalues)
27  {
28  UblasMatrix loc_obs(observations);
29  for (unsigned int i = 0; i < loc_obs.size1(); ++i)
30  SubMean(row(loc_obs, i).begin(), row(loc_obs, i).end());
31  UblasMatrix covariance = Cov(loc_obs);
32 
33  const int nchannels = covariance.size1();
34 
35  gplib::rvec s(nchannels);
36  gplib::cmat vl(nchannels, nchannels), vr(nchannels, nchannels), in(
37  nchannels, nchannels);
38  boost::numeric::bindings::lapack::geev(covariance, evalues, &vl,
39  &evectors, boost::numeric::bindings::lapack::optimal_workspace());
40  }
41 
42  //! Calculate the Whitening Matrix
43  /*! Given the complex matrix of eigenvectors evectors
44  * and the complex vector of eigenvalues as calculated by PCA,
45  * calculate the Whitening Matrix and return it*/
46  gplib::cmat WhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
47  {
48  gplib::cmat result(evectors);
49  for (unsigned int i = 0; i < evectors.size2(); ++i)
50  ublas::column(result, i) /= std::sqrt(evalues(i));
51  return trans(result);
52  }
53 
54  //! Calculate the Dewhitening Matrix
55  /*! Given the complex matrix of eigenvectors evectors
56  * and the complex vector of eigenvalues as calculated by PCA,
57  * calculate the DeWhitening Matrix that reverses the effect of the Whitening Matrix
58  * and return it. */
59  gplib::cmat DeWhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
60  {
61  gplib::cmat result(evectors);
62  for (unsigned int i = 0; i < evectors.size2(); ++i)
63  ublas::column(result, i) *= std::sqrt(evalues(i));
64  return result;
65  }
66  /* @} */
67  }
68 #endif /*PCA_H_*/
gplib::cmat WhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
Calculate the Whitening Matrix.
Definition: PCA.h:46
UblasMatrix Cov(const UblasMatrix &observations)
Calculate the NxN covariance matrix for a NxM matrix of observations with 0 mean. ...
Definition: Cov.h:14
void SubMean(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mean)
Substract the mean from each element in the container, mean is passed as a parameter.
Definition: statutils.h:85
void PCA(const UblasMatrix &observations, gplib::cmat &evectors, gplib::cvec &evalues)
This template function calculates the principal component rotation matrix from a matrix of observatio...
Definition: PCA.h:25
gplib::cmat DeWhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
Calculate the Dewhitening Matrix.
Definition: PCA.h:59