My first instinct was to reach for scipy.stats and they immediately said no.
Write a simulation function that flips n fair coins many times (e.g., 100,000 iterations) and records the number of heads per simulation. Then compute the two-sided empirical p-value as the proportion of simulated head counts whose absolute deviation from the expected value (n/2) is at least as extreme as the observed deviation. Avoid any closed-form probability formulas; rely solely on the simulated distribution.
Pro tip: Mention that you would set a random seed for reproducibility and discuss the trade-off between number of simulations and Monte Carlo error, showing awareness of practical implementation concerns.
Specify n (number of flips per simulation), number of simulations (e.g., 100,000), and the observed number of heads. Use a fixed random seed for reproducibility.
For each simulation, generate n independent Bernoulli trials (e.g., using random.random() < 0.5) and count the number of heads. Store these counts in an array.
Calculate the absolute deviation of the observed count from the expected value (n/2). Then compute the proportion of simulated counts whose absolute deviation is greater than or equal to the observed deviation.
Check that the simulated distribution is centered around n/2 and that the p-value is between 0 and 1. Discuss how increasing the number of simulations reduces Monte Carlo error.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.