Skip to content

Statistics (std::stats)

std::stats provides statistical functions over numeric arrays — from means and medians to standard deviation, percentiles and linear regression. All operations are pure computations over in-memory data and run directly in the sandbox.


Central tendency

stats.mean, stats.median and stats.mode compute the three measures of central tendency. stats.min, stats.max and stats.sum complete the set of basic utilities over a sequence.

Mean, median, mode, min, max and sum of a sample array.

01-mean-median.zolo
Playground
// Feature: stats.mean / stats.median / stats.mode — central tendencies
// When to use: data analysis, performance metrics, monitoring.

use std::stats

let sample = [10, 20, 30, 40, 50]

// arithmetic mean.
print(stats.mean(sample))  // expected: 30

// median (middle value once sorted).
print(stats.median(sample))  // expected: 30

// mode (most frequent value).
let bimodal = [1, 2, 2, 3, 3, 3, 4]
print(stats.mode(bimodal))  // expected: 3

// min, max, sum — basic utilities.
print(stats.min(sample))  // expected: 10
print(stats.max(sample))  // expected: 50
print(stats.sum(sample))  // expected: 150

Dispersion, percentiles and correlation

For latency series or monitoring metrics, stats.variance and stats.stddev measure dispersion; stats.percentile(arr, p) computes the p-th percentile (useful for p95/p99). stats.correlation returns the Pearson coefficient between two vectors, and stats.linear_regression fits a line y = slope * x + intercept.

Variance, standard deviation, p95, correlation and simple linear regression.

02-variance-stddev.zolo
Playground
// Feature: stats.variance / stats.stddev / stats.percentile / correlation
// When to use: measure dispersion, deviation-based alerts, correlations.

use std::stats

let latencies = [120, 130, 125, 140, 110, 135, 128, 122]

// variance (mean of the squared deviations).
print(stats.variance(latencies))  // expected: float ~80

// standard deviation = sqrt(variance).
let sd = stats.stddev(latencies)
print(sd > 0)  // expected: true

// percentile(arr, p) — p in [0, 100].
print(stats.percentile(latencies, 50))  // expected: ~127 (median)
print(stats.percentile(latencies, 95))  // expected: p95 ~140

// Pearson correlation between two series (-1.0 .. 1.0).
let xs = [1, 2, 3, 4, 5]
let ys = [2, 4, 6, 8, 10]
let r = stats.correlation(xs, ys)
print(r)  // expected: 1.0 (perfectly correlated)

// Simple linear regression -> { slope, intercept }.
let lr = stats.linear_regression(xs, ys)
print(lr.slope)  // expected: 2.0
print(lr.intercept)  // expected: 0.0

Challenge

Load a list of API response times and emit an alert via std::log.warn if the p95 exceeds 200 ms. Combine std::stats and std::log.

enespt-br