source: trunk/yat/utility/KernelPCA.cc @ 2324

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

new class KernelPCA. closes #639

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1// $Id: KernelPCA.cc 2324 2010-09-21 21:19:25Z peter $
2
3/*
4  Copyright (C) 2010 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include <iostream>
23
24#include "KernelPCA.h"
25
26#include "Matrix.h"
27#include "PCA.h"
28
29#include <gsl/gsl_linalg.h>
30
31#include <algorithm>
32#include <cassert>
33#include <cmath>
34
35namespace theplu {
36namespace yat {
37namespace utility {
38
39  class KernelPCA::Impl
40  {
41  public:
42    Impl(const Matrix& kernel)
43      : projection_(0,0)
44    {
45      assert(kernel.rows()==kernel.columns());
46
47      // decompose K = Z' * Z
48
49      // k is typically based on centralized data, which implies k has
50      // one zero eigenvalue, i.e., the samples span a hyperspace
51      // N-1. This might cause numerical problems so therefore
52      // translate the data points away (perpendicular) from the
53      // hyperspace. In practice that means that we add all
54      // scalar-products with 1.0.
55      //      k += 1.0;
56
57      Matrix Z(kernel);
58      Z += 1.0e-10;
59      gsl_linalg_cholesky_decomp(Z.gsl_matrix_p());
60      for (size_t row=0; row<Z.rows(); ++row)
61        for (size_t column=0; column<row; ++column)
62          Z(row, column) = 0.0;
63
64      PCA pca(Z);
65     
66      eigenvalues_ = pca.eigenvalues();
67      projection_ = pca.projection(Z);
68    }
69
70    Vector eigenvalues_;
71    Matrix projection_;
72  };
73
74
75  KernelPCA::KernelPCA(const Matrix& kernel)
76    : impl_(new KernelPCA::Impl(kernel))
77  {
78  }
79
80
81  KernelPCA::~KernelPCA(void)
82  {
83    delete impl_;
84    impl_ = NULL;
85  }
86
87
88  const Vector& KernelPCA::eigenvalues(void) const
89  {
90    return impl_->eigenvalues_;
91  }
92
93
94  const Matrix& KernelPCA::projection(void) const
95  {
96    return impl_->projection_;
97  }
98
99}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.