Descriptive statistics, correlation, tests, and fitting over tensors and frame columns.
Import with use math.stats. Functions become top-level after import.
| Function | Description | Example |
|---|---|---|
median[x] | Median of a 1D array | Statistics: use math.stats |
std[x] | Population standard deviation | Statistics: use math.stats |
var[x] | Population variance | Statistics: use math.stats |
percentile[x, p] | p-th percentile (0-100) with linear interpolation | Statistics: use math.stats |
zscore[x] | Z-score normalization: (x - mean) / std → returns array | Statistics: use math.stats |
corr[x, y] | Pearson correlation coefficient between two arrays | Statistics: use math.stats |
cov[x, y] | Population covariance between two arrays | Statistics: use math.stats |
ttest[x, y] | Welch's two-sample t-test → returns map with t and p | Statistics: use math.stats |
linreg[x, y] | Simple linear regression → returns map with slope, intercept, r2 | Statistics: use math.stats |
chisq[observed, expected] | Chi-square goodness-of-fit test → map with chi2, p | Statistics: use math.stats |
anova[g1, g2, ...] | One-way ANOVA → map with f, p | Statistics: use math.stats |
kstest[data, dist_name] | Kolmogorov-Smirnov test → map with d, p. Distributions: "normal", "uniform" | Statistics: use math.stats |
fit[data, dist_name] | Distribution fitting (MLE) → map of parameters. Distributions: "normal" → {mu, sigma}, "exponential" → {lambda}, "uniform" → {a, b}, "poisson" → {lambda} | Statistics: use math.stats |
A statistic of no values is NaN. The series a function pairs up (corr, cov, linreg, chisq) are one length, percentile's p is from 0 to 100, anova compares two groups or more, and kstest and fit know the distributions above; anything else stops the program with the reason (see When a Program Stops). They had answered NaN, 0.5 or an empty map.
The module is written in Crow (src/modules/stats.co, design rule 20): each statistic is a formula over the math builtins — a median the middle of sort, a variance the mean of squared distances, a regression its sums of products — and the tests' p-values are the t, F and chi-square distributions written over the sys layer's betainc and gammainc, which stay validated kernels. Its rows keep the contract above in both modes; its tests are the module's own (crow test src/modules/stats.co): each statistic at known values, and its laws over a spread of inputs (a median is the 50th percentile, a series' covariance with itself is its variance, a correlation is symmetric and within −1 and 1, a z-score keeps nothing of the scale, a line through its own points has r² 1).
use math
use math.stats
x = linspace[1, 10, 10]
log[median[x]] --- 5.5
log[std[x]] --- 2.872...
log[var[x]] --- 8.25
log[percentile[x, 25]] --- 3.25
z = zscore[x] --- mean=0, std=1
y = linspace[2, 20, 10]
log[corr[x, y]] --- 1.0 (perfect correlation)
log[cov[x, y]] --- 16.5
result = ttest[x, y]
log[result.t] --- t-statistic
log[result.p] --- p-value
Generated by crow test --docs --write from the language specification, section 11 and 13. Edit the spec, not this page.