rust_qrng 0.1.2

Tsotchkes quantum random number generator library with cryptographic, financial, and gaming applications converted to Rust
Documentation
// src/statistical/mod.rs

pub mod tests;

/// Calculate the mean of a dataset
pub fn mean(data: &[f64]) -> f64 {
    if data.is_empty() {
        return 0.0;
    }
    data.iter().sum::<f64>() / data.len() as f64
}

/// Calculate the variance of a dataset
pub fn variance(data: &[f64]) -> f64 {
    if data.is_empty() {
        return 0.0;
    }
    let mean_val = mean(data);
    data.iter()
        .map(|x| (x - mean_val).powi(2))
        .sum::<f64>() / data.len() as f64
}

/// Calculate the standard deviation of a dataset
pub fn standard_deviation(data: &[f64]) -> f64 {
    variance(data).sqrt()
}

/// Calculate the median of a dataset
pub fn median(data: &[f64]) -> f64 {
    if data.is_empty() {
        return 0.0;
    }
    let mut sorted = data.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
    let len = sorted.len();
    if len % 2 == 0 {
        (sorted[len / 2 - 1] + sorted[len / 2]) / 2.0
    } else {
        sorted[len / 2]
    }
}

/// Calculate the mode of a dataset
pub fn mode(data: &[f64]) -> Option<f64> {
    use std::collections::HashMap;
    let mut counts = HashMap::new();
    for &value in data {
        *counts.entry(value.to_bits()).or_insert(0) += 1;
    }
    counts.into_iter()
        .max_by_key(|&(_, count)| count)
        .map(|(bits, _)| f64::from_bits(bits))
}