Lesson 7: Probability, Distributions and Simulation
Probability is the language of uncertainty: a number between 0 and 1 that says how likely an event is. A probability of 0 means the event cannot happen; 1 means it certainly will; 0.5 means it is as likely as not. Most of data science rests on this idea, because data is always a sample of something bigger, and probability is how we reason about what the sample can and cannot tell us.
A distribution describes how probabilities are spread across possible values. The normal distribution — the bell curve — is the most famous: values cluster near the middle and thin out symmetrically towards the edges, so most observations fall close to the average. Heights, test scores and measurement errors often look roughly normal. But many things are not normal: incomes are skewed, with a long tail of high earners; the number of goals in a match follows a different shape again. Knowing which distribution fits your data matters, because the methods that follow assume one shape or another.
Simulation: Learning by Pretending
Some probability questions are hard to solve with a formula and easy to answer by simulation: repeat a random process many times on a computer and count what happens. The code below simulates 10,000 coin tosses and counts the heads — you already know the answer should be near 5,000, and watching the simulation land there builds trust in the method. Then try something harder, such as the chance of getting at least 8 heads in 10 tosses, which is awkward by hand and trivial by simulation.
import numpy as np
rng = np.random.default_rng(42)
tosses = rng.integers(0, 2, size=10_000) # 0 = tails, 1 = heads
print("heads:", tosses.sum())
print("share of heads:", tosses.mean())
trials = rng.integers(0, 2, size=(10_000, 10))
print("P(at least 8 heads in 10):", (trials.sum(axis=1) >= 8).mean())
Simulation is also a way to check your understanding. If a formula says the chance is 5% and the simulation says 50%, one of them is wrong — and the simulation, which simply counts what happened, is usually the one to trust while you find the error. Data scientists simulate constantly: to test an idea, to see how noisy a result might be, and to build intuition for how randomness behaves.
Randomness Is Not Evenness
One intuition to correct early: random does not mean evenly spread. Ten coin tosses will often give 7 heads and 3 tails, and a run of four heads in a row is common, not suspicious. People expect randomness to look balanced in the short run, and when it does not they invent patterns — the "hot hand" in sport, or a "lucky" coin. Simulation is the cure for that instinct: run the process, look at the results, and learn what randomness actually looks like.
Simulation as a Laboratory
Simulation is more than a way to answer probability questions; it is a laboratory for testing your intuition. When you are unsure whether a result could have arisen by chance, simulate a world in which nothing is happening and see how often a result that large appears. When you are unsure how a method behaves, simulate data with a known answer and check whether the method recovers it. This habit — building a small world, running it, and comparing — is how data scientists test ideas before trusting them on real data, and it is far cheaper than collecting more data to check a hunch.
Simulation also teaches a humility that formulas can hide. A formula gives one number; a simulation gives a distribution, and the spread of that distribution is the honest statement of what you know. If the simulated results vary widely, then the real result is uncertain, however precise the formula's answer looks. Learning to ask "how much would this vary if I ran it again?" is one of the most useful questions in statistics, and simulation is the easiest way to answer it.
When Simulation Disagrees with Theory
Sometimes a simulation and a formula give different answers, and deciding which to trust is a skill in itself. First, check the simulation: did you simulate the process you meant to, or a slightly different one? A coin-toss simulation that uses the wrong probabilities will confidently give the wrong answer. Second, check the formula: does it assume something your process violates, such as independence between events that are actually linked? If both check out, trust the simulation while you look for the error — it is counting actual outcomes, while the formula is only as good as its assumptions.
A useful example is the birthday problem: in a group of 23 people, the chance that two share a birthday is about one in two, which surprises most people who guess a much smaller number. A short simulation confirms it quickly, and the surprise is the lesson: human intuition about probability is unreliable, and simulation is the tool that corrects it. Whenever a probability result feels wrong, simulate it before arguing about it.
Key Terms
- Probability
- A number from 0 to 1 measuring how likely an event is.
- Distribution
- How probabilities are spread across the possible values of a variable.
- Normal distribution
- The bell-shaped distribution, with values clustering near the middle.
- Simulation
- Repeating a random process many times on a computer and counting the outcomes.
- Random seed
- A starting number that makes a simulation's random choices repeatable.
- Expected value
- The long-run average of a random quantity over many repeats.
Discussion Questions
- A coin lands heads four times in a row. Is the coin biased? How would simulation help you decide?
- Why do people expect random results to look evenly balanced, and what goes wrong when they do?
- If a formula and a simulation disagree, which should you trust first — and why?
Practice Questions
- What does a probability of 0.25 mean?
- What shape is the normal distribution, and where do most values fall?
- In the simulation code, what does
rng.integers(0, 2, size=10_000)produce? - Why is simulation useful for a probability question that is hard to solve by formula?
- A basketball player makes four shots in a row. Does that prove a 'hot hand'?
Show answers
- The event happens about one time in four in the long run. Probability 0 means impossible, 1 means certain.
- It is bell-shaped, with most values clustering near the middle and fewer towards the edges.
- 10,000 random values, each 0 or 1 — modelling 10,000 coin tosses, with 1 as heads.
- Because you can simply repeat the random process many times and count how often the event happens, which estimates the probability directly.
- No — runs of successes happen often by chance alone, and simulation shows they are common in random sequences. You would need a proper test, not just the run itself.