Skip to content

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.

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

  • sum add up a list, units included
  • avg the mean (average), or with weights, the mean where each item counts as many times as its weight
  • trimmed_mean the mean after dropping the lowest and highest fraction p of the items, so outliers count less
  • winsorized_mean the mean after replacing the lowest and highest fraction p of the items with the nearest items kept
  • product multiply all the items together
  • median the middle value once sorted, or with an even count, halfway between the middle two
  • mode the item that appears most often, or on a tie, the one that appears first
  • percentile the value p% of the way through the sorted data (p from 0 to 100), so the 90th percentile is above 90% of it
  • variance how spread out the numbers are
  • stdev how spread out the numbers are, in the same units as them
  • geo_mean the geometric mean: the nth root of the product, the right average for growth factors
  • harmonic_mean the harmonic mean: the reciprocal of the average reciprocal, the right average for rates
  • parallel the combined value of the arguments in parallel: the reciprocal of the sum of their reciprocals, as for resistors side by side
  • rms the root mean square: the square root of the average of the squares
  • mad the median absolute deviation: the median distance from the median, a spread that outliers barely move
  • sem the standard error of the mean: the standard deviation divided by the square root of the count
  • iqr the interquartile range: the 75th percentile minus the 25th
  • spread the largest minus the smallest (range makes a list of numbers)
  • cov the sample covariance of two lists: positive when they rise together
  • ranks each item’s place in the sorted order, starting at 1, with ties sharing the average place
  • spearman how closely two lists rise and fall together in order, not necessarily in a straight line (rank correlation)
  • min the smallest of a list, or of the arguments
  • max the largest of a list, or of the arguments
  • describe a summary in one map
  • corr how closely two lists rise and fall together (Pearson correlation)
  • fit the straight line y = slope × x + intercept that best fits the points (least squares), as {slope, intercept, r2}
  • zscore each item as how many standard deviations above (positive) or below (negative) the mean it is
  • normalize rescale the list so the smallest becomes 0, the largest 1, and the rest fall in between
  • cumsum running totals: each item plus everything before it
  • deltas the difference between each item and the one before
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].sum6
2
[1 m, 50 cm].sum1.5 m
Example outputCtrl+Enter to run

Tags: aggregation

See also avg, reduce

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.33333
2
[2 h, 30 min].avg1.25 h
3
[90, 80].avg([3, 1])87.5
Example outputCtrl+Enter to run

Tags: aggregation

See also sum, median, 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%)3
Example outputCtrl+Enter to run

See also avg, winsorized_mean, median

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%)3
Example outputCtrl+Enter to run

See also avg, trimmed_mean

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].product24
2
[2 m, 3 m].product6 m^2
Example outputCtrl+Enter to run

Tags: aggregation

See also sum, factorial

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].median2
2
[1 m, 3 m, 50 cm, 2 m].median1.5 m
Example outputCtrl+Enter to run

Tags: 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].mode2
2
"hello".chars.mode"l"
Example outputCtrl+Enter to run

Tags: aggregation

See also median, count

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.6
2
(1..=100).percentile(25)25.75
3
[1, 2, 3, 4].percentile(50, "lower")2
Example outputCtrl+Enter to run

See also median

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.57143
2
[2, 4, 4, 4, 5, 5, 7, 9].variance("pop")4
Example outputCtrl+Enter to run

Tags: 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.13809
2
[1 m, 2 m, 3 m].stdev1 m
3
[2, 4, 4, 4, 5, 5, 7, 9].stdev("pop")2
Example outputCtrl+Enter to run

Tags: aggregation

See also variance, avg

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_mean2
2
[1.1, 1.2, 0.9].geo_mean1.0591
Example outputCtrl+Enter to run

See also avg, 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_mean48
2
[1, 2, 4].harmonic_mean1.71429
Example outputCtrl+Enter to run

See also avg, geo_mean

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

1
parallel(10 ohm, 6 ohm)3.75 Ω
2
parallel(100, 100, 100)33.3333
Example outputCtrl+Enter to run

See 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.53553
2
[1, -1, 1, -1].rms1
Example outputCtrl+Enter to run

See also avg, stdev

mad(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].mad1
Example outputCtrl+Enter to run

See also stdev, iqr

sem(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.755929
Example outputCtrl+Enter to run

See also stdev, avg

iqr(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.5
Example outputCtrl+Enter to run

See also percentile, mad

spread(xs: list) -> float|quantity

the largest minus the smallest (range makes a list of numbers)

1
[3, 9, 4].spread6
2
[2 m, 50 cm].spread1.5 m
Example outputCtrl+Enter to run

See also iqr, stdev

cov(xs: list, ys: list) -> float

the sample covariance of two lists: positive when they rise together

1
cov([1, 2, 3, 4], [2, 4, 5, 9])3.66667
Example outputCtrl+Enter to run

See 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]
Example outputCtrl+Enter to run

See also spearman, sort

spearman(xs: list, ys: list) -> float

how closely two lists rise and fall together in order, not necessarily in a straight line (rank correlation)

1
spearman([1, 2, 3, 4], [1, 4, 9, 16])1
Example outputCtrl+Enter to run

See also corr, ranks

min(xs: list) -> any / min(a: any, b: any, *rest: any) -> any

the smallest of a list, or of the arguments

1
min(3, 9, 4)3
2
[2 km, 1 mi].min1 mi
Example outputCtrl+Enter to run

Tags: aggregation

See also max, sort

max(xs: list) -> any / max(a: any, b: any, *rest: any) -> any

the largest of a list, or of the arguments

1
max(3, 9, 4)9
2
["b", "a"].max"b"
Example outputCtrl+Enter to run

Tags: aggregation

See also min, sort

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}
Example outputCtrl+Enter to run

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

1
corr([1, 2, 3, 4], [2, 4, 5, 9])0.964764
Example outputCtrl+Enter to run

See 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

1
fit([1, 2, 3], [2, 4, 6]){slope: 2, intercept: 0, r2: 1}
2
fit([1, 2, 3, 4], [2.1, 3.9, 6.2, 7.8]){slope: 1.94, intercept: 0.15, r2: 0.995661}
3
fit([2, 4, 6]){slope: 2, intercept: 0, r2: 1}
4
fit([0, 1, 2, 3], [1, 2, 5, 10], 2).map(round)[1, 0, 1]
Example outputCtrl+Enter to run

See also corr

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]
Example outputCtrl+Enter to run

See also normalize, stdev

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]
Example outputCtrl+Enter to run

See also zscore, lerp

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]
Example outputCtrl+Enter to run

See also deltas, sum

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]
Example outputCtrl+Enter to run

See also cumsum, windows

1
# standard deviation
2
[2, 4, 4, 4, 5, 5, 7, 9].stdev2.13809
3
# the value 90% of the data is below
4
(1..=100).percentile(90)90.1
5
# everything at once
6
[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 points
8
fit([1, 2, 3, 4], [2.1, 3.9, 6.2, 7.8]){slope: 1.94, intercept: 0.15, r2: 0.995661}
9
# running total
10
[$5, $12, $3].cumsum[$5.00, $17.00, $20.00]
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.