Statistical Tests¶
Parametric and non-parametric hypothesis tests, normality and variance checks, effect sizes (Cohen's d, Hedges' g, Glass' delta, Cliff's delta), rank-based correlation and Benjamini-Hochberg p-value adjustment. All computations use scipy.stats.
compute_skewness_kurtosis(df, columns)
¶
Compute skewness and excess kurtosis for specified columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing numeric columns. |
required |
columns
|
list of str
|
Column names to compute skewness and kurtosis for. |
required |
Returns:
| Type | Description |
|---|---|
dict of str -> dict
|
Nested dict mapping column name to {'skewness': float, 'kurtosis': float}. Kurtosis is Fisher's (excess) definition, where normal distribution = 0. |
Examples:
>>> import pandas as pd, numpy as np
>>> df = pd.DataFrame({'a': np.random.normal(0, 1, 100)})
>>> result = compute_skewness_kurtosis(df, ['a'])
>>> 'skewness' in result['a']
True
Source code in utils/stats_tests_utils.py
summarize_descriptive_statistics(df, columns)
¶
Compute a descriptive statistics summary table for selected columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame. |
required |
columns
|
list of str
|
Columns to include in the summary. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Transposed summary DataFrame with columns: Mean, Median, Std Dev, Min, Max, Variance, Std Error. |
Examples:
>>> import pandas as pd, numpy as np
>>> df = pd.DataFrame({'x': np.random.normal(0, 1, 200)})
>>> summary = summarize_descriptive_statistics(df, ['x'])
>>> list(summary.columns)
['Mean', 'Median', 'Std Dev', 'Min', 'Max', 'Variance', 'Std Error']
Source code in utils/stats_tests_utils.py
run_one_sample_ttest(data, popmean)
¶
Perform a one-sample t-test to determine if the sample mean differs from a population mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Sample data. |
required |
popmean
|
float
|
Hypothesized population mean (null hypothesis H0: mean == popmean). |
required |
Returns:
| Type | Description |
|---|---|
dict of str -> float
|
{'t_stat': float, 'p_value': float} |
Examples:
>>> import numpy as np
>>> data = np.random.normal(5, 1, 100)
>>> result = run_one_sample_ttest(data, 5.0)
>>> 'p_value' in result
True
Source code in utils/stats_tests_utils.py
run_two_sample_ttest(data1, data2, equal_var=True)
¶
Perform an independent two-sample t-test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
array - like
|
First sample. |
required |
data2
|
array - like
|
Second sample. |
required |
equal_var
|
bool
|
If True, assumes equal population variances (Student's t-test). If False, uses Welch's t-test (does not assume equal variance). |
True
|
Returns:
| Type | Description |
|---|---|
dict of str -> float
|
{'t_stat': float, 'p_value': float} |
Source code in utils/stats_tests_utils.py
run_paired_ttest(before, after)
¶
Perform a paired (related samples) t-test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
before
|
array - like
|
Measurements from the first condition (e.g., pre-treatment). |
required |
after
|
array - like
|
Measurements from the second condition (e.g., post-treatment).
Must be the same length as |
required |
Returns:
| Type | Description |
|---|---|
dict of str -> float
|
{'t_stat': float, 'p_value': float} |
Source code in utils/stats_tests_utils.py
run_normality_tests(data)
¶
Run a battery of normality tests on the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Sample data to test for normality. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Results from three normality tests: - 'shapiro': ShapiroResult(statistic, pvalue) - 'dagostino': NormaltestResult(statistic, pvalue) (D'Agostino & Pearson) - 'anderson': AndersonResult(statistic, critical_values, significance_level) |
Notes
Shapiro-Wilk is most reliable for small samples (n < 50). D'Agostino's test is preferred for larger samples. Anderson-Darling provides critical values rather than a p-value.
Source code in utils/stats_tests_utils.py
run_variance_tests(data1, data2)
¶
Test equality of variances between two samples using three methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
array - like
|
First sample. |
required |
data2
|
array - like
|
Second sample. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Results from three variance equality tests: - 'levene': LeveneResult(statistic, pvalue) — robust to non-normality - 'bartlett': BartlettResult(statistic, pvalue) — for normally distributed data - 'fligner': FlignerResult(statistic, pvalue) — non-parametric alternative |
Source code in utils/stats_tests_utils.py
format_test_result(result_dict, test_name)
¶
Print a formatted summary of a hypothesis test result dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_dict
|
dict of str -> float
|
Dictionary of test results with string keys and numeric values. |
required |
test_name
|
str
|
Human-readable name for the test (used as the header in output). |
required |
Source code in utils/stats_tests_utils.py
cohens_d_independent(x, y, equal_var=True)
¶
Compute Cohen's d effect size for two independent samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
First sample. |
required |
y
|
array - like
|
Second sample. |
required |
equal_var
|
bool
|
If True, uses pooled standard deviation. If False, uses average of the two group SDs. |
True
|
Returns:
| Type | Description |
|---|---|
float
|
Cohen's d value. Interpretation: |d| < 0.2 small, 0.2–0.5 medium, > 0.8 large. |
Source code in utils/stats_tests_utils.py
hedges_g_independent(x, y, equal_var=True)
¶
Compute Hedges' g effect size — a bias-corrected version of Cohen's d.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
First sample. |
required |
y
|
array - like
|
Second sample. |
required |
equal_var
|
bool
|
Passed to cohens_d_independent for the underlying calculation. |
True
|
Returns:
| Type | Description |
|---|---|
float
|
Hedges' g value. Slightly smaller than Cohen's d due to small-sample bias correction factor J = 1 - 3/(4N - 9). |
Source code in utils/stats_tests_utils.py
glass_delta(x, y, ref='y')
¶
Compute Glass's delta effect size using only the control group SD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
Experimental or treatment group. |
required |
y
|
array - like
|
Control group (reference group by default). |
required |
ref
|
('y', 'x')
|
Which group's standard deviation to use as the reference. |
'y'
|
Returns:
| Type | Description |
|---|---|
float
|
Glass's delta value. Preferred when variances are unequal and one group is clearly the control. |
Source code in utils/stats_tests_utils.py
cliffs_delta(x, y)
¶
Compute Cliff's delta — a non-parametric effect size measure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
First sample. |
required |
y
|
array - like
|
Second sample. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Cliff's delta in [-1, 1]. Interpretation thresholds: |δ| < 0.147 negligible, 0.147–0.33 small, 0.33–0.474 medium, > 0.474 large. |
Notes
Cliff's delta measures how often values in one distribution are larger than values in another. It is robust to non-normality.
Source code in utils/stats_tests_utils.py
p_adjust_bh(pvals)
¶
Apply Benjamini-Hochberg (BH) False Discovery Rate correction to p-values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pvals
|
array-like of float
|
Raw p-values from multiple hypothesis tests. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
BH-adjusted p-values, clipped to [0, 1]. |
Notes
The BH procedure controls the expected proportion of false positives among rejected hypotheses (FDR), making it less conservative than Bonferroni correction.
Examples:
Source code in utils/stats_tests_utils.py
run_mannwhitney_u_test(group1, group2, alternative='two-sided')
¶
Perform Mann–Whitney U test (non-parametric test for two independent samples).
Parameters: - group1 (array-like): First independent sample - group2 (array-like): Second independent sample - alternative (str): 'two-sided', 'less', or 'greater'
Returns: - dict: { 'statistic': float, 'p_value': float, 'alternative': str }
Source code in utils/stats_tests_utils.py
format_mannwhitney_result(result_dict, test_name='Mann–Whitney U Test')
¶
Format the result dictionary from run_mannwhitney_u_test for readable output.
Source code in utils/stats_tests_utils.py
format_effect_sizes(cohens_d, hedges_g, cliffs_delta)
¶
Nicely formatted output for effect size metrics.
Source code in utils/stats_tests_utils.py
run_wilcoxon_signedrank(x, y)
¶
Perform Wilcoxon signed-rank test for paired samples (non-parametric).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
First set of measurements (e.g., pre-treatment). |
required |
y
|
array - like
|
Second set of measurements (e.g., post-treatment). Must be the same length as x. |
required |
Returns:
| Type | Description |
|---|---|
dict of str -> float
|
{'statistic': float, 'p_value': float} |
Notes
Non-parametric alternative to the paired t-test. Use when normality of differences cannot be assumed.
Source code in utils/stats_tests_utils.py
run_spearman_correlation(x, y)
¶
Compute Spearman rank correlation coefficient between two variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
First variable. |
required |
y
|
array - like
|
Second variable. |
required |
Returns:
| Type | Description |
|---|---|
dict of str -> float
|
{'spearman_r': float, 'p_value': float} spearman_r is in [-1, 1] where ±1 means perfect monotonic relationship. |
Source code in utils/stats_tests_utils.py
run_kendall_tau(x, y)
¶
Compute Kendall's tau rank correlation coefficient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
First variable. |
required |
y
|
array - like
|
Second variable. |
required |
Returns:
| Type | Description |
|---|---|
dict of str -> float
|
{'kendall_tau': float, 'p_value': float} tau is in [-1, 1]; more robust to ties than Spearman's r. |
Source code in utils/stats_tests_utils.py
rank_biserial_effect_size(x, y)
¶
Compute rank-biserial correlation effect size for Mann-Whitney U or Wilcoxon test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
First sample. |
required |
y
|
array - like
|
Second sample. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Rank-biserial correlation in [-1, 1]. |r| < 0.1 negligible, 0.1–0.3 small, 0.3–0.5 medium, > 0.5 large. |
Source code in utils/stats_tests_utils.py
compute_trimmed_stats(series, trim=0.1)
¶
Computes trimmed mean and trimmed standard deviation for a given series. :param series: pd.Series or array-like :param trim: proportion to cut from each tail (0 to 0.5) :return: dict with trimmed mean and std
Source code in utils/stats_tests_utils.py
compute_robust_summaries(series)
¶
Computes robust statistics (median, MAD, IQR) that are less sensitive to outliers. :param series: pd.Series or array-like :return: dict with robust metrics
Source code in utils/stats_tests_utils.py
perform_shapiro_test(data)
¶
Perform Shapiro-Wilk test for normality.
Parameters: data (array-like): Sample data.
Returns: dict: Test statistic and p-value.
Source code in utils/stats_tests_utils.py
levene_variance_test(group1, group2)
¶
Perform Levene's test to assess equality of variances.
Parameters: group1, group2 (array-like): Two samples to compare.
Returns: dict: Test statistic and p-value.