Using Monte Carlo Simulation to Estimate Probabilities

Karan Oberoi
2 min read4 days ago

--

Monte Carlo simulation is a versatile and powerful method for solving probability problems that are analytically challenging. In this article, we’ll explore how to use Monte Carlo simulation to estimate the likelihood of a specific event: the median of four random numbers exceeding a certain value, by taking a simple example.

The Problem

What is the probability that the median of four numbers, randomly selected from a uniform distribution between 0 and 3, is greater than 2.5?

The Monte Carlo Simulation Approach

Monte Carlo simulation estimates probabilities by generating and analyzing random samples. Here’s a step-by-step outline of how we address this problem:

  1. Generate Random Samples — Randomly select four numbers from a uniform distribution between 0 and 3. These form one sample.
  2. Calculate the Median — For each sample, calculate the median. The median is the middle value in a sorted list of numbers.
  3. Evaluate the Condition — Determine whether the calculated median exceeds 2.5. Keep count of how often this condition is met.
  4. Repeat the Process — Repeat the above steps for a large number of trials (e.g., 100,000). The proportion of trials where the condition is satisfied gives an estimate of the probability.

The Python Implementation

Below is a Python function to perform this simulation:

# Function to execute montecarlo simulation
def monte_carlo_median_simulation(n_iter, dist_lower_limit, dist_upper_limit, n_val, n_med):

# create counting variable
count = 0

for _ in range(n_iter):

# create an array of uniform distribution
uniform_dist = np.random.uniform(dist_lower_limit, dist_upper_limit, n_val)

# calculate median
med = np.median(uniform_dist)

if med > n_med:
count += 1

return count / n_iter

Results

Using the above simulation, the estimated probability that the median of four numbers exceeds 2.5 is approximately 0.031 or 3.1%. This estimate is robust because of the large number of trials, which reduces variability in the results.

Detailed code link — Statistics/Monte_carlo_simulation_example.ipynb at main · karanoberoi28/Statistics

Key Takeaways

Monte Carlo simulation is invaluable for estimating probabilities in scenarios where analytical solutions are complex or infeasible. Applications of Monte Carlo methods span finance, engineering, and risk analysis, making it a critical tool for decision-making under uncertainty.

Discussion

Have you used Monte Carlo methods in your projects? What challenges or successes have you experienced? Share your thoughts in the comments!

--

--

Karan Oberoi
Karan Oberoi

Written by Karan Oberoi

40 under 40 Data Scientist | Mentor | Causal-AI

No responses yet