Your first simulationLesson 06 of 76 · 7 min
Check against an exact answer
Use a tractable case to challenge a simulation’s implementation.
When an exact answer is available, it is a valuable reference. Return to the room model: independent attendance with a fixed probability gives an exact binomial distribution for the attendance count.
A cumulative probability counts values at or below a threshold, P(X ≤ k). Its complement is the upper tail, the chance above k. SciPy's survival function sf(k) calculates that strict upper tail directly, matching attendance > seats. Direct tail calculation avoids loss of numerical accuracy when a very small answer is obtained by subtracting almost-equal numbers.
The simulation estimate will usually differ from the exact model probability because it uses finitely many trials. A discrepancy should be judged against Monte Carlo error, not an expectation of identical decimal strings. Agreement supports this implementation for this model, not the assumption that real attendance is independent.
The model probability p is the exact reference; the estimate with a hat uses finitely many complete events. Run-to-run numerical error is the variation in that estimate over fresh simulations; lesson 15 quantifies it.
- Simulate the event
- Independent model reference
- Compare with sampling error
allow for run-to-run error
Should a finite simulation match the exact probability to every displayed decimal?
Write a prediction (optional)
Seeded teaching simulation
Select Run Python below with the code unchanged.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
Published example output
This result comes from the original Python. Your runs appear above. Build environment ↗
Simulated overflow probability: 0.32735 Exact model probability: 0.32861 Difference: -0.00126
Read the first result
Compare Simulated overflow probability with Exact model probability. Difference is simulation minus exact. Both calculate the same strict boundary using the same attendance model.
Try an edit
Replace (attendance > seats).mean() with (attendance >= seats).mean() and stats.binom.sf(seats, invited, probability) with stats.binom.sf(seats - 1, invited, probability). Select Run Python again. Compare Simulated overflow probability and Exact model probability. Including 110 is the same as counting strictly above 109 for the exact calculation.
Scope. This checks a known binomial case. It does not validate every model built with the same random library.
Try a fresh case
Which exact expression matches for integer X?
Need a hint?
Hint 1
Write the allowed integer outcomes explicitly.
Hint 2
Find the largest excluded value.