1 | #ifndef _theplu_yat_utility_pca_ |
---|
2 | #define _theplu_yat_utility_pca_ |
---|
3 | |
---|
4 | // $Id: PCA.h 703 2006-12-18 00:47:44Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) The authors contributing to this file. |
---|
8 | |
---|
9 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "matrix.h" |
---|
28 | #include "vector.h" |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace utility { |
---|
33 | |
---|
34 | /** |
---|
35 | Class performing PCA using SVD. This class assumes that |
---|
36 | the columns corresponds to the dimenension of the problem. |
---|
37 | That means if data has dimension NxM (M=columns) the number |
---|
38 | of principal-axes will equal M-1. When projecting data into |
---|
39 | this space, all Nx1 vectors will have dimension Mx1. Hence |
---|
40 | the projection will have dimension MxM where each column is |
---|
41 | a point in the new space. Also, it assumes that M>N. The opposite |
---|
42 | problem is added in the functions: process_transposed_problem and |
---|
43 | projection_transposed()... |
---|
44 | */ |
---|
45 | class PCA |
---|
46 | { |
---|
47 | public: |
---|
48 | /** |
---|
49 | Constructor taking the data-matrix as input. No row-centering |
---|
50 | should have been performed and no products. |
---|
51 | */ |
---|
52 | explicit PCA(const utility::matrix&); |
---|
53 | |
---|
54 | /** |
---|
55 | Will perform PCA according to the following scheme: \n |
---|
56 | 1: Rowcenter A \n |
---|
57 | 2: SVD(A) --> USV' \n |
---|
58 | 3: Calculate eigenvalues according to \n |
---|
59 | \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n |
---|
60 | 4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n |
---|
61 | */ |
---|
62 | void process(void); |
---|
63 | |
---|
64 | /** |
---|
65 | If M<N use this method instead. Using the same format as before |
---|
66 | where rows in the matrix corresponds to the dimensional coordinate. |
---|
67 | The only difference is in the SVD step where the matrix V is used |
---|
68 | after running the transposed matrix. For projections, see |
---|
69 | projection_transposed() method. |
---|
70 | */ |
---|
71 | void process_transposed_problem(void); |
---|
72 | |
---|
73 | /** |
---|
74 | @return Eigenvector \a i. |
---|
75 | */ |
---|
76 | inline utility::vector get_eigenvector(size_t i) const |
---|
77 | { return utility::vector(eigenvectors_,i); } |
---|
78 | |
---|
79 | /** |
---|
80 | Returns eigenvalues to covariance matrix |
---|
81 | \f$ C = \frac{1}{N^2}A^TA \f$ |
---|
82 | */ |
---|
83 | inline double get_eigenvalue(size_t i) const { return eigenvalues_[i]; } |
---|
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 get_explained_intensity( const size_t& k ); |
---|
91 | |
---|
92 | /** |
---|
93 | This function will project data onto the new coordinate-system |
---|
94 | where the axes are the calculated eigenvectors. This means that |
---|
95 | PCA must have been run before this function can be used! |
---|
96 | Output is presented as coordinates in the N-dimensional room |
---|
97 | spanned by the eigenvectors. |
---|
98 | */ |
---|
99 | utility::matrix projection( const utility::matrix& ) const; |
---|
100 | |
---|
101 | /** |
---|
102 | Same as projection() but works when used |
---|
103 | process_transposed_problem(). |
---|
104 | */ |
---|
105 | utility::matrix projection_transposed( const utility::matrix& ) const; |
---|
106 | |
---|
107 | |
---|
108 | private: |
---|
109 | utility::matrix A_; |
---|
110 | utility::matrix eigenvectors_; |
---|
111 | utility::vector eigenvalues_; |
---|
112 | utility::vector explained_intensity_; |
---|
113 | utility::vector meanvalues_; |
---|
114 | bool process_, explained_calc_; |
---|
115 | |
---|
116 | /** |
---|
117 | Private function that will row-center the matrix A, |
---|
118 | that is, A = A - M, where M is a matrix |
---|
119 | with the meanvalues of each row |
---|
120 | */ |
---|
121 | void row_center( utility::matrix& A_center ); |
---|
122 | |
---|
123 | /** |
---|
124 | Private function that will calculate the explained |
---|
125 | intensity |
---|
126 | */ |
---|
127 | void calculate_explained_intensity(); |
---|
128 | }; // class PCA |
---|
129 | |
---|
130 | |
---|
131 | }}} // of namespace utility, yat, and theplu |
---|
132 | |
---|
133 | #endif |
---|