CMP++: Uncertainty Quantification & Bayesian Calibration
Loading...
Searching...
No Matches
cmp::scaler::PCA Class Reference

Principal Component Analysis (PCA) feature scaler and dimension reducer. More...

#include <scaler.h>

Inheritance diagram for cmp::scaler::PCA:
Collaboration diagram for cmp::scaler::PCA:

Public Member Functions

 PCA (size_t nComponents)
 Constructs a PCA scaler targeting a specific latent dimensionality.
 
 PCA (const PCA &other)=default
 
 PCA (PCA &&other)=default
 
 ~PCA ()=default
 
PCAoperator= (const PCA &other)=default
 
PCAoperator= (PCA &&other)=default
 
Eigen::VectorXd transform (const Eigen::Ref< const Eigen::VectorXd > &data) const override
 Transforms the input data from physical space to the scaled latent space.
 
Eigen::VectorXd inverseTransform (const Eigen::Ref< const Eigen::VectorXd > &data) const override
 Reconstructs the data from the scaled latent space back to physical space.
 
Eigen::VectorXd getIntercept () const override
 Retrieves the intercept vector \( \mu \) used in the transformation.
 
Eigen::MatrixXd getScale () const override
 Retrieves the scaling matrix \( S \) used in the transformation.
 
void fit (const Eigen::Ref< const Eigen::MatrixXd > &data) override
 Learns the scaling parameters (e.g., mean, variance) from the training data.
 
Eigen::MatrixXd fit_transform (const Eigen::Ref< const Eigen::MatrixXd > &data) override
 Fits the scaling parameters to the data and subsequently transforms the data.
 
void resize (size_t nComponents)
 Resizes the number of retained principal components \( M \) after fitting.
 
Eigen::VectorXd getEigenvalues () const
 Returns the eigenvalues \( \mathbf{\Lambda} \) of the covariance matrix.
 
Eigen::MatrixXd getEigenvectors () const
 Returns the eigenvectors \( \mathbf{V} \) of the covariance matrix.
 

Private Member Functions

void eigenDecomposition ()
 Internal helper to execute the eigendecomposition on the current covariance matrix.
 

Private Attributes

Eigen::VectorXd mean_
 The empirical mean vector \( \boldsymbol{\mu} \).
 
Eigen::SelfAdjointEigenSolver< Eigen::MatrixXd > eigenSolver_
 Eigen solver storing the eigenvectors \( \mathbf{V} \) and eigenvalues \( \mathbf{\Lambda} \).
 
Eigen::MatrixXd sqrtCov_
 Precomputed square root of the truncated eigenvalues matrix \( \mathbf{\Lambda}_M^{1/2} \).
 
Eigen::MatrixXd sqrtCovInv_
 Precomputed inverse square root of the truncated eigenvalues matrix \( \mathbf{\Lambda}_M^{-1/2} \).
 
size_t nComponents_
 The number of principal components \( M \) to retain.
 

Detailed Description

Principal Component Analysis (PCA) feature scaler and dimension reducer.

Mathematical Formulation

Centers data matrix \(\mathbf{X} \in \mathbb{R}^{N \times D}\) around column mean \(\boldsymbol{\mu}\) and computes the eigendecomposition of the sample covariance matrix \(\boldsymbol{\Sigma}\):

\[ \boldsymbol{\Sigma} = \mathbf{V} \mathbf{\Lambda} \mathbf{V}^T \]

where \(\mathbf{V}\) contains orthogonal eigenvectors and \(\mathbf{\Lambda}\) contains eigenvalues in descending order. The transformation projects \(\mathbf{x}\) into a lower-dimensional latent space of size \(M\):

\[ \mathbf{y} = \mathbf{\Lambda}_M^{-1/2} \mathbf{V}_M^T (\mathbf{x} - \boldsymbol{\mu}) \]

The inverse transformation reconstructs the point:

\[ \mathbf{x} = \mathbf{V}_M \mathbf{\Lambda}_M^{1/2} \mathbf{y} + \boldsymbol{\mu} \]

  • ### Implementation Algorithm
  1. fit() computes the mean \(\boldsymbol{\mu}\) and covariance \(\boldsymbol{\Sigma}\) of the training matrix, then runs Eigen::SelfAdjointEigenSolver.
  2. transform() centers the query vector, projects it onto the principal eigenvectors, and scales it by the inverse square root of eigenvalues.
  3. inverseTransform() rescales by square root eigenvalues, projects back to physical coordinates, and adds the mean.

Constructor & Destructor Documentation

◆ PCA() [1/3]

cmp::scaler::PCA::PCA ( size_t  nComponents)
inline

