Monte Carlo Simulation
1 Introduction
Monte Carlo Simulation is a computational technique that uses repeated random
sampling to obtain numerical results. It is widely used in various fields such
as finance, engineering, and physics to model complex systems and estimate
probabilities.
2 Concept
The main idea behind Monte Carlo methods is to use randomness to solve
problems that might be deterministic in principle. It relies on the Law of Large
Numbers, which states that as the number of trials increases, the estimated
result converges to the actual value.
3 Example: Estimating the Value of π
A classic example of Monte Carlo simulation is estimating the value of π by
randomly generating points inside a square and counting how many fall inside
a quarter-circle inscribed within the square.
4 Python Implementation
Below is a Python implementation of Monte Carlo simulation to estimate the
value of π.
import numpy as np
import matplotlib.pyplot as plt
def monte_carlo_pi(num_samples):
inside_circle = 0
x_inside, y_inside = [], []
x_outside, y_outside = [], []
for _ in range(num_samples):
x, y = np.random.uniform(-1, 1), np.random.uniform(-1, 1)
1
if x**2 + y**2 <= 1:
inside_circle += 1
x_inside.append(x)
y_inside.append(y)
else:
x_outside.append(x)
y_outside.append(y)
pi_estimate = (inside_circle / num_samples) * 4
# Plot the Monte Carlo Simulation
plt.figure(figsize=(6,6))
plt.scatter(x_inside, y_inside, color=’blue’, s=1, label=’Inside Circle’)
plt.scatter(x_outside, y_outside, color=’red’, s=1, label=’Outside Circle’)
circle = plt.Circle((0,0),1,color=’black’,fill=False)
plt.gca().add_patch(circle)
plt.xlim(-1, 1)
plt.ylim(-1, 1)
plt.legend()
plt.title(f"Monte Carlo Pi Estimation: {pi_estimate}")
plt.show()
return pi_estimate
Run the simulation
monte_carlo_pi(10000)
5 Conclusion
Monte Carlo simulations provide a powerful tool for solving problems involving
uncertainty. The more samples used, the more accurate the estimation becomes,
making it a widely used approach in computational mathematics.