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 | |
---|
18 | namespace 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 |
---|
28 | opposite problem is yet not implemented. However, could easily |
---|
29 | be done by using that, if A=input-data=MxN, then SVM(A)->A=USV, |
---|
30 | and SVM(A')->A'=VSU. So by changing eigenvectors=U to |
---|
31 | eigenvectors=V and rewrite the projection-method in terms of columns |
---|
32 | instead of rows ... |
---|
33 | */ |
---|
34 | |
---|
35 | class PCA |
---|
36 | { |
---|
37 | public: |
---|
38 | /** |
---|
39 | This constructor is only to be used in test-class |
---|
40 | */ |
---|
41 | PCA(); |
---|
42 | |
---|
43 | /** |
---|
44 | Constructor taking the data-matrix as input. No row-centering |
---|
45 | should have been performed and no products. |
---|
46 | */ |
---|
47 | explicit PCA( const thep_gsl_api::matrix& ); |
---|
48 | |
---|
49 | |
---|
50 | /** |
---|
51 | Will perform PCA according to the following scheme: \n |
---|
52 | 1: Rowcenter A \n |
---|
53 | 2: SVD(A) --> USV' \n |
---|
54 | 3: Calculate eigenvalues according to \n |
---|
55 | \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n |
---|
56 | 4: Sort eigenvectors (from matrix V) according to descending eigenvalues \n |
---|
57 | */ |
---|
58 | void process(); |
---|
59 | |
---|
60 | |
---|
61 | /** |
---|
62 | Performes a simple test on performance. Not optimal! |
---|
63 | Returns true if ok otherwise false. |
---|
64 | */ |
---|
65 | bool test(); |
---|
66 | |
---|
67 | |
---|
68 | /** |
---|
69 | Returns eigenvector \a i |
---|
70 | */ |
---|
71 | thep_gsl_api::matrix get_eigenvector( const size_t& i ) const |
---|
72 | { |
---|
73 | return eigenvectors_.row( 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 | thep_gsl_api::matrix projection( const thep_gsl_api::matrix& ) const; |
---|
102 | |
---|
103 | |
---|
104 | private: |
---|
105 | thep_gsl_api::matrix A_; |
---|
106 | thep_gsl_api::matrix eigenvectors_; |
---|
107 | thep_gsl_api::vector eigenvalues_; |
---|
108 | thep_gsl_api::vector explained_intensity_; |
---|
109 | thep_gsl_api::vector meanvalues_; |
---|
110 | bool process_, explained_calc_; |
---|
111 | |
---|
112 | /** |
---|
113 | Private function that will row-center the matrix A, |
---|
114 | that is, A = A - M, where M is a matrix |
---|
115 | with the meanvalues of each row |
---|
116 | */ |
---|
117 | void row_center( thep_gsl_api::matrix& A_center ); |
---|
118 | |
---|
119 | /** |
---|
120 | Private function that will calculate the explained |
---|
121 | intensity |
---|
122 | */ |
---|
123 | void calculate_explained_intensity(); |
---|
124 | |
---|
125 | |
---|
126 | }; // class PCA |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | #endif |
---|
131 | |
---|