Statistics
Statistics turns data into statements about the process that produced them. Descriptive statistics summarise a sample; correlation measures association between variables; hypothesis tests ask whether an apparent effect could be chance; density estimation recovers the shape of a distribution without assuming its family; and the bootstrap estimates the uncertainty of any statistic by resampling. All of them rest on the random numbers and statistical distribution functions of earlier chapters.
The central questions
- Is the computation sound? Summaries of large or offset data lose accuracy to cancellation unless computed carefully (the variance is the classic example).
- Is the statistic robust? A single wild value can move a mean or a standard deviation arbitrarily far; medians, ranks and the median absolute deviation resist it.
- Is the reference distribution right? A p-value is only as good as the assumptions behind it: normality for tests and ANOVA, equal variances for Student’s two-sample test, large expected counts for . Rank tests and the bootstrap need fewer assumptions.
- How much data? Kernel density estimates converge like , and bootstrap intervals for statistics that depend on the tails under-cover badly for small samples.
Testing the library
Every statistic and p-value was compared with SciPy 1.17 on fixed data sets (hypothesis tests, descriptive statistics): all agree to about , except the Kolmogorov-Smirnov p-values, which are asymptotic approximations. Simulations check that the tests reject a true null hypothesis at the stated rate.
In the library
The crate is lib_jsl_statistics (API reference). Functions take slices of f64 and return
Results; tests return a TestResult with the statistic, p-value and degrees of freedom.
| You need | Use | Entry |
|---|---|---|
| Mean, variance, quantiles, robust and shape measures | descriptive::*, summary | Descriptive statistics |
| Probabilities, quantiles and moments of a named distribution; fitting one to data | Normal, Gamma, Weibull, … with ContinuousDistribution, Fit | Probability distributions |
| Association between two variables | pearson, spearman, kendall, correlation_matrix | Correlation and covariance |
| Compare means or distributions, test counts | t_test_*, anova_one_way, mann_whitney_u, ks_test_*, chi_square_* | Hypothesis tests |
| Test normality or equal variances; rank, exact and permutation tests; many tests at once | shapiro, levene, kruskal, wilcoxon, fisher_exact, permutation_test, adjust_p_values | More hypothesis tests |
| A smooth density from a sample | GaussianKde | Kernel density estimation |
| Standard errors and confidence intervals for any statistic | bootstrap, bca_interval | The bootstrap |
| A straight-line fit, robust to outliers or with standard errors | linregress, theilslopes, siegelslopes | More descriptive statistics and robust regression |
| Trimmed means, z-scores, the mode; averages of angles; the Box-Cox transform | trim_mean, zscore, mode, circmean, boxcox | More descriptive statistics and robust regression |
| Distances between observations | pdist, cdist, squareform | More descriptive statistics and robust regression |
| Signal amplitude in noise; random vectors, probabilities and covariance matrices | Rice, MultivariateNormal, MultivariateT, Dirichlet, Wishart, InvWishart, Multinomial | Multivariate distributions and further tests |
| Goodness of fit by Cramér-von Mises; groups with unequal spreads; all pairwise comparisons of means | cramervonmises, cramervonmises_2samp, brunnermunzel, tukey_hsd | Multivariate distributions and further tests |
| Odds ratios, relative risks and association in contingency tables; tests by simulation | odds_ratio, relative_risk, association, monte_carlo_test | Multivariate distributions and further tests |