math.stats
sums, averages, spread and extremes of lists; units welcome
Example results generated on 2026-10-08. Ones using
now,todayor randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.
Functions
| function | description |
|---|---|
sum(xs: list) | add up a list; works with units |
avg(xs: list) | mean of a list |
product(xs: list) | multiply a list together |
median(xs: list) | middle value; mean of the middle two for an even count |
mode(xs: list) | most common item; the first one on ties |
percentile(xs: list, p: num) | the p-th percentile (0-100), interpolating between items |
variance(xs: list) | sample variance (n - 1) |
stdev(xs: list) | sample standard deviation (n - 1); works with units |
min(xs: list) / min(a: any, b: any, ...) | smallest value |
max(xs: list) / max(a: any, b: any, ...) | largest value |
describe(xs: list) | n, mean, stdev, min, quartiles and max in one map; units welcome |
corr(xs: list, ys: list) | Pearson correlation, from -1 to 1 |
fit(xs: list, ys: list) | least-squares line: {slope, intercept, r2} |
zscore(xs: list) | how many standard deviations each item is from the mean |
normalize(xs: list) | rescale so the min is 0 and the max is 1 |
cumsum(xs: list) | running totals |
deltas(xs: list) | the difference between each item and the one before |
sum
sum(xs: list): add up a list; works with units
[1, 2, 3].sum
# → 6
[1 m, 50 cm].sum
# → 1.5 m
avg
avg(xs: list): mean of a list
[1, 2, 4].avg
# → 2.33333
[2 h, 30 min].avg
# → 1.25 h
product
product(xs: list): multiply a list together
[2, 3, 4].product
# → 24
[2 m, 3 m].product
# → 6 m^2
median
median(xs: list): middle value; mean of the middle two for an even count
[3, 1, 2].median
# → 2
[1 m, 3 m, 50 cm, 2 m].median
# → 1.5 m
See also: avg, percentile
mode
mode(xs: list): most common item; the first one on ties
[1, 2, 2, 3].mode
# → 2
"hello".chars.mode
# → "l"
percentile
percentile(xs: list, p: num): the p-th percentile (0-100), interpolating between items
[1, 2, 3, 4, 5].percentile(90)
# → 4.6
(1..=100).percentile(25)
# → 25.75
See also: median
variance
variance(xs: list): sample variance (n - 1)
[2, 4, 4, 4, 5, 5, 7, 9].variance
# → 4.57143
See also: stdev
stdev
stdev(xs: list): sample standard deviation (n - 1); works with units
[2, 4, 4, 4, 5, 5, 7, 9].stdev
# → 2.13809
[1 m, 2 m, 3 m].stdev
# → 1 m
min
min(xs: list) / min(a: any, b: any, ...): smallest value
min(3, 9, 4)
# → 3
[2 km, 1 mi].min
# → 1 mi
max
max(xs: list) / max(a: any, b: any, ...): largest value
max(3, 9, 4)
# → 9
["b", "a"].max
# → "b"
describe
describe(xs: list): n, mean, stdev, min, quartiles and max in one map; units welcome
[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
corr(xs: list, ys: list): Pearson correlation, from -1 to 1
corr([1, 2, 3, 4], [2, 4, 5, 9])
# → 0.964764
See also: fit
fit
fit(xs: list, ys: list): least-squares line: {slope, intercept, r2}
fit([1, 2, 3], [2, 4, 6])
# → {slope: 2, intercept: 0, r2: 1}
fit([1, 2, 3, 4], [2.1, 3.9, 6.2, 7.8])
# → {slope: 1.94, intercept: 0.15, r2: 0.995661}
See also: corr
zscore
zscore(xs: list): how many standard deviations each item is from the mean
[2, 4, 4, 4, 5, 5, 7, 9].zscore
# → [-1.40312, -0.467707, -0.467707, -0.467707, 0, 0, 0.935414, 1.87083]
normalize
normalize(xs: list): rescale so the min is 0 and the max is 1
[10, 15, 20].normalize
# → [0, 0.5, 1]
cumsum
cumsum(xs: list): running totals
[1, 2, 3, 4].cumsum
# → [1, 3, 6, 10]
[1 km, 500 m].cumsum
# → [1 km, 1.5 km]
deltas
deltas(xs: list): the difference between each item and the one before
[1, 4, 9, 16].deltas
# → [3, 5, 7]
[1, 4, 9, 16].deltas.deltas
# → [2, 2]
More examples
stats
# standard deviation
[2, 4, 4, 4, 5, 5, 7, 9].stdev
# → 2.13809
# percentile
(1..=100).percentile(90)
# → 90.1
# summary
[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}
# trend line
fit([1, 2, 3, 4], [2.1, 3.9, 6.2, 7.8])
# → {slope: 1.94, intercept: 0.15, r2: 0.995661}
# running total
[$5, $12, $3].cumsum
# → [$5.00, $17.00, $20.00]