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