TExcelTAD
Home › SciPy › SciPy Statistical Significance Tests

SciPy Statistical Significance Tests

Suppose a new teaching method raised the average test score of one class from 71 to 75. Is the method really better, or could a difference that size happen just by chance? A statistical significance test answers this kind of question. The scipy.stats module implements dozens of them.

Key ideas

Term Meaning
Null hypothesis (H₀) The "nothing special" assumption: no difference, no effect
Alternative hypothesis (H₁) What you suspect instead: there is a difference
Test statistic A number summarising how far the data is from what H₀ predicts
p-value The probability of seeing data at least this extreme if H₀ were true
Significance level α Your threshold, usually 0.05

The decision rule: if p < α, reject the null hypothesis ("the result is statistically significant"). If p ≥ α, you fail to reject it — which is not the same as proving it true.

One-sample t-test

ttest_1samp() checks whether the mean of a sample differs from a given value. A bakery claims its loaves weigh 500 g; we weigh twelve:

python · try it yourself
import numpy as np
from scipy import stats

loaves = np.array([492, 497, 501, 489, 495, 498, 493, 500, 490, 496, 494, 499])
res = stats.ttest_1samp(loaves, popmean=500)
print(f"mean weight = {loaves.mean():.2f} g")
print(f"t = {res.statistic:.3f}, p = {res.pvalue:.4f}")
print("significant at 5%?", res.pvalue < 0.05)
mean weight = 495.33 g
t = -4.178, p = 0.0015
significant at 5%? True

The p-value is tiny, so a mean this far below 500 g would be very unlikely if the claim were true.

Comparing two groups

ttest_ind() compares the means of two independent groups. Passing equal_var=False runs Welch's t-test, which doesn't assume the groups have the same spread and is the safer default:

python · try it yourself
import numpy as np
from scipy import stats

rng = np.random.default_rng(7)
old_method = rng.normal(71, 8, size=30).round()
new_method = rng.normal(76, 8, size=30).round()

res = stats.ttest_ind(new_method, old_method, equal_var=False)
print(f"old mean = {old_method.mean():.2f}, new mean = {new_method.mean():.2f}")
print(f"t = {res.statistic:.3f}, p = {res.pvalue:.2e}")
ci = res.confidence_interval(0.95)
print(f"95% CI for the difference: ({ci.low:.2f}, {ci.high:.2f})")
old mean = 67.73, new mean = 76.00
t = 4.588, p = 2.46e-05
95% CI for the difference: (4.66, 11.87)

If the same subjects are measured twice (before and after), use the paired test ttest_rel() instead.

python · try it yourself
from scipy import stats

before = [82, 75, 90, 68, 77, 85, 79, 72]
after  = [85, 78, 91, 73, 80, 86, 83, 74]
res = stats.ttest_rel(after, before)
print(f"t = {res.statistic:.3f}, p = {res.pvalue:.5f}")
t = 5.601, p = 0.00082

Checking for normality

t-tests assume roughly normally distributed data. The Shapiro–Wilk test checks that: here H₀ is "the data is normal", so a small p-value means the data is probably not normal. Skewness and kurtosis help describe the shape:

python · try it yourself
import numpy as np
from scipy import stats

rng = np.random.default_rng(0)
normal_data = rng.normal(50, 5, size=100)
skewed_data = rng.exponential(5, size=100)

for name, data in [("normal", normal_data), ("skewed", skewed_data)]:
    w, p = stats.shapiro(data)
    print(f"{name}: W = {w:.3f}, p = {p:.3g}, skew = {stats.skew(data):.2f}, "
          f"kurtosis = {stats.kurtosis(data):.2f}")
normal: W = 0.982, p = 0.187, skew = 0.02, kurtosis = -0.58
skewed: W = 0.875, p = 1.16e-07, skew = 1.28, kurtosis = 1.25

Non-parametric alternatives

When the data clearly isn't normal, use tests based on ranks instead of means. They make fewer assumptions:

Situation Parametric test Non-parametric alternative
One sample vs a value ttest_1samp wilcoxon (on differences)
Two independent groups ttest_ind mannwhitneyu
Two paired measurements ttest_rel wilcoxon
Three or more groups f_oneway (ANOVA) kruskal
Distribution shape — kstest, shapiro, normaltest
python · try it yourself
import numpy as np
from scipy import stats

# Page load times (ms) for two server setups: skewed, with outliers
rng = np.random.default_rng(3)
setup_a = rng.exponential(120, size=40) + 80
setup_b = rng.exponential(90, size=40) + 80

res = stats.mannwhitneyu(setup_a, setup_b)
print(f"median A = {np.median(setup_a):.1f} ms, median B = {np.median(setup_b):.1f} ms")
print(f"U = {res.statistic:.0f}, p = {res.pvalue:.4f}")

# Kolmogorov-Smirnov: does setup A follow a normal distribution?
z = (setup_a - setup_a.mean()) / setup_a.std(ddof=1)
print(f"KS vs normal: p = {stats.kstest(z, 'norm').pvalue:.4f}")
median A = 154.6 ms, median B = 146.2 ms
U = 837, p = 0.7254
KS vs normal: p = 0.3034

Here both p-values are large. The medians differ by only a few milliseconds, and with 40 noisy measurements per setup that difference is well within what chance alone produces, so we cannot claim either setup is faster.

Note: "Not significant" never means "proven equal". With a small sample, even a real difference may not reach significance. Always report the effect size (the difference itself, or a confidence interval) alongside the p-value.

Test yourself

A test returns p = 0.003 and you use α = 0.05. What do you conclude?

  1. Fail to reject the null hypothesis
  2. Reject the null hypothesis: the result is significant
  3. The null hypothesis is proven true
  4. The test is invalid

Which test compares two groups measured on the same subjects (before/after)?

  1. ttest_ind
  2. ttest_rel
  3. shapiro
  4. kstest

In the Shapiro–Wilk test, what does a very small p-value suggest?

  1. The data is normal
  2. The data is probably not normally distributed
  3. The sample is too small
  4. The mean is zero