A11 · Lesson 7 · Lab: probability by simulation
Checking probability rules by running the experiment a million times
Lessons 1 to 6 computed probabilities with rules: complements, counting, conditioning, Bayes, independence. Each rule can be checked by brute force: simulate the random experiment many times and count. In this lab you'll do that for the birthday problem, a fault test where Bayes's rule surprises, a voting system's reliability, packet losses and bit errors. When the simulation and the formula agree, you can trust both; when they don't, one of them has a mistake.
- A simulated estimate of a probability from trials has a standard deviation of .
- The curve shows that spread against for the you choose. The readout gives the trials needed for a 1% relative error.
- Small probabilities need many trials: the relative error is about .
Predict
A test catches 95% of faulty boards and false-alarms on 5% of good ones. Faults affect 1% of boards. A board is flagged. What is the chance it's really faulty?
About 16%. Out of 10 000 boards, 100 are faulty and 95 of them are flagged; of the 9900 good ones, 495 are flagged too. So only of flags are real. A simulation of a million boards confirms it. When faults are rare, most alarms are false, however good the test sounds.
Name it
Simulating with NumPy
rng = np.random.default_rng(seed)makes a reproducible generator.rng.random(n) < pgivesnindependent events, each true with probabilityp.rng.integers(0, k, shape)draws whole numbers belowk.- Averaging a boolean array,
mask.mean(), estimates a probability;x[mask].mean()estimates a conditional average, keeping only the trials where the condition held.
How many trials
An estimate from trials has standard deviation , so a probability near 0.5 is known to about 0.1% after a million trials, and one near only to 10%. Set from the precision you need before running.
Worked example
Estimate the birthday probability for 23 people by simulation.
Show the solution
- Draw the groups. Draw 100 000 groups of 23 birthdays with
rng.integers(0, 365, (100000, 23)). - Look for a match. Sort each row and check for any two equal neighbors.
- Compare with the exact value. One run gives 0.507, matching the exact 0.5073 to within the expected spread of
Answer the simulation gives about 0.507, in line with the exact 0.5073.
Worked example
Simulate the fault test: 1% faulty, 95% detection, 5% false alarms. What fraction of flagged boards are faulty?
Show the solution
- Draw the faults. For a million boards, draw
fault = rng.random(N) < 0.01. - Draw the flags. Flag faulty boards with probability 0.95 and good ones with probability 0.05.
- Count the faulty among the flagged. The fraction comes out 0.162.
- Compare with Bayes's rule.
Answer the simulation finds 0.162 of flagged boards faulty, against 0.161 from Bayes's rule.
Check
A simulation estimates a probability near 0.1 from 10000 trials. What is the estimate's standard deviation?
Check
A test detects 0.9 of faults and false-alarms on 0.01 of good boards; faults affect 0.01 of boards. What fraction of flagged boards are faulty?
Check
Simulate 100 000 groups of 23 birthdays with rng = np.random.default_rng(1) and set p_shared to the fraction with at least one shared birthday.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Simulate a million boards: faulty with probability 0.01, flagged with probability 0.95 if faulty and 0.05 if good. Set post to the fraction of flagged boards that are faulty, and bayes to Bayes's formula.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Three channels each work with probability 0.9, independently, and the system works if at least two do. Simulate a million systems and set sim, and set formula to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Each of 500 packets is lost independently with probability 0.001. Simulate 100 000 such batches and set sim to the fraction with at least one loss, and exact to the formula.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
An 8-bit word has each bit flipped with probability 0.01, and a code corrects any single flip. Set exact to the chance a word is decoded correctly (at most one flip), and sim to a simulation over 10⁶ words.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A simulation and a formula disagree by more than the simulation's standard deviation. What should you suspect?
First, check whether the disagreement is really significant: an estimate from trials is off by more than one standard deviation about a third of the time, and by more than three only 0.3% of the time. A gap of several standard deviations means a real mistake. It can be in the formula, such as treating dependent events as independent or forgetting that two events overlap, or in the simulation, such as a wrong probability in the random draw, drawing the same random numbers for things that should be independent, or estimating the wrong conditional (counting all trials instead of only those where the condition held). Running both on a simpler case where the answer is known usually reveals which.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.