Constructs a PCA scaler targeting a specific latent dimensionality.

Parameters
nComponentsNumber of principal components \( M \) to retain.

◆ PCA() [2/3]

cmp::scaler::PCA::PCA ( const PCA other)
default

◆ PCA() [3/3]

cmp::scaler::PCA::PCA ( PCA &&  other)
default

◆ ~PCA()

cmp::scaler::PCA::~PCA ( )
default

Member Function Documentation

◆ eigenDecomposition()

void cmp::scaler::PCA::eigenDecomposition ( )
private

Internal helper to execute the eigendecomposition on the current covariance matrix.

◆ fit()

void cmp::scaler::PCA::fit ( const Eigen::Ref< const Eigen::MatrixXd > &  data)
overridevirtual

Learns the scaling parameters (e.g., mean, variance) from the training data.

Parameters
dataA matrix \( X \in \mathbb{R}^{n \times d} \) of training samples.

Implements cmp::scaler::Scaler.

◆ fit_transform()

Eigen::MatrixXd cmp::scaler::PCA::fit_transform ( const Eigen::Ref< const Eigen::MatrixXd > &  data)
overridevirtual

Fits the scaling parameters to the data and subsequently transforms the data.

Parameters
dataA matrix \( X \in \mathbb{R}^{n \times d} \) of training samples.
Returns
The transformed dataset matrix \( Y \).

Implements cmp::scaler::Scaler.

◆ getEigenvalues()

Eigen::VectorXd cmp::scaler::PCA::getEigenvalues ( ) const
inline

Returns the eigenvalues \( \mathbf{\Lambda} \) of the covariance matrix.

Returns
A vector of eigenvalues.

◆ getEigenvectors()

Eigen::MatrixXd cmp::scaler::PCA::getEigenvectors ( ) const
inline

Returns the eigenvectors \( \mathbf{V} \) of the covariance matrix.

Returns
A matrix whose columns are the eigenvectors.

◆ getIntercept()

Eigen::VectorXd cmp::scaler::PCA::getIntercept ( ) const
inlineoverridevirtual

Retrieves the intercept vector \( \mu \) used in the transformation.

Returns
The intercept vector.

Implements cmp::scaler::Scaler.

◆ getScale()

Eigen::MatrixXd cmp::scaler::PCA::getScale ( ) const
inlineoverridevirtual

Retrieves the scaling matrix \( S \) used in the transformation.

Returns
The scaling matrix.

Implements cmp::scaler::Scaler.

◆ inverseTransform()

Eigen::VectorXd cmp::scaler::PCA::inverseTransform ( const Eigen::Ref< const Eigen::VectorXd > &  data) const
overridevirtual

Reconstructs the data from the scaled latent space back to physical space.

Parameters
dataA column vector \( y \) in the scaled space.
Returns
The unscaled physical data vector \( x \).

Implements cmp::scaler::Scaler.

◆ operator=() [1/2]

PCA & cmp::scaler::PCA::operator= ( const PCA other)
default

◆ operator=() [2/2]

PCA & cmp::scaler::PCA::operator= ( PCA &&  other)
default

◆ resize()

void cmp::scaler::PCA::resize ( size_t  nComponents)

Resizes the number of retained principal components \( M \) after fitting.

Parameters
nComponentsThe new number of components to retain.

◆ transform()

Eigen::VectorXd cmp::scaler::PCA::transform ( const Eigen::Ref< const Eigen::VectorXd > &  data) const
overridevirtual

Transforms the input data from physical space to the scaled latent space.

Parameters
dataA column vector \( x \) representing a single data point in physical space.
Returns
The scaled data vector \( y \).

Implements cmp::scaler::Scaler.

Member Data Documentation

◆ eigenSolver_

Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> cmp::scaler::PCA::eigenSolver_
private

Eigen solver storing the eigenvectors \( \mathbf{V} \) and eigenvalues \( \mathbf{\Lambda} \).

◆ mean_

Eigen::VectorXd cmp::scaler::PCA::mean_
private

The empirical mean vector \( \boldsymbol{\mu} \).

◆ nComponents_

size_t cmp::scaler::PCA::nComponents_
private

The number of principal components \( M \) to retain.

◆ sqrtCov_

Eigen::MatrixXd cmp::scaler::PCA::sqrtCov_
private

Precomputed square root of the truncated eigenvalues matrix \( \mathbf{\Lambda}_M^{1/2} \).

◆ sqrtCovInv_

Eigen::MatrixXd cmp::scaler::PCA::sqrtCovInv_
private

Precomputed inverse square root of the truncated eigenvalues matrix \( \mathbf{\Lambda}_M^{-1/2} \).


The documentation for this class was generated from the following files: