math-stats provides summary statistics and numeric utility functions. It is used internally by libmath and can also be imported directly.
stats := import('math-stats')
// or destructure
{ mean: mean, median: median, stddev: stddev, clamp: clamp, round: round } := import('math-stats')
Returns the sum of all arguments. Accepts a list or individual values.
sum(1, 2, 3) // => 6
sum([1, 2, 3]...) // => 6
Returns the product of all arguments.
prod(2, 3, 4) // => 24
Returns the minimum value among all arguments.
min(3, 1, 4, 1, 5) // => 1
Returns the maximum value among all arguments.
max(3, 1, 4, 1, 5) // => 5
Returns the arithmetic mean of list xs. Returns ? for an empty list.
mean([1, 2, 3, 4]) // => 2.5
mean([]) // => ?
Returns the median of list xs (sorts the list first). For even-length lists returns the average of the two middle values. Returns ? for an empty list.
median([3, 1, 4, 1, 5]) // => 3
median([1, 2, 3, 4]) // => 2.5
Returns the population standard deviation of list xs. Returns ? for an empty list.
stddev([2, 4, 4, 4, 5, 5, 7, 9]) // => 2.0
Returns x clamped to the range [a, b].
clamp(10, 0, 5) // => 5
clamp(-3, 0, 5) // => 0
clamp(3, 0, 5) // => 3
Rounds n to decimals decimal places (default: 0). Uses round-half-away-from-zero.
round(3.14159, 2) // => 3.14
round(2.5) // => 3
round(-2.5) // => -3
round(123.456, 0) // => 123
Computes mean of multiple datasets in parallel.
pbatchMean([[1, 2, 3], [4, 5, 6]]) // => [2, 5]
Computes standard deviation of multiple datasets in parallel.
pbatchStddev([[1, 2, 3], [4, 5, 6]])