use crate::algorithms::moments::{max_abs_deviation, mean};
use crate::errors::stats_error::StatsError;
use crate::types::pairwise_sum::PairwiseSum;
use alloc::vec::Vec;
use deep_causality_algebra::RealField;
use deep_causality_num::FromPrimitive;
pub fn pearson<T>(x: &[T], y: &[T]) -> Result<(T, usize), StatsError>
where
T: RealField + FromPrimitive,
{
if x.len() != y.len() {
return Err(StatsError::DimensionMismatch(
"a correlation needs two samples of the same length",
));
}
correlate(x, y)
}
fn correlate<T>(x: &[T], y: &[T]) -> Result<(T, usize), StatsError>
where
T: RealField + FromPrimitive,
{
if x.is_empty() {
return Err(StatsError::EmptyInput(
"a correlation over no pairs is undefined",
));
}
if x.len() < 2 {
return Err(StatsError::InsufficientSamples(
"a correlation needs two pairs: one pair has no dispersion to relate",
));
}
if x.iter().chain(y.iter()).any(|v| !v.is_finite()) {
return Err(StatsError::NonFiniteInput(
"a non-finite observation has no correlation",
));
}
let n = x.len();
let mean_x = mean(x)?;
let mean_y = mean(y)?;
let mut sxy_acc = PairwiseSum::new();
let mut sxx_acc = PairwiseSum::new();
let mut syy_acc = PairwiseSum::new();
for (&xi, &yi) in x.iter().zip(y.iter()) {
let dx = xi - mean_x;
let dy = yi - mean_y;
sxy_acc.push(dx * dy);
sxx_acc.push(dx * dx);
syy_acc.push(dy * dy);
}
let (sxy, sxx, syy) = (sxy_acc.total(), sxx_acc.total(), syy_acc.total());
let usable = sxx.is_finite() && syy.is_finite() && sxy.is_finite();
if usable && sxx > T::zero() && syy > T::zero() {
return Ok((ratio(sxy, sxx, syy), n));
}
let scale_x = max_abs_deviation(x, mean_x);
let scale_y = max_abs_deviation(y, mean_y);
if scale_x <= T::zero() || scale_y <= T::zero() {
return Ok((T::zero(), n));
}
let mut txy_acc = PairwiseSum::new();
let mut txx_acc = PairwiseSum::new();
let mut tyy_acc = PairwiseSum::new();
for (&xi, &yi) in x.iter().zip(y.iter()) {
let dx = (xi - mean_x) / scale_x;
let dy = (yi - mean_y) / scale_y;
txy_acc.push(dx * dy);
txx_acc.push(dx * dx);
tyy_acc.push(dy * dy);
}
let (txy, txx, tyy) = (txy_acc.total(), txx_acc.total(), tyy_acc.total());
if txx <= T::zero() || tyy <= T::zero() {
return Ok((T::zero(), n));
}
Ok((ratio(txy, txx, tyy), n))
}
fn ratio<T>(sxy: T, sxx: T, syy: T) -> T
where
T: RealField,
{
let product = sxx * syy;
if product.is_finite() && product > T::zero() {
sxy / product.sqrt()
} else {
(sxy / sxx.sqrt()) / syy.sqrt()
}
}
pub fn pearson_pairwise_complete<T>(
x: &[Option<T>],
y: &[Option<T>],
) -> Result<(T, usize), StatsError>
where
T: RealField + FromPrimitive,
{
if x.len() != y.len() {
return Err(StatsError::DimensionMismatch(
"a correlation needs two samples of the same length",
));
}
let mut xs: Vec<T> = Vec::new();
let mut ys: Vec<T> = Vec::new();
for (a, b) in x.iter().zip(y.iter()) {
if let (Some(&a), Some(&b)) = (a.as_ref(), b.as_ref()) {
xs.push(a);
ys.push(b);
}
}
correlate(&xs, &ys)
}