std/stats¶
module stats
Descriptive statistics over a collection of numbers: the mean, the
sample and population variance and standard deviation, the median,
quantiles, the mode, covariance and correlation. A statistic of Int or
Float values is a Float; a statistic of Decimal values is a
Decimal, exact except where a quotient does not terminate, which is
rounded half to even to 100 significant digits as Decimal / is. An
Int statistic is computed exactly and rounded to a Float once. A
statistic that is undefined over its input, the mean of nothing or the
sample variance of one value, is none. Not in the prelude: a program
writes import stats for stats.mean(…), or from stats import *
for mean(…). The prelude's variance is the population variance of
Int and Float values; stats.variance is the sample variance.
mean— The arithmetic mean, the sum divided by the count, ornoneover an empty collection:[1, 2, 3, 4] | meanis2.5.variance— The sample variance, the sum of squared deviations from the mean divided byn - 1, ornoneover fewer than two values:[1, 2, 3, 4] | stats.varianceis1.6666666666666667.stdev— The sample standard deviation, the square root ofvariance, ornoneover fewer than two values.pvariance— The population variance, the sum of squared deviations from the mean divided byn, ornoneover an empty collection:[1, 2, 3, 4] | pvarianceis1.25.pstdev— The population standard deviation, the square root ofpvariance, ornoneover an empty collection.covariance— The sample covariance of the pairs'firstandsecondvalues, the sum of the products of their deviations from their means divided byn - 1, ornoneover fewer than two pairs.correlation— Pearson's correlation coefficient of the pairs'firstandsecondvalues, the covariance divided by the product of the two standard deviations, ornoneover fewer than two pairs or where either side has zero variance.median— The middle value of the collection sorted ascending, the mean of the two middle values where the count is even;quantile(0.5).quantile— Theqquantile, by linear interpolation between the sorted values (Hyndman and Fan's type 7, the default of R and NumPy): with the values sorted ass_0tos_(n-1)andh = (n - 1) * q, it iss_floor(h) + (h - floor(h)) * (s_(floor(h) + 1) - s_floor(h)).quantiles— Then - 1cut points that divide the sorted values intongroups of equal probability, the quantiles at1/nto(n-1)/nby the same interpolation asquantile, each computed as(s_j * (n - d) + s_(j+1) * d) / nwith one division.mode— The most frequent value, and among equally frequent values the least by the type's order, ornoneover an empty collection:[3, 1, 3, 1, 2] | modeis1.