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 . Skewness is and kurtosis with ; kurtosis is a fraction, and skewness is exact when is a perfect square. summary(data) collects the count, mean, five-number summary, variance, standard deviation, modes and the outliers beyond 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
| method | Position among the sorted values |
|---|---|
| linear | , interpolated. The default of most software. |
| weibull | , interpolated |
| hazen | , interpolated |
| median-unbiased | , interpolated |
| inverse | the least value whose cumulative proportion reaches |
| lower, higher, nearest, midpoint | the linear position, moved to a data value below, above or nearest, or the midpoint of the two |
| halves | quartiles only: the medians of the lower and upper halves, leaving the median out when is odd. This is how most school textbooks and the TI-84 find and . |
| tukey | quartiles 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 ; its slope is and it passes through . 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.
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.