Index
daspi.statistics.hypothesis
¶
Statistical hypothesis-testing functions.
This module collects a curated set of hypothesis tests commonly used in industrial and scientific data analysis. The tests cover normality checks, variance equality, location differences, distributional goodness-of-fit, and shape statistics.
Every test function returns a tuple whose first element is always the p-value, followed by the test statistic (and additional values where applicable). This consistent signature makes it easy to pass the functions to higher-level routines.
Normality tests
anderson_darling_test– Anderson-Darling test against the normal distribution; recommended for both small and large samples.all_normal– convenience wrapper that checks whether all given samples pass the Anderson-Darling test.kolmogorov_smirnov_test– one-sample KS test against any continuous SciPy distribution.
Variance tests
f_test– F-test for equal variances between two independent samples (assumes normality).levene_test– Levene test for equal variances; robust alternative to the F-test.variance_stability_test– internal variance stability of a single sample (Levene applied to time-ordered sections).variance_test– selects F-test or Levene test depending on normality.
Location / mean tests
t_test– one-sample t-test against a hypothesised population mean.mean_stability_test– internal mean stability of a single sample (one-way ANOVA applied to time-ordered sections).position_test– two-sample location test; dispatches to the independent-samples t-test or the Mann-Whitney U test based on normality and variance equality.
Proportion tests
proportions_test– two-sample proportions test (automatically selects Fisher's exact test for small samples).
Shape tests
kurtosis_test– D'Agostino kurtosis test.skew_test– D'Agostino skewness test.
Utilities
chunker– divides an array into n roughly equal sections; used internally by the stability tests.ensure_generic– normalises a distribution argument to arv_continuousinstance.
Notes
The stability tests (variance_stability_test, mean_stability_test)
split a single time-ordered sample into sections and test whether the
statistic of interest is constant across sections — a lightweight
alternative to control-chart analysis.
anderson_darling_test(sample)
¶
The Anderson-Darling test compares the measured values with the theoretical values of a given distribution (in this case the normal distribution). This test is considered to be one of the most powerful tests for normality for both small and large sample sizes.
The Anderson-Darling statistic \(A^2\) is computed as a squared distance between the empirical cumulative distribution function and the theoretical CDF \(F\), with stronger weighting in the tails. For a sample of size \(N\) with ordered values \(x_1 \le \ldots \le x_N\):
To obtain a p-value the statistic is adjusted for finite sample size:
The p-value is then derived from \(A^*\) using the following piecewise approximation:
| PARAMETER | DESCRIPTION |
|---|---|
sample
|
A one-dimensional array-like object containing the samples.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
The p-value for the test.
TYPE:
|
A_star
|
The adjusted Anderson-Darling test statistic \(A^*\).
TYPE:
|
Notes
This test was inspired by the Excel Add-in by Charles Zaiontz, see: https://real-statistics.com/non-parametric-tests/goodness-of-fit-tests/anderson-darling-test/
all_normal(*samples, p_threshold=0.05)
¶
Performs the Anderson-Darling test against the normal distribution for each given sample data. Only one-dimensional samples are accepted.
| PARAMETER | DESCRIPTION |
|---|---|
*samples
|
One or more one-dimensional array-like objects containing the samples.
TYPE:
|
p_threshold
|
The threshold p-value for significance (default is 0.05).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if all p-values are greater than the specified p_threshold, False otherwise. |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If p_threshold is not within the range (0, 1). |
kolmogorov_smirnov_test(sample, dist, alternative='two-sided')
¶
Perform a one-sample Kolmogorov-Smirnov-Test. This hypothesis test compares the underlying distribution F(x) of a sample against a given distribution G(x). This test is valid only for continuous distributions.
| PARAMETER | DESCRIPTION |
|---|---|
sample
|
A one-dimensional array-like object containing the samples.
TYPE:
|
dist
|
If a string, it should be the name of a continous distribution in scipy.stats, which will be used as the cdf function.
TYPE:
|
alternative
|
The alternative hypothesis to use for the test. 'two-sided' : the cdf of the distribution is not the same as the cdf of the sample 'less' : the cdf of the distribution is the same as or less than the cdf of the sample 'greater' : the cdf of the distribution is the same as or greater than the cdf of the sample
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
The two-tailed p-value for the test
TYPE:
|
D
|
Kolmogorov-Smirnov test statistic, either D, D+ or D-.
TYPE:
|
params
|
Estimates for any shape parameters (if applicable), followed by
those for location and scale. For most random variables, shape
statistics will be returned, but there are exceptions
(e.g.
TYPE:
|
f_test(sample1, sample2)
¶
F-test for equal variances between two independent populations.
The F-test compares the variances of two samples. The underlying
probability distribution is the F-distribution (Fisher distribution),
which depends on the degrees of freedom :math:df_1 and
:math:df_2 of the two populations. The test statistic is the
ratio of the two sample variances:
The two-sided p-value is computed as:
where \(F_{\text{cdf}}\) is the cumulative distribution function of the F-distribution with \(df_1 = n_1 - 1\) and \(df_2 = n_2 - 1\) degrees of freedom.
The null hypothesis \(H_0: \sigma_1^2 = \sigma_2^2\) is rejected when \(F < F_{1-\alpha/2}\) or \(F > F_{\alpha/2}\).
| PARAMETER | DESCRIPTION |
|---|---|
sample1
|
A one-dimensional array-like object containing the first sample.
TYPE:
|
sample2
|
A one-dimensional array-like object containing the second sample.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
The two-sided p-value for the test.
TYPE:
|
F
|
The F-test statistic.
TYPE:
|
Notes
The F-test assumes that both samples are drawn from normally
distributed populations. For non-normal data or heavy-tailed
distributions, consider using levene_test instead.
levene_test(sample1, sample2, heavy_tailed=False)
¶
Levene test for equal variances (variance homogeneity).
The Levene test checks the null hypothesis that all input samples are drawn from populations with equal variances. It is a robust alternative to the F-test and does not require normality.
Given a variable Y with N observations divided into k groups (where \(N_i\) is the size of the i-th group), the Levene statistic W is defined as:
where \(\bar{Z}_i\) is the group mean of the \(Z_{ij}\) values and \(\bar{Z}\) is the overall mean. The \(Z_{ij}\) values are absolute deviations from a group centre measure:
The choice of centre measure controls the robustness of the test:
- Median (\(\tilde{Y}_i\) = group median) — recommended for
skewed distributions (default when
heavy_tailed=False). - Trimmed mean (10 % trimmed, \(\tilde{Y}_i'\)) — recommended
for heavy-tailed (leptokurtic) distributions (used when
heavy_tailed=True). - Mean (\(\bar{Y}_i\)) — original Levene definition, best for symmetric, non-heavy-tailed data.
| PARAMETER | DESCRIPTION |
|---|---|
sample1
|
A one-dimensional array-like object containing the first sample.
TYPE:
|
sample2
|
A one-dimensional array-like object containing the second sample.
TYPE:
|
heavy_tailed
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
p-value for the test.
TYPE:
|
L
|
Levene test statistic W.
TYPE:
|
variance_stability_test(sample, n_sections=3)
¶
Perform Levene test for equal variances within one sample.
Divides the data into the number of n_sections. A Levene test is then performed between these intercepts to check whether the variance remains stable
| PARAMETER | DESCRIPTION |
|---|---|
sample
|
A one-dimensional array-like object containing the samples.
TYPE:
|
n_sections
|
Amount of sections to divide the data into, by default 3
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
p-value for the test
TYPE:
|
L
|
Levene test statistic
TYPE:
|
mean_stability_test(sample, n_sections=3)
¶
Perform one-way ANOVA for equal means within one sample.
Divides the data into the number of n_sections. A f_oneway test is then performed between these intercepts to check whether the mean remains stable
| PARAMETER | DESCRIPTION |
|---|---|
sample
|
A one-dimensional array-like object containing the samples.
TYPE:
|
n_sections
|
Amount of sections to divide the data into, by default 3
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
p-value for the test
TYPE:
|
statistic
|
The computed F statistic of the test.
TYPE:
|
position_test(sample1, sample2, equal_var=True, normal=None, u_test=True)
¶
calculate the test for the means of two independent samples of scores. This is a two-sided test for the null hypothesis that 2 independent samples have identical average (expected) values. This test assumes that the populations have identical variances by default. If u_test is true and normal is false perform the Mann-Whitney U rank test on two independent samples. The Mann-Whitney U test is a nonparametric test of the null hypothesis that the distribution underlying sample x is the same as the distribution underlying sample y. It is often used as a test of difference in location between distributions.
| PARAMETER | DESCRIPTION |
|---|---|
sample1
|
A one-dimensional array-like object containing the first sample.
TYPE:
|
sample2
|
A one-dimensional array-like object containing the second sample.
TYPE:
|
equal_var
|
If True (default), perform a standard independent 2 sample test that assumes equal population variances. If False, perform Welch's t-test, which does not assume equal population variance
TYPE:
|
normal
|
Set to True if both sample data are normally distributed. If True, perform a t-test. If False and u_test is True, perform a Mann Whitney U test. If None, an Anderson-Darling test for normal distribution is performed for both sample data. If one of the two data sets is not normally distributed, normal is set to False, by default None
TYPE:
|
u_test
|
If True and data are not normally distributed, perform a Mann Whitney U test, by default True
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
p-value for the test
TYPE:
|
statistic
|
f if normal, else Levene test statistic
TYPE:
|
test
|
name of performed test
TYPE:
|
variance_test(sample1, sample2, normal=None, heavy_tailed=False)
¶
Perform test for equal variances of two independent variables. This test tests the null hypothesis that all input samples are from populations with equal variances.
| PARAMETER | DESCRIPTION |
|---|---|
sample1
|
A one-dimensional array-like object containing the first sample.
TYPE:
|
sample2
|
A one-dimensional array-like object containing the second sample.
TYPE:
|
normal
|
Set to True if both sample data are normally distributed. If true, an F-test is performed, otherwise a Levene test. If None, an Anderson-Darling test for normal distribution is performed for both sample data. If one of the two data sets is not normally distributed, normal is set to False, by default None
TYPE:
|
heavy_tailed
|
set True if data is heavy tailed. Is only taken into account if normal is False , by default False
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
p-value for the test
TYPE:
|
statistic
|
f if normal, else Levene test statistic
TYPE:
|
test
|
name of performed test
TYPE:
|
proportions_test(events1, observations1, events2, observations2, decision_threshold=1000)
¶
Hypothesis test for comparing two independent proportions This assumes that we have two independent binomial samples.
Fisher's exact test is one of exact tests. Especially when more than 20% of cells have expected frequencies < 5, we need to use Fisher's exact test because applying approximation method is inadequate. Fisher's exact test assesses the null hypothesis of independence applying hypergeometric distribution of the numbers in the cells of the table.
| PARAMETER | DESCRIPTION |
|---|---|
events1
|
Counted number of events of sample 1.
TYPE:
|
observations1
|
Total number of observations of sample 1.
TYPE:
|
events2
|
counted number of events of sample 2.
TYPE:
|
observations2
|
Total number of observations of sample 2.
TYPE:
|
decision_threshold
|
if the sum of sample size (observations1 + observations2) is greater than decision_threshold, the Fisher exact test is performed, by default 1000
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
p-value for the test
TYPE:
|
statistic
|
test statistic
TYPE:
|
test
|
name of performed test
TYPE:
|
kurtosis_test(sample)
¶
Two sided hypothesis test whether a dataset has normal kurtosis.
This function tests the null hypothesis that the kurtosis of the population from which the sample was drawn is that of the normal distribution. Performs the calculations ignoring nan values
| PARAMETER | DESCRIPTION |
|---|---|
sample
|
A one-dimensional array-like object containing the samples.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
p-value for the test
TYPE:
|
statistic
|
The computed z-score for this test
TYPE:
|
skew_test(sample)
¶
Two sided hypothesis whether the skew is different from the normal distribution.
This function tests the null hypothesis that the skewness of the population that the sample was drawn from is the same as that of a corresponding normal distribution. Performs the calculations ignoring nan values.
| PARAMETER | DESCRIPTION |
|---|---|
sample
|
A one-dimensional array-like object containing the samples.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
p
|
p-value for the test
TYPE:
|
statistic
|
The computed z-score for this test
TYPE:
|