flowchart TD
subgraph Outcome["Outcome Type"]
Q0["Identify Outcome<br/>Continuous, Skewed, or Binary"]
end
subgraph TestFamily["Hypothesis Tests"]
C1["Continuous (Normal)<br/>2 grps: Welch t + Cohen d<br/>k>=3: One-Way ANOVA"]
C2["Skewed / Ordinal<br/>2 grps: Mann-Whitney U<br/>k>=3: Kruskal-Wallis H"]
C3["Binary / 2x2 Table<br/>expected_min >= 5: Chi2<br/>expected_min < 5: Fisher"]
end
subgraph Model["Adjusted Model"]
M1["Epidemiological Metrics<br/>Wilson CI, RR, Woolf OR<br/>+ BH FDR (Multi-Test)"]
M2["Multivariable Regression<br/>sm.Logit(y, add_constant(X))<br/>exp(params) -> Adjusted OR"]
end
Q0 -->|"Approx. Normal"| C1
Q0 -->|"Skewed / Ties"| C2
Q0 -->|"Categorical"| C3
C1 --> M2
C2 --> M2
C3 --> M1
M1 --> M2
20 Probability, Statistical Inference, Hypothesis Testing, and Epidemiological Metrics in Python
Chapter Mission: Complete Part IV (The Scientific Data Stack & Mathematical Foundations) by uniting probability theory, statistical inference, hypothesis testing, multiple-testing control, and multivariable logistic regression in Python. Using
scipy.statsandstatsmodels.apion Bangladesh maternal and child health survey data (mch_survey_sample), you will move from descriptive tables to publication-grade confidence intervals, effect sizes, risk/odds ratios, and adjusted epidemiological models.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch20 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch20 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch20 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Compute exact distribution probabilities with
scipy.stats(Normal, Binomial) and verify the Central Limit Theorem (CLT) through reproducible NumPy Monte Carlo simulation. - Estimate clinical proportions with 95% Wilson score confidence intervals, avoiding the zero-event collapse and boundary overshoot of naive Wald intervals.
- Choose and execute the right group-comparison test—Welch’s two-sample \(t\)-test with pooled Cohen’s \(d\), Mann-Whitney \(U\), One-Way ANOVA, and Kruskal-Wallis \(H\)—based on outcome scale, variance heterogeneity, and skewness.
- Audit \(2 \times 2\) contingency tables using Pearson’s \(\chi^2\) (
correction=False), expected-cell checks (expected_min < 5), Fisher’s exact test, Risk Ratios (RR), and Odds Ratios (OR) with Woolf 95% confidence intervals. - Guard multi-hypothesis analyses against false positives using Benjamini-Hochberg False Discovery Rate (FDR) adjustment (
scipy.stats.false_discovery_control). - Clean messy epidemiological survey covariates and fit a multivariable logistic regression model in
statsmodels.api.Logit, reporting exponentiated Adjusted Odds Ratios (aOR), 95% CIs, and \(p\)-values for publication tables.
20.1 Stage 1 — First-Principles Intuition: From Descriptive Tables to Inferential Evidence
Across Chapters 16–19, you built the full computational foundation of modern data science: vectorized NumPy arrays, clean Pandas data pipelines, publication Matplotlib/Seaborn graphics, and linear algebra & calculus intuition. Yet a mean, median, or bar chart alone describes only the 500 households in your sample.
Peer reviewers, ministries of health, and clinical trialists ask a deeper question: Could this observed rural–urban anemia gap be mere sampling luck, how large is the effect clinically, and does it survive after controlling for confounders?
That leap—from finite sample to population claim—is statistical inference. Five core principles govern every test in this chapter:
- Sampling Variability & the Central Limit Theorem: Even if individual antenatal care (ANC) visits are heavily right-skewed, the distribution of sample means \(\bar{X}\) across repeated samples approaches a Normal distribution centered at \(\mu\) with standard error \(\text{SE} = \sigma / \sqrt{n}\).
- Interval Estimation Over Bare Point Estimates: Saying “anemia prevalence is 35.8%” hides sample-size precision. Reporting “35.8% (95% Wilson CI: 31.7%–40.1%)” communicates both estimate and uncertainty transparently.
- Effect Size + \(p\)-Value Together: A \(p\)-value answers whether an observed difference is incompatible with the null hypothesis (\(H_0\)); it says nothing about clinical magnitude. Always pair \(p\)-values with effect sizes (Cohen’s \(d\), RR, OR, or aOR).
- Multiple Testing Inflates False Positives: If you run 20 independent tests at \(\alpha = 0.05\) when every null is true, you expect one false positive by pure chance! Controlling the False Discovery Rate (FDR) via Benjamini-Hochberg prevents cherry-picking noise.
- Crude vs. Adjusted Associations (Confounding): In
mch_survey_sample, rural mothers show a slightly higher crude anemia rate than urban mothers—yet urban mothers also attend significantly more ANC visits. Multivariable logistic regression untangles which predictor actually drives the risk.
Never memorize a wall of test names in isolation. Ask just two diagnostic questions:
- What is the outcome variable’s scale? Continuous/numeric (
maternal_hb_g_dl), skewed count/ordinal (anc_visits), or binary/categorical (maternal_anemia)? - Are you comparing 2 groups, \(k \ge 3\) groups, or adjusting for multiple covariates simultaneously?
Once you answer those two questions, the exact SciPy or Statsmodels tool follows deterministically!
20.2 Stage 2 — Visual Mental Model: Statistical Test Selector
Use this decision map whenever you move from exploratory data analysis (EDA) to formal hypothesis testing and multivariable modeling:
Notice how bivariate tests (Welch \(t\), Mann-Whitney \(U\), \(\chi^2\), ANOVA, Kruskal-Wallis) populate Table 1 & Table 2 of a scientific paper, whereas multivariable regression (sm.Logit) powers Table 3, isolating independent Adjusted Odds Ratios (aOR) while controlling for confounders.
scipy.stats.ttest_ind(a, b)defaults toequal_var=True(Student’s \(t\)-test). Always passequal_var=Falsefor Welch’s \(t\)-test, which stays valid when group variances or sample sizes differ.scipy.stats.chi2_contingency(table)defaults tocorrection=True(Yates’ continuity correction for \(2 \times 2\) tables), which is overly conservative. Passcorrection=Falsefor standard Pearson’s \(\chi^2\), and switch tostats.fisher_exactwheneverexpected_min < 5.
20.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_04_data_science_math/ch20_biostatistics_inference/01_walkthrough.ipynb in Colab and execute every cell top-to-bottom alongside the dissections below.
20.3.1 Setup: Loading & Cleaning mch_survey_sample
import math
import numpy as np
import pandas as pd
from scipy import stats
import statsmodels.api as sm
import sharukh_tutor as tutor
mch_raw = tutor.load_data("mch_survey_sample", as_pandas=True)
mch = mch_raw.copy()
mch["residence"] = mch["residence"].astype(str).str.strip().str.title()
mch["anc_visits"] = mch["anc_visits"].replace(999, np.nan)
print("Loaded mch_survey_sample:", mch.shape)
print(mch["residence"].value_counts().to_dict())Expected Output:
Before running a single statistical test, we standardize mixed casing in residence ("rural" / "urban" \(\rightarrow\) "Rural" / "Urban") and replace the field-survey missing sentinel anc_visits == 999 with np.nan on a clean .copy(). Leaving sentinel 999 inside numeric covariates is one of the fastest ways to silently corrupt means, variances, and regression slopes.
20.3.2 Probability Distributions & CLT Monte Carlo Simulation
# 1. Theoretical Normal & Binomial probabilities
p_anemic_hb = stats.norm.cdf(11.0, loc=11.3, scale=1.4)
p_20_or_more = stats.binom.sf(19, n=50, p=0.35)
print(f"P(Hb < 11.0 g/dL | N(11.3, 1.4)): {p_anemic_hb:.4f}")
print(f"P(X >= 20 cases | Binom(50, 0.35)): {p_20_or_more:.4f}")
# 2. Central Limit Theorem (CLT) from skewed Exponential(scale=2.0)
rng = np.random.default_rng(42)
draws = rng.exponential(scale=2.0, size=(2000, 30))
sample_means = draws.mean(axis=1)
theo_se = 2.0 / math.sqrt(30)
print(f"CLT mean of 2,000 means: {sample_means.mean():.4f} (true=2.0)")
print(f"Empirical SE: {sample_means.std(ddof=1):.4f} "
f"| Theoretical SE: {theo_se:.4f}")Expected Output:
Look closely at two details:
- Survival function (
sf): For discretestats.binom,P(X >= 20)equals1 - P(X <= 19)=stats.binom.sf(19, n=50, p=0.35)(note19, not20!). - CLT in action: Even though individual
Exponential(scale=2.0)observations are severely skewed, the standard deviation of 2,000 sample means (0.3636) matches the theoretical standard error \(\sigma / \sqrt{30} = 2.0 / \sqrt{30} = 0.3651\) to three decimal places!
20.3.3 Proportion Estimation & 95% Wilson Score Confidence Interval
For a binomial proportion \(\hat{p} = k / n\) with critical value \(z\) (\(z = 1.96\) for 95% CI), the Wilson score interval centers and scales the interval so it never falls below \(0\), never exceeds \(1\), and remains accurate even at \(k = 0\):
\[d = 1 + \frac{z^2}{n}, \qquad c = \frac{\hat{p} + \frac{z^2}{2n}}{d}, \qquad m = \frac{z \sqrt{\frac{\hat{p}(1 - \hat{p}) + \frac{z^2}{4n}}{n}}}{d}\]
def wilson_ci_demo(successes, n, z=1.96):
p = successes / n
denom = 1 + (z ** 2) / n
center = (p + (z ** 2) / (2 * n)) / denom
margin = (
z * math.sqrt((p * (1 - p) + (z ** 2) / (4 * n)) / n)
) / denom
return (round(p, 4), round(max(0.0, center - margin), 4),
round(min(1.0, center + margin), 4))
anemic_n = int(mch["maternal_anemia"].sum())
total_n = len(mch)
prop, lo, hi = wilson_ci_demo(anemic_n, total_n)
print(f"Maternal anemia: {anemic_n}/{total_n} = {prop:.4f} "
f"(95% Wilson CI: [{lo:.4f}, {hi:.4f}])")
print("Zero-event sample (0/20) Wilson CI:", wilson_ci_demo(0, 20))Expected Output:
Notice the 0/20 boundary check: a naive Wald interval \(\hat{p} \pm 1.96\sqrt{\hat{p}(1-\hat{p})/n}\) collapses to [0.0, 0.0]—falsely claiming zero uncertainty! Wilson’s score interval returns [0.0, 0.1611], honestly showing that true prevalence could still be up to 16.1%.
20.3.4 Parametric Comparison: Welch’s \(t\)-Test & Pooled Cohen’s \(d\)
To quantify both statistical significance and standardized effect size between two continuous groups, pair Welch’s \(t\)-test (equal_var=False) with pooled Cohen’s \(d\):
\[s_p = \sqrt{\frac{(n_a - 1) s_a^2 + (n_b - 1) s_b^2}{n_a + n_b - 2}}, \qquad d = \frac{\bar{x}_a - \bar{x}_b}{s_p}\]
hb_urban = mch.loc[mch["residence"] == "Urban", "maternal_hb_g_dl"]
hb_rural = mch.loc[mch["residence"] == "Rural", "maternal_hb_g_dl"]
a = hb_urban.dropna().to_numpy()
b = hb_rural.dropna().to_numpy()
t_res = stats.ttest_ind(a, b, equal_var=False)
var_a, var_b = np.var(a, ddof=1), np.var(b, ddof=1)
pooled_sd = math.sqrt(
((len(a) - 1) * var_a + (len(b) - 1) * var_b) / (len(a) + len(b) - 2)
)
cohens_d = (a.mean() - b.mean()) / pooled_sd
print(f"Urban Hb mean: {a.mean():.3f} (n={len(a)}) | "
f"Rural Hb mean: {b.mean():.3f} (n={len(b)})")
print(f"Welch t={t_res.statistic:.4f}, p={t_res.pvalue:.4f}, "
f"Cohen's d={cohens_d:.4f}")Expected Output:
Urban mothers average 11.598 g/dL vs. 11.406 g/dL in rural areas—a very small effect (d = 0.1385) that is not statistically significant at \(\alpha = 0.05\) (p = 0.1857). Notice how .dropna() was essential because 22 mothers had missing hemoglobin readings!
20.3.5 Nonparametric Rank Tests: Mann-Whitney \(U\)
Antenatal care (anc_visits) is a discrete, right-skewed count variable with many ties. Instead of assuming normality, compare medians and run a two-sided Mann-Whitney \(U\) rank-sum test:
anc_urban = mch.loc[mch["residence"] == "Urban", "anc_visits"].dropna()
anc_rural = mch.loc[mch["residence"] == "Rural", "anc_visits"].dropna()
u_res = stats.mannwhitneyu(anc_urban, anc_rural, alternative="two-sided")
print(f"Median ANC visits — Urban: {anc_urban.median():.1f}, "
f"Rural: {anc_rural.median():.1f}")
print(f"Mann-Whitney U={u_res.statistic:.2f}, "
f"p={u_res.pvalue:.6f}, significant={bool(u_res.pvalue < 0.05)}")Expected Output:
Urban mothers receive a median of 5.0 ANC visits compared with 3.0 for rural mothers (p < 0.0001). Remember this striking rural–urban service gap—it will reappear as a classic confounder in our logistic regression!
20.3.6 Categorical Association: Pearson \(\chi^2\) & Fisher’s Exact Test
ct = pd.crosstab(mch["residence"], mch["maternal_anemia"])
a_cell = int(ct.loc["Rural", 1])
b_cell = int(ct.loc["Rural", 0])
c_cell = int(ct.loc["Urban", 1])
d_cell = int(ct.loc["Urban", 0])
table_2x2 = [[a_cell, b_cell], [c_cell, d_cell]]
chi2, p_chi2, dof, expected = stats.chi2_contingency(
table_2x2, correction=False
)
print("2x2 Table (Rural vs Urban x Anemia):", table_2x2)
print(f"Pearson chi2={chi2:.4f}, p={p_chi2:.4f}, "
f"dof={dof}, expected_min={expected.min():.2f}")
# Small-cell subgroup where expected_min < 5 -> use Fisher's Exact
sparse_tbl = [[1, 14], [7, 8]]
_, _, _, exp_sparse = stats.chi2_contingency(sparse_tbl, correction=False)
fish_res = stats.fisher_exact(sparse_tbl)
print(f"Sparse expected_min={exp_sparse.min():.2f} -> "
f"Fisher OR={fish_res.statistic:.3f}, p={fish_res.pvalue:.4f}")Expected Output:
For the full survey table, expected_min = 51.55 >= 5, so Pearson’s \(\chi^2\) (1.3081, p = 0.2527) is completely valid. In contrast, the sparse subgroup ([[1, 14], [7, 8]]) has expected_min = 4.00 < 5, triggering Fisher’s exact test (p = 0.0352).
20.3.7 Epidemiological Effect Measures: Risk Ratio & Woolf Odds Ratio 95% CI
In a standard \(2 \times 2\) epidemiological table (a = exposed cases, b = exposed non-cases, c = unexposed cases, d = unexposed non-cases):
\[\text{RR} = \frac{a / (a + b)}{c / (c + d)}, \qquad \text{OR} = \frac{ad}{bc}, \qquad \text{SE}(\ln \text{OR}) = \sqrt{\frac{1}{a} + \frac{1}{b} + \frac{1}{c} + \frac{1}{d}}\]
risk_rural = a_cell / (a_cell + b_cell)
risk_urban = c_cell / (c_cell + d_cell)
risk_ratio = risk_rural / risk_urban
odds_ratio = (a_cell * d_cell) / (b_cell * c_cell)
se_log_or = math.sqrt(1/a_cell + 1/b_cell + 1/c_cell + 1/d_cell)
ci_low = math.exp(math.log(odds_ratio) - 1.96 * se_log_or)
ci_high = math.exp(math.log(odds_ratio) + 1.96 * se_log_or)
print(f"Risk (Rural): {risk_rural:.4f} | Risk (Urban): {risk_urban:.4f}")
print(f"Risk Ratio (RR): {risk_ratio:.3f}")
print(f"Crude OR: {odds_ratio:.3f} "
f"(95% Woolf CI: [{ci_low:.3f}, {ci_high:.3f}])")Expected Output:
Because the 95% CI [0.843, 1.916] spans the null value 1.0, the crude rural–urban anemia odds ratio is not statistically significant—completely consistent with Pearson’s \(\chi^2\) (p = 0.2527). Notice also that because anemia is common (~36%), the Odds Ratio (1.271) is noticeably further from 1.0 than the Risk Ratio (1.170).
20.3.8 Multiple Hypothesis Testing & Benjamini-Hochberg FDR
raw_p = [0.004, 0.032, 0.048, 0.210, 0.650]
bh_q = stats.false_discovery_control(raw_p, method="bh")
bonf_p = [min(1.0, p * len(raw_p)) for p in raw_p]
audit = pd.DataFrame({
"raw_p": raw_p,
"bh_q": np.round(bh_q, 4),
"bonferroni_p": np.round(bonf_p, 4),
"survives_fdr_05": bh_q < 0.05,
})
print(audit.to_string(index=False))Expected Output:
Observe how three raw \(p\)-values sat below 0.05, yet after Benjamini-Hochberg FDR correction only 0.004 (q = 0.0200) remains significant! Notice also why both 0.032 and 0.048 yield bh_q = 0.0800: the BH step-up procedure enforces monotonicity from right to left (\(\min(0.032 \times 5 / 2, 0.048 \times 5 / 3) = \min(0.08, 0.08) = 0.08\)).
20.3.9 Multivariable Logistic Regression (statsmodels.api.Logit)
mch["is_urban"] = (mch["residence"] == "Urban").astype(int)
model_df = mch[["maternal_anemia", "anc_visits", "is_urban"]].dropna()
X = sm.add_constant(model_df[["anc_visits", "is_urban"]])
y = model_df["maternal_anemia"]
logit_fit = sm.Logit(y, X).fit(disp=False)
conf = logit_fit.conf_int()
aor_table = pd.DataFrame({
"aor": np.exp(logit_fit.params),
"ci_low": np.exp(conf[0]),
"ci_high": np.exp(conf[1]),
"p_value": logit_fit.pvalues,
}).round(4)
print(f"Complete cases fitted: n={len(model_df)}")
print(aor_table)Expected Output:
Read this publication-ready Table 3 output carefully:
anc_visits(aOR = 0.9006, 95% CI0.8158–0.9941,p = 0.0378): Controlling for urban/rural residence, each additional antenatal care visit is associated with a statistically significant 9.9% reduction in the odds of maternal anemia.is_urban(aOR = 0.9078, 95% CI0.5877–1.4024,p = 0.6630): Once ANC visit frequency is controlled for, urban vs. rural residence has no independent statistical effect—classic confounding uncovered by multivariable modeling!- Why
sm.add_constant(X)andnp.exp(...)? Unlike Scikit-Learn, Statsmodels does not add an intercept automatically, and its raw.paramslive on the log-odds (\(\beta\)) scale until exponentiated into Adjusted Odds Ratios (\(e^{\beta}\)).
20.3.10 Break It On Purpose! (Biostatistics Error Anatomy)
Python’s Traceback:
Diagnosis: Contingency tables represent event counts (\(\ge 0\)). Negative entries—often caused by passing differences, \(z\)-scores, or uncleaned sentinel codes like -99—immediately violate the \(\chi^2\) definition.
Fix: Assert (table >= 0).all() and construct tables directly from categorical labels with pd.crosstab(...).
Python’s Traceback:
Diagnosis: Woolf’s log-OR variance formula \(\frac{1}{a} + \frac{1}{b} + \frac{1}{c} + \frac{1}{d}\) blows up whenever any cell in the \(2 \times 2\) table equals 0.
Fix: Apply the Haldane-Anscombe correction (add 0.5 to all four cells) for approximate OR/CI reporting, and use scipy.stats.fisher_exact for the exact \(p\)-value.
sm.Logit)
Python’s Traceback:
Diagnosis: After converting sentinel 999 to np.nan, mch["anc_visits"] has 8 missing values. Statsmodels refuses to silently drop rows by default and raises MissingDataError (analogous to NumPy’s ValueError for non-finite inputs).
Fix: Explicitly slice the exact outcome and predictor columns and call .dropna() before passing y and sm.add_constant(X) into sm.Logit(...) (or pass missing="drop" deliberately and log the complete-case sample size \(n\)).
20.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: Your epidemiology group needs a single reproducible function build_mch_evidence_summary(raw_df) that takes the raw mch_survey_sample DataFrame (without mutating it) and produces three core manuscript artifacts in one call: (1) overall anemia prevalence with 95% Wilson CI, (2) a dual parametric/nonparametric test of maternal hemoglobin across all 5 wealth quintiles (One-Way ANOVA \(F\) + Kruskal-Wallis \(H\)), and (3) a clean multivariable logistic regression aOR table adjusting for anc_visits and is_urban.
20.4.1 Blueprint (Completed on Paper Before Coding)
Data Hygiene Layer (Zero Mutation):
- Copy
raw_dffirst (df = raw_df.copy()). - Normalize
residencevia.astype(str).str.strip().str.title(), convertanc_visits == 999tonp.nan, and derive integeris_urban = (df["residence"] == "Urban").astype(int).
- Copy
Three Statistical Modules:
- Prevalence: Pass
(int(df["maternal_anemia"].sum()), len(df))towilson_ci_demo(...). - Wealth Gradient: Extract non-null
maternal_hb_g_dlarrays perwealth_quintileviagroupby, then run bothstats.f_oneway(*groups)andstats.kruskal(*groups). - Multivariable Model: Complete-case
.dropna()on["maternal_anemia", "anc_visits", "is_urban"], add intercept viasm.add_constant, fitsm.Logit(y, X).fit(disp=False), and exponentiate bothparamsandconf_int().
- Prevalence: Pass
Verification Plan:
- Overall anemia must match
179/500 = 0.358with 95% CI(0.3172, 0.401). - Both ANOVA and Kruskal-Wallis should confirm a highly significant socioeconomic gradient across the 5 quintiles (\(p < 0.0001\)).
- Complete-case sample size for Logit must be \(n = 492\) (500 minus 8 sentinel
999rows), withanc_visitsprotective (aOR = 0.9006, p = 0.0378).
- Overall anemia must match
20.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
def build_mch_evidence_summary(raw_df):
"""Run end-to-end prevalence, wealth-gradient & adjusted logit."""
df = raw_df.copy()
df["residence"] = df["residence"].astype(str).str.strip().str.title()
df["anc_visits"] = df["anc_visits"].replace(999, np.nan)
df["is_urban"] = (df["residence"] == "Urban").astype(int)
# 1. Overall anemia prevalence + 95% Wilson CI
prev = wilson_ci_demo(int(df["maternal_anemia"].sum()), len(df))
# 2. Hemoglobin across 5 wealth quintiles (ANOVA + Kruskal-Wallis)
groups = [
grp["maternal_hb_g_dl"].dropna().to_numpy()
for _, grp in df.groupby("wealth_quintile")
]
f_stat, anova_p = stats.f_oneway(*groups)
h_stat, kruskal_p = stats.kruskal(*groups)
# 3. Multivariable logistic regression (aOR table)
m_df = df[["maternal_anemia", "anc_visits", "is_urban"]].dropna()
X_m = sm.add_constant(m_df[["anc_visits", "is_urban"]])
fit = sm.Logit(m_df["maternal_anemia"], X_m).fit(disp=False)
ci = fit.conf_int()
aor_df = pd.DataFrame({
"aor": np.exp(fit.params),
"ci_low": np.exp(ci[0]),
"ci_high": np.exp(ci[1]),
"p_value": fit.pvalues,
}).round(4)
wealth_tuple = (
round(float(f_stat), 4), round(float(anova_p), 4),
round(float(h_stat), 4), round(float(kruskal_p), 4),
)
return prev, wealth_tuple, aor_df
prev_ci, wealth_stats, aor_res = build_mch_evidence_summary(mch_raw)
print("Anemia Wilson CI:", prev_ci)
print("Wealth Hb (F, p, H, p):", wealth_stats)
print(aor_res)Output:
20.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_04_data_science_math/ch20_biostatistics_inference/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and implement all six reusable biostatistical functions below. Pass each function name (without parentheses!) to tutor.check(...).
- To-Do 20.1 (
wilson_ci(successes, n, z=1.96)): Compute the sample proportion and Wilson score confidence interval returning(prop, low, high)as floats rounded to 4 decimals (clamped to[0.0, 1.0]). - To-Do 20.2 (
compare_two_groups_welch(group_a, group_b)): Run Welch’s two-sample \(t\)-test (stats.ttest_ind(..., equal_var=False)) and pooled Cohen’s \(d = (\bar{x}_a - \bar{x}_b) / s_p\) (ddof=1), returning{"t_stat": round4, "p_value": round4, "cohens_d": round4, "significant_05": bool}. - To-Do 20.3 (
nonparametric_mannwhitney(group_a, group_b)): Runstats.mannwhitneyu(group_a, group_b, alternative="two-sided")and return{"u_stat": round2, "p_value": round4, "significant_05": bool}. - To-Do 20.4 (
chi2_contingency_audit(table_2x2)): Run Pearson’s chi-square viastats.chi2_contingency(table_2x2, correction=False)and return{"chi2": round4, "p_value": round4, "dof": int, "expected_min": round2}. - To-Do 20.5 (
odds_ratio_with_ci(a, b, c, d)): Compute crude \(\text{OR} = (ad)/(bc)\) and Woolf 95% CI usingz = 1.96(se = sqrt(1/a + 1/b + 1/c + 1/d)), returning{"or": round3, "ci_low": round3, "ci_high": round3}. - To-Do 20.6 (
benjamini_hochberg_adjust(p_values)): Return a Pythonlistof Benjamini-Hochberg FDR-adjusted \(q\)-values rounded to 4 decimals matchingscipy.stats.false_discovery_control(p_values, method="bh")in the original input order.
20.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Work through all 10 biostatistics and Python reasoning questions on paper before checking Stage 8:
- CLT Standard Error Scaling: A biomarker has population standard deviation \(\sigma = 2.0\). If you quadruple your sample size from \(n = 25\) to \(n = 100\), how does the standard error of the sample mean \(\text{SE} = \sigma / \sqrt{n}\) change?
- Wald vs. Wilson at Zero Events: In a pilot safety study of \(n = 20\) patients, \(0\) adverse events occur (\(\hat{p} = 0\)). Why is the naive Wald 95% CI misleading, and what key property makes the Wilson interval scientifically superior?
- Welch vs. Student’s \(t\)-Test: What happens if you call
stats.ttest_ind(group_a, group_b)without specifyingequal_var? Which parameter value gives Welch’s \(t\)-test? - Effect Size vs. \(p\)-Value: Study A (\(n = 20{,}000\)) finds a hemoglobin difference of
0.05 g/dLwith \(p = 0.002\) and Cohen’s \(d = 0.04\). Study B (\(n = 60\)) finds a difference of1.10 g/dLwith \(p = 0.008\) and Cohen’s \(d = 0.78\). Which finding has greater clinical magnitude, and why can \(p\)-value alone mislead? - Expected Cell Audit: A \(2 \times 2\) clinical table yields
expected_min = 3.2. Should you report Pearson’s \(\chi^2\) \(p\)-value or switch to another test—and which SciPy function implements it? - RR vs. OR Hand Calculation: Given
a = 40(exposed cases),b = 60(exposed non-cases),c = 20(unexposed cases), andd = 80(unexposed non-cases), compute the exact Risk Ratio (RR) and Odds Ratio (OR) by hand. Why is \(\text{OR} > \text{RR}\)? - Woolf CI Scale Symmetry: Why is the 95% confidence interval for an Odds Ratio
[0.843, 1.916]asymmetric aroundOR = 1.271, and on what transformed scale IS it symmetric? - Benjamini-Hochberg Step-Up: You test 4 independent hypotheses and obtain sorted raw \(p\)-values
[0.01, 0.04, 0.20, 0.80]. Compute the BH-adjusted \(q\)-value for rank \(i = 1\) (\(p_1 = 0.01\)) and rank \(i = 2\) (\(p_2 = 0.04\)) using \(q_i = p_i \cdot m / i\). - Missing Values in
stats.f_oneway: One clinic group in your DataFrame contains[11.2, np.nan, 12.1]. If you pass that array directly intostats.f_oneway(*groups)without.dropna(), what does SciPy return forf_statandanova_p? - Statsmodels
LogitPitfalls: A colleague runssm.Logit(y, X).fit(disp=False)and reportsfit.params["anc_visits"] = -0.1047as an “Adjusted Odds Ratio of-0.10.” Spot the two critical bugs they may have made (one in interpretation/transformation, and one common omission in buildingX).
20.7 Stage 7 — Chapter 20 Part IV Capstone Coding Exam (100 Points)
Open part_04_data_science_math/ch20_biostatistics_inference/03_coding_exam.ipynb, implement all three functions, and run tutor.check_exam("ch20", exam_submissions) to earn your Part IV Capstone score out of 100 points.
20.7.1 Exam Task 20.1 (30 Points): epidemiological_2x2_report(a, b, c, d)
Given integer \(2 \times 2\) counts (a, b, c, d), return a single dict containing:
"risk_exposed"(\(a / (a + b)\), 4 decimals) and"risk_unexposed"(\(c / (c + d)\), 4 decimals),"risk_ratio"(raw exposed risk / raw unexposed risk, 3 decimals),"odds_ratio"(\((ad)/(bc)\), 3 decimals),"or_ci_low"and"or_ci_high"(Woolf 95% CI with \(z = 1.96\), 3 decimals),"chi2_p_value"(Pearson \(\chi^2\) \(p\)-value usingstats.chi2_contingency([[a, b], [c, d]], correction=False), 4 decimals).
20.7.2 Exam Task 20.2 (35 Points): anova_and_kruskal_by_group(df, value_col, group_col)
Without mutating df:
- Group
dfbygroup_col, extract non-null numeric arrays ofvalue_colper group (droppingNaNs and skipping any empty group). - Run
stats.f_oneway(*groups)andstats.kruskal(*groups). - Return
{"n_groups": int, "f_stat": round4, "anova_p": round4, "h_stat": round4, "kruskal_p": round4}.
20.7.3 Exam Task 20.3 (35 Points): fit_adjusted_anemia_logit(mch_df)
Without mutating mch_df:
- Clean
residencecasing (str.strip().str.title()) and replaceanc_visits == 999withnp.nan. - Create binary
is_urban = (residence == "Urban").astype(int). - Drop rows with missing
maternal_anemia,anc_visits, oris_urban. - Fit
statsmodels.api.Logit(y, sm.add_constant(X)).fit(disp=False). - Return a
pd.DataFrameindexed by["const", "anc_visits", "is_urban"]with columns["aor", "ci_low", "ci_high", "p_value"]rounded to 4 decimals.
20.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
20.8.1 A. Concept & Execution Trace Test Key
- \(\text{SE}\) is cut in half! At \(n = 25\), \(\text{SE} = 2.0 / 5 = 0.40\); at \(n = 100\), \(\text{SE} = 2.0 / 10 = 0.20\). Quadrupling sample size halves the standard error (\(\sqrt{4} = 2\)).
- The Wald CI collapses to
[0.0, 0.0], pretending zero uncertainty despite only 20 observations. Wilson’s score interval always stays inside[0, 1]and gives a realistic non-zero upper bound (0.1611) even at \(0/20\). - By default
equal_var=True, which runs Student’s equal-variance \(t\)-test. Passequal_var=Falsefor Welch’s \(t\)-test. - Study B (
d = 0.78,1.10 g/dL) has far greater clinical magnitude! Huge sample sizes can make tiny, clinically trivial differences (0.05 g/dL) yield tiny \(p\)-values. - Switch to Fisher’s exact test using
scipy.stats.fisher_exact(table_2x2), because Pearson’s \(\chi^2\) asymptotic approximation degrades whenexpected_min < 5. - \(\text{Risk}_{\text{exp}} = 40/100 = 0.40\), \(\text{Risk}_{\text{unexp}} = 20/100 = 0.20 \Rightarrow \mathbf{RR = 2.00}\). \(\mathbf{OR} = (40 \times 80)/(60 \times 20) = 3200 / 1200 \approx \mathbf{2.667}\). Because baseline risk (
20%) is common, odds diverge from probabilities and \(\text{OR}\) exaggerates \(\text{RR}\). - The CI is symmetric on the natural-log scale (\(\ln(\text{OR}) \pm 1.96 \cdot \text{SE}\)). Exponentiating a symmetric interval produces an asymmetric ratio interval on \((0, \infty)\).
- For \(m = 4\): \(q_1 = 0.01 \times 4 / 1 = \mathbf{0.04}\); \(q_2 = 0.04 \times 4 / 2 = \mathbf{0.08}\) (since \(q_3 = 0.20 \times 4 / 3 = 0.2667\) and \(q_4 = 0.80\), monotonicity is already satisfied).
- SciPy returns
nanfor bothf_statandanova_p(silent NaN propagation unless you drop NaNs first!). - First,
fit.paramsare log-odds (\(\beta\)), never odds ratios—you must exponentiate vianp.exp(-0.1047) = 0.9006(an Odds Ratio can never be negative!). Second, callers frequently forgetsm.add_constant(X), forcing the regression through zero intercept.
20.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
import math
import numpy as np
from scipy import stats
# To-Do 20.1
def wilson_ci(successes, n, z=1.96):
"""Return (prop, low, high) 95% Wilson CI rounded to 4 decimals."""
p = successes / n
denom = 1.0 + (z ** 2) / n
center = (p + (z ** 2) / (2.0 * n)) / denom
margin = (
z * math.sqrt((p * (1.0 - p) + (z ** 2) / (4.0 * n)) / n)
) / denom
low = max(0.0, center - margin)
high = min(1.0, center + margin)
return (round(p, 4), round(low, 4), round(high, 4))
# To-Do 20.2
def compare_two_groups_welch(group_a, group_b):
"""Run Welch's t-test and compute pooled Cohen's d."""
a = np.asarray(group_a, dtype=float)
b = np.asarray(group_b, dtype=float)
res = stats.ttest_ind(a, b, equal_var=False)
n_a, n_b = len(a), len(b)
var_a = float(np.var(a, ddof=1))
var_b = float(np.var(b, ddof=1))
pooled_sd = math.sqrt(
((n_a - 1) * var_a + (n_b - 1) * var_b) / (n_a + n_b - 2)
)
cohens_d = (float(np.mean(a)) - float(np.mean(b))) / pooled_sd
return {
"t_stat": round(float(res.statistic), 4),
"p_value": round(float(res.pvalue), 4),
"cohens_d": round(float(cohens_d), 4),
"significant_05": bool(res.pvalue < 0.05),
}
# To-Do 20.3
def nonparametric_mannwhitney(group_a, group_b):
"""Run a two-sided Mann-Whitney U test on two independent groups."""
res = stats.mannwhitneyu(group_a, group_b, alternative="two-sided")
return {
"u_stat": round(float(res.statistic), 2),
"p_value": round(float(res.pvalue), 4),
"significant_05": bool(res.pvalue < 0.05),
}
# To-Do 20.4
def chi2_contingency_audit(table_2x2):
"""Run Pearson chi-square (no Yates) and audit min expected count."""
chi2, p_val, dof, expected = stats.chi2_contingency(
table_2x2, correction=False
)
return {
"chi2": round(float(chi2), 4),
"p_value": round(float(p_val), 4),
"dof": int(dof),
"expected_min": round(float(np.min(expected)), 2),
}
# To-Do 20.5
def odds_ratio_with_ci(a, b, c, d):
"""Compute crude Odds Ratio and Woolf 95% CI rounded to 3 decimals."""
or_val = (a * d) / (b * c)
se = math.sqrt(1 / a + 1 / b + 1 / c + 1 / d)
log_or = math.log(or_val)
return {
"or": round(or_val, 3),
"ci_low": round(math.exp(log_or - 1.96 * se), 3),
"ci_high": round(math.exp(log_or + 1.96 * se), 3),
}
# To-Do 20.6
def benjamini_hochberg_adjust(p_values):
"""Return BH FDR-adjusted q-values as a 4-decimal Python list."""
q_vals = stats.false_discovery_control(p_values, method="bh")
return [round(float(q), 4) for q in q_vals]20.8.3 C. Annotated Solutions — Part IV Capstone Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
import pandas as pd
import statsmodels.api as sm
# Exam Task 20.1 (30 pts)
def epidemiological_2x2_report(a, b, c, d):
"""Return full 2x2 risk, RR, OR, Woolf 95% CI, and chi2 p-value."""
risk_exp = a / (a + b)
risk_unexp = c / (c + d)
rr = risk_exp / risk_unexp
or_val = (a * d) / (b * c)
se = math.sqrt(1 / a + 1 / b + 1 / c + 1 / d)
log_or = math.log(or_val)
_, p_val, _, _ = stats.chi2_contingency(
[[a, b], [c, d]], correction=False
)
return {
"risk_exposed": round(risk_exp, 4),
"risk_unexposed": round(risk_unexp, 4),
"risk_ratio": round(rr, 3),
"odds_ratio": round(or_val, 3),
"or_ci_low": round(math.exp(log_or - 1.96 * se), 3),
"or_ci_high": round(math.exp(log_or + 1.96 * se), 3),
"chi2_p_value": round(float(p_val), 4),
}
# Exam Task 20.2 (35 pts)
def anova_and_kruskal_by_group(df, value_col, group_col):
"""Run NaN-safe One-Way ANOVA and Kruskal-Wallis across groups."""
groups = [
grp[value_col].dropna().to_numpy()
for _, grp in df.groupby(group_col)
if grp[value_col].notna().any()
]
f_stat, anova_p = stats.f_oneway(*groups)
h_stat, kruskal_p = stats.kruskal(*groups)
return {
"n_groups": int(len(groups)),
"f_stat": round(float(f_stat), 4),
"anova_p": round(float(anova_p), 4),
"h_stat": round(float(h_stat), 4),
"kruskal_p": round(float(kruskal_p), 4),
}
# Exam Task 20.3 (35 pts)
def fit_adjusted_anemia_logit(mch_df):
"""Clean MCH survey covariates and fit multivariable Logit aORs."""
work = mch_df.copy()
work["residence"] = (
work["residence"].astype(str).str.strip().str.title()
)
work["anc_visits"] = work["anc_visits"].replace(999, np.nan)
work["is_urban"] = (work["residence"] == "Urban").astype(int)
model_df = work[
["maternal_anemia", "anc_visits", "is_urban"]
].dropna()
X = sm.add_constant(model_df[["anc_visits", "is_urban"]])
y = model_df["maternal_anemia"]
fit = sm.Logit(y, X).fit(disp=False)
conf = fit.conf_int()
return pd.DataFrame(
{
"aor": np.exp(fit.params),
"ci_low": np.exp(conf[0]),
"ci_high": np.exp(conf[1]),
"p_value": fit.pvalues,
},
index=["const", "anc_visits", "is_urban"],
).round(4)20.8.4 D. Debugging Post-Mortem: Top 3 Chapter 20 Failure Modes
- Forgetting
equal_var=Falseorcorrection=Falsein SciPy: Leaving SciPy’s defaults silently switches Welch’s \(t\)-test to Student’s \(t\)-test and Pearson’s \(\chi^2\) to Yates’ corrected \(\chi^2\), shifting test statistics and \(p\)-values away from standard epidemiological reporting conventions. - Leaving Survey Sentinels (
999) orNaNs Uncleaned Before Modeling: Sentinel999inanc_visitsinflates predictor variance by orders of magnitude and crushes the logistic slope toward zero, while uncleanedNaNs either propagatenaninf_oneway/kruskalor crashsm.LogitwithMissingDataError. - Reporting Raw Log-Odds Instead of Exponentiated aORs (or Omitting
sm.add_constant): Statsmodels estimates linear log-odds \(\beta\) without an automatic intercept. Always wrap predictors withsm.add_constant(X)and applynp.exp(...)to bothfit.paramsandfit.conf_int()before building your manuscript table.
Congratulations! By completing Chapter 20, you have mastered the entire Scientific Data Stack & Mathematical Foundations (Part IV): vectorized NumPy computation, Pandas data engineering, publication-grade Matplotlib/Seaborn visualization, linear algebra & calculus intuition, and formal biostatistical inference with SciPy and Statsmodels.
In Part V (Classical Machine Learning with Scikit-Learn), you will pivot from explanatory statistical inference (estimating \(p\)-values, confidence intervals, and Adjusted Odds Ratios) to predictive out-of-sample generalization—building leakage-free Pipeline and ColumnTransformer workflows, cross-validating tree ensembles and regularized classifiers, evaluating ROC-AUC and calibration curves, and explaining predictions with SHAP values.


