use crate::errors::stats_error::StatsError;
use crate::types::pairwise_sum::PairwiseSum;
use alloc::vec;
use alloc::vec::Vec;
use deep_causality_algebra::RealField;
use deep_causality_linear::{DenseMatrix, DenseVector, solve};
use deep_causality_num::FromPrimitive;
fn shape<T>(data: &[T], observations: usize, variables: usize) -> Result<(), StatsError> {
if observations == 0 || variables == 0 {
return Err(StatsError::EmptyInput(
"a data matrix needs at least one observation and one variable",
));
}
let cells = observations.checked_mul(variables).ok_or_else(|| {
StatsError::DimensionMismatch(
"the stated observation and variable counts overflow a usize: no matrix has that shape",
)
})?;
if data.len() != cells {
return Err(StatsError::DimensionMismatch(
"the data length is not the product of the stated observation and variable counts",
));
}
Ok(())
}
pub fn column_means<T>(
data: &[T],
observations: usize,
variables: usize,
) -> Result<Vec<T>, StatsError>
where
T: RealField + FromPrimitive,
{
shape(data, observations, variables)?;
let n = T::from_usize(observations).ok_or_else(|| {
StatsError::ConversionFailed(
"an observation count is not representable in the working scalar",
)
})?;
let mut sums = vec![PairwiseSum::new(); variables];
for row in 0..observations {
let base = row * variables;
for (col, sum) in sums.iter_mut().enumerate() {
sum.push(data[base + col]);
}
}
Ok(sums.iter().map(|sum| sum.total() / n).collect())
}
pub fn covariance_matrix<T>(
data: &[T],
observations: usize,
variables: usize,
) -> Result<Vec<T>, StatsError>
where
T: RealField + FromPrimitive,
{
shape(data, observations, variables)?;
if observations < 2 {
return Err(StatsError::InsufficientSamples(
"the corrected covariance needs two observations: with one the divisor n − 1 is zero",
));
}
let means = column_means(data, observations, variables)?;
let denominator = T::from_usize(observations - 1).ok_or_else(|| {
StatsError::ConversionFailed(
"an observation count is not representable in the working scalar",
)
})?;
let mut sums = vec![PairwiseSum::new(); variables * variables];
for row in 0..observations {
let base = row * variables;
for i in 0..variables {
let di = data[base + i] - means[i];
for j in 0..variables {
sums[i * variables + j].push(di * (data[base + j] - means[j]));
}
}
}
Ok(sums.iter().map(|sum| sum.total() / denominator).collect())
}
pub fn conditional_variance<T>(
covariance: &[T],
variables: usize,
target: usize,
parents: &[usize],
ridge: T,
) -> Result<T, StatsError>
where
T: RealField + FromPrimitive,
{
shape(covariance, variables, variables)?;
if target >= variables || parents.iter().any(|&p| p >= variables) {
return Err(StatsError::DimensionMismatch(
"a target or parent index lies outside the covariance matrix",
));
}
if parents.contains(&target) {
return Err(StatsError::DimensionMismatch(
"the target is among its own parents: the variance of a variable given itself is zero \
by definition, and a positive ridge would return a positive number for it",
));
}
if parents
.iter()
.enumerate()
.any(|(i, p)| parents[..i].contains(p))
{
return Err(StatsError::DimensionMismatch(
"a parent is listed twice: the parent block is then singular, and at a positive ridge \
the answer would depend on how many times a variable was named",
));
}
let sigma_yy = covariance[target * variables + target];
let k = parents.len();
if k == 0 {
return Ok(sigma_yy);
}
let mut sigma_yp = vec![T::zero(); k];
for (slot, &p) in sigma_yp.iter_mut().zip(parents.iter()) {
*slot = covariance[target * variables + p];
}
let mut sigma_pp = vec![T::zero(); k * k];
for (i, &pi) in parents.iter().enumerate() {
for (j, &pj) in parents.iter().enumerate() {
let mut v = covariance[pi * variables + pj];
if i == j {
v += ridge;
}
sigma_pp[i * k + j] = v;
}
}
let block = DenseMatrix::from_vec(sigma_pp, k, k)
.map_err(|_| StatsError::DimensionMismatch("the parent block is not square"))?;
let z = solve(&block, &DenseVector::from_vec(sigma_yp.clone())).map_err(|_| {
StatsError::RankDeficient(
"the parent block has no unique solution at this ridge: the parents are collinear",
)
})?;
let mut reduction = T::zero();
for (i, zi) in z.as_slice().iter().enumerate() {
reduction += sigma_yp[i] * *zi;
}
Ok(sigma_yy - reduction)
}