PCA.h

Go to the documentation of this file.
00001 #ifndef PCA_H_
00002 #define PCA_H_
00003 
00004 #include "VecMat.h"
00005 #include "statutils.h"
00006 #include "Cov.h"
00007 
00008 namespace ublas = boost::numeric::ublas;
00009 
00010 namespace gplib
00011   {
00012 
00013     /** \addtogroup statistics Statistical methods */
00014     /* @{ */
00015 
00016     /*! /file
00017      *  This file contains function connected to Principal Component Analysis
00018      */
00019 
00020     //! This template function calculates the principal component rotation matrix from a matrix of observations
00021     /*! The input matrix observations has the different channels (or datasets) as rows and corresponding samples as columns
00022      * the parameter pcatrans will contain the matrix of principal component vectors, at the moment in no particular order
00023      */
00024     template<typename UblasMatrix>
00025     void PCA(const UblasMatrix &observations, gplib::cmat &evectors,
00026         gplib::cvec &evalues)
00027       {
00028         UblasMatrix loc_obs(observations);
00029         for (unsigned int i = 0; i < loc_obs.size1(); ++i)
00030           SubMean(row(loc_obs, i).begin(), row(loc_obs, i).end());
00031         UblasMatrix covariance = Cov(loc_obs);
00032 
00033         const int nchannels = covariance.size1();
00034 
00035         gplib::rvec s(nchannels);
00036         gplib::cmat vl(nchannels, nchannels), vr(nchannels, nchannels), in(
00037             nchannels, nchannels);
00038         boost::numeric::bindings::lapack::geev(covariance, evalues, &vl,
00039             &evectors, boost::numeric::bindings::lapack::optimal_workspace());
00040       }
00041 
00042     //! Calculate the Whitening Matrix
00043     /*! Given the complex matrix of eigenvectors evectors
00044      *  and the complex vector of eigenvalues as calculated by PCA,
00045      * calculate the Whitening Matrix and return it*/
00046     gplib::cmat WhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
00047       {
00048         gplib::cmat result(evectors);
00049         for (unsigned int i = 0; i < evectors.size2(); ++i)
00050           ublas::column(result, i) /=  std::sqrt(evalues(i));
00051         return trans(result);
00052       }
00053 
00054     //! Calculate the Dewhitening Matrix
00055     /*! Given the complex matrix of eigenvectors evectors
00056      *  and the complex vector of eigenvalues as calculated by PCA,
00057      * calculate the DeWhitening Matrix that reverses the effect of the Whitening Matrix
00058      * and return it. */
00059     gplib::cmat DeWhiteMat(gplib::cmat &evectors, gplib::cvec &evalues)
00060       {
00061         gplib::cmat result(evectors);
00062         for (unsigned int i = 0; i < evectors.size2(); ++i)
00063           ublas::column(result, i) *= std::sqrt(evalues(i));
00064         return result;
00065       }
00066   /* @} */
00067   }
00068 #endif /*PCA_H_*/

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