1 | #ifndef _theplu_yat_utility_pca_ |
---|
2 | #define _theplu_yat_utility_pca_ |
---|
3 | |
---|
4 | // $Id: PCA.h 2322 2010-09-19 01:18:22Z 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. |
---|
46 | |
---|
47 | \note Currently number of rows, N, must be larger (or equal) than |
---|
48 | number of columns, M. |
---|
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 | */ |
---|
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 |
---|