fml  0.1-0
Fused Matrix Library
pca.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_GPU_STATS_PCA_H
6 #define FML_GPU_STATS_PCA_H
7 #pragma once
8 
9 
10 #include "../dimops.hh"
11 
12 #include "../linalg/svd.hh"
13 #include "../linalg/xpose.hh"
14 
15 
16 namespace fml
17 {
19 namespace stats
20 {
39  template <typename REAL>
40  void pca(const bool rm_mean, const bool rm_sd, gpumat<REAL> &x, gpuvec<REAL> &sdev, gpumat<REAL> &rot)
41  {
42  linalg::err::check_card(x, sdev, rot);
43 
44  dimops::scale(rm_mean, rm_sd, x);
45 
46  auto c = x.get_card();
47  gpumat<REAL> u(c);
48  gpumat<REAL> trot(c);
49  linalg::svd(x, sdev, u, trot);
50 
51  const REAL d = 1.0 / sqrt((REAL)std::max(x.nrows()-1, 1));
52  sdev.scale(d);
53 
54  linalg::xpose(trot, rot);
55  }
56 
58  template <typename REAL>
59  void pca(const bool rm_mean, const bool rm_sd, gpumat<REAL> &x, gpuvec<REAL> &sdev)
60  {
61  linalg::err::check_card(x, sdev);
62 
63  dimops::scale(rm_mean, rm_sd, x);
64 
65  linalg::svd(x, sdev);
66 
67  const REAL d = 1.0 / sqrt((REAL)std::max(x.nrows()-1, 1));
68  sdev.scale(d);
69  }
70 }
71 }
72 
73 
74 #endif
fml::linalg::xpose
void xpose(const cpumat< REAL > &x, cpumat< REAL > &tx)
Computes the transpose out-of-place (i.e. in a copy).
Definition: xpose.hh:37
fml::gpuvec
Vector class for data held on a single GPU.
Definition: gpuvec.hh:32
fml::unimat::nrows
len_t nrows() const
Number of rows.
Definition: unimat.hh:36
fml::stats::pca
void pca(const bool rm_mean, const bool rm_sd, cpumat< REAL > &x, cpuvec< REAL > &sdev, cpumat< REAL > &rot)
Principal components analysis.
Definition: pca.hh:40
fml::dimops::scale
void scale(const bool rm_mean, const bool rm_sd, cpumat< REAL > &x)
Remove the mean and/or the sd from a matrix.
Definition: dimops.hh:376
fml::linalg::svd
void svd(cpumat< REAL > &x, cpuvec< REAL > &s)
Computes the singular value decomposition.
Definition: svd.hh:101
fml::gpuvec::scale
void scale(const T s)
Multiply all values by the input value.
Definition: gpuvec.hh:438
fml
Core namespace.
Definition: dimops.hh:10
fml::gpumat
Matrix class for data held on a single GPU.
Definition: gpumat.hh:35