A117 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 p from N trials has a standard deviation of p(1−p)/N.
  • The curve shows that spread against N for the p you choose. The readout gives the trials needed for a 1% relative error.
  • Small probabilities need many trials: the relative error is about 1pN.

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?

Name it

Simulating with NumPy

  • rng = np.random.default_rng(seed) makes a reproducible generator.
  • rng.random(n) < p gives n independent events, each true with probability p.
  • rng.integers(0, k, shape) draws whole numbers below k.
  • 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 N trials has standard deviation p(1−p)/N, so a probability near 0.5 is known to about 0.1% after a million trials, and one near 10−4 only to 10%. Set N from the precision you need before running.

Worked example

Estimate the birthday probability for 23 people by simulation.

Show the solution
  1. Draw the groups. Draw 100 000 groups of 23 birthdays with rng.integers(0, 365, (100000, 23)).
  2. Look for a match. Sort each row and check for any two equal neighbors.
  3. Compare with the exact value. One run gives 0.507, matching the exact 0.5073 to within the expected spread of 0.25/105=0.0016

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
  1. Draw the faults. For a million boards, draw fault = rng.random(N) < 0.01.
  2. Draw the flags. Flag faulty boards with probability 0.95 and good ones with probability 0.05.
  3. Count the faulty among the flagged. The fraction comes out 0.162.
  4. Compare with Bayes's rule. 0.00950.0095+0.0495=0.161

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 3r2−2r3.

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?

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.