Statistical Test Functions¶
All test functions return a HypoResult object.
Parametric Tests¶
- hypotestx.tests.parametric.one_sample_ttest(data: List[float], mu: float = 0.0, alpha: float = 0.05, alternative: str = 'two-sided') HypoResult[source]¶
One-sample t-test implemented from scratch
- Parameters:
data – Sample data
mu – Hypothesized population mean
alpha – Significance level
alternative – “two-sided”, “greater”, or “less”
- Returns:
HypoResult object with test results
- hypotestx.tests.parametric.two_sample_ttest(group1: List[float], group2: List[float], alpha: float = 0.05, alternative: str = 'two-sided', equal_var: bool = True) HypoResult[source]¶
Two-sample t-test (Student’s t-test or Welch’s t-test)
- Parameters:
group1 – First group data
group2 – Second group data
alpha – Significance level
alternative – “two-sided”, “greater”, or “less”
equal_var – Whether to assume equal variances (Student’s vs Welch’s)
- Returns:
HypoResult object with test results
- hypotestx.tests.parametric.paired_ttest(before: List[float], after: List[float], alpha: float = 0.05, alternative: str = 'two-sided') HypoResult[source]¶
Paired t-test for dependent samples
- Parameters:
before – Before measurements
after – After measurements
alpha – Significance level
alternative – “two-sided”, “greater”, or “less”
- Returns:
HypoResult object with test results
- hypotestx.tests.parametric.anova_one_way(*groups: List[float], alpha: float = 0.05) HypoResult[source]¶
One-way analysis of variance (ANOVA).
Tests whether the population means of three or more independent groups are equal. Assumes normality and homogeneity of variance within groups.
- Parameters:
*groups – Two or more group samples
alpha – Significance level
- Returns:
HypoResult with statistic=F, effect_size=eta-squared
Examples
>>> result = anova_one_way([5, 6, 7], [8, 9, 10], [3, 4, 5]) >>> print(result.summary())
Non-parametric Tests¶
- hypotestx.tests.nonparametric.mann_whitney_u(group1: List[float], group2: List[float], alpha: float = 0.05, alternative: str = 'two-sided') HypoResult[source]¶
Mann-Whitney U test for two independent samples.
A non-parametric alternative to the two-sample t-test that tests whether one group tends to have larger values than the other.
Uses a normal approximation (with tie correction) for the p-value.
- Parameters:
group1 – First group sample values
group2 – Second group sample values
alpha – Significance level (default 0.05)
alternative – ‘two-sided’, ‘greater’ (group1 > group2), or ‘less’
- Returns:
HypoResult with statistic=U1, effect_size=rank-biserial r
Examples
>>> result = mann_whitney_u([1, 2, 3, 4], [5, 6, 7, 8]) >>> print(result.summary())
- hypotestx.tests.nonparametric.wilcoxon_signed_rank(x: List[float], y: List[float] | None = None, mu: float = 0.0, alpha: float = 0.05, alternative: str = 'two-sided') HypoResult[source]¶
Wilcoxon signed-rank test.
Tests whether the median of differences (or of a single sample relative to mu) differs from zero. Non-parametric alternative to the one-sample or paired t-test.
- Parameters:
x – Data values (or differences if y is not supplied)
y – Optional second paired sample; if provided, differences = x - y
mu – Hypothesised median under H0 (default 0)
alpha – Significance level
alternative – ‘two-sided’, ‘greater’, or ‘less’
- Returns:
HypoResult with statistic=W+ (sum of positive ranks)
Examples
>>> result = wilcoxon_signed_rank([1, 2, 3, 4, 5], mu=2) >>> result = wilcoxon_signed_rank(before, after) # paired
- hypotestx.tests.nonparametric.kruskal_wallis(*groups: List[float], alpha: float = 0.05) HypoResult[source]¶
Kruskal-Wallis H test for k independent groups.
Non-parametric one-way ANOVA. Tests whether the population medians of all groups are equal. P-value is obtained from the chi-square distribution with k-1 degrees of freedom.
- Parameters:
*groups – Two or more group samples (each a list of floats)
alpha – Significance level
- Returns:
HypoResult with statistic=H, effect_size=eta-squared
Examples
>>> result = kruskal_wallis([1,2,3], [4,5,6], [7,8,9]) >>> print(result.summary())
Categorical Tests¶
- hypotestx.tests.categorical.chi_square_test(observed: List[List[float]] | List[float], expected: List[float] | None = None, alpha: float = 0.05, correction: bool = False) HypoResult[source]¶
Chi-square test of independence (2-D table) or goodness-of-fit (1-D).
For a 2-D contingency table the test checks whether row and column variables are independent. For a 1-D array of observed counts it checks whether they follow the specified (or uniform) expected distribution.
- Parameters:
observed – 2-D contingency table or 1-D list of observed counts
expected – Expected counts for each category (1-D case only). If None, a uniform expected distribution is assumed.
alpha – Significance level
correction – Apply Yates’ continuity correction (2×2 tables only)
- Returns:
HypoResult with statistic=chi2, effect_size=Cramer’s V (or phi for 2×2)
Examples
>>> # 2-D contingency table >>> table = [[30, 10], [20, 40]] >>> result = chi_square_test(table)
>>> # Goodness-of-fit >>> result = chi_square_test([50, 30, 20], expected=[40, 30, 30])
- hypotestx.tests.categorical.fisher_exact_test(table: List[List[float]], alpha: float = 0.05, alternative: str = 'two-sided') HypoResult[source]¶
Fisher’s Exact Test for a 2×2 contingency table.
Computes the exact p-value using the hypergeometric distribution. Preferred over the chi-square test when any expected cell count is < 5.
- Parameters:
table – 2×2 contingency table [[a, b], [c, d]]
alpha – Significance level
alternative – ‘two-sided’, ‘greater’ (more association than expected), or ‘less’
- Returns:
HypoResult with statistic=odds_ratio
Examples
>>> table = [[8, 2], [1, 5]] >>> result = fisher_exact_test(table) >>> print(result.p_value)
Correlation Tests¶
- hypotestx.tests.correlation.pearson_correlation(x: List[float], y: List[float], alpha: float = 0.05, alternative: str = 'two-sided') HypoResult[source]¶
Pearson product-moment correlation coefficient and test.
Tests whether the linear relationship between x and y is significantly different from zero. Assumes both variables are approximately continuous and bivariate normal.
- Parameters:
x – First variable
y – Second variable (same length as x)
alpha – Significance level
alternative – ‘two-sided’, ‘greater’ (positive correlation), or ‘less’
- Returns:
HypoResult with statistic=t, effect_size=r (Pearson r)
Examples
>>> result = pearson_correlation([1,2,3,4,5], [2,4,5,4,5]) >>> print(f"r = {result.effect_size:.3f}, p = {result.p_value:.4f}")
- hypotestx.tests.correlation.spearman_correlation(x: List[float], y: List[float], alpha: float = 0.05, alternative: str = 'two-sided') HypoResult[source]¶
Spearman rank-order correlation coefficient and test.
A non-parametric measure of monotonic association. Computed by ranking both variables and calculating their Pearson correlation.
- Parameters:
x – First variable
y – Second variable (same length as x)
alpha – Significance level
alternative – ‘two-sided’, ‘greater’, or ‘less’
- Returns:
HypoResult with statistic=t, effect_size=rho (Spearman rho)
Examples
>>> result = spearman_correlation([3,1,4,1,5], [9,2,6,5,3]) >>> print(f"rho = {result.effect_size:.3f}")
- hypotestx.tests.correlation.point_biserial_correlation(continuous: List[float], binary: List, alpha: float = 0.05, alternative: str = 'two-sided') HypoResult[source]¶
Point-biserial correlation between a continuous and a dichotomous variable.
Equivalent to Pearson correlation when one variable is binary (0/1 coded). Tests whether the mean of the continuous variable differs across the two groups.
- Parameters:
continuous – Continuous-scale measurements
binary – Binary group indicator (must contain exactly 2 unique values)
alpha – Significance level
alternative – ‘two-sided’, ‘greater’, or ‘less’
- Returns:
HypoResult with statistic=t, effect_size=r_pb
Examples
>>> result = point_biserial_correlation( ... [5.5, 6.1, 7.2, 4.8, 5.9], ... [0, 1, 1, 0, 0] ... )
Public Aliases¶
The following short-form names are exposed at the top-level hypotestx
namespace for convenience:
hx.ttest_1samp # one_sample_ttest
hx.ttest_2samp # two_sample_ttest
hx.welch_ttest # two_sample_ttest with equal_var=False
hx.ttest_paired # paired_ttest
hx.anova_1way # anova_one_way
hx.mannwhitney # mann_whitney_u
hx.wilcoxon # wilcoxon_signed_rank
hx.kruskal # kruskal_wallis
hx.chi2_test # chi_square_test
hx.fisher_exact # fisher_exact_test
hx.pearson # pearson_correlation
hx.spearman # spearman_correlation
hx.pointbiserial # point_biserial_correlation