pub struct DescriptiveStats<'a> { /* private fields */ }Expand description
Descriptive statistics computed on a vector of f32 values.
Holds a reference to the data vector to avoid unnecessary copying. Uses lazy evaluation and caching for repeated computations.
Implementations§
Source§impl<'a> DescriptiveStats<'a>
impl<'a> DescriptiveStats<'a>
Sourcepub fn new(data: &'a Vector<f32>) -> DescriptiveStats<'a>
pub fn new(data: &'a Vector<f32>) -> DescriptiveStats<'a>
Sourcepub fn quantile(&self, q: f64) -> Result<f32, String>
pub fn quantile(&self, q: f64) -> Result<f32, String>
Compute quantile using linear interpolation (R-7 method).
Uses the method from Hyndman & Fan (1996) commonly used in
statistical packages (R, NumPy, Pandas).
§Performance
Uses QuickSelect (select_nth_unstable) for O(n) average-case
performance instead of full sort O(n log n). This is a Toyota Way
Muda elimination optimization (Floyd & Rivest 1975).
§Arguments
q- Quantile value in [0, 1]
§Returns
Interpolated quantile value
§Errors
Returns error if:
- Data vector is empty
- Quantile q is not in [0, 1]
§Examples
use aprender::stats::DescriptiveStats;
use trueno::Vector;
let data = Vector::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let stats = DescriptiveStats::new(&data);
assert_eq!(stats.quantile(0.5).expect("median should be computable for valid data"), 3.0); // median
assert_eq!(stats.quantile(0.0).expect("min quantile should be computable for valid data"), 1.0); // min
assert_eq!(stats.quantile(1.0).expect("max quantile should be computable for valid data"), 5.0); // maxSourcepub fn percentiles(&self, percentiles: &[f64]) -> Result<Vec<f32>, String>
pub fn percentiles(&self, percentiles: &[f64]) -> Result<Vec<f32>, String>
Compute multiple percentiles efficiently (single sort).
When computing multiple quantiles, it’s more efficient to sort once and then index into the sorted array. This is O(n log n) amortized.
§Arguments
percentiles- Slice of percentile values (0-100)
§Returns
Vector of percentile values in the same order as input
§Examples
use aprender::stats::DescriptiveStats;
use trueno::Vector;
let data = Vector::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let stats = DescriptiveStats::new(&data);
let p = stats.percentiles(&[25.0, 50.0, 75.0]).expect("percentiles should be computable for valid data");
assert_eq!(p, vec![2.0, 3.0, 4.0]);Sourcepub fn five_number_summary(&self) -> Result<FiveNumberSummary, String>
pub fn five_number_summary(&self) -> Result<FiveNumberSummary, String>
Compute five-number summary: min, Q1, median, Q3, max.
This is the foundation for box plots and outlier detection.
§Examples
use aprender::stats::DescriptiveStats;
use trueno::Vector;
let data = Vector::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let stats = DescriptiveStats::new(&data);
let summary = stats.five_number_summary().expect("five-number summary should be computable for valid data");
assert_eq!(summary.min, 1.0);
assert_eq!(summary.q1, 2.0);
assert_eq!(summary.median, 3.0);
assert_eq!(summary.q3, 4.0);
assert_eq!(summary.max, 5.0);Sourcepub fn iqr(&self) -> Result<f32, String>
pub fn iqr(&self) -> Result<f32, String>
Compute interquartile range (IQR = Q3 - Q1).
The IQR is a measure of statistical dispersion, being equal to the difference between 75th and 25th percentiles.
§Examples
use aprender::stats::DescriptiveStats;
use trueno::Vector;
let data = Vector::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let stats = DescriptiveStats::new(&data);
assert_eq!(stats.iqr().expect("IQR should be computable for valid data"), 2.0);Sourcepub fn histogram_auto(&self) -> Result<Histogram, String>
pub fn histogram_auto(&self) -> Result<Histogram, String>
Compute histogram with automatic bin selection (Freedman-Diaconis rule).
Uses Freedman-Diaconis rule: bin_width = 2 * IQR / n^(1/3)
This is optimal for unimodal, symmetric distributions.
§Examples
use aprender::stats::DescriptiveStats;
use trueno::Vector;
let data = Vector::from_slice(&[1.0, 2.0, 2.0, 3.0, 5.0]);
let stats = DescriptiveStats::new(&data);
let hist = stats.histogram_auto().expect("histogram should be computable for valid data");
assert_eq!(hist.bins.len(), hist.counts.len() + 1);Source§impl DescriptiveStats<'_>
impl DescriptiveStats<'_>
Sourcepub fn histogram_method(&self, method: BinMethod) -> Result<Histogram, String>
pub fn histogram_method(&self, method: BinMethod) -> Result<Histogram, String>
Compute histogram with specified bin selection method.
§Arguments
method- Bin selection method to use
§Examples
use aprender::stats::{DescriptiveStats, BinMethod};
use trueno::Vector;
let data = Vector::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let stats = DescriptiveStats::new(&data);
let hist = stats.histogram_method(BinMethod::Sturges).expect("histogram should be computable for valid data");Sourcepub fn histogram(&self, n_bins: usize) -> Result<Histogram, String>
pub fn histogram(&self, n_bins: usize) -> Result<Histogram, String>
Compute histogram with fixed number of bins.
§Arguments
n_bins- Number of bins (must be >= 1)
§Examples
use aprender::stats::DescriptiveStats;
use trueno::Vector;
let data = Vector::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let stats = DescriptiveStats::new(&data);
let hist = stats.histogram(3).expect("histogram should be computable for valid data");
assert_eq!(hist.bins.len(), 4); // n_bins + 1 edges
assert_eq!(hist.counts.len(), 3);Sourcepub fn histogram_edges(&self, edges: &[f32]) -> Result<Histogram, String>
pub fn histogram_edges(&self, edges: &[f32]) -> Result<Histogram, String>
Compute histogram with custom bin edges.
§Arguments
edges- Bin edges (must be sorted and have length >= 2)
§Examples
use aprender::stats::DescriptiveStats;
use trueno::Vector;
let data = Vector::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let stats = DescriptiveStats::new(&data);
let hist = stats.histogram_edges(&[0.0, 2.5, 5.0, 10.0]).expect("histogram should be computable for valid bin edges");
assert_eq!(hist.bins.len(), 4);
assert_eq!(hist.counts.len(), 3);Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for DescriptiveStats<'a>
impl<'a> RefUnwindSafe for DescriptiveStats<'a>
impl<'a> Send for DescriptiveStats<'a>
impl<'a> Sync for DescriptiveStats<'a>
impl<'a> Unpin for DescriptiveStats<'a>
impl<'a> UnsafeUnpin for DescriptiveStats<'a>
impl<'a> UnwindSafe for DescriptiveStats<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more