math.stats
summarize lists of numbers: totals, averages, spread, extremes and trends, units welcome
Summaries of lists of numbers: totals, averages, spread and trends. Every function here works on a list of numbers, and most accept quantities with units, like a list of lengths or prices.
good to know
Section titled “good to know”variance and stdev treat the list as a sample from something bigger, the usual choice, so they divide by n - 1 rather than n.
Tags: statistics
sumadd up a list, units includedavgthe mean (average), or with weights, the mean where each item counts as many times as its weighttrimmed_meanthe mean after dropping the lowest and highest fraction p of the items, so outliers count lesswinsorized_meanthe mean after replacing the lowest and highest fraction p of the items with the nearest items keptproductmultiply all the items togethermedianthe middle value once sorted, or with an even count, halfway between the middle twomodethe item that appears most often, or on a tie, the one that appears firstpercentilethe value p% of the way through the sorted data (p from 0 to 100), so the 90th percentile is above 90% of itvariancehow spread out the numbers arestdevhow spread out the numbers are, in the same units as themgeo_meanthe geometric mean: the nth root of the product, the right average for growth factorsharmonic_meanthe harmonic mean: the reciprocal of the average reciprocal, the right average for ratesparallelthe combined value of the arguments in parallel: the reciprocal of the sum of their reciprocals, as for resistors side by sidermsthe root mean square: the square root of the average of the squaresmadthe median absolute deviation: the median distance from the median, a spread that outliers barely movesemthe standard error of the mean: the standard deviation divided by the square root of the countiqrthe interquartile range: the 75th percentile minus the 25thspreadthe largest minus the smallest (rangemakes a list of numbers)covthe sample covariance of two lists: positive when they rise togetherrankseach item’s place in the sorted order, starting at 1, with ties sharing the average placespearmanhow closely two lists rise and fall together in order, not necessarily in a straight line (rank correlation)minthe smallest of a list, or of the argumentsmaxthe largest of a list, or of the argumentsdescribea summary in one mapcorrhow closely two lists rise and fall together (Pearson correlation)fitthe straight line y = slope × x + intercept that best fits the points (least squares), as {slope, intercept, r2}zscoreeach item as how many standard deviations above (positive) or below (negative) the mean it isnormalizerescale the list so the smallest becomes 0, the largest 1, and the rest fall in betweencumsumrunning totals: each item plus everything before itdeltasthe difference between each item and the one before
Functions
Section titled “Functions”sum(xs: list) -> num|quantity / sum(a: num|quantity, b: num|quantity, *rest: num|quantity) -> num|quantity
add up a list, units included
1[1, 2, 3].sum62[1 m, 50 cm].sum1.5 mTags: aggregation
avg(xs: list, weights?: list) -> num|quantity / avg(a: num|quantity, b: num|quantity, *rest: num|quantity) -> num|quantity
the mean (average), or with weights, the mean where each item counts as many times as its weight
Without weights, the sum divided by how many items there are
1[1, 2, 4].avg2.333332[2 h, 30 min].avg1.25 h3[90, 80].avg([3, 1])87.5Tags: aggregation
See also sum, median, trimmed_mean
trimmed_mean
Section titled “trimmed_mean”trimmed_mean(xs: list, p: num) -> float|quantity
the mean after dropping the lowest and highest fraction p of the items, so outliers count less
p is a fraction from 0 to 0.5 dropped from each end, like 10%
1[1, 2, 3, 4, 100].trimmed_mean(20%)3See also avg, winsorized_mean, median
winsorized_mean
Section titled “winsorized_mean”winsorized_mean(xs: list, p: num) -> float|quantity
the mean after replacing the lowest and highest fraction p of the items with the nearest items kept
p is a fraction from 0 to 0.5 replaced at each end, like 10%
1[1, 2, 3, 4, 100].winsorized_mean(20%)3See also avg, trimmed_mean
product
Section titled “product”product(xs: list) -> num|quantity / product(a: num|quantity, b: num|quantity, *rest: num|quantity) -> num|quantity
multiply all the items together
1[2, 3, 4].product242[2 m, 3 m].product6 m^2Tags: aggregation
median
Section titled “median”median(xs: list) -> float|quantity / median(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
the middle value once sorted, or with an even count, halfway between the middle two
1[3, 1, 2].median22[1 m, 3 m, 50 cm, 2 m].median1.5 mTags: aggregation
See also avg, percentile
mode(xs: list) -> any / mode(a: any, b: any, *rest: any) -> any
the item that appears most often, or on a tie, the one that appears first
1[1, 2, 2, 3].mode22"hello".chars.mode"l"Tags: aggregation
percentile
Section titled “percentile”percentile(xs: list, p: num, method?: str) -> float|quantity
the value p% of the way through the sorted data (p from 0 to 100), so the 90th percentile is above 90% of it
Between two items, it’s in between them by default (method “linear”). The method can instead be “lower”, “higher”, “nearest” or “midpoint” to pick from the two items around it
1[1, 2, 3, 4, 5].percentile(90)4.62(1..=100).percentile(25)25.753[1, 2, 3, 4].percentile(50, "lower")2See also median
variance
Section titled “variance”variance(xs: list, mode?: str) -> float|quantity / variance(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
how spread out the numbers are
The average squared distance from the mean, for a sample (dividing by n - 1), or for a whole population (dividing by n). mode is “sample”, the default, or “pop”
1[2, 4, 4, 4, 5, 5, 7, 9].variance4.571432[2, 4, 4, 4, 5, 5, 7, 9].variance("pop")4Tags: aggregation
See also stdev
stdev(xs: list, mode?: str) -> float|quantity / stdev(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
how spread out the numbers are, in the same units as them
The square root of the variance, for a sample (dividing by n - 1), or for a whole population (dividing by n). mode is “sample”, the default, or “pop”
1[2, 4, 4, 4, 5, 5, 7, 9].stdev2.138092[1 m, 2 m, 3 m].stdev1 m3[2, 4, 4, 4, 5, 5, 7, 9].stdev("pop")2Tags: aggregation
geo_mean
Section titled “geo_mean”geo_mean(xs: list) -> float|quantity / geo_mean(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
the geometric mean: the nth root of the product, the right average for growth factors
1[1, 2, 4].geo_mean22[1.1, 1.2, 0.9].geo_mean1.0591See also avg, harmonic_mean
harmonic_mean
Section titled “harmonic_mean”harmonic_mean(xs: list) -> float|quantity / harmonic_mean(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
the harmonic mean: the reciprocal of the average reciprocal, the right average for rates
1[40, 60].harmonic_mean482[1, 2, 4].harmonic_mean1.71429parallel
Section titled “parallel”parallel(a: any, b: any, *rest: any) -> float|quantity
the combined value of the arguments in parallel: the reciprocal of the sum of their reciprocals, as for resistors side by side
1parallel(10 ohm, 6 ohm)3.75 Ω2parallel(100, 100, 100)33.3333See also harmonic_mean
rms(xs: list) -> float|quantity / rms(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
the root mean square: the square root of the average of the squares
1[3, 4].rms3.535532[1, -1, 1, -1].rms1mad(xs: list) -> float|quantity / mad(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
the median absolute deviation: the median distance from the median, a spread that outliers barely move
1[1, 2, 3, 4, 100].mad1sem(xs: list) -> float|quantity / sem(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
the standard error of the mean: the standard deviation divided by the square root of the count
1[2, 4, 4, 4, 5, 5, 7, 9].sem0.755929iqr(xs: list) -> float|quantity / iqr(a: num|quantity, b: num|quantity, *rest: num|quantity) -> float|quantity
the interquartile range: the 75th percentile minus the 25th
1[1, 2, 3, 4, 5, 6, 7, 8].iqr3.5See also percentile, mad
spread
Section titled “spread”spread(xs: list) -> float|quantity
the largest minus the smallest (range makes a list of numbers)
1[3, 9, 4].spread62[2 m, 50 cm].spread1.5 mcov(xs: list, ys: list) -> float
the sample covariance of two lists: positive when they rise together
1cov([1, 2, 3, 4], [2, 4, 5, 9])3.66667See also corr
ranks(xs: list) -> list
each item’s place in the sorted order, starting at 1, with ties sharing the average place
1[10, 30, 20].ranks[1, 3, 2]2[1, 2, 2, 3].ranks[1, 2.5, 2.5, 4]spearman
Section titled “spearman”spearman(xs: list, ys: list) -> float
how closely two lists rise and fall together in order, not necessarily in a straight line (rank correlation)
1spearman([1, 2, 3, 4], [1, 4, 9, 16])1min(xs: list) -> any / min(a: any, b: any, *rest: any) -> any
the smallest of a list, or of the arguments
1min(3, 9, 4)32[2 km, 1 mi].min1 miTags: aggregation
max(xs: list) -> any / max(a: any, b: any, *rest: any) -> any
the largest of a list, or of the arguments
1max(3, 9, 4)92["b", "a"].max"b"Tags: aggregation
describe
Section titled “describe”describe(xs: list) -> map
a summary in one map
The count, mean, standard deviation, minimum, quartiles (the 25th, 50th and 75th percentiles) and maximum
1[3, 1, 4, 1, 5, 9, 2, 6].describe{n: 8, mean: 3.875, stdev: 2.74838, min: 1, p25: 1.75, median: 3.5, p75: 5.25, max: 9}See also avg, percentile
corr(xs: list, ys: list) -> float
how closely two lists rise and fall together (Pearson correlation)
1 is a perfect straight-line match, -1 perfectly opposite, 0 no relationship
1corr([1, 2, 3, 4], [2, 4, 5, 9])0.964764See also fit
fit(xs: list, ys: list) -> map / fit(ys: list) -> map / fit(xs: list, ys: list, degree: int) -> list
the straight line y = slope × x + intercept that best fits the points (least squares), as {slope, intercept, r2}
r2 is from 0 to 1, how well the line explains the points With one list, the x values are 1, 2, 3 and so on Given a degree as a third argument, fits a polynomial instead and returns its coefficients highest power first, for poly_eval: fit(xs, ys, 1) gives [slope, intercept]. Degrees above about 8 lose accuracy
1fit([1, 2, 3], [2, 4, 6]){slope: 2, intercept: 0, r2: 1}2fit([1, 2, 3, 4], [2.1, 3.9, 6.2, 7.8]){slope: 1.94, intercept: 0.15, r2: 0.995661}3fit([2, 4, 6]){slope: 2, intercept: 0, r2: 1}4fit([0, 1, 2, 3], [1, 2, 5, 10], 2).map(round)[1, 0, 1]See also corr
zscore
Section titled “zscore”zscore(xs: list) -> list
each item as how many standard deviations above (positive) or below (negative) the mean it is
1[2, 4, 4, 4, 5, 5, 7, 9].zscore[-1.40312, -0.467707, -0.467707, -0.467707, 0, 0, 0.935414, 1.87083]normalize
Section titled “normalize”normalize(xs: list) -> list
rescale the list so the smallest becomes 0, the largest 1, and the rest fall in between
1[10, 15, 20].normalize[0, 0.5, 1]cumsum
Section titled “cumsum”cumsum(xs: list) -> list
running totals: each item plus everything before it
1[1, 2, 3, 4].cumsum[1, 3, 6, 10]2[1 km, 500 m].cumsum[1 km, 1.5 km]deltas
Section titled “deltas”deltas(xs: list) -> list
the difference between each item and the one before
1[1, 4, 9, 16].deltas[3, 5, 7]2[1, 4, 9, 16].deltas.deltas[2, 2]More examples
Section titled “More examples”1# standard deviation2[2, 4, 4, 4, 5, 5, 7, 9].stdev2.138093# the value 90% of the data is below4(1..=100).percentile(90)90.15# everything at once6[3, 1, 4, 1, 5, 9, 2, 6].describe{n: 8, mean: 3.875, stdev: 2.74838, min: 1, p25: 1.75, median: 3.5, p75: 5.25, max: 9}7# the straight line closest to the points8fit([1, 2, 3, 4], [2.1, 3.9, 6.2, 7.8]){slope: 1.94, intercept: 0.15, r2: 0.995661}9# running total10[$5, $12, $3].cumsum[$5.00, $17.00, $20.00]Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.