Skip to main content

factor_analysis

Function factor_analysis 

Source
pub fn factor_analysis(
    data: &[Vec<f64>],
    n_components: usize,
    max_iter: usize,
    tol: f64,
) -> FactorResult
Expand description

Fits a k-factor model to data by SVD-based EM.

§Arguments

  • data — observations; each inner slice is one row of equal dimension. Empty input yields an empty result.
  • n_components — number of latent factors k, clamped to the feature dimension.
  • max_iter — maximum EM iterations.
  • tol — convergence tolerance on the log-likelihood increase between iterations.

§Returns

A FactorResult with the loadings, noise variance, and reconstruction error.

§Examples

use stats_claw::algorithms::decomposition::factor_analysis;

let data = vec![
    vec![1.0, 1.1],
    vec![2.0, 2.2],
    vec![3.0, 2.9],
    vec![4.0, 4.1],
];
let r = factor_analysis(&data, 1, 1000, 1e-2);
// A single factor captures the shared trend, so reconstruction is tight.
assert!(r.reconstruction_error < 0.1, "error was {}", r.reconstruction_error);