source: trunk/lib/utility/PCA.h @ 303

Last change on this file since 303 was 303, checked in by Peter, 17 years ago

docs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1// $Id: PCA.h 303 2005-04-30 16:17:35Z peter $
2
3#ifndef _theplu_utility_pca_
4#define _theplu_utility_pca_
5
6#include <c++_tools/gslapi/matrix.h>
7#include <c++_tools/gslapi/vector.h>
8
9// Standard C++ includes
10////////////////////////
11// #include <vector>
12// #include <iostream>
13// #include <memory>
14// #include <cstdlib>
15
16
17namespace theplu {
18namespace utility {
19
20  /**
21     Class performing PCA using SVD. This class assumes that
22     the columns corresponds to the dimenension of the problem.
23     That means if data has dimension NxM (M=columns) the number
24     of principal-axes will equal M-1. When projecting data into
25     this space, all Nx1 vectors will have dimension Mx1. Hence
26     the projection will have dimension MxM where each column is
27     a point in the new space. Also, it assumes that M>N. The opposite
28     problem is added in the functions: process_transposed_problem and
29     projection_transposed()...
30  */
31 
32  class PCA
33  {
34  public:
35    /**
36       Default constructor (not implemented)
37    */
38    PCA(); 
39
40    /**
41       Constructor taking the data-matrix as input. No row-centering
42       should have been performed and no products.
43     */
44    explicit PCA( const gslapi::matrix& );
45
46   
47    /**
48       Will perform PCA according to the following scheme: \n
49       1: Rowcenter A  \n
50       2: SVD(A)  --> USV' \n
51       3: Calculate eigenvalues according to \n
52          \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n
53       4: Sort eigenvectors (from matrix V) according to descending eigenvalues \n
54    */
55    void process();
56
57    /**
58       If M<N use this method instead. Using the same format as before
59       where rows in the matrix corresponds to the dimensional coordinate.
60       The only difference is in the SVD step where the matrix V is used
61       after running the transposed matrix. For projections, see
62       projection_transposed() method.
63     */
64    void process_transposed_problem();
65   
66    /**
67       Returns eigenvector \a i
68    */
69    // Jari, change this to
70//     const gslapi::vector& get_eigenvector( const size_t& i ) const
71    const gslapi::vector get_eigenvector( const size_t& i ) const
72    {
73      return eigenvectors_[i];
74    }
75
76    /**
77       Returns eigenvalues to covariance matrix
78       \f$ C = \frac{1}{N^2}A^TA \f$
79    */
80    double get_eigenvalue( const size_t& i ) const
81    {
82      return eigenvalues_[ i ];
83    }
84
85    /**
86       Returns the explained intensity of component \a K \n
87       \f$I = \frac{ \sum^{K}_{i=1} \lambda_i }{ \sum^{N}_{j=1} \lambda_j }\f$ \n
88       where \f$N\f$ is the dimension
89    */
90    double PCA::get_explained_intensity( const size_t& k );
91
92
93
94    /**
95        This function will project data onto the new coordinate-system
96  where the axes are the calculated eigenvectors. This means that
97  PCA must have been run before this function can be used!
98  Output is presented as coordinates in the N-dimensional room
99  spanned by the eigenvectors.
100    */
101    gslapi::matrix projection( const gslapi::matrix& ) const;
102   
103    /**
104  Same as projection() but works when used process_transposed_problem()
105    */
106    gslapi::matrix projection_transposed( const gslapi::matrix& ) const;
107
108   
109   
110  private:
111    gslapi::matrix A_; 
112    gslapi::matrix  eigenvectors_;
113    gslapi::vector  eigenvalues_;
114    gslapi::vector  explained_intensity_;
115    gslapi::vector  meanvalues_;
116    bool process_, explained_calc_;
117   
118    /**
119       Private function that will row-center the matrix A,
120       that is, A = A - M, where M is a matrix
121       with the meanvalues of each row
122    */
123    void row_center( gslapi::matrix& A_center );
124
125    /**
126       Private function that will calculate the explained
127       intensity
128    */
129    void calculate_explained_intensity();
130
131   
132  }; // class PCA 
133 
134}} // of namespace utility and namespace theplu
135
136#endif
137
Note: See TracBrowser for help on using the repository browser.