Python Central HubSign in

Data Analytics with Python / Phase 8: Statistics for Data Analytics

Hypothesis Testing (p-value, alpha, errors)

Learn hypothesis testing basics, interpret p-values, and understand Type I/II errors.

The basic workflow

  1. Define null hypothesis (H0) and alternative (H1)
  2. Choose a significance level (\alpha) (commonly 0.05)
  3. Compute a test statistic and p-value
  4. 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?

Python
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.

diagram Hypothesis testing decision flow mermaid
From H0/H1 through the p-value comparison to a final decision

Check your understanding

quizCheck yourself
  1. What is a p-value?

    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.

  2. What is a Type I error?

    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.

  3. What is statistical power?

    Show answer

    C — 1 minus beta — Power is the probability of detecting a real effect, 1 - beta.

  4. In the delivery-time example, stats.ttest_1samp(x, popmean=30) tests which question?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Deciding based on alpha

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Type I vs. Type II error

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
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.

Was this page helpful?

Let us know how we did