Skip to main content

Module factor_analysis

Module factor_analysis 

Source
Expand description

Factor Analysis (FA) via the EM algorithm.

Factor Analysis assumes that data is generated by a linear combination of latent factors plus independent Gaussian noise:

X = W Z + μ + ε,   Z ~ N(0, I),   ε ~ N(0, diag(ψ))

where:

  • W is the (n_features × n_components) loading matrix,
  • Z is the (n_components,) latent factor vector,
  • ψ is the (n_features,) noise variance vector.

§Algorithm

  1. Centre the data: X_c = X - μ.
  2. E-step: compute the posterior mean and covariance of Z:
    Σ_z = (I + W^T diag(ψ)⁻¹ W)⁻¹
    E[Z | X] = Σ_z W^T diag(ψ)⁻¹ X_c^T
  3. M-step: update W and ψ via maximum-likelihood closed-form updates.
  4. Repeat until convergence (log-likelihood change < tol).

§Examples

use ferrolearn_decomp::factor_analysis::FactorAnalysis;
use ferrolearn_core::traits::{Fit, Transform};
use ndarray::Array2;

let fa = FactorAnalysis::new(2);
let x = Array2::from_shape_vec(
    (10, 4),
    (0..40).map(|v| v as f64 * 0.1 + (v % 3) as f64 * 0.5).collect(),
).unwrap();
let fitted = fa.fit(&x, &()).unwrap();
let scores = fitted.transform(&x).unwrap();
assert_eq!(scores.ncols(), 2);

Structs§

FactorAnalysis
Factor Analysis configuration.
FittedFactorAnalysis
A fitted Factor Analysis model.