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 computed by SciPy using interpolation from pre-calculated tables, which provides more accurate results than piecewise approximation formulas.

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 implementation uses SciPy's anderson function with method='interpolate' to compute accurate p-values from pre-calculated tables. The adjusted statistic \(A^*\) is returned for compatibility with the literature and for manual interpretation using critical value tables.

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

dunn_test(groups, p_adjust='bonferroni')

Dunn's test for pairwise comparisons after Kruskal-Wallis.

Dunn's test is a post-hoc test used after a significant Kruskal-Wallis test to determine which groups differ from each other. It performs pairwise comparisons using rank sums with multiple comparison correction.

This test is appropriate for: - Non-parametric data (no normality assumption) - Unbalanced designs (different sample sizes per group) - Ordinal or continuous data - Multiple group comparisons

PARAMETER DESCRIPTION
groups

Dictionary mapping group names to sample data. Example: {'Group1': [1, 2, 3], 'Group2': [4, 5, 6]}

TYPE: dict[str, NumericSample1D]

p_adjust

Method for p-value adjustment. Options: - 'bonferroni': Conservative, controls FWER - 'holm': Less conservative than Bonferroni - 'hochberg': Similar to Holm - 'BH' (Benjamini-Hochberg): Controls FDR - 'BY' (Benjamini-Yekutieli): More conservative FDR - 'none': No adjustment Default is 'bonferroni'.

TYPE: str DEFAULT: 'bonferroni'

RETURNS DESCRIPTION
DataFrame

Results table with columns: - 'Group1': First group in comparison - 'Group2': Second group in comparison - 'z_statistic': Standardized test statistic - 'p_raw': Unadjusted p-value - 'p_adjusted': Adjusted p-value - 'significant': Boolean, True if p_adjusted < 0.05

Examples:

After a significant Kruskal-Wallis test:

import daspi as dsp
import pandas as pd

# Example data: defect counts for 3 factor combinations
data = {
    'A1_B1': [12, 10, 11, 13],
    'A1_B2': [8, 7, 9, 8],
    'A2_B1': [5, 4, 6, 5],
}

# Perform Dunn's test
results = dsp.dunn_test(data, p_adjust='bonferroni')
print(results)
Notes

Dunn's test compares rank sums between groups using the formula:

\[ z_{ij} = \frac{\bar{R}_i - \bar{R}_j}{SE} \]

where \(\bar{R}_i\) and \(\bar{R}_j\) are mean ranks for groups i and j, and SE is the standard error accounting for ties.

Multiple comparison correction: - Bonferroni: \(p_{adj} = \min(p_{raw} \times k, 1)\) where k = number of comparisons - Benjamini-Hochberg: Controls False Discovery Rate (FDR)

When to use which correction: - Bonferroni/Holm: When you want strong control of Type I error (FWER) - BH/BY: When you're willing to accept some false positives (FDR)

References

Dunn, O. J. (1964). Multiple comparisons using rank sums. Technometrics, 6(3), 241-252.

pairwise_tests(groups, test='auto', p_adjust='bonferroni', equal_var=True)

Perform pairwise comparisons between groups with multiple test correction.

This function performs all pairwise comparisons between groups and applies multiple comparison correction. It can automatically select between parametric (t-test) and non-parametric (Mann-Whitney U) tests based on normality.

PARAMETER DESCRIPTION
groups

Dictionary mapping group names to sample data.

TYPE: dict[str, NumericSample1D]

test

Statistical test to use: - 't': Independent samples t-test (parametric) - 'mannwhitneyu': Mann-Whitney U test (non-parametric) - 'auto': Automatically select based on normality (default)

TYPE: Literal['t', 'mannwhitneyu', 'auto'] DEFAULT: 'auto'

p_adjust

Method for p-value adjustment (same options as dunn_test). Default is 'bonferroni'.

TYPE: str DEFAULT: 'bonferroni'

equal_var

For t-test only: assume equal variances. Default is True.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
DataFrame

Results table with columns similar to dunn_test plus: - 'test_used': Which test was applied - 'mean_diff': Difference in means (for t-test)

Examples:

import daspi as dsp

data = {
    'Control': [10, 12, 11, 13, 12],
    'Treatment1': [15, 16, 14, 17, 15],
    'Treatment2': [8, 9, 7, 10, 8]
}

# Automatic test selection with Bonferroni correction
results = dsp.pairwise_tests(data, test='auto', p_adjust='bonferroni')
print(results)

# Force Mann-Whitney U test (non-parametric)
results_np = dsp.pairwise_tests(data, test='mannwhitneyu')
print(results_np)
Notes

Test selection (when test='auto'): - Checks normality of all groups using Anderson-Darling test - If all groups are normal → uses t-test - If any group is non-normal → uses Mann-Whitney U

Multiple comparison methods: - Use Bonferroni for strong Type I error control - Use BH (Benjamini-Hochberg) for more power when many comparisons

Interpretation: - p_adjusted < 0.05: Groups significantly different at α=0.05 level - mean_diff: Positive means Group1 > Group2