ruthril 0.1.2

A powerful AI/ML framework is under development
Documentation
/// Basic statistical utilities for the Ruthril framework.
/// Implements mean, variance, and standard deviation.

pub struct Statistics;

impl Statistics {
    /// Calculates the arithmetic mean of a slice of f64.
    pub fn mean(data: &[f64]) -> Option<f64> {
        if data.is_empty() {
            return None;
        }
        let sum: f64 = data.iter().sum();
        Some(sum / data.len() as f64)
    }

    /// Calculates the sample variance of a slice of f64.
    pub fn variance(data: &[f64]) -> Option<f64> {
        if data.len() < 2 {
            return None;
        }
        let mean = Self::mean(data)?;
        let var = data.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (data.len() as f64 - 1.0);
        Some(var)
    }

    /// Calculates the sample standard deviation of a slice of f64.
    pub fn std_deviation(data: &[f64]) -> Option<f64> {
        Self::variance(data).map(|v| v.sqrt())
    }
}