How to Mock a Confidence Interval for a Proportion in Python
Simulate a Bernoulli sample and compute a 95% confidence interval for a proportion using the normal approximation in Python.
Python code
16 linesimport random
import math
def mock_ci(n=100, p_true=0.5, z=1.96, seed=42):
"""Simulate a sample proportion and compute its 95% confidence interval."""
random.seed(seed)
successes = sum(1 for _ in range(n) if random.random() < p_true)
p_hat = successes / n
se = math.sqrt(p_hat * (1 - p_hat) / n)
margin = z * se
return p_hat, p_hat - margin, p_hat + margin
if __name__ == "__main__":
p_hat, lower, upper = mock_ci()
print(f"Sample proportion: {p_hat:.3f}")
print(f"95% CI: [{lower:.3f}, {upper:.3f}]")
Output
Sample proportion: 0.470
95% CI: [0.372, 0.568]
How it works
This function simulates n Bernoulli trials using random.random() < p_true to generate successes, then computes the sample proportion p_hat. The standard error is calculated with the normal approximation formula sqrt(p_hat*(1-p_hat)/n), and the margin of error is z * se. Seeding the RNG ensures reproducible results, which is critical for mock experiments. The returned tuple gives the point estimate and the lower/upper bounds of the confidence interval.
Common mistakes
- Using `random.seed` inside a loop instead of once before all trials
- Forgetting to convert successes to a float for accurate division
- Using `p_true` instead of `p_hat` when computing the standard error
- Assuming the normal approximation is valid for very small `n` or extreme proportions
Variations
- Use `random.binomialvariate(n, p_true)` available in Python 3.12+ for a single call
- Implement the Wilson score interval for better coverage with small samples
Real-world use cases
- Simulating A/B test results to estimate the conversion rate difference before launching an experiment.
- Generating synthetic datasets for unit tests of dashboard metrics that display confidence intervals.
- Validating the sample size needed for a survey by estimating the expected CI width from mock data.
Sponsored
More from A/B testing & experimentation
Keep learning
Related tutorials and quizzes for this topic.