cheatah
Module

statistics

Descriptive statistics over numeric sequences — mean, median, variance, and standard deviation (population and sample).

import statistics

statistics.mean([2, 4, 4, 4, 5, 5, 7, 9])   # 5.0
statistics.median([3, 1, 2])                 # 2.0
statistics.stdev([1, 2, 3, 4, 5])            # sample std-dev

This is a header-only module: every function is a template constrained by the NumericRange concept, so it accepts any iterable of arithmetic values (list[float], array[int], …).

Functions

Reductions:

  • sum(data) — sum of the elements (as double).

  • count(data) — number of elements.

  • mean(data) — arithmetic mean (0.0 if empty).

Spread:

  • pvariance(data) / pstdev(data) — population variance / std-dev (÷ N).

  • variance(data) / stdev(data) — sample variance / std-dev (÷ N−1).

Order statistics:

  • median(data) — middle value, or the mean of the two middle values.

Per-function docs (parameters, runtime complexity, heap behavior) are in statistics.hpp. Tested in ../tests/statistics_test.cpp; ASan + Valgrind clean via the QA gate (security/run-valgrind.sh).

Functions

fn double sum(const R &data) source#

Sum of the elements.

Accumulates every element into a double, so an empty range sums to 0.0 and integer inputs are widened before adding (no integer overflow).

Parameters
data

the numeric range.

Returns

Σdata as double.

Complexity

O(n) single pass.

Allocation

none.

Compile-run testStatisticsCompileRun.Sum
Performance20.22 ns/call in cheatah · 1456 ns/call in NumPy 1.26.4 · ≈72× faster
fn std::size_t count(const R &data) source#

Element count.

Parameters
data

the numeric range.

Returns

the number of elements.

Complexity

O(n) single pass.

Allocation

none.

Performance8.92 ns/call in cheatah · 122 ns/call in NumPy 1.26.4 · ≈13.7× faster
fn double mean(const R &data) source#

Arithmetic mean.

Computes sum/count, but guards division by zero: an empty range returns 0.0 rather than NaN.

Parameters
data

the numeric range.

Returns

the mean, or 0.0 if empty.

Complexity

O(n) (two passes: count + sum).

Allocation

none.

Performance19.77 ns/call in cheatah · 2052 ns/call in NumPy 1.26.4 · ≈103.2× faster
fn double pvariance(const R &data) source#

Population variance (divide by N).

Mean of the squared deviations from the mean, dividing by N (treats data as the entire population). Returns 0.0 for an empty or single-element range.

Parameters
data

the numeric range.

Returns

the variance, or 0.0 if empty.

Complexity

O(n).

Allocation

none.

Performance39.83 ns/call in cheatah · 6704 ns/call in NumPy 1.26.4 · ≈170.9× faster
fn double pstdev(const R &data) source#

Population standard deviation.

Square root of pvariance, so it is 0.0 for empty or single-element ranges and never negative.

Parameters
data

the numeric range.

Returns

√pvariance(data).

Complexity

O(n).

Allocation

none.

Performance42.71 ns/call in cheatah · 7191 ns/call in NumPy 1.26.4 · ≈168.3× faster
fn double variance(const R &data) source#

Sample variance (divide by N−1).

Sum of squared deviations from the mean divided by N−1 (Bessel's correction, estimating the variance of the wider population from a sample). Requires at least two elements; an empty or single-element range returns 0.0 rather than dividing by zero.

Parameters
data

the numeric range.

Returns

the variance, or 0.0 if fewer than 2 elements.

Complexity

O(n).

Allocation

none.

Performance44.73 ns/call in cheatah · 6692 ns/call in NumPy 1.26.4 · ≈152.1× faster
fn double stdev(const R &data) source#

Sample standard deviation.

Square root of variance, so it is 0.0 when there are fewer than two elements.

Parameters
data

the numeric range.

Returns

√variance(data).

Complexity

O(n).

Allocation

none.

Performance47.87 ns/call in cheatah · 7286 ns/call in NumPy 1.26.4 · ≈154.7× faster
fn double median(const R &data) source#

Median (mean of the two middle values when the count is even).

Copies the elements into a double vector, sorts ascending, and returns the middle value (averaging the two central values when the count is even). Returns 0.0 for an empty range.

Parameters
data

the numeric range.

Returns

the median, or 0.0 if empty.

Complexity

O(n log n) — copies the elements into a vector and sorts.

Allocation

allocates a temporary std::vector<double>.

Performance92.61 ns/call in cheatah · 6786 ns/call in NumPy 1.26.4 · ≈73.3× faster