use ndarray::{Array1, Array2};
use solow_core::error::{Error, Result};
use solow_linalg::{inv, svd};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RotationMethod {
Varimax,
Quartimax,
Oblimin,
Quartimin,
Promax,
}
impl RotationMethod {
pub fn from_name(name: &str) -> Result<Self> {
match name.to_ascii_lowercase().as_str() {
"varimax" => Ok(RotationMethod::Varimax),
"quartimax" => Ok(RotationMethod::Quartimax),
"oblimin" => Ok(RotationMethod::Oblimin),
"quartimin" => Ok(RotationMethod::Quartimin),
"promax" => Ok(RotationMethod::Promax),
other => Err(Error::Shape(format!("unknown rotation method: {other}"))),
}
}
}
#[derive(Clone, Debug)]
pub struct Rotation {
pub loadings: Array2<f64>,
pub rotation: Array2<f64>,
}
pub fn rotate_factors(loadings: &Array2<f64>, method: &str) -> Result<Rotation> {
rotate_with(loadings, RotationMethod::from_name(method)?)
}
pub fn rotate_with(loadings: &Array2<f64>, method: RotationMethod) -> Result<Rotation> {
match method {
RotationMethod::Promax => promax(loadings, 2.0),
RotationMethod::Varimax => gpa(loadings, 1.0, true),
RotationMethod::Quartimax => gpa(loadings, 0.0, true),
RotationMethod::Oblimin | RotationMethod::Quartimin => gpa(loadings, 0.0, false),
}
}
fn rotate_a(a: &Array2<f64>, t: &Array2<f64>, orthogonal: bool) -> Result<Array2<f64>> {
if orthogonal {
Ok(a.dot(t))
} else {
let ti = inv(t)?;
Ok(a.dot(&ti.t()))
}
}
fn objective(l: &Array2<f64>, gamma: f64, orthogonal: bool) -> (f64, Array2<f64>) {
let (p, k) = l.dim();
let l2 = l.mapv(|x| x * x);
if orthogonal {
let x = if gamma == 0.0 {
l2.clone()
} else {
let mut col_sums = Array1::<f64>::zeros(k);
for j in 0..k {
col_sums[j] = l2.column(j).sum();
}
let mut x = l2.clone();
for i in 0..p {
for j in 0..k {
x[[i, j]] -= (gamma / p as f64) * col_sums[j];
}
}
x
};
let phi = -elem_dot(&l2, &x) / 4.0;
let gphi = -hadamard(l, &x);
(phi, gphi)
} else {
let m = if gamma == 0.0 {
l2.clone()
} else {
let mut col_sums = Array1::<f64>::zeros(k);
for j in 0..k {
col_sums[j] = l2.column(j).sum();
}
let mut m = l2.clone();
for i in 0..p {
for j in 0..k {
m[[i, j]] -= (gamma / p as f64) * col_sums[j];
}
}
m
};
let mut x = Array2::<f64>::zeros((p, k));
for i in 0..p {
let row_sum: f64 = (0..k).map(|j| m[[i, j]]).sum();
for c in 0..k {
x[[i, c]] = row_sum - m[[i, c]];
}
}
let phi = elem_dot(&l2, &x) / 4.0;
let gphi = hadamard(l, &x);
(phi, gphi)
}
}
fn hadamard(a: &Array2<f64>, b: &Array2<f64>) -> Array2<f64> {
let mut out = a.clone();
out.zip_mut_with(b, |x, &y| *x *= y);
out
}
fn elem_dot(a: &Array2<f64>, b: &Array2<f64>) -> f64 {
a.iter().zip(b.iter()).map(|(&x, &y)| x * y).sum()
}
fn fro_norm(a: &Array2<f64>) -> f64 {
a.iter().map(|&x| x * x).sum::<f64>().sqrt()
}
fn gpa(a: &Array2<f64>, gamma: f64, orthogonal: bool) -> Result<Rotation> {
let max_tries = 501usize;
let tol = 1e-5;
let k = a.ncols();
let mut t = Array2::<f64>::eye(k);
let mut al = 1.0_f64;
let (mut f, mut g) = compute_fg(a, &t, gamma, orthogonal)?;
for _ in 0..max_tries {
let gp = if orthogonal {
let m = t.t().dot(&g);
let s = (&m + &m.t()).mapv(|x| x / 2.0);
&g - &t.dot(&s)
} else {
let tg = hadamard(&t, &g);
let mut gp = g.clone();
for j in 0..k {
let col_sum: f64 = tg.column(j).sum();
for i in 0..k {
gp[[i, j]] -= t[[i, j]] * col_sum;
}
}
gp
};
let s = fro_norm(&gp);
if s < tol {
break;
}
al *= 2.0;
let mut tt = t.clone();
let mut ft = f;
let mut g_next = g.clone();
for _ in 0..11 {
let x = &t - &gp.mapv(|v| v * al);
tt = if orthogonal {
let (u, _s, vt) = svd(&x)?;
u.dot(&vt)
} else {
let mut tt = x.clone();
for j in 0..k {
let nrm: f64 = (0..k).map(|i| x[[i, j]] * x[[i, j]]).sum::<f64>().sqrt();
let v = 1.0 / nrm;
for i in 0..k {
tt[[i, j]] = x[[i, j]] * v;
}
}
tt
};
let (fnew, gnew) = compute_fg(a, &tt, gamma, orthogonal)?;
ft = fnew;
g_next = gnew;
if ft < f - 0.5 * s * s * al {
break;
}
al /= 2.0;
}
t = tt;
f = ft;
g = g_next;
}
let loadings = rotate_a(a, &t, orthogonal)?;
Ok(Rotation {
loadings,
rotation: t,
})
}
fn compute_fg(
a: &Array2<f64>,
t: &Array2<f64>,
gamma: f64,
orthogonal: bool,
) -> Result<(f64, Array2<f64>)> {
if orthogonal {
let l = a.dot(t);
let (f, gq) = objective(&l, gamma, true);
let g = a.t().dot(&gq);
Ok((f, g))
} else {
let ti = inv(t)?;
let l = a.dot(&ti.t());
let (f, gq) = objective(&l, gamma, false);
let inner = l.t().dot(&gq).dot(&ti);
let g = inner.t().mapv(|x| -x);
Ok((f, g))
}
}
fn promax(a: &Array2<f64>, power: f64) -> Result<Rotation> {
let v = gpa(a, 1.0, true)?.loadings;
let h = v.mapv(|x| {
if x == 0.0 {
0.0
} else {
x.abs().powf(power) / x
}
});
let ata = a.t().dot(a);
let ata_inv = inv(&ata)?;
let s = ata_inv.dot(&a.t()).dot(&h);
let sts = s.t().dot(&s);
let sts_inv = inv(&sts)?;
let k = s.ncols();
let mut d = Array1::<f64>::zeros(k);
for j in 0..k {
d[j] = sts_inv[[j, j]].sqrt();
}
let mut sd = s.clone();
for j in 0..k {
for i in 0..sd.nrows() {
sd[[i, j]] *= d[j];
}
}
let sd_inv = inv(&sd)?;
let t = sd_inv.t().to_owned();
let loadings = a.dot(&t);
Ok(Rotation {
loadings,
rotation: t,
})
}
#[cfg(test)]
mod tests {
use super::*;
use ndarray::array;
fn sample() -> Array2<f64> {
array![[0.8, 0.1], [0.7, 0.2], [0.2, 0.9], [0.1, 0.8], [0.5, 0.5],]
}
#[test]
fn varimax_is_orthogonal_and_consistent() {
let a = sample();
let r = rotate_factors(&a, "varimax").unwrap();
let tt = r.rotation.t().dot(&r.rotation);
for i in 0..2 {
for j in 0..2 {
let want = if i == j { 1.0 } else { 0.0 };
assert!((tt[[i, j]] - want).abs() < 1e-9, "TᵀT not identity");
}
}
let l = a.dot(&r.rotation);
for i in 0..a.nrows() {
for j in 0..2 {
assert!((l[[i, j]] - r.loadings[[i, j]]).abs() < 1e-12);
}
}
}
#[test]
fn quartimax_runs() {
let a = sample();
let r = rotate_factors(&a, "quartimax").unwrap();
let l = a.dot(&r.rotation);
for i in 0..a.nrows() {
for j in 0..2 {
assert!((l[[i, j]] - r.loadings[[i, j]]).abs() < 1e-12);
}
}
}
#[test]
fn quartimin_is_oblique_consistent() {
let a = sample();
let r = rotate_factors(&a, "quartimin").unwrap();
let l = rotate_a(&a, &r.rotation, false).unwrap();
for i in 0..a.nrows() {
for j in 0..2 {
assert!((l[[i, j]] - r.loadings[[i, j]]).abs() < 1e-12);
}
}
for j in 0..2 {
let nrm: f64 = (0..2)
.map(|i| r.rotation[[i, j]].powi(2))
.sum::<f64>()
.sqrt();
assert!((nrm - 1.0).abs() < 1e-9);
}
}
#[test]
fn oblimin_defaults_to_quartimin() {
let a = sample();
let r1 = rotate_factors(&a, "oblimin").unwrap();
let r2 = rotate_factors(&a, "quartimin").unwrap();
for i in 0..a.nrows() {
for j in 0..2 {
assert!((r1.loadings[[i, j]] - r2.loadings[[i, j]]).abs() < 1e-12);
}
}
}
#[test]
fn promax_consistent() {
let a = sample();
let r = rotate_factors(&a, "promax").unwrap();
let l = a.dot(&r.rotation);
for i in 0..a.nrows() {
for j in 0..2 {
assert!((l[[i, j]] - r.loadings[[i, j]]).abs() < 1e-12);
}
}
}
#[test]
fn unknown_method_errs() {
let a = sample();
assert!(rotate_factors(&a, "nonsense").is_err());
}
}