Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 needUseEntry
Mean, variance, quantiles, robust and shape measuresdescriptive::*, summaryDescriptive statistics
Probabilities, quantiles and moments of a named distribution; fitting one to dataNormal, Gamma, Weibull, … with ContinuousDistribution, FitProbability distributions
Association between two variablespearson, spearman, kendall, correlation_matrixCorrelation and covariance
Compare means or distributions, test countst_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 onceshapiro, levene, kruskal, wilcoxon, fisher_exact, permutation_test, adjust_p_valuesMore hypothesis tests
A smooth density from a sampleGaussianKdeKernel density estimation
Standard errors and confidence intervals for any statisticbootstrap, bca_intervalThe bootstrap
A straight-line fit, robust to outliers or with standard errorslinregress, theilslopes, siegelslopesMore descriptive statistics and robust regression
Trimmed means, z-scores, the mode; averages of angles; the Box-Cox transformtrim_mean, zscore, mode, circmean, boxcoxMore descriptive statistics and robust regression
Distances between observationspdist, cdist, squareformMore descriptive statistics and robust regression
Signal amplitude in noise; random vectors, probabilities and covariance matricesRice, MultivariateNormal, MultivariateT, Dirichlet, Wishart, InvWishart, MultinomialMultivariate distributions and further tests
Goodness of fit by Cramér-von Mises; groups with unequal spreads; all pairwise comparisons of meanscramervonmises, cramervonmises_2samp, brunnermunzel, tukey_hsdMultivariate distributions and further tests
Odds ratios, relative risks and association in contingency tables; tests by simulationodds_ratio, relative_risk, association, monte_carlo_testMultivariate distributions and further tests