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. |
---|
22 | */ |
---|
23 | |
---|
24 | class PCA |
---|
25 | { |
---|
26 | public: |
---|
27 | /** |
---|
28 | This constructor is only to be used in test-class |
---|
29 | */ |
---|
30 | PCA(); |
---|
31 | |
---|
32 | /** |
---|
33 | Constructor taking the data-matrix as input. No row-centering |
---|
34 | should have been performed and no products. |
---|
35 | */ |
---|
36 | explicit PCA( const thep_gsl_api::matrix& ); |
---|
37 | |
---|
38 | |
---|
39 | /** |
---|
40 | Will perform PCA according to the following scheme: \n |
---|
41 | 1: Rowcenter A \n |
---|
42 | 2: SVD(A) --> USV' \n |
---|
43 | 3: Calculate eigenvalues according to \n |
---|
44 | \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n |
---|
45 | 4: Sort eigenvectors (from matrix V) according to descending eigenvalues \n |
---|
46 | */ |
---|
47 | void process(); |
---|
48 | |
---|
49 | |
---|
50 | /** |
---|
51 | Performes a simple test on performance. Not optimal! |
---|
52 | Returns true if ok otherwise false. |
---|
53 | */ |
---|
54 | bool test(); |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | Returns eigenvector \a i |
---|
59 | */ |
---|
60 | thep_gsl_api::matrix get_eigenvector( const size_t& i ) const |
---|
61 | { |
---|
62 | return eigenvectors_.row( i ); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | Returns eigenvalues to covariance matrix |
---|
67 | \f$ C = \frac{1}{N^2}A^TA \f$ |
---|
68 | */ |
---|
69 | double get_eigenvalue( const size_t& i ) const |
---|
70 | { |
---|
71 | return eigenvalues_[ i ]; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | Returns the explained intensity of component \a K \n |
---|
76 | \f$I = \frac{ \sum^{K}_{i=1} \lambda_i }{ \sum^{N}_{j=1} \lambda_j }\f$ \n |
---|
77 | where \f$N\f$ is the dimension |
---|
78 | */ |
---|
79 | double PCA::get_explained_intensity( const size_t& k ); |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | This function will project data onto the new coordinate-system |
---|
85 | where the axes are the calculated eigenvectors. This means that |
---|
86 | PCA must have been run before this function can be used! |
---|
87 | Output is presented as coordinates in the N-dimensional room |
---|
88 | spanned by the eigenvectors. |
---|
89 | */ |
---|
90 | thep_gsl_api::matrix projection( const thep_gsl_api::matrix& ) const; |
---|
91 | |
---|
92 | |
---|
93 | private: |
---|
94 | thep_gsl_api::matrix A_; |
---|
95 | thep_gsl_api::matrix eigenvectors_; |
---|
96 | thep_gsl_api::vector eigenvalues_; |
---|
97 | thep_gsl_api::vector explained_intensity_; |
---|
98 | thep_gsl_api::vector meanvalues_; |
---|
99 | bool process_, explained_calc_; |
---|
100 | |
---|
101 | /** |
---|
102 | Private function that will row-center the matrix A, |
---|
103 | that is, A = A - M, where M is a matrix |
---|
104 | with the meanvalues of each row |
---|
105 | */ |
---|
106 | void row_center( thep_gsl_api::matrix& A_center ); |
---|
107 | |
---|
108 | /** |
---|
109 | Private function that will calculate the explained |
---|
110 | intensity |
---|
111 | */ |
---|
112 | void calculate_explained_intensity(); |
---|
113 | |
---|
114 | |
---|
115 | }; // class PCA |
---|
116 | |
---|
117 | } |
---|
118 | |
---|
119 | #endif |
---|
120 | |
---|