Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Introduction to Data Science Β· Lesson 8 of 9

Module Seven

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
8
Module 7: Statistics for Data Science

Module 7: Statistics for Data Science

Introduction to Data Science β€” Week 7

Probability, sampling, hypothesis testing, and confidence intervals

Duration: 1 Week (approx. 5–7 hours of study + 2 hours lab)

Format: Lectures, readings, discussion, and a hands-on lab

Prerequisites: Modules 1–6

Learning Materials: This module, a computer with Python (Google Colab works), and a notebook

Module Overview

Statistics is the mathematical foundation of data science. It gives us tools for drawing reliable conclusions from limited data. This module covers the core concepts every data scientist needs: measures of spread, probability fundamentals, sampling distributions, hypothesis testing, and confidence intervals. The goal is not to memorize formulas but to develop sound statistical intuition β€” the ability to tell when a result is meaningful and when it might be noise.

Learning Objectives

By the end of this module, students will be able to:

  • Compute and interpret measures of spread: range, variance, standard deviation, IQR.
  • Explain basic probability concepts: events, independence, and conditional probability.
  • Describe what a sampling distribution is and why it matters.
  • Formulate null and alternative hypotheses for a research question.
  • Interpret p-values and statistical significance correctly.
  • Construct and interpret confidence intervals.
  • Run a t-test in Python and interpret the result.

Lesson 1: Measures of Spread Revisited

In Module 4 we introduced measures of spread. Here we'll go deeper β€” because in data science, understanding variability is often more important than understanding the average.

Measure Formula / Definition Interpretation
Range Max βˆ’ Min Quick estimate; very sensitive to outliers
Variance (sΒ²) Average of squared deviations from the mean Average squared spread; used in many statistical formulas
Standard deviation (s) √variance Average distance from the mean; same units as the data
IQR Q3 βˆ’ Q1 Spread of the middle 50% of the data; robust to outliers
sΒ² = Ξ£(xα΅’ βˆ’ xΜ„)Β² / (n βˆ’ 1)

Why Divide by (n βˆ’ 1)?

When you compute variance from a sample, dividing by n βˆ’ 1 (instead of n) gives an unbiased estimate of the population variance. This is called Bessel's correction. In practice, pandas and NumPy handle this automatically.

🎯 Think of it like a dartboard: Standard deviation is like the average distance of your darts from the bullseye. Variance is that distance squared β€” less intuitive, but mathematically convenient for many formulas.
πŸ’‘ Key Idea: Always report a measure of spread alongside a measure of center. "Average income is $50,000" means very different things if the standard deviation is $5,000 versus $50,000.

Lesson 2: Probability Fundamentals

Probability is a number between 0 and 1 that measures how likely an event is. 0 means impossible; 1 means certain.

Key Concepts

  • Event: An outcome or set of outcomes (e.g., "rolling a 6").
  • Independent events: The occurrence of one doesn't affect the other (e.g., two coin flips).
  • Conditional probability: The probability of A given that B has occurred, written P(A|B).

Basic Rules

P(A and B) = P(A) Γ— P(B)    (if A and B are independent)
P(A or B) = P(A) + P(B) βˆ’ P(A and B)
P(A|B) = P(A and B) / P(B)

Example: Medical Testing

A disease affects 1% of people. A test is 99% accurate for people with the disease, and 95% accurate for people without it. If you test positive, what's the probability you actually have the disease?

This is Bayes' theorem territory. It's not 99% β€” it's closer to 17%, because the disease is so rare that false positives dominate. This is a classic counterintuitive result that matters deeply in data science.

⚠️ Base Rate Fallacy: Ignoring how common something is (the base rate) leads to wildly wrong conclusions. This is why good data scientists always ask: "What's the base rate here?"
πŸ’‘ Key Idea: Probability is the language of uncertainty. Every model, prediction, and statistical test is built on it.

Lesson 3: Sampling Distributions and the Central Limit Theorem

A sampling distribution is the distribution of a statistic (like the mean) across many samples from the same population. It's a foundational concept in statistics β€” but often poorly understood.

What It Means

Suppose you take many samples of size 30 from a population and compute the mean of each. The distribution of those sample means is the sampling distribution of the mean.

Central Limit Theorem (CLT)

As the sample size increases, the sampling distribution of the mean approaches a normal distribution β€” regardless of the shape of the population distribution. This is one of the most important theorems in statistics.

