Exact

Descriptive statistics

Summaries of a list of numbers, computed in fractions. Data typed as decimals are read as the decimals they are, so the mean of 0.1, 0.2 and 0.3 is exactly 1/5. Data given as intervals (a measurement with its uncertainty) give enclosures.

Centre and spread

import { mean, variance, sd, median, quartiles, iqr, mad, trimmedMean, skewness, kurtosis, summary, show } from "@ratmath/stats";

const data = [2, 4, 4, 4, 5, 5, 7, 9];
mean(data);                              // 5
variance(data);                          // 32/7   (divides by n − 1)
variance(data, { population: true });    // 4      (divides by n)
sd(data, { population: true });          // 2: exact, because 4 is a perfect square
show(sd(data), 12);                      // "2.138089935299": certified otherwise
median(data);                            // 9/2
iqr(data);                               // 3/2
mad(data);                               // 1/2: the median distance from the median
trimmedMean(data, "1/8");                // 29/6: drop an eighth from each end
mean([0.1, 0.2, 0.3]);                   // 1/5

The sample variance is s2=1n−1∑(xi−xˉ)2s^2 = \frac{1}{n-1}\sum (x_i - \bar{x})^2. Skewness is m3/m23/2m_3 / m_2^{3/2} and kurtosis m4/m22m_4 / m_2^2 with mk=1n∑(xi−xˉ)km_k = \frac1n \sum (x_i - \bar x)^k; kurtosis is a fraction, and skewness is exact when m2m_2 is a perfect square. summary(data) collects the count, mean, five-number summary, variance, standard deviation, modes and the outliers beyond 1.51.5 interquartile ranges.

Quantiles have rules

There is no single definition of a sample quantile, and packages disagree. Here the rule has a name.

import { quantile } from "@ratmath/stats";

quantile(data, "0.9");                         // 38/5   "linear": the usual default
quantile(data, "0.9", { method: "hazen" });    // 42/5
quantile(data, "0.9", { method: "lower" });    // 7: always one of the data values
methodPosition among the sorted values x1≤⋯≤xnx_1 \le \dots \le x_n
linear1+(n−1)p1 + (n-1)p, interpolated. The default of most software.
weibull(n+1)p(n+1)p, interpolated
hazennp+12np + \tfrac12, interpolated
median-unbiased(n+13)p+13(n + \tfrac13)p + \tfrac13, interpolated
inversethe least value whose cumulative proportion reaches pp
lower, higher, nearest, midpointthe linear position, moved to a data value below, above or nearest, or the midpoint of the two
halvesquartiles only: the medians of the lower and upper halves, leaving the median out when nn is odd. This is how most school textbooks and the TI-84 find Q1Q_1 and Q3Q_3.
tukeyquartiles only: the same with the median kept in both halves (Tukey's hinges)

Counts

import { frequencies, modes, histogram } from "@ratmath/stats";

frequencies(data);      // rows { value, count, proportion, cumulative }
modes(data);            // [4]
histogram(data, { width: 2, start: 2 });
// rows { from, to, count, proportion, density } for [2, 4), [4, 6), [6, 8), [8, 10]

Two variables

import { covariance, correlation, leastSquares } from "@ratmath/stats";

const x = [1, 2, 3, 4, 5];
const y = [2, 4, 5, 4, 5];
covariance(x, y);                    // 3/2
show(correlation(x, y), 12);         // "0.774596669241"
const fit = leastSquares(x, y);
fit.slope;                           // 3/5
fit.intercept;                       // 11/5
fit.r2;                              // 3/5
fit.predict(6);                      // 29/5
fit.residuals.map(String);           // ["-4/5", "3/5", "1", "-3/5", "-1/5"]

The least-squares line minimises ∑(yi−a−bxi)2\sum (y_i - a - b x_i)^2; its slope is b=sxy/sxxb = s_{xy} / s_{xx} and it passes through (xˉ,yˉ)(\bar x, \bar y). In fractions the residuals add up to exactly zero, which is a check you can run. Regression with several predictors, and the inference that goes with it, is the planned phase 5.

Measurements with uncertainty

String(mean(["1.2:1.4", "2.0:2.2", 3]));       // "31/15:11/5": every possible mean lies in here
show(mean(["1.5[+-0.1]", "2.5[+-0.1]"]), 6);   // "2.0…"

An interval datum means "somewhere in here". Sums and means are enclosures of every value the statistic could take. Order statistics of overlapping intervals are not defined, so quantiles need exact data.

The fast side

fast.mean, fast.sd, fast.median, fast.quantile, fast.trimmedMean, fast.correlation, fast.leastSquares and the rest do the same in floating point on plain numbers. They are for simulations, where a statistic is computed thousands of times on random data and the last digit carries no meaning.

Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs