1 | #ifndef _theplu_yat_utility_pca_ |
---|
2 | #define _theplu_yat_utility_pca_ |
---|
3 | |
---|
4 | // $Id: PCA.h 2119 2009-12-12 23:11:43Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2003 Daniel Dalevi |
---|
8 | Copyright (C) 2004 Jari Häkkinen |
---|
9 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2006 Jari Häkkinen |
---|
11 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
12 | |
---|
13 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
14 | |
---|
15 | The yat library is free software; you can redistribute it and/or |
---|
16 | modify it under the terms of the GNU General Public License as |
---|
17 | published by the Free Software Foundation; either version 3 of the |
---|
18 | License, or (at your option) any later version. |
---|
19 | |
---|
20 | The yat library is distributed in the hope that it will be useful, |
---|
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
23 | General Public License for more details. |
---|
24 | |
---|
25 | You should have received a copy of the GNU General Public License |
---|
26 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Matrix.h" |
---|
30 | #include "Vector.h" |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace utility { |
---|
35 | |
---|
36 | /** |
---|
37 | @brief Principal Component Analysis |
---|
38 | |
---|
39 | Class performing PCA using SVD. This class assumes that |
---|
40 | the columns corresponds to the dimenension of the problem. |
---|
41 | That means if data has dimension NxM (M=columns) the number |
---|
42 | of principal-axes will equal M-1. When projecting data into |
---|
43 | this space, all Nx1 vectors will have dimension Mx1. Hence |
---|
44 | the projection will have dimension MxM where each column is |
---|
45 | a point in the new space. Also, it assumes that M>N. The opposite |
---|
46 | problem is added in the functions: process_transposed_problem and |
---|
47 | projection_transposed()... |
---|
48 | */ |
---|
49 | class PCA |
---|
50 | { |
---|
51 | public: |
---|
52 | /** |
---|
53 | Constructor taking the data-matrix as input. No row-centering |
---|
54 | should have been performed and no products. |
---|
55 | */ |
---|
56 | explicit PCA(const utility::Matrix&); |
---|
57 | |
---|
58 | /** |
---|
59 | If M<N use this method instead. Using the same format as before |
---|
60 | where rows in the matrix corresponds to the dimensional coordinate. |
---|
61 | The only difference is in the SVD step where the matrix V is used |
---|
62 | after running the transposed matrix. For projections, see |
---|
63 | projection_transposed() method. |
---|
64 | */ |
---|
65 | // void process_transposed_problem(void); |
---|
66 | |
---|
67 | /** |
---|
68 | \brief Returns eigenvalues in a utility::vector. |
---|
69 | |
---|
70 | \return A const reference to the internal vector containing all |
---|
71 | eigenvalues. |
---|
72 | */ |
---|
73 | const utility::Vector& eigenvalues(void) const; |
---|
74 | |
---|
75 | /** |
---|
76 | \brief Get all eigenvectors in a utility::matrix. |
---|
77 | |
---|
78 | \return A const reference to the internal matrix containing all |
---|
79 | eigenvectors. |
---|
80 | */ |
---|
81 | const utility::Matrix& eigenvectors(void) const; |
---|
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 | utility::Matrix projection( const utility::Matrix& ) const; |
---|
91 | |
---|
92 | /** |
---|
93 | Same as projection() but works when used |
---|
94 | process_transposed_problem(). |
---|
95 | */ |
---|
96 | // utility::matrix projection_transposed( const utility::matrix& ) const; |
---|
97 | |
---|
98 | |
---|
99 | private: |
---|
100 | |
---|
101 | /** |
---|
102 | Will perform PCA according to the following scheme: \n |
---|
103 | 1: Rowcenter A \n |
---|
104 | 2: SVD(A) --> USV' \n |
---|
105 | 3: Calculate eigenvalues according to \n |
---|
106 | \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n |
---|
107 | 4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n |
---|
108 | */ |
---|
109 | void process(void); |
---|
110 | |
---|
111 | /** |
---|
112 | Private function that will row-center the matrix A, |
---|
113 | that is, A = A - M, where M is a matrix |
---|
114 | with the meanvalues of each row |
---|
115 | */ |
---|
116 | void row_center( utility::Matrix& A_center ); |
---|
117 | |
---|
118 | utility::Matrix A_; |
---|
119 | utility::Vector eigenvalues_; |
---|
120 | utility::Matrix eigenvectors_; |
---|
121 | utility::Vector meanvalues_; |
---|
122 | }; |
---|
123 | |
---|
124 | }}} // of namespace utility, yat, and theplu |
---|
125 | |
---|
126 | #endif |
---|