Monte Carlo Simulations

M CHO
Oct 12, 2023

--

Using Monte Carlo simulations, you can obtain approximate numerical results via random sampling.

Monte Carlo methods are often used when there are many uncertain inputs in a mathematical model, making the model difficult to solve analytically. Instead, by randomly sampling inputs and running the model many times(often thousands or even millions), a probabilistic range of potential outcomes can be estimated.

e.g. You can calculate the value of Pi using Monte Carlo.

import numpy as np

# area of a disk = pi * radius^2
def estimate_pi(n=10000):
count = 0
for _ in range(n):
x, y = np.random.rand(2)
if x**2 + y**2 < 1:
count += 1
return (count / n) / (0.5)**2

estimate_pi()

--

--