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.

Check the same event two ways
  1. Simulate the event
    p^=mean⁡(X>110)\widehat p=\operatorname{mean}(X>110)
  2. Independent model reference
    p=P(X>110)p=P(X>110)
  3. Compare with sampling error

    allow for run-to-run error

Reference illustration. For X binomial with 150 opportunities and attendance probability 0.72, compare the strict overflow event with the exact survival function at 110. Sampling variation remains; agreement is not empirical model validation.

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.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

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.

Back to the Python editor

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 P(X≥10)P(X\ge10) for integer X?

Choose an answer
Need a hint?
Hint 1

Write the allowed integer outcomes explicitly.

Hint 2

Find the largest excluded value.

Your notes