🎲 Think of it like this: If you roll one die, the outcomes are uniform (1–6 equally likely). But if you roll 30 dice and average them, the distribution of averages will be bell-shaped. That's the CLT in action β€” the more dice, the more bell-shaped.
πŸ’‘ Key Idea: The CLT is why we can use normal-distribution-based methods (like t-tests) even when the underlying data isn't normal β€” as long as the sample is big enough. A common rule of thumb is n β‰₯ 30.
⚠️ Watch Out! The CLT applies to the distribution of sample means, not to the raw data. A large sample doesn't make the underlying data bell-shaped β€” it just makes the sample mean more predictable.

Lesson 4: Hypothesis Testing

Hypothesis testing is a formal procedure for deciding whether observed data is consistent with a claim, or whether it provides evidence against it.

The Framework

  1. Null hypothesis (Hβ‚€): The "default" assumption β€” no effect, no difference.
  2. Alternative hypothesis (H₁): The claim you want to test β€” a difference exists.
  3. Choose a significance level (Ξ±): Usually 0.05. This is your threshold for "surprising enough."
  4. Compute a test statistic: A number that summarizes how far the data is from the null hypothesis.
  5. Compute the p-value: The probability of observing data this extreme if Hβ‚€ is true.
  6. Decide: If p-value < Ξ±, reject Hβ‚€. Otherwise, fail to reject Hβ‚€.
βš–οΈ Think of it like a trial: The null hypothesis is "innocent until proven guilty." The alternative is "guilty." The data is the evidence. The p-value is how surprising the evidence would be if the defendant were innocent. A low p-value means the evidence is unlikely under innocence β€” which is grounds to reject innocence.

The p-value β€” What It Is and Isn't

βœ… The p-value IS ❌ The p-value is NOT
The probability of data this extreme if Hβ‚€ is true The probability that Hβ‚€ is true
A measure of evidence against Hβ‚€ A measure of effect size or importance
A number that depends on sample size A guarantee that the result will replicate
⚠️ Common Misconception: A p-value of 0.04 does not mean "there is a 4% chance the null is true." It means: if the null were true, we would see data this extreme only 4% of the time. That's a subtle but crucial difference.
πŸ’‘ Key Idea: Hypothesis testing tells you whether an effect is likely to be real β€” but not whether it's large enough to matter. Always report effect size alongside p-values.

Lesson 5: Confidence Intervals

A confidence interval is a range of plausible values for a population parameter, based on a sample. It's often more informative than a single p-value.

Interpretation

A 95% confidence interval means: if we repeated the sampling process many times, about 95% of the intervals we construct would contain the true value. It does not mean "there's a 95% chance the true value is in this interval" β€” a common misinterpretation.

CI β‰ˆ xΜ„ Β± (t* Γ— s / √n)

Where t* depends on the confidence level and sample size (for large samples, roughly 1.96 for 95%).

🎯 Think of it like fishing: A confidence interval is a net. You cast it out, and you hope it catches the true value. A wider net (higher confidence level) catches more β€” but is less precise. A narrower net (smaller sample) is precise but might miss.
⚠️ Watch Out! A 95% confidence interval isn't "95% likely to contain the true value." The true value is fixed (we just don't know it). The 95% refers to the procedure, not any single interval.

Lesson 6: Comparing Two Groups β€” t-tests

The t-test is one of the most common statistical tests. It compares the means of two groups and asks: is the difference likely real, or could it be due to chance?

Types of t-tests

  • Independent samples t-test: Comparing means from two unrelated groups (e.g., treatment vs. control).
  • Paired t-test: Comparing two measurements on the same subjects (e.g., before vs. after).
  • One-sample t-test: Comparing a sample mean to a known value.

Running a t-test in Python

from scipy import stats
import pandas as pd

# Load cleaned data
data = pd.read_csv("experiment.csv")

# Split into two groups
group_a = data[data["group"] == "A"]["score"]
group_b = data[data["group"] == "B"]["score"]

# Run an independent samples t-test
t_stat, p_value = stats.ttest_ind(group_a, group_b)

print(f"t-statistic: {t_stat:.3f}")
print(f"p-value: {p_value:.4f}")
t-statistic: 2.847 p-value: 0.0049

Interpreting the Result

A t-statistic of 2.847 means the difference between group means is 2.847 standard errors. A p-value of 0.0049 means: if there were no real difference between groups, we'd see a difference this large only about 0.5% of the time. At Ξ± = 0.05, we'd reject the null hypothesis.

πŸ€ Think of it like a referee: The t-test is like a referee deciding whether two basketball players really have different shooting percentages, or whether the difference is just luck. A low p-value says: "This difference is too big to be luck." A high p-value says: "Could easily be chance β€” let's not call a foul."
πŸ’‘ Key Idea: t-tests are powerful but limited. They assume roughly normal distributions and independence. For complex data (multiple groups, repeated measures, non-normal data), other tests (ANOVA, Mann-Whitney U, etc.) may be more appropriate.

