Skip to main content

DescriptiveStats

Struct DescriptiveStats 

Source
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>

Source

pub fn new(data: &'a Vector<f32>) -> DescriptiveStats<'a>

Create a new DescriptiveStats instance from a data vector.

§Arguments
  • data - Reference to a Vector<f32> containing the data
§Examples
use aprender::stats::DescriptiveStats;
use trueno::Vector;

let data = Vector::from_slice(&[1.0, 2.0, 3.0]);
let stats = DescriptiveStats::new(&data);
Source

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); // max
Source

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]);
Source

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);
Source

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);
Source

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<'_>

Source

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");
Source

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);
Source

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§

Source§

impl<'a> Debug for DescriptiveStats<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,