pub struct CompositionalPca {
pub components: Vec<Vec<f64>>,
pub explained_variance: Vec<f64>,
pub explained_variance_ratio: Vec<f64>,
pub clr_mean: Vec<f64>,
pub n_parts: usize,
pub n_components: usize,
}Expand description
Principal Component Analysis in the Aitchison simplex.
Implemented by applying the CLR transform to each observation and then performing standard PCA on the CLR-transformed data. This is equivalent to PCA in the Aitchison geometry because CLR is an isometry.
§References
- Aitchison, J. (1983). Principal component analysis of compositional data. Biometrika, 70(1), 57–65.
Fields§
§components: Vec<Vec<f64>>Principal component loadings in CLR space (n_components × D).
explained_variance: Vec<f64>Explained variance for each component.
explained_variance_ratio: Vec<f64>Proportion of total variance explained by each component.
clr_mean: Vec<f64>Column means of CLR-transformed training data (used for centering).
n_parts: usizeNumber of parts D.
n_components: usizeNumber of components retained.
Implementations§
Source§impl CompositionalPca
impl CompositionalPca
Sourcepub fn fit(data: &[Vec<f64>], n_components: usize) -> StatsResult<Self>
pub fn fit(data: &[Vec<f64>], n_components: usize) -> StatsResult<Self>
Fit the model on a set of compositional observations.
data: N × D matrix of compositions (each row is one observation).
n_components: number of principal components to retain (capped at D−1).
§Errors
Returns an error if data is too small or inconsistent.
§Examples
use scirs2_stats::compositional::CompositionalPca;
let data = vec![
vec![0.5, 0.3, 0.2],
vec![0.4, 0.4, 0.2],
vec![0.3, 0.5, 0.2],
vec![0.6, 0.2, 0.2],
];
let pca = CompositionalPca::fit(&data, 2).unwrap();
assert_eq!(pca.n_components, 2);Sourcepub fn transform(&self, data: &[Vec<f64>]) -> StatsResult<Vec<Vec<f64>>>
pub fn transform(&self, data: &[Vec<f64>]) -> StatsResult<Vec<Vec<f64>>>
Project observations onto the principal components.
Each input row is CLR-transformed, centred, and projected.
Returns an N × n_components matrix as Vec<Vec<f64>>.
§Errors
Returns an error if any row has non-positive components or wrong length.
§Examples
use scirs2_stats::compositional::CompositionalPca;
let data = vec![
vec![0.5, 0.3, 0.2],
vec![0.4, 0.4, 0.2],
vec![0.3, 0.5, 0.2],
vec![0.6, 0.2, 0.2],
];
let pca = CompositionalPca::fit(&data, 2).unwrap();
let scores = pca.transform(&data).unwrap();
assert_eq!(scores.len(), 4);
assert_eq!(scores[0].len(), 2);Sourcepub fn components(&self) -> &[Vec<f64>]
pub fn components(&self) -> &[Vec<f64>]
Return the principal component loadings (n_components × D).
Sourcepub fn explained_variance(&self) -> &[f64]
pub fn explained_variance(&self) -> &[f64]
Return the explained variance for each component.
Trait Implementations§
Source§impl Clone for CompositionalPca
impl Clone for CompositionalPca
Source§fn clone(&self) -> CompositionalPca
fn clone(&self) -> CompositionalPca
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for CompositionalPca
impl RefUnwindSafe for CompositionalPca
impl Send for CompositionalPca
impl Sync for CompositionalPca
impl Unpin for CompositionalPca
impl UnsafeUnpin for CompositionalPca
impl UnwindSafe for CompositionalPca
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.