source: trunk/yat/utility/PCA.h @ 2323

Last change on this file since 2323 was 2323, checked in by Peter, 12 years ago

removing some out-commented code and avoid using namespace ::utility withing that namespace.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1#ifndef _theplu_yat_utility_pca_
2#define _theplu_yat_utility_pca_
3
4// $Id: PCA.h 2323 2010-09-19 16:29:55Z 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
32namespace theplu {
33namespace yat {
34namespace 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. Also, it assumes that M>N. The opposite
46     problem is added in the functions: process_transposed_problem and
47     projection_transposed()...
48  */
49  class PCA
50  {
51  public:
52    /**
53       Constructor taking the data-matrix as input. No row-centering
54       should have been performed and no products.
55     */
56    explicit PCA(const Matrix&);
57 
58    /**
59       \brief Returns eigenvalues.
60
61       \return A const reference to the internal vector containing all
62       eigenvalues.
63    */
64    const Vector& eigenvalues(void) const;
65
66    /**
67       \brief Get all eigenvectors in a Matrix.
68
69       \return A const reference to the internal matrix containing all
70       eigenvectors.
71    */
72    const Matrix& eigenvectors(void) const;
73
74    /**
75       This function will project data onto the new coordinate-system
76       where the axes are the calculated eigenvectors. This means that
77       PCA must have been run before this function can be used!
78       Output is presented as coordinates in the N-dimensional room
79       spanned by the eigenvectors.
80    */
81    Matrix projection(const Matrix&) const;
82
83  private:
84
85    /**
86       Will perform PCA according to the following scheme: \n
87       1: Rowcenter A  \n
88       2: SVD(A)  --> USV' \n
89       3: Calculate eigenvalues according to \n
90          \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n
91       4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n
92    */
93    void process(void);
94
95    /**
96       Private function that will row-center the matrix A,
97       that is, A = A - M, where M is a matrix
98       with the meanvalues of each row
99    */
100    void row_center(Matrix& A_center);
101
102    utility::Matrix A_; 
103    utility::Vector eigenvalues_;
104    utility::Matrix eigenvectors_;
105    utility::Vector meanvalues_;
106  };
107
108}}} // of namespace utility, yat, and theplu
109
110#endif
Note: See TracBrowser for help on using the repository browser.