source: trunk/src/PCA.h @ 15

Last change on this file since 15 was 15, checked in by daniel, 19 years ago

Added the transposed problem to PCA!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1#ifndef GENETICS_PCA_ANALYZER_H
2#define GENETICS_PCA_ANALYZER_H
3
4// C++ tools include
5/////////////////////
6#include "vector.h"
7#include "matrix.h"
8#include "SVD.h"
9
10// Standard C++ includes
11////////////////////////
12#include <vector>
13#include <iostream>
14#include <memory>
15#include <cstdlib>
16
17
18namespace thep_cpp_tools
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       This constructor is only to be used in test-class
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 thep_gsl_api::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
59     */
60    void process_transposed_problem();
61   
62    /**
63       Performes a simple test on performance. Not optimal!
64       Returns true if ok otherwise false.
65    */
66    bool test();
67
68
69    /**
70       Returns eigenvector \a i
71    */
72    thep_gsl_api::matrix get_eigenvector( const size_t& i ) const
73    {
74      return eigenvectors_.row( i );
75    }
76
77    /**
78       Returns eigenvalues to covariance matrix
79       \f$ C = \frac{1}{N^2}A^TA \f$
80    */
81    double get_eigenvalue( const size_t& i ) const
82    {
83      return eigenvalues_[ i ];
84    }
85
86    /**
87       Returns the explained intensity of component \a K \n
88       \f$I = \frac{ \sum^{K}_{i=1} \lambda_i }{ \sum^{N}_{j=1} \lambda_j }\f$ \n
89       where \f$N\f$ is the dimension
90    */
91    double PCA::get_explained_intensity( const size_t& k );
92
93
94
95    /**
96        This function will project data onto the new coordinate-system
97  where the axes are the calculated eigenvectors. This means that
98  PCA must have been run before this function can be used!
99  Output is presented as coordinates in the N-dimensional room
100  spanned by the eigenvectors.
101    */
102    thep_gsl_api::matrix projection( const thep_gsl_api::matrix& ) const;
103    thep_gsl_api::matrix projection_transposed( const thep_gsl_api::matrix& ) const;
104
105   
106   
107  private:
108    thep_gsl_api::matrix A_; 
109    thep_gsl_api::matrix  eigenvectors_;
110    thep_gsl_api::vector  eigenvalues_;
111    thep_gsl_api::vector  explained_intensity_;
112    thep_gsl_api::vector  meanvalues_;
113    bool process_, explained_calc_;
114   
115    /**
116       Private function that will row-center the matrix A,
117       that is, A = A - M, where M is a matrix
118       with the meanvalues of each row
119    */
120    void row_center( thep_gsl_api::matrix& A_center );
121
122    /**
123       Private function that will calculate the explained
124       intensity
125    */
126    void calculate_explained_intensity();
127
128   
129  }; // class PCA 
130 
131}
132
133#endif
134
Note: See TracBrowser for help on using the repository browser.