use crate::matrix::{
common_io::{file_ext, write_lines},
sparse_stat::{narrow, variance_at},
traits::{IoOps, RunningStatOps},
};
use ndarray::{stack, ArrayBase, Axis, Data, Dimension, NdIndex, OwnedRepr, RemoveAxis, Zip};
#[inline]
fn safe_denom(s0: f64) -> f64 {
if s0 > 0.0 {
s0
} else {
1e-8
}
}
#[inline]
fn accumulate(npos: &mut f64, s0: &mut f64, s1: &mut f64, s2: &mut f64, x: f32) {
if x.is_finite() {
let v = f64::from(x);
if v > 0.0 {
*npos += 1.0;
}
*s0 += 1.0;
*s1 += v;
*s2 += v * v;
}
}
#[derive(Clone)]
pub struct RunningStatistics<S>
where
S: Dimension + RemoveAxis,
{
npos: ArrayBase<OwnedRepr<f64>, S>,
s0: ArrayBase<OwnedRepr<f64>, S>,
s1: ArrayBase<OwnedRepr<f64>, S>,
s2: ArrayBase<OwnedRepr<f64>, S>,
}
impl<S> RunningStatistics<S>
where
S: Dimension + RemoveAxis,
{
pub fn new(shape: S) -> Self {
let npos = ArrayBase::zeros(shape.clone());
let s0 = ArrayBase::zeros(shape.clone());
let s1 = ArrayBase::zeros(shape.clone());
let s2 = ArrayBase::zeros(shape);
RunningStatistics { npos, s0, s1, s2 }
}
pub fn add<V>(&mut self, xx: &ArrayBase<V, S>)
where
V: Data<Elem = f32>,
{
let xx = xx
.broadcast(self.s0.raw_dim())
.expect("incompatible shape for running statistics");
Zip::from(&mut self.npos)
.and(&mut self.s0)
.and(&mut self.s1)
.and(&mut self.s2)
.and(&xx)
.for_each(|npos, s0, s1, s2, &x| accumulate(npos, s0, s1, s2, x));
}
pub fn add_element<I>(&mut self, idx: &I, val: f32)
where
I: NdIndex<S> + Clone,
{
fn get<'a, S, I>(mat: &'a mut ArrayBase<OwnedRepr<f64>, S>, idx: &I) -> &'a mut f64
where
S: Dimension + RemoveAxis,
I: NdIndex<S> + Clone,
{
mat.get_mut(idx.clone()).expect("failed to access matrix")
}
accumulate(
get(&mut self.npos, idx),
get(&mut self.s0, idx),
get(&mut self.s1, idx),
get(&mut self.s2, idx),
val,
);
}
pub fn clear(&mut self) {
self.npos.fill(0.0);
self.s0.fill(0.0);
self.s1.fill(0.0);
self.s2.fill(0.0);
}
pub fn count_positives(&self) -> ArrayBase<OwnedRepr<f32>, S> {
self.npos.mapv(narrow::<f32>)
}
pub fn sum(&self) -> ArrayBase<OwnedRepr<f32>, S> {
self.s1.mapv(narrow::<f32>)
}
pub fn mean(&self) -> ArrayBase<OwnedRepr<f32>, S> {
Zip::from(&self.s0)
.and(&self.s1)
.map_collect(|&s0, &s1| narrow(s1 / safe_denom(s0)))
}
pub fn variance(&self) -> ArrayBase<OwnedRepr<f32>, S> {
Zip::from(&self.s0)
.and(&self.s1)
.and(&self.s2)
.map_collect(|&s0, &s1, &s2| narrow(variance_at(s1, s2, safe_denom(s0))))
}
pub fn std(&self) -> ArrayBase<OwnedRepr<f32>, S> {
Zip::from(&self.s0)
.and(&self.s1)
.and(&self.s2)
.map_collect(|&s0, &s1, &s2| narrow(variance_at(s1, s2, safe_denom(s0)).sqrt()))
}
pub fn shape(&self) -> &[usize] {
self.s0.shape()
}
pub fn save(
&self,
filename: &str,
names: &[Box<str>],
sep: &str,
row_column_name: Option<&str>,
) -> anyhow::Result<()> {
match file_ext(filename).unwrap_or(Box::from("")).as_ref() {
"parquet" => {
let nnz = &self.count_positives();
let tot = &self.sum();
let mu = &self.mean();
let sig = &self.std();
let n = nnz.len();
let nnz_col = nnz.clone().into_shape_with_order((n,)).unwrap();
let tot_col = tot.clone().into_shape_with_order((n,)).unwrap();
let mu_col = mu.clone().into_shape_with_order((n,)).unwrap();
let sig_col = sig.clone().into_shape_with_order((n,)).unwrap();
let stacked = stack(
Axis(1),
&[
nnz_col.view(),
tot_col.view(),
mu_col.view(),
sig_col.view(),
],
)
.unwrap();
let column_names: Vec<Box<str>> = vec!["nnz", "tot", "mu", "sig"]
.into_iter()
.map(|s| s.into())
.collect();
let row_col = row_column_name.or(Some("stat"));
stacked.to_parquet_with_names(
filename,
(Some(names), row_col),
Some(&column_names),
)?;
}
_ => {
let mut out = self.to_string_vec(names, sep)?;
let header = format!("#name{}nnz{}tot{}mu{}sig", sep, sep, sep, sep);
out.insert(0, header.into_boxed_str());
write_lines(&out, filename)?;
}
};
Ok(())
}
pub fn to_string_vec(&self, names: &[Box<str>], sep: &str) -> anyhow::Result<Vec<Box<str>>> {
if names.len() != self.shape()[0] {
anyhow::bail!(
"The number of names does not match the number of the first dimension of the statistics"
);
}
let nnz_: Vec<Box<str>> = to_string_vec(&self.count_positives(), sep);
let tot_ = to_string_vec(&self.sum(), sep);
let mu_: Vec<Box<str>> = to_string_vec(&self.mean(), sep);
let sig_: Vec<Box<str>> = to_string_vec(&self.std(), sep);
let out: Vec<Box<str>> = (0..self.shape()[0])
.map(|i| {
format!(
"{}{}{}{}{}{}{}{}{}",
names[i], sep, nnz_[i], sep, tot_[i], sep, mu_[i], sep, sig_[i]
)
.into_boxed_str()
})
.collect();
Ok(out)
}
}
impl<S> RunningStatOps<f32> for RunningStatistics<S>
where
S: Dimension + RemoveAxis,
{
type Output = ArrayBase<OwnedRepr<f32>, S>;
fn clear(&mut self) {
Self::clear(self)
}
fn count_positives(&self) -> Self::Output {
Self::count_positives(self)
}
fn sum(&self) -> Self::Output {
Self::sum(self)
}
fn mean(&self) -> Self::Output {
Self::mean(self)
}
fn variance(&self) -> Self::Output {
Self::variance(self)
}
fn std(&self) -> Self::Output {
Self::std(self)
}
}
fn to_string_vec<S>(xx: &ArrayBase<OwnedRepr<f32>, S>, sep: &str) -> Vec<Box<str>>
where
S: Dimension + RemoveAxis,
{
xx.axis_iter(Axis(0))
.map(|m| {
m.iter()
.map(|v| {
if *v > 1e-4 {
format!("{:.4}", v)
.trim_end_matches('0')
.trim_end_matches('.')
.to_string()
} else if *v > 1e-20 {
format!("{:.4e}", v)
} else {
"0".to_string()
}
})
.collect::<Vec<String>>()
.join(sep)
.clone()
.into_boxed_str()
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use ndarray::{arr1, Ix1};
#[test]
fn f32_sums_stay_exact_past_two_to_the_24() {
let n = (1usize << 24) + 10;
let mut stat = RunningStatistics::new(Ix1(2));
for _ in 0..n {
stat.add_element(&0, 1.0);
}
assert_eq!(stat.sum(), arr1(&[n as f32, 0.0]));
assert_eq!(stat.count_positives(), arr1(&[n as f32, 0.0]));
assert_eq!(stat.std(), arr1(&[0.0, 0.0]));
}
#[test]
fn f32_variance_of_large_mean_small_spread() {
let mut stat = RunningStatistics::new(Ix1(1));
for j in 0..10_000 {
let v = 1000.0f32 + if j % 2 == 0 { 0.5 } else { -0.5 };
stat.add_element(&0, v);
}
let var = stat.variance()[0];
assert!((var - 0.25).abs() < 1e-4, "variance {var}");
assert!((stat.std()[0] - 0.5).abs() < 1e-4);
let via_trait = <RunningStatistics<Ix1> as RunningStatOps<f32>>::variance(&stat);
assert_eq!(via_trait, stat.variance());
}
#[test]
fn constant_row_has_zero_sd() {
let mut stat = RunningStatistics::new(Ix1(1));
let x = arr1(&[0.1f32]);
for _ in 0..1000 {
stat.add(&x);
}
assert!(stat.variance()[0] >= 0.0);
assert!(!stat.std()[0].is_nan());
let sd = <RunningStatistics<Ix1> as RunningStatOps<f32>>::std(&stat);
assert!(!sd[0].is_nan());
}
#[test]
fn non_finite_values_are_skipped() {
let mut stat = RunningStatistics::new(Ix1(2));
stat.add(&arr1(&[2.0f32, f32::NAN]));
stat.add(&arr1(&[4.0f32, f32::INFINITY]));
assert_eq!(stat.count_positives(), arr1(&[2.0, 0.0]));
assert_eq!(stat.sum(), arr1(&[6.0, 0.0]));
assert!((stat.mean()[0] - 3.0).abs() < 1e-6);
assert_eq!(stat.mean()[1], 0.0);
assert!((stat.variance()[0] - 1.0).abs() < 1e-6);
}
#[test]
fn trait_matches_inherent() {
type Stat = RunningStatistics<Ix1>;
let mut stat = Stat::new(Ix1(2));
stat.add(&arr1(&[1.0f32, -2.0]));
stat.add(&arr1(&[3.0f32, 0.0]));
assert_eq!(
RunningStatOps::count_positives(&stat),
Stat::count_positives(&stat)
);
assert_eq!(RunningStatOps::sum(&stat), Stat::sum(&stat));
assert_eq!(RunningStatOps::mean(&stat), Stat::mean(&stat));
assert_eq!(RunningStatOps::variance(&stat), Stat::variance(&stat));
assert_eq!(RunningStatOps::std(&stat), Stat::std(&stat));
RunningStatOps::clear(&mut stat);
assert_eq!(stat.sum(), arr1(&[0.0, 0.0]));
}
}