Lesson 7: Statistical Pitfalls to Avoid

Even experienced analysts make these mistakes. Learn to spot them:

Pitfall What It Looks Like How to Avoid It
P-hacking Testing many hypotheses and reporting only the significant ones Pre-register hypotheses; adjust for multiple comparisons
Garden of forking paths Making many small data-cleaning or analysis choices that all push toward a result Document all decisions; use pre-registration
Simpson's paradox A trend reverses when data is grouped differently Always examine subgroups before concluding
Correlation β‰  causation Claiming X causes Y just because they're correlated Use experimental design or causal inference methods
Overfitting to noise Finding patterns in random fluctuation Use train/test splits; validate on new data
⚠️ The Reproducibility Crisis: In several fields, many published findings have failed to replicate. A key cause: p-hacking and publication bias. As a data scientist, you're responsible for doing analysis honestly, not for finding a "significant" result.
πŸ’‘ Key Idea: Good statistics is not about finding results β€” it's about being honest about what the data actually supports. A null result is still a result.

Hands-On Lab: Comparing Two Groups

Goal: Practice hypothesis testing by comparing two groups in a real dataset.

Instructions

  1. Choose a dataset with at least one categorical variable (two groups) and one numeric variable (e.g., Titanic: survival by sex; tips: bill by smoker; iris: petal length by species).
  2. Open it in Jupyter Notebook or Google Colab.
  3. Complete the following:
    • Compute the mean, median, standard deviation, and IQR for each group.
    • Create a box plot comparing the two groups.
    • State a null and alternative hypothesis.
    • Run an independent samples t-test using scipy.stats.ttest_ind.
    • Compute a 95% confidence interval for the difference in means (using scipy.stats or a manual calculation).
    • Interpret the result in plain language: what does the p-value mean? What's the practical significance?
    • Check assumptions: are the sample sizes reasonable? Are the distributions roughly normal?
  4. Write a 1–2 page report containing your findings, interpretation, and any caveats.

Submission

Your Jupyter Notebook (with code and charts) + a 1–2 page report.

Assessment for Module 7

  • Quiz (10 questions): Covers measures of spread, probability, sampling, hypothesis testing, confidence intervals, and t-tests.
  • Lab: Graded on correct use of statistical tests and clarity of interpretation.
  • Participation: Contribution to the group activity and discussion prompts.

Key Terms

  • Variance β€” average squared deviation from the mean.
  • Standard deviation β€” square root of variance.
  • Bessel's correction β€” dividing by n βˆ’ 1 for an unbiased sample variance.
  • Conditional probability β€” probability of A given B.
  • Base rate β€” how common something is in the population.
  • Sampling distribution β€” the distribution of a statistic across many samples.
  • Central Limit Theorem β€” sample means approach a normal distribution as n grows.
  • Null hypothesis β€” the default "no effect" assumption.
  • Alternative hypothesis β€” the claim being tested.
  • p-value β€” probability of data this extreme under the null.
  • Confidence interval β€” a range of plausible values for a parameter.
  • t-test β€” a statistical test comparing means of two groups.
  • p-hacking β€” testing many hypotheses and reporting only significant ones.

Discussion Prompts

  1. Why is it important to report a measure of spread alongside a mean?
  2. Can you think of a real-world example of the base rate fallacy?
  3. What does a p-value of 0.04 actually mean? What does it not mean?
  4. Why is a confidence interval often more informative than a p-value?
  5. What is Simpson's paradox, and can you think of a possible example?
  6. If you run 20 t-tests, roughly how many "significant" results would you expect by chance alone?

Further Reading

  • David Spiegelhalter, The Art of Statistics
  • Andrew Gelman & Jennifer Hill, Data Analysis Using Regression and Multilevel/Hierarchical Models (for depth)
  • Allen Downey, Think Stats (free online)
  • Khan Academy statistics course (free)
  • scipy.stats documentation β€” for t-tests, chi-square, ANOVA, and more
  • β€œThe ASA Statement on p-Values” (American Statistical Association, 2016)
πŸ’‘ Instructor Note: Statistics is where students often get the most anxious. Emphasize intuition over formulas: p-values, confidence intervals, and t-tests are just tools for reasoning about uncertainty. The most important lesson is honesty β€” reporting what the data actually shows, not what you hoped to find. Encourage students to compute statistics in code and interpret the output in plain language.

© 2026 Introduction to Data Science β€” Module 7

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’