pulsatrix 1.1.0
Loading...
Searching...
No Matches
pca.hpp
Go to the documentation of this file.
1
20#pragma once
21
22#include <cstdint>
23#include <vector>
24
25#include "pulsatrix/tensor.hpp"
26
27namespace pulsatrix {
28
30struct Pca {
32 int64_t width = 0;
34 int64_t samples = 0;
36 std::vector<double> mean;
38 std::vector<double> components;
40 std::vector<double> explained_variance;
42 std::vector<double> explained_variance_ratio;
44 double total_variance = 0;
45
46 [[nodiscard]] int64_t num_components() const { return static_cast<int64_t>(explained_variance.size()); }
47};
48
58[[nodiscard]] Pca FitPca(const Tensor& x, int64_t components, bool center = true);
59
67[[nodiscard]] Tensor PcaProject(const Pca& pca, const Tensor& x, bool whiten = false);
68
75[[nodiscard]] Tensor PcaReconstruct(const Pca& pca, const Tensor& scores);
76
82[[nodiscard]] std::vector<float> PcaComponent(const Pca& pca, int64_t index);
83
92[[nodiscard]] double SubspaceOverlap(const Pca& a, const Pca& b, int64_t k);
93
100[[nodiscard]] double PcaSplitHalfOverlap(const Tensor& x, int64_t k, uint64_t seed = 0);
101
102} // namespace pulsatrix
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Definition acquisition_functions.hpp:16
std::vector< float > PcaComponent(const Pca &pca, int64_t index)
Component index as a unit vector of floats, for SteeringHook(). One standard deviation along it is sq...
Tensor PcaProject(const Pca &pca, const Tensor &x, bool whiten=false)
Each row's coordinates on the components: (x - mean) components^T, shape (n, components),...
Pca FitPca(const Tensor &x, int64_t components, bool center=true)
Fits the components directions of most variance.
Tensor PcaReconstruct(const Pca &pca, const Tensor &scores)
The rows that coordinates map back to: mean + scores components, shape (n, width)....
double SubspaceOverlap(const Pca &a, const Pca &b, int64_t k)
How much two PCAs' first k components span the same space: the mean squared cosine of the principal a...
double PcaSplitHalfOverlap(const Tensor &x, int64_t k, uint64_t seed=0)
Fits k components on each half of the rows, split at random, and returns their SubspaceOverlap()....
A fitted PCA.
Definition pca.hpp:30
std::vector< double > explained_variance
Each component's variance: the variance of the projections, dividing by n - 1.
Definition pca.hpp:40
std::vector< double > mean
The rows' mean, subtracted before projecting. All zeros when fitted uncentered.
Definition pca.hpp:36
std::vector< double > explained_variance_ratio
Each component's share of the total variance.
Definition pca.hpp:42
int64_t samples
How many rows it was fitted on.
Definition pca.hpp:34
int64_t num_components() const
Definition pca.hpp:46
double total_variance
The variance over every direction, the sum of the per-column variances.
Definition pca.hpp:44
int64_t width
The width of the rows it was fitted on.
Definition pca.hpp:32
std::vector< double > components
Row-major (components, width): unit directions, most variance first.
Definition pca.hpp:38
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).