1 | #ifndef _theplu_yat_utility_pca_ |
---|
2 | #define _theplu_yat_utility_pca_ |
---|
3 | |
---|
4 | // $Id: PCA.h 3999 2020-10-08 23:22:32Z 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 | Copyright (C) 2010, 2018, 2020 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 "config_public.h" |
---|
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. |
---|
49 | |
---|
50 | \note Currently number of rows, N, must be larger (or equal) than |
---|
51 | number of columns, M. |
---|
52 | */ |
---|
53 | class PCA |
---|
54 | { |
---|
55 | public: |
---|
56 | /** |
---|
57 | Constructor taking the data-matrix as input. No row-centering |
---|
58 | should have been performed and no products. |
---|
59 | */ |
---|
60 | explicit PCA(const Matrix&); |
---|
61 | |
---|
62 | /** |
---|
63 | Same as PCA(const Matrix&) but moves \a M rather than copy. |
---|
64 | |
---|
65 | \since new in yat 0.16 |
---|
66 | */ |
---|
67 | explicit PCA(Matrix&& M); |
---|
68 | |
---|
69 | /** |
---|
70 | \brief Returns eigenvalues. |
---|
71 | |
---|
72 | \return A const reference to the internal vector containing all |
---|
73 | eigenvalues. |
---|
74 | */ |
---|
75 | const Vector& eigenvalues(void) const; |
---|
76 | |
---|
77 | /** |
---|
78 | \brief Get all eigenvectors in a Matrix. |
---|
79 | |
---|
80 | \return A const reference to the internal matrix containing all |
---|
81 | eigenvectors. |
---|
82 | */ |
---|
83 | const 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 | Matrix projection(const Matrix&) const; |
---|
93 | |
---|
94 | private: |
---|
95 | |
---|
96 | /** |
---|
97 | Will perform PCA according to the following scheme: \n |
---|
98 | 1: Rowcenter A \n |
---|
99 | 2: SVD(A) --> USV' \n |
---|
100 | 3: Calculate eigenvalues according to \n |
---|
101 | \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n |
---|
102 | */ |
---|
103 | void process(void); |
---|
104 | |
---|
105 | /** |
---|
106 | Private function that will row-center the matrix A, |
---|
107 | that is, A = A - M, where M is a matrix |
---|
108 | with the meanvalues of each row |
---|
109 | */ |
---|
110 | void row_center(Matrix& A_center); |
---|
111 | |
---|
112 | utility::Matrix A_; |
---|
113 | utility::Vector eigenvalues_; |
---|
114 | utility::Matrix eigenvectors_; |
---|
115 | utility::Vector meanvalues_; |
---|
116 | }; |
---|
117 | |
---|
118 | }}} // of namespace utility, yat, and theplu |
---|
119 | |
---|
120 | #endif |
---|