Parametric Tests

SciPy - Statistical Testing

2 min read

Published Nov 17 2025


9
0
0
0

PythonSciPyStatistics

Parametric tests assume (approximately) normal data and continuous measurements.
They are among the most commonly used statistical tests in practice.




One-Sample t-Test

Question: Does the mean of a sample differ from a specific value?


Used for:

  • Comparing a sample to a known benchmark
  • Checking whether a measurement meets a target
  • Testing changes from a historical or expected mean

Example

from scipy import stats

sample = [5.2, 5.5, 4.9, 5.1, 5.0]

t_stat, p_value = stats.ttest_1samp(sample, popmean=5.0)
print(t_stat, p_value)

Interpretation

  • p < 0.05 → sample mean is significantly different from 5.0
  • p ≥ 0.05 → no significant difference





Independent Two-Sample t-Test

Question: Do two independent groups have different means?


Used for:

  • A/B tests
  • Male vs female comparisons
  • Treatment vs control groups

Example

group1 = [4.9, 5.1, 5.0, 5.3]
group2 = [5.6, 5.7, 5.4, 5.8]

t_stat, p_value = stats.ttest_ind(group1, group2)
print(t_stat, p_value)

Welch’s t-test (if variances differ)

t_stat, p_value = stats.ttest_ind(group1, group2, equal_var=False)

Interpretation

  • p < 0.05 → groups differ significantly
  • p ≥ 0.05 → no evidence of difference





Paired t-Test

Question: Do two related measurements differ?


Used when:

  • Measurements are on the same subjects
  • Before/after experiments
  • Paired samples (e.g. left hand vs right hand)

Example

before = [100, 102, 98, 105]
after = [103, 107, 101, 110]

t_stat, p_value = stats.ttest_rel(before, after)
print(t_stat, p_value)

Interpretation

  • p < 0.05 → the before/after difference is significant
  • Good for clinical improvements, performance upgrades, etc.





One-Way ANOVA (3+ groups)

Question: Do 3 or more groups differ?


Used for:

  • Comparing multiple product versions
  • Multiple study groups
  • Any ≥3 independent samples

Example

group_a = [5.1, 5.3, 5.2]
group_b = [6.2, 6.1, 6.3]
group_c = [4.9, 5.0, 4.8]

f_stat, p_value = stats.f_oneway(group_a, group_b, group_c)
print(f_stat, p_value)

Interpretation

  • p < 0.05 → at least one group differs
  • Does not tell which groups differ (requires post-hoc tests)

Post-hoc example using Statsmodels

(Not SciPy, but commonly paired.)

pip install statsmodels
import statsmodels.stats.multicomp as mc
import pandas as pd

data = pd.DataFrame({
    'value': group_a + group_b + group_c,
    'group': ['A']*3 + ['B']*3 + ['C']*3
})

comp = mc.MultiComparison(data['value'], data['group'])
post_hoc = comp.tukeyhsd()
print(post_hoc)





wo-Way ANOVA (Not in SciPy)

SciPy only supports one-way ANOVA directly.

For factorial designs (e.g., "treatment × gender"), use Statsmodels:

import statsmodels.api as sm
from statsmodels.formula.api import ols

df = pd.DataFrame(...)

model = ols("score ~ C(treatment) * C(gender)", data=df).fit()
anova_table = sm.stats.anova_lm(model, typ=2)
print(anova_table)





Z-Tests (Statsmodels, not SciPy)

SciPy does not include z-tests, but Statsmodels does.


Two-sample z-test

from statsmodels.stats.weightstats import ztest

z_stat, p_value = ztest(group1, group2)
print(z_stat, p_value)

Use for:

  • Large samples (n > 30)
  • Known population variance (rare)

One-sample z-test

z_stat, p_value = ztest(group1, value=5.0)
print(z_stat, p_value)





Assumptions Checklist (Practical)

Parametric tests work well if:

  • Data are roughly normal
  • Measurements are continuous
  • Groups have similar variances (for t-tests & ANOVA)
  • Sample size is reasonably large (CLT helps)

If not:

  • Use non-parametric tests





Quick Summary Table (Parametric Tests)

Goal

Test

SciPy Function

Compare sample to known value

One-sample t-test

ttest_1samp

Compare two groups

Independent t-test

ttest_ind

Compare two related measurements

Paired t-test

ttest_rel

Compare 3+ groups

One-way ANOVA

f_oneway

Compare mean(s) using z

Z-tests

Statsmodels


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact