1 | #ifndef _theplu_yat_utility_pca_ |
---|
2 | #define _theplu_yat_utility_pca_ |
---|
3 | |
---|
4 | // $Id: PCA.h 1437 2008-08-25 17:55:00Z 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 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 2 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 this program; if not, write to the Free Software |
---|
28 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
29 | 02111-1307, USA. |
---|
30 | */ |
---|
31 | |
---|
32 | #include "Matrix.h" |
---|
33 | #include "Vector.h" |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace utility { |
---|
38 | |
---|
39 | /** |
---|
40 | @brief Principal Component Analysis |
---|
41 | |
---|
42 | Class performing PCA using SVD. This class assumes that |
---|
43 | the columns corresponds to the dimenension of the problem. |
---|
44 | That means if data has dimension NxM (M=columns) the number |
---|
45 | of principal-axes will equal M-1. When projecting data into |
---|
46 | this space, all Nx1 vectors will have dimension Mx1. Hence |
---|
47 | the projection will have dimension MxM where each column is |
---|
48 | a point in the new space. Also, it assumes that M>N. The opposite |
---|
49 | problem is added in the functions: process_transposed_problem and |
---|
50 | projection_transposed()... |
---|
51 | */ |
---|
52 | class PCA |
---|
53 | { |
---|
54 | public: |
---|
55 | /** |
---|
56 | Constructor taking the data-matrix as input. No row-centering |
---|
57 | should have been performed and no products. |
---|
58 | */ |
---|
59 | explicit PCA(const utility::Matrix&); |
---|
60 | |
---|
61 | /** |
---|
62 | If M<N use this method instead. Using the same format as before |
---|
63 | where rows in the matrix corresponds to the dimensional coordinate. |
---|
64 | The only difference is in the SVD step where the matrix V is used |
---|
65 | after running the transposed matrix. For projections, see |
---|
66 | projection_transposed() method. |
---|
67 | */ |
---|
68 | // void process_transposed_problem(void); |
---|
69 | |
---|
70 | /** |
---|
71 | \brief Returns eigenvalues in a utility::vector. |
---|
72 | |
---|
73 | \return A const reference to the internal vector containing all |
---|
74 | eigenvalues. |
---|
75 | */ |
---|
76 | const utility::Vector& eigenvalues(void) const; |
---|
77 | |
---|
78 | /** |
---|
79 | \brief Get all eigenvectors in a utility::matrix. |
---|
80 | |
---|
81 | \return A const reference to the internal matrix containing all |
---|
82 | eigenvectors. |
---|
83 | */ |
---|
84 | const utility::Matrix& eigenvectors(void) const; |
---|
85 | |
---|
86 | /** |
---|
87 | This function will project data onto the new coordinate-system |
---|
88 | where the axes are the calculated eigenvectors. This means that |
---|
89 | PCA must have been run before this function can be used! |
---|
90 | Output is presented as coordinates in the N-dimensional room |
---|
91 | spanned by the eigenvectors. |
---|
92 | */ |
---|
93 | utility::Matrix projection( const utility::Matrix& ) const; |
---|
94 | |
---|
95 | /** |
---|
96 | Same as projection() but works when used |
---|
97 | process_transposed_problem(). |
---|
98 | */ |
---|
99 | // utility::matrix projection_transposed( const utility::matrix& ) const; |
---|
100 | |
---|
101 | |
---|
102 | private: |
---|
103 | |
---|
104 | /** |
---|
105 | Will perform PCA according to the following scheme: \n |
---|
106 | 1: Rowcenter A \n |
---|
107 | 2: SVD(A) --> USV' \n |
---|
108 | 3: Calculate eigenvalues according to \n |
---|
109 | \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n |
---|
110 | 4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n |
---|
111 | */ |
---|
112 | void process(void); |
---|
113 | |
---|
114 | /** |
---|
115 | Private function that will row-center the matrix A, |
---|
116 | that is, A = A - M, where M is a matrix |
---|
117 | with the meanvalues of each row |
---|
118 | */ |
---|
119 | void row_center( utility::Matrix& A_center ); |
---|
120 | |
---|
121 | utility::Matrix A_; |
---|
122 | utility::Vector eigenvalues_; |
---|
123 | utility::Matrix eigenvectors_; |
---|
124 | utility::Vector meanvalues_; |
---|
125 | }; |
---|
126 | |
---|
127 | }}} // of namespace utility, yat, and theplu |
---|
128 | |
---|
129 | #endif |
---|