CASE STUDY 03 · MATHEMATICS

Probability Binomial + Monte Carlo

Probability Simulator

Build an exact binomial model, reproduce it through random experiments and explain why simulated results approach—but rarely equal—the theoretical value.

01 · PROBLEM DEFINITION

Connect a formula to observable experiments

A placement aptitude test contains ten independent true/false questions. If a student guesses randomly, what is the probability of exactly seven correct answers? We can calculate the answer using the binomial distribution and also estimate it by simulating many ten-question attempts.

The two answers serve different purposes. Theory describes the model exactly. Simulation demonstrates long-run frequency, helps test implementation and supports cases where an exact formula may later be unavailable.

Model

Define trials, success probability and independence.

Calculate

Find exact and at-least probabilities.

Experiment

Simulate repeatedly with a reproducible seed.

02 · BINOMIAL MODEL ASSUMPTIONS

Four conditions must hold

  1. A fixed number n of trials occurs in every experiment.
  2. Each trial has two modeled outcomes: success or failure.
  3. The success probability p remains constant.
  4. Trials are independent under the model.

Let X be the number of successes. Then X can take integer values 0 through n. Its expected value is E[X]=np and variance is np(1−p).

Assumption check: Drawing cards without replacement does not keep p constant and trials are dependent. A binomial model would not be exact for that process.
InputAllowed valuesMeaning
nPositive integerTrials per experiment
p0 ≤ p ≤ 1Success chance per trial
k0 ≤ k ≤ nTarget successes
MPositive integerRepeated experiments
03 · EXACT BINOMIAL PROBABILITY

Choose positions, then assign outcomes

P(X=k) = C(n,k) × p^k × (1−p)^(n−k)

C(n,k) counts which k of the n positions are successes. Every particular arrangement has probability p^k(1−p)^(n−k). Multiplication combines independent outcomes; multiplication by the combination count covers all arrangements with exactly k successes.

At least k successes

P(X≥k) = Σ from j=k to n of P(X=j)

“Exactly seven” and “at least seven” are not interchangeable. At least seven includes 7, 8, 9 and 10. For thresholds near zero, a complementary calculation 1−P(X<k) may require fewer terms.

Distribution check: Summing P(X=k) over k=0…n must equal 1, apart from tiny floating-point rounding.
04 · MONTE CARLO SIMULATION

Turn probability into repeated Boolean trials

  1. Generate a uniform pseudo-random number u in [0,1).
  2. Count success when u<p.
  3. Repeat n times to obtain one value of X.
  4. Record whether X=k and whether X≥k.
  5. Repeat the complete experiment M times.
  6. Estimate probability as matching count divided by M.
estimated P(X=k) = number of experiments with k successes / M

A pseudo-random generator is deterministic. A seed selects its starting state. Using the same program, inputs and seed gives the same simulation, which is essential for debugging and teaching. Changing the seed provides another valid sample.

Set seed
Run n trials
Count successes
Repeat M times
Compare frequency
05 · WORKED EXAMPLE

Exactly seven correct guesses out of ten

For random guessing on independent true/false questions, n=10, p=0.5 and k=7.

  1. C(10,7)=120.
  2. 0.5^7 × 0.5^3 = 0.5^10 = 1/1024.
  3. P(X=7)=120/1024=0.1171875.
  4. P(X≥7)=[C(10,7)+C(10,8)+C(10,9)+C(10,10)]/1024.
  5. The numerator is 120+45+10+1=176, so the result is 0.171875.

A run of 10,000 experiments might produce 0.119 or 0.115 for exactly seven. That difference is expected sampling variation, not automatic evidence that the formula or code is wrong.

06 · PROGRAMMATIC VERIFICATION

Complete Python implementation

The program accepts a general binomial experiment, calculates exact probabilities, runs a reproducible simulation and compares both exact-k and at-least-k events. It also checks the simulated average successes against np.

programs/probability-simulator.py
Open Compiler
Loading source…
Reproducible experiment: Keep n, p, k, M and seed in every report. Without them, another student cannot recreate the result.
07 · INTERACTIVE EXPERIMENT TRACE

Trace one experiment with n=4 and p=0.5

  1. Define the experiment.
  2. Compare first random value.
  3. Record failure.
  4. Record second success.
  5. Finish one experiment.
  6. Update event counters.
  7. Begin another experiment.
  8. Convert counts to relative frequency.
  9. Evaluate sampling difference.
Current state

Press Next to begin.

08 · MODEL & PROGRAM TESTS

Start with probabilities that must be exact

p = 0 boundary
X is always 0. Therefore P(X=0)=1 and P(X=k)=0 for every positive k; simulation must agree exactly.
p = 1 boundary
X is always n. P(X=n)=1 and the simulated mean must equal n.
Single trial
For n=1, P(X=1)=p and P(X=0)=1−p.
Distribution sum
For selected n and p, sum exact probabilities for k=0…n and verify a value extremely close to 1.
Reproducibility
Two runs with identical inputs and seed must have identical simulated output.
Larger sample comparison
Compare M=1,000 and M=100,000 across several seeds. Larger M should usually reduce error, though not in every individual run.
09 · SAMPLING ERROR & COMPLEXITY

Convergence is a tendency, not equality

By the law of large numbers, relative frequency tends toward the modeled probability as M grows. Typical Monte Carlo error decreases proportionally to 1/√M. To reduce typical error by a factor of ten, we therefore need roughly one hundred times as many experiments.

ComponentTimeSpace
Exact P(X=k)O(1) high-level operationsO(1)
P(X≥k)O(n−k+1)O(1)
SimulationO(Mn)O(1)

Simulation output is evidence about the code and the assumed process, not proof that real-world trials satisfy independence or constant p. Model validation must come before numerical confidence.

10 · PRACTICE & EXTENSIONS

Check theoretical and experimental reasoning

Which values belong to X≥7 when n=10?

About how many times more experiments reduce typical error by 10×?

Extensions

  1. Display the complete theoretical and simulated distribution for k=0…n.
  2. Repeat many seeds and summarize the distribution of estimation error.
  3. Add a confidence interval for the estimated event probability.
  4. Simulate unequal trial probabilities and explain why the binomial formula no longer applies.
  5. Build a without-replacement simulation and compare it with a hypergeometric model.
11 · INTERVIEW PREPARATION

Distinguish model, estimate and observation

Why does C(n,k) appear?

It counts the different choices of k success positions among n trials; each such arrangement has the same probability under constant independent p.

Why does simulation not equal theory?

A finite random sample varies. Theory gives the model probability; simulation gives one relative-frequency estimate.

What does a seed do?

It fixes the starting state of the pseudo-random generator, making an experiment reproducible; it does not make the numbers truly random.

When is binomial inappropriate?

When trial count is not fixed, outcomes are not binary under the model, p changes, or trials are dependent.

Why might more simulations be wasteful?

Error falls only with the square root of M, so very high precision is expensive. If an exact reliable formula exists, simulation may be best used for demonstration or verification.

12 · KEY TAKEAWAY

Probability is a model; simulation is an experiment on that model

The exact binomial result and Monte Carlo estimate should be connected but not confused. Careful work states assumptions, derives the event probability, records the seed and sample size, measures error and resists interpreting random variation as a contradiction.