1 | #ifndef _theplu_yat_utility_pca_ |
---|
2 | #define _theplu_yat_utility_pca_ |
---|
3 | |
---|
4 | // $Id: PCA.h 687 2006-10-16 23:51:10Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) The authors contributing to this file. |
---|
8 | |
---|
9 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "matrix.h" |
---|
28 | #include "vector.h" |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace utility { |
---|
33 | |
---|
34 | /** |
---|
35 | Class performing PCA using SVD. This class assumes that |
---|
36 | the columns corresponds to the dimenension of the problem. |
---|
37 | That means if data has dimension NxM (M=columns) the number |
---|
38 | of principal-axes will equal M-1. When projecting data into |
---|
39 | this space, all Nx1 vectors will have dimension Mx1. Hence |
---|
40 | the projection will have dimension MxM where each column is |
---|
41 | a point in the new space. Also, it assumes that M>N. The opposite |
---|
42 | problem is added in the functions: process_transposed_problem and |
---|
43 | projection_transposed()... |
---|
44 | */ |
---|
45 | class PCA |
---|
46 | { |
---|
47 | public: |
---|
48 | /** |
---|
49 | Default constructor (not implemented) |
---|
50 | */ |
---|
51 | PCA(void); |
---|
52 | |
---|
53 | /** |
---|
54 | Constructor taking the data-matrix as input. No row-centering |
---|
55 | should have been performed and no products. |
---|
56 | */ |
---|
57 | inline explicit PCA(const utility::matrix& A) |
---|
58 | : A_(A), process_(false), explained_calc_(false) {} |
---|
59 | |
---|
60 | /** |
---|
61 | Will perform PCA according to the following scheme: \n |
---|
62 | 1: Rowcenter A \n |
---|
63 | 2: SVD(A) --> USV' \n |
---|
64 | 3: Calculate eigenvalues according to \n |
---|
65 | \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n |
---|
66 | 4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n |
---|
67 | */ |
---|
68 | void process(void); |
---|
69 | |
---|
70 | /** |
---|
71 | If M<N use this method instead. Using the same format as before |
---|
72 | where rows in the matrix corresponds to the dimensional coordinate. |
---|
73 | The only difference is in the SVD step where the matrix V is used |
---|
74 | after running the transposed matrix. For projections, see |
---|
75 | projection_transposed() method. |
---|
76 | */ |
---|
77 | void process_transposed_problem(void); |
---|
78 | |
---|
79 | /** |
---|
80 | @return Eigenvector \a i. |
---|
81 | */ |
---|
82 | inline utility::vector get_eigenvector(const size_t& i) const |
---|
83 | { return utility::vector(eigenvectors_,i); } |
---|
84 | |
---|
85 | /** |
---|
86 | Returns eigenvalues to covariance matrix |
---|
87 | \f$ C = \frac{1}{N^2}A^TA \f$ |
---|
88 | */ |
---|
89 | inline double |
---|
90 | get_eigenvalue(const size_t& i) const { return eigenvalues_[i]; } |
---|
91 | |
---|
92 | /** |
---|
93 | Returns the explained intensity of component \a K \n |
---|
94 | \f$I = \frac{ \sum^{K}_{i=1} \lambda_i }{ \sum^{N}_{j=1} \lambda_j }\f$ \n |
---|
95 | where \f$N\f$ is the dimension |
---|
96 | */ |
---|
97 | double get_explained_intensity( const size_t& k ); |
---|
98 | |
---|
99 | /** |
---|
100 | This function will project data onto the new coordinate-system |
---|
101 | where the axes are the calculated eigenvectors. This means that |
---|
102 | PCA must have been run before this function can be used! |
---|
103 | Output is presented as coordinates in the N-dimensional room |
---|
104 | spanned by the eigenvectors. |
---|
105 | */ |
---|
106 | utility::matrix projection( const utility::matrix& ) const; |
---|
107 | |
---|
108 | /** |
---|
109 | Same as projection() but works when used |
---|
110 | process_transposed_problem(). |
---|
111 | */ |
---|
112 | utility::matrix projection_transposed( const utility::matrix& ) const; |
---|
113 | |
---|
114 | |
---|
115 | private: |
---|
116 | utility::matrix A_; |
---|
117 | utility::matrix eigenvectors_; |
---|
118 | utility::vector eigenvalues_; |
---|
119 | utility::vector explained_intensity_; |
---|
120 | utility::vector meanvalues_; |
---|
121 | bool process_, explained_calc_; |
---|
122 | |
---|
123 | /** |
---|
124 | Private function that will row-center the matrix A, |
---|
125 | that is, A = A - M, where M is a matrix |
---|
126 | with the meanvalues of each row |
---|
127 | */ |
---|
128 | void row_center( utility::matrix& A_center ); |
---|
129 | |
---|
130 | /** |
---|
131 | Private function that will calculate the explained |
---|
132 | intensity |
---|
133 | */ |
---|
134 | void calculate_explained_intensity(); |
---|
135 | }; // class PCA |
---|
136 | |
---|
137 | |
---|
138 | }}} // of namespace utility, yat, and theplu |
---|
139 | |
---|
140 | #endif |
---|