stats::hypothesis
Hypothesis tests. Each returns its statistic and p-value (and the
degrees of freedom where the reference distribution has them) and
throws TooFew, NotFinite or InvalidArgument for input it cannot
test. The definitions, defaults and p-value methods follow SciPy's
scipy.stats functions of the same names, which the tests compare
against.
Structs
AnovaSource
pub struct AnovaThe outcome of a one-way ANOVA: F, its p-value and the two
degrees of freedom.
F = (SS_between / (k - 1)) / (SS_within / (N - k)).P(F(k - 1, N - k) >= statistic).k - 1.N - k.
ContingencySource
pub struct ContingencyThe chi-squared test of independence in a contingency table: its
statistic, p-value, degrees of freedom (r - 1)(c - 1) and the
expected counts under independence.
Pearson's statistic (Yates-corrected for one degree of freedom when asked).
Its p-value.
(rows - 1) (columns - 1).Row total times column total over the grand total.
TTestSource
pub struct TTestThe outcome of a t-test: the t statistic, its p-value and degrees
of freedom, the estimate (a mean or a difference of means) and its
standard error, from which ci gives a confidence interval.
let t = try stats::ttest_1samp([5.1, 4.9, 5.6, 5.8, 6.0, 5.4], 5.0);
let ci = t.ci(95pct);
assert(5.0 < ci.lo && t.estimate in ci);
The t statistic.
Its p-value.
The degrees of freedom (Welch-Satterthwaite's for unequal variances, so not always an integer).
The mean (one sample), the difference of means (two samples) or the mean difference (paired).
The standard error of
estimate.
Sourcepub alternative: Alternative§The alternative the p-value is for.
The confidence interval for the estimate at
level(e.g.95pct): two-sided, or one-sided (open on one end) to match the alternative.nabilet t = try stats::ttest_1samp([1.0, 2.0, 3.0], 0.0); let ci = t.ci(95pct); assert((ci.mid() - 2.0).abs() < 1e-15 && (ci.width() - 2.0 * 4.302652729749464 / 3.0.sqrt()).abs() < 1e-14); // t(2): 0.95 / sqrt(0.04875)
Sourcepub fn result(§self) -> TestResult The statistic, p-value and degrees of freedom as a
TestResult.nabilet r = (try stats::ttest_1samp([1.0, 2.0, 3.0], 0.0)).result(); assert(r.df == 2.0);
Trait implementations 1
impl Display for TTest
TestResultSource
pub struct TestResultThe outcome of a test: the statistic, its p-value and, for tests with a t, chi-squared or similar reference distribution, the degrees of freedom.
let r = try stats::chisquare([16.0, 18.0, 16.0, 14.0, 12.0, 12.0]);
assert(r.statistic == 2.0 && r.df == 5.0);
assert((r.pvalue - 0.8491450360846096).abs() < 1e-15);
The test statistic.
The probability, under the null hypothesis, of a statistic at least as extreme as the one observed.
The degrees of freedom of the reference distribution, or
none.Whether the test rejects the null hypothesis at level
alpha(pvalue < alpha).nabilet r = try stats::ttest_ind([1.0, 2.0, 3.0, 4.0], [6.0, 7.0, 8.0, 9.0]); assert(r.result().rejects(5pct));
Trait implementations 1
impl Display for TestResult
Enums
AlternativeSource
pub enum AlternativeWhich way the alternative hypothesis points.
use stats::Alternative;
let x = [5.1, 4.9, 5.6, 5.8, 6.0, 5.4];
let two = try stats::ttest_1samp(x, 5.0);
let up = try stats::ttest_1samp(x, 5.0, Alternative::Greater);
assert((up.pvalue - two.pvalue / 2.0).abs() < 1e-15);
The parameter differs from the null value, either way.
It is below the null value.
It is above the null value.
Functions
f_onewaySource
pub fn f_oneway<X: Sample>(groups: List<X>) throws (StatsError) -> Anova The one-way ANOVA F-test that k >= 2 groups share one mean
(SciPy's f_oneway). The sums of squares are taken about the group
means and the grand mean in two passes. Throws TooFew for fewer
than 2 groups or an empty group, InvalidArgument when no degree of
freedom is left within the groups.
// Montgomery, Design and Analysis of Experiments, ex. 3.1 (etch rate).
let a = [575.0, 542.0, 530.0, 539.0, 570.0];
let b = [565.0, 593.0, 590.0, 579.0, 610.0];
let c = [600.0, 651.0, 610.0, 637.0, 629.0];
let d = [725.0, 700.0, 715.0, 685.0, 710.0];
let r = try stats::f_oneway([a, b, c, d]);
assert((r.statistic - 66.79707321945864).abs() < 1e-10 && r.df_within == 16.0);
kendalltauSource
pub fn kendalltau<X: Sample, Y: Sample>(x: X, y: Y, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TestResult Kendall's tau-b and the p-value of tau = 0: exact (the
distribution of inversions of a random permutation) for up to 33
pairs without ties, else the normal approximation with the
tie-corrected variance of Kendall (1970) (SciPy's kendalltau).
let r = try stats::kendalltau([12.0, 2.0, 1.0, 12.0, 2.0], [1.0, 4.0, 7.0, 1.0, 0.0]);
assert((r.statistic + 0.4714045207910316).abs() < 1e-15);
assert((r.pvalue - 0.2827454599327748).abs() < 1e-14);
pearsonrSource
pub fn pearsonr<X: Sample, Y: Sample>(x: X, y: Y, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TestResult Pearson's r and the p-value of r = 0 for normal data (the t-test
on n - 2 degrees of freedom; SciPy's pearsonr). Throws TooFew
below 3 pairs.
let r = try stats::pearsonr([1.0, 2.0, 3.0, 4.0, 5.0], [2.0, 1.0, 4.0, 3.0, 5.0]);
// SciPy: pearsonr(x, y) = (0.8, 0.1040880386618278)
assert((r.statistic - 0.8).abs() < 1e-15 && (r.pvalue - 0.1040880386618278).abs() < 1e-15);
spearmanrSource
pub fn spearmanr<X: Sample, Y: Sample>(x: X, y: Y, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TestResult Spearman's rho and the p-value of rho = 0 by the same t
approximation (SciPy's spearmanr).
let r = try stats::spearmanr([1.0, 2.0, 3.0, 4.0, 5.0], [5.0, 6.0, 7.0, 8.0, 7.0]);
assert((r.statistic - 0.8207826816681233).abs() < 1e-15);
chi2_contingencySource
pub fn chi2_contingency(table: Matrix<f64>, correction: bool = true) throws (StatsError) -> Contingency Pearson's chi-squared test of independence of the rows and columns
of a table of counts. With correction (the default, as SciPy) and
one degree of freedom, Yates' continuity correction moves each count
half a unit toward its expected value. Throws InvalidArgument for
a negative count or a zero row or column total.
use core::array::Matrix;
let t: Matrix<f64> = [[10, 10, 20], [20, 20, 20]];
let r = try stats::chi2_contingency(t);
assert(r.df == 2.0 && (r.statistic - 2.7777777777777777).abs() < 1e-14);
chisquareSource
pub fn chisquare<X: Sample>(observed: X, expected: Vector<f64> | none = none, ddof: usize = 0) throws (StatsError) -> TestResult Pearson's goodness-of-fit test of observed counts against expected
ones: sum((o - e)^2 / e) on k - 1 - ddof degrees of freedom.
Without expected every category expects the same count; given, it
must have the same total as observed within 1e-8 relative. SciPy:
chisquare(f_obs, f_exp).
let r = try stats::chisquare([16.0, 18.0, 16.0, 14.0, 12.0, 12.0]);
assert(r.statistic == 2.0 && r.df == 5.0);
let r = try stats::chisquare([43.0, 52.0, 54.0, 40.0], [47.25, 47.25, 47.25, 47.25]);
assert((r.statistic - 2.9365079365079363).abs() < 1e-15);
fisher_exactSource
pub fn fisher_exact(table: Matrix<f64>, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TestResult Fisher's exact test of independence in a 2 x 2 table [[a, b], [c, d]]: with the margins fixed, a is hypergeometric. The statistic
is the sample odds ratio a d / (b c); the two-sided p-value sums
the tables no more probable than the one observed (relative slack
1e-7, as R and SciPy). SciPy: fisher_exact.
use core::array::Matrix;
// The tea-tasting experiment (Fisher 1935): 3 of 4 cups right.
let t: Matrix<f64> = [[3, 1], [1, 3]];
let r = try stats::fisher_exact(t);
assert(r.statistic == 9.0 && (r.pvalue - 0.4857142857142857).abs() < 1e-15);
ks_2sampSource
pub fn ks_2samp<X: Sample, Y: Sample>(x: X, y: Y, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TestResult The two-sample Kolmogorov-Smirnov test that x and y come from
one distribution. The statistic is sup |F_x - F_y| (two-sided),
sup (F_x - F_y) (Greater) or sup (F_y - F_x) (Less). The
p-value is exact (lattice paths) for n m <= 1e7, else asymptotic
as in SciPy.
let x = [0.61, 0.29, 0.06, 0.59, -1.73, -0.74, 0.51, -0.56, 0.39, 1.64];
let y = [2.17, 1.31, 3.03, 1.11, 0.05, 2.62, 1.65, 0.73, 2.21, 1.48];
let r = try stats::ks_2samp(x, y);
// SciPy: ks_2samp(x, y) = (0.8, 0.002056766762649115)
assert(r.statistic == 0.8 && (r.pvalue - 0.002056766762649115).abs() < 1e-16);
kstestSource
pub fn kstest<X: Sample, D: Continuous>(x: X, d: D, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TestResult The one-sample Kolmogorov-Smirnov test of x against the
continuous distribution d. Two-sided by default (statistic
sup |F_n - F|); Greater uses D+ = sup(F_n - F), Less uses
D- = sup(F - F_n), both with the exact Birnbaum-Tingey p-value.
use stats::Normal;
let x = [-1.2, -0.5, 0.1, 0.3, 0.9, 1.6, -0.2, 0.4];
let r = try stats::kstest(x, Normal::standard());
// SciPy: kstest(x, 'norm') = (0.18353753872598688, 0.9085351243205685)
assert((r.statistic - 0.18353753872598688).abs() < 1e-15);
assert((r.pvalue - 0.9085351243205685).abs() < 1e-15);
shapiroSource
pub fn shapiro<X: Sample>(x: X) throws (StatsError) -> TestResult The Shapiro-Wilk test that x comes from a normal distribution, 3 <= n <= 5000. The statistic W is near 1 for normal data; small
p-values reject normality. Throws TooFew below 3 values,
InvalidArgument above 5000 or for constant data. SciPy: shapiro
(the same algorithm).
let x = [148.0, 154.0, 158.0, 160.0, 161.0, 162.0, 166.0, 170.0, 182.0, 195.0, 236.0];
let r = try stats::shapiro(x);
assert(r.statistic < 0.8 && r.pvalue < 0.01);
mannwhitneyuSource
pub fn mannwhitneyu<X: Sample, Y: Sample>(x: X, y: Y, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TestResult The Mann-Whitney U test that x and y come from one distribution
against a shift (SciPy's mannwhitneyu). The statistic is `U1 = R1
- n1 (n1 + 1) / 2
forx`. The p-value is exact when there are no ties and a sample has fewer than 8 values, else from the normal approximation with the tie correction and a continuity correction.
let x = [19.0, 22.0, 16.0, 29.0, 24.0];
let y = [20.0, 11.0, 17.0, 12.0];
let r = try stats::mannwhitneyu(x, y);
assert(r.statistic == 17.0 && (r.pvalue - 0.1111111111111111).abs() < 1e-15);
wilcoxonSource
pub fn wilcoxon<X: Sample>(x: X, y: Vector<f64> | none = none, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TestResult The Wilcoxon signed-rank test that the values x are symmetric
about 0, or with y that the differences x - y of paired samples
are. Zero values are dropped (Wilcoxon's method). The statistic is
min(W+, W-) two-sided and W+ one-sided, as SciPy. The p-value
is exact for up to 50 nonzero values without ties, else from the
normal approximation with the tie correction. SciPy: wilcoxon(x, y).
use stats::Alternative;
let d = [6.0, 8.0, 14.0, 16.0, 23.0, 24.0, 28.0, 29.0, 41.0, -48.0, 49.0, 56.0, 60.0, -67.0, 75.0];
let r = try stats::wilcoxon(d);
// SciPy: wilcoxon(d) = (24.0, 0.041259765625), exact.
assert(r.statistic == 24.0 && r.pvalue == 0.041259765625);
let x = [125.0, 115.0, 130.0, 140.0, 140.0, 115.0, 140.0, 125.0, 140.0, 135.0];
let r = try stats::wilcoxon(x, [110.0, 122.0, 125.0, 120.0, 140.0, 124.0, 123.0, 137.0, 135.0, 145.0]);
assert(r.statistic == 18.0);
let g = try stats::wilcoxon(d, alternative: Alternative::Greater);
assert(g.pvalue < 0.041259765625);
ttest_1sampSource
pub fn ttest_1samp<X: Sample>(x: X, popmean: f64, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TTest The one-sample t-test of mean(x) = popmean: t = (mean - popmean) / (s / sqrt(n)) on n - 1 degrees of freedom. Constant data give
NaN (or infinite) t, as SciPy.
// SciPy: ttest_1samp([5.1, 4.9, 5.6, 5.8, 6.0, 5.4], 5.0)
let t = try stats::ttest_1samp([5.1, 4.9, 5.6, 5.8, 6.0, 5.4], 5.0);
assert((t.statistic - 2.7351263280759683).abs() < 1e-14);
assert((t.pvalue - 0.0410291985518795).abs() < 1e-15);
ttest_indSource
pub fn ttest_ind<X: Sample, Y: Sample>(x: X, y: Y, equal_var: bool = false, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TTest The two-sample t-test of equal means. By default Welch's test
(equal_var: false), which does not assume equal variances: t = (m1 - m2) / sqrt(s1^2 / n1 + s2^2 / n2) on the Welch-Satterthwaite
degrees of freedom. equal_var: true gives Student's pooled test on
n1 + n2 - 2.
let x = [19.7, 20.3, 21.4, 18.9, 20.1, 22.0];
let y = [22.4, 21.8, 23.9, 22.1, 24.0];
let w = try stats::ttest_ind(x, y);
// SciPy: ttest_ind(x, y, equal_var=False)
assert((w.statistic + 3.7299393321764254).abs() < 1e-13);
assert((w.df - 8.882968116619413).abs() < 1e-13);
ttest_relSource
pub fn ttest_rel<X: Sample, Y: Sample>(x: X, y: Y, alternative: Alternative = Alternative::TwoSided) throws (StatsError) -> TTest The paired t-test: the one-sample test of the differences x - y
against 0.
let before = [200.0, 190.0, 210.0, 220.0, 205.0];
let after = [195.0, 186.0, 207.0, 211.0, 202.0];
let t = try stats::ttest_rel(before, after);
assert(t.estimate == 4.8 && t.df == 4.0);