The basic workflow
- Define null hypothesis (H0) and alternative (H1)
- Choose a significance level (\alpha) (commonly 0.05)
- Compute a test statistic and p-value
- Decide whether to reject H0
P-value intuition
A p-value is:
- Probability of seeing results at least as extreme as your observation if H0 were true.
Low p-value means:
- Data is unlikely under H0 → evidence against H0
Errors
- Type I error: reject H0 when H0 is true (false positive). Probability ≈ (\alpha).
- Type II error: fail to reject H0 when H0 is false (false negative). Probability = (\beta).
- Power: (1 - \beta)
Example: one-sample t-test
Is the average delivery time different from 30 minutes?
import numpy as np
from scipy import stats
x = np.array([28, 32, 31, 29, 26, 30, 33, 27])
t_stat, p = stats.ttest_1samp(x, popmean=30)
print("t:", t_stat)
print("p:", p)Good practice
- Report effect size + CI, not just p-value.
- Don’t equate “not significant” with “no difference”.
- Predefine your hypothesis; avoid testing many variants blindly.
Visualize it
This flowchart traces the decision path from stating your hypotheses to accepting or rejecting H0 based on the p-value.
flowchart TD A["State H0 and H1"] --> B["Collect data"] B --> C["Compute p-value"] C --> D["Compare to alpha (0.05)"] D -->|"p < alpha"| E["Reject H0 (significant)"] D -->|"p >= alpha"| F["Fail to reject H0 (not significant)"]
Check your understanding
What is a p-value?
A low p-value means the data is unlikely under H0, which is evidence against it.
Show answer
B — The probability of results at least as extreme as observed, if H0 were true — A low p-value means the data is unlikely under H0, which is evidence against it.
What is a Type I error?
A Type I error is a false positive with probability about alpha. A Type II error is a false negative with probability beta.
Show answer
B — Rejecting a true H0 (false positive) — A Type I error is a false positive with probability about alpha. A Type II error is a false negative with probability beta.
What is statistical power?
Power is the probability of detecting a real effect, 1 - beta.
Show answer
C — 1 minus beta — Power is the probability of detecting a real effect, 1 - beta.
In the delivery-time example, stats.ttest_1samp(x, popmean=30) tests which question?
It is a one-sample t-test comparing the sample mean to 30 minutes.
Show answer
B — Is the average delivery time different from 30 minutes — It is a one-sample t-test comparing the sample mean to 30 minutes.
🧪 Try It Yourself
Exercise 1 – One-sample t-test
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Deciding based on alpha
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Type I vs. Type II error
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Next
Continue to t-test (independent and paired) to apply this decision framework to comparing two groups' means.