Lesson 14 of 15

Monte Carlo Sampling

Monte Carlo Sampling

Monte Carlo simulation uses random sampling to model uncertainty. In quantitative finance, it is used to price derivatives, estimate risk (VaR), and simulate portfolio paths.

A key building block is generating samples from a normal distribution using the Box-Muller transform. Given two independent uniform samples U1,U2(0,1)U_1, U_2 \in (0, 1):

Z0=2lnU1cos(2πU2)Z_0 = \sqrt{-2 \ln U_1} \cos(2\pi U_2) Z1=2lnU1sin(2πU2)Z_1 = \sqrt{-2 \ln U_1} \sin(2\pi U_2)

Both Z0Z_0 and Z1Z_1 are independent standard normal samples. Scale to mean μ\mu and std σ\sigma: X=μ+σZX = \mu + \sigma Z.

This transform is efficient because each pair (U1,U2)(U_1, U_2) yields two normal samples.

Your Task

Implement:

  • mc_normal_samples(n, mu, sigma, seed) — generate n normal samples using Box-Muller and random.seed(seed)
  • mc_mean(samples) — mean of a list of samples
Python runtime loading...
Loading...
Click "Run" to execute your code.