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