Statistical tests
This module contains various statistical tests.
Methods
================================================= =========== Method Description ================================================= =========== ttest1Samp_ (a, popmean[, alternative]) T-test on one sample ttestIndFromStats_ (...) T-test from descriptive statistics ttestInd_ (a, b[, equal_var, alternative]) T-test on two independent samples ttestRel_ (a, b[, alternative]) T-test on two related samples chisquare_ (f_obs[, f_exp, ddof] Chi-square test powerDivergence_ (f_obs[, f_exp, ddof, method]) Cressie-Read power divergence test kstest_ 2 methods Perform Kolmogorov-Smirnov test ks_1samp_ (data_in, cdf[, alternative]) Perform one-sample Kolmogorov-Smirnov test ks_2samp_ (a, b[, alternative]) Perform two-sample Kolmogorov-Smirnov test rankdata_ (a[, method]) Compute rank in data tiecorrect_ (a) Compute adjustment due to ties kruskal_ (samples) Kruskal-Wallis H-test ranksums_ (a, b[, alternative]) Wilcoxon rank-sum statistic friedmanchisquare_ (samples) Friedman test wilcoxon_ (x, y[, alternative]) Wilcoxon signed-rank test mannwhitneyu_ (x, y[, alternative, continuity]) Mann-Whitney U rank test ================================================= ===========
Description
.. _ttest1Samp:
.. function:: ttest1Samp(a, popmean[, alternative])
:module: stats.stattest
:param T: type parameter, implements Float
:param a: Vector<T>, sample to be analyzed
:param popmean: T, hypothesis mean
:param alternative: String, one of "two-sided" (default), "greater" or "less"
:return: (Float64, Float64)
Compute T-test on one sample.
.. _ttestIndFromStats:
.. function:: ttestIndFromStats(mean1, std1, nobs1, mean2, std2, nobs2[, equal_var, alternative])
:module: stats.stattest
:param mean1: Float64, mean of first sample
:param std1: Float64, standard deviation of first sample
:param nobs1: Int64, number of observation of first sample
:param mean2: Float64, mean of second sample
:param std2: Float64, standard deviation of second sample
:param nobs2: Int64, number of observation of second sample
:param equal_var: Bool, whether variances of two samples are assumed to be equal
:param alternative: String, one of "two-sided" (default), "greater" or "less"
:return: (Float64, Float64)
Compute T-test from descriptive statistics.
.. _ttestInd:
.. function:: ttestInd(a, b[, equal_var, alternative])
:module: stats.stattest
:param T: type parameter, implements Float
:param a: Vector<T>, first sample
:param b: Vector<T>, second sample
:param equal_var: Bool, whether variances of two samples are assumed to be equal
:param alternative: String, one of "two-sided" (default), "greater" or "less"
:return: (Float64, Float64)
Compute T-test from two independent samples.
.. _ttestRel:
.. function:: ttestRel(a, b[, alternative])
:module: stats.stattest
:param T: type parameter, implements Float
:param a: Vector<T>, first sample
:param b: Vector<T>, second sample
:param alternative: String, one of "two-sided" (default), "greater" or "less"
:return: (Float64, Float64)
Compute T-test from two related samples.
.. _chisquare:
.. function:: chisquare(f_obs[, f_exp, ddof])
:module: stats.stattest
:param f_obs: Vector<Float64>, observed frequencies
:param f_exp: Vector<Float64>, expected frequencies, optional, default to uniform distribution
:param ddof: Int64, difference in degree of freedom
:return: (Float64, Float64), statistics and goodness-of-fit
Perform the chi-square test.
Example:
1. Simple chi-square test, with expected frequency set to uniform.
::
let (stat, prob) = chisquare(
vector([16.0, 18.0, 16.0, 14.0, 12.0, 12.0])
)
Result: stat = :math:`2.0`, prob = :math:`0.84914503608460956`.
2. Test with both observed and expected frequencies.
::
let (stat, prob) = chisquare(
vector([16.0, 18.0, 16.0, 14.0, 12.0, 12.0]),
vector([16.0, 16.0, 16.0, 16.0, 16.0, 8.0])
)
Result: stat = :math:`3.5`, prob = :math:`0.62338762774958223`.
3. Test with custom degree of freedom.
::
let (stat, prob) = chisquare(
vector([16.0, 18.0, 16.0, 14.0, 12.0, 12.0]), ddof:1
)
Result: stat = :math:`2.0`, prob = :math:`0.73575888234288467`.
.. _powerDivergence:
.. function:: powerDivergence(f_obs[, f_exp, ddof, method])
:module: stats.stattest
:param f_obs: Vector<Float64>, observed frequencies
:param f_exp: Vector<Float64>, expected frequencies, optional, default to uniform distribution
:param ddof: Int64, difference in degree of freedom
:param method: String, see below
:return: (Float64, Float64), statistics and goodness-of-fit
Perform the Cressie-Read power divergence test.
Example:
1. Test log-likelihood with expected frequency set to uniform.
::
let (stat, prob) = powerDivergence(
vector([16.0, 18.0, 16.0, 14.0, 12.0, 12.0]),
method:"log-likelihood"
)
Result: stat = :math:`2.006573162632538`, prob = :math:`0.84823476779463769`.
2. Test log-likelihood with expected frequencies.
::
let (stat, prob) = powerDivergence(
vector([16.0, 18.0, 16.0, 14.0, 12.0, 12.0]),
vector([16.0, 16.0, 16.0, 16.0, 16.0, 8.0]),
method:"log-likelihood"
)
Result: stat = :math:`3.3281031458963746`, prob = :math:`0.6495419288047497`.
.. _kstest:
.. function:: kstest(a, b[, alternative])
:module: stats.stattest
:param a: Vector<Float64>
:param b: Vector<Float64>
:param alternative: String, one of "two-sided" (default), "less" and "greater"
:return: (Float64, Float64)
.. function:: kstest(a, cdf[, alternative])
:module: stats.stattest
:param a: Vector<Float64>
:param cdf: (Float64) -> Float64
:param alternative: String, one of "two-sided" (default), "less" and "greater"
:return: (Float64, Float64)
Perform the Kolmogorov-Smirnov test on one or two samples, depending on whether
the second input is a list of values or a function.
.. _ks_1samp:
.. function:: ks_1samp(a, cdf[, alternative])
:module: stats.stattest
:param a: Vector<Float64>
:param cdf: (Float64) -> Float64
:param alternative: String, one of "two-sided" (default), "less" and "greater"
:return: (Float64, Float64)
Perform the Kolmogorov-Smirnov test on one sample.
.. _ks_2samp:
.. function:: ks_2samp(a, b[, alternative])
:module: stats.stattest
:param a: Vector<Float64>
:param b: Vector<Float64>
:param alternative: String, one of "two-sided" (default), "less" and "greater"
:return: (Float64, Float64)
Perform the Kolmogorov-Smirnov test on two samples.
.. _rankdata:
.. function:: rankdata(a[, method])
:module: stats.stattest
:param T: type parameter, implements Real, Hashable.
:param a: Vector<T>
:param method: String, see below
:return: Vector<Float64>, rank of each element in ``a``.
Compute rank of each element in a vector.
Example:
1. Result on a simple vector according to each method.
::
let a1 = vector<Int64>([0, 2, 3, 2])
let r1 = rankdata(a1)
let r2 = rankdata(a1, method: "min")
let r3 = rankdata(a1, method: "max")
let r4 = rankdata(a1, method: "dense")
let r5 = rankdata(a1, method: "ordinal")
Result:
* r1 = :math:`[1.0, 2.5, 4.0, 2.5]`.
* r2 = :math:`[1.0, 2.0, 4.0, 2.0]`.
* r3 = :math:`[1.0, 3.0, 4.0, 3.0]`.
* r4 = :math:`[1.0, 2.0, 3.0, 2.0]`.
* r5 = :math:`[1.0, 2.0, 4.0, 3.0]`.
.. _tiecorrect:
.. function:: tiecorrect(a)
:module: stats.stattest
:param a: Vector<Float64>, ranks returned by function ``rankdata``.
:return: Adjustment due to ties
Example:
::
let a1 = vector([1.0, 2.5, 2.5, 4.0])
tie_correct(a1)
Result: :math:`0.9`.
::
let a2 = vector<Int64>([1, 3, 2, 4, 5, 7, 2, 8, 4])
let r2 = rankdata(a2)
tie_correct(r2)
Result: :math:`0.9833333333333333`.
.. _kruskal:
.. function:: kruskal(samples)
:module: stats.stattest
:param T: type parameter, implements Real, Hashable.
:param samples: Array<Vector<T>>
:return: (Float64, Float64)
Computes Kruskal-Wallis H-test for independent samples.
.. _ranksums:
.. function:: ranksums(a, b)
:module: stats.stattest
:param T: type parameter, implements Real, Hashable.
:param a: Vector<T>
:param b: Vector<T>
:param alternative: String, one of "two-sided" (default), "greater" or "less"
:return: (Float64, Float64)
Compute the Wilcoxon rank-sum statistic for two samples.
.. _friedmanchisquare:
.. function:: friedmanchisquare(samples)
:module: stats.stattest
:param T: type parameter, implements Real, Hashable.
:param samples: Matrix<T>
:return: (Float64, Float64)
Compute the Friedman test for repeated measurements.
.. _wilcoxon:
.. function:: wilcoxon(x, y[, alternative])
:module: stats.stattest
:param T: type parameter, implements Real, Hashable.
:param x: Vector<T>
:param y: Vector<T>
:param alternative: String, one of "two-sided" (default), "greater" or "less"
:return: (Float64, Float64)
Calculate the Wilcoxon signed-rank test.
.. _mannwhitneyu:
.. function:: mannwhitneyu(x, y[, alternative, continuity])
:module: stats.stattest
:param T: type parameter, implements Real, Hashable.
:param x: Vector<T>
:param y: Vector<T>
:param alternative: String, one of "two-sided" (default), "greater" or "less"
:param continuity: Bool, default to true
:return: (Float64, Float64)