Skip to content

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 a rv_continuous instance.
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\):

\[ A^2 = -N - \frac{1}{N} \sum_{i=1}^{N} (2i-1) \bigl(\ln F(x_i) + \ln(1 - F(x_{N-i+1}))\bigr) \]

To obtain a p-value the statistic is adjusted for finite sample size:

\[ A^* = A^2 \left(1 + \frac{0.75}{N} + \frac{2.25}{N^2}\right) \]

The p-value is then derived from \(A^*\) using the following piecewise approximation:

\[ p = \begin{cases} 0 & A^* \ge 13 \\ \exp(1.2937 - 5.709\,A^* + 0.0186\,(A^*)^2) & 6 \le A^* < 13 \\ \exp(0.9177 - 4.279\,A^* - 1.38\,(A^*)^2) & 0.34 < A^* < 0.6 \\ 1 - \exp(42.796\,A^* - 59.938\,(A^*)^2 - 8.318) & 0.2 < A^* \le 0.34 \\ 1 - \exp(101.14\,A^* - 223.73\,(A^*)^2 - 13.436) & A^* \le 0.2 \end{cases} \]
PARAMETER DESCRIPTION
sample

A one-dimensional array-like object containing the samples.

TYPE: NumericSample1D

RETURNS DESCRIPTION
p

The p-value for the test.

TYPE: float

A_star

The adjusted Anderson-Darling test statistic \(A^*\).

TYPE: float

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: NumericSample1D DEFAULT: ()

p_threshold

The threshold p-value for significance (default is 0.05).

TYPE: float DEFAULT: 0.05

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: NumericSample1D

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: str or scipy.stats rv_continous

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: (two - sided, less, greater) DEFAULT: 'two-sided'

RETURNS DESCRIPTION
p

The two-tailed p-value for the test

TYPE: float

D

Kolmogorov-Smirnov test statistic, either D, D+ or D-.

TYPE: float

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. norm).

TYPE: Tuple[float, ...]

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:

\[ F = \frac{s_1^2}{s_2^2} \]

The two-sided p-value is computed as:

\[ p = 2 \cdot \min\bigl(F_{\text{cdf}}(F),\; 1 - F_{\text{cdf}}(F)\bigr) \]

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: NumericSample1D

sample2

A one-dimensional array-like object containing the second sample.

TYPE: NumericSample1D

RETURNS DESCRIPTION
p

The two-sided p-value for the test.

TYPE: float

F

The F-test statistic.

TYPE: float

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:

\[ W = \frac{N - k}{k - 1} \cdot \frac{\sum_{i=1}^{k} N_i (\bar{Z}_i - \bar{Z})^2} {\sum_{i=1}^{k} \sum_{j=1}^{N_i} (Z_{ij} - \bar{Z}_i)^2} \]

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:

\[ Z_{ij} = \lvert Y_{ij} - \tilde{Y}_i \rvert \]

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: NumericSample1D

sample2

A one-dimensional array-like object containing the second sample.

TYPE: NumericSample1D

heavy_tailed

If True, a 10 % trimmed mean is used as the centre measure (robust against heavy-tailed distributions). If False (default), the median is used.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
p

p-value for the test.

TYPE: float

L

Levene test statistic W.

TYPE: float

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: NumericSample1D

n_sections

Amount of sections to divide the data into, by default 3

TYPE: int DEFAULT: 3

RETURNS DESCRIPTION
p

p-value for the test

TYPE: float

L

Levene test statistic

TYPE: float

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: NumericSample1D

n_sections

Amount of sections to divide the data into, by default 3

TYPE: int DEFAULT: 3

RETURNS DESCRIPTION
p

p-value for the test

TYPE: float

statistic

The computed F statistic of the test.

TYPE: float

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: NumericSample1D

sample2

A one-dimensional array-like object containing the second sample.

TYPE: NumericSample1D

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: bool DEFAULT: True

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: bool or None DEFAULT: None

u_test

If True and data are not normally distributed, perform a Mann Whitney U test, by default True

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
p

p-value for the test

TYPE: float

statistic

f if normal, else Levene test statistic

TYPE: float

test

name of performed test

TYPE: string

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: NumericSample1D

sample2

A one-dimensional array-like object containing the second sample.

TYPE: NumericSample1D

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: bool or None DEFAULT: None

heavy_tailed

set True if data is heavy tailed. Is only taken into account if normal is False , by default False

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
p

p-value for the test

TYPE: float

statistic

f if normal, else Levene test statistic

TYPE: float

test

name of performed test

TYPE: string

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: int

observations1

Total number of observations of sample 1.

TYPE: int

events2

counted number of events of sample 2.

TYPE: int

observations2

Total number of observations of sample 2.

TYPE: int

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: int DEFAULT: 1000

RETURNS DESCRIPTION
p

p-value for the test

TYPE: float

statistic

test statistic

TYPE: float

test

name of performed test

TYPE: string

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: NumericSample1D

RETURNS DESCRIPTION
p

p-value for the test

TYPE: float

statistic

The computed z-score for this test

TYPE: float

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: NumericSample1D

RETURNS DESCRIPTION
p

p-value for the test

TYPE: float

statistic

The computed z-score for this test

TYPE: float