Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Monte Carlo Methods

Open in Colab

Monte carlo methods are a class of methods that use randomness to make it easier to solve deterministic problems. Specifally, we wish to compute integrals of the form

∫f(x)p(x)dx\int f(x) p(x) dx

where pp is a probability density, i.e. a non-negative “function” that integrates to 1.

The idea of Monte-Carlo, is to observe that, if XX is a random variable distributed according to pp, then

E[f(X)]=∫f(x)p(x)dx\mathbb{E}[f(X)] = \int f(x) p(x)dx

We can then estimate the expectation using the law of large numbers. We suppose that X1,  X2,  X3,  …X_1,\;X_2,\;X_3,\;\ldots are all random variables distributed according to pp. Then the law of large numbers gives that

1n∑i=1nf(Xi)→E[f(X1)]=∫f(x)p(x)dx.\frac1n \sum_{i=1}^n f(X_i) \to \mathbb{E}[f(X_1)]=\int f(x)p(x)dx.

A classical example of this, is to compute the area of the unit circle (π\pi)

π=∫D11dx=∫[−1,1]×[−1,1]χD1(x)dx=4E[χD1(U[−1,1]×[−1,1])],\pi=\int_{D_1} 1 dx = \int_{[-1,1]\times[-1,1]} \chi_{D_1}(x) dx = 4 \mathbb{E[\chi_{D_1}(U_{[-1,1]\times[-1,1]})}],

where χD1\chi_{D_1} is the indicator function of the unit disk.

'error = 0.04240734641020705'
<Figure size 640x480 with 1 Axes>

Let’s look at how this converges with nn

<Figure size 640x480 with 1 Axes>

Let’s look at why the error is decaying like 1n\frac{1}{\sqrt{n}}. Let’s let the estimator

f^n=1n∑nf(Xi).\hat{f}_n = \frac1n \sum_n f(X_i).

The variance of f^n\hat{f}_n is

E[(f^n−E^[f(X)])2]=1n2E[(∑i=1nf(Xi)−E[f^(X)])2]=1n2∑i,jE[(f(Xi)−E[f^(X)])(f(Xj)−E^[f(X)])].\mathbb{E}\left[(\hat{f}_n-\hat{E}[f(X)])^2\right] = \frac{1}{n^2} \mathbb{E}\left[\left(\sum_{i=1}^n f(X_i)-\mathbb{E}[\hat{f}(X)]\right)^2\right] = \frac{1}{n^2} \sum_{i,j} \mathbb{E}\left[\left(f(X_i)-\mathbb{E}[\hat{f}(X)]\right)\left(f(X_j)-\hat{E}[f(X)]\right)\right].

If we rearrange this a bit, we get

E[(f^n−E^[f(X)])2]=1n2(∑iE[(f(X1)−E[f^(X)])2]+∑i≠jCov(f(Xi),(Xj)))\mathbb{E}\left[(\hat{f}_n-\hat{E}[f(X)])^2\right] =\frac{1}{n^2} \left(\sum_{i} \mathbb{E}\left[\left(f(X_1)-\mathbb{E}[\hat{f}(X)]\right)^2\right] +\sum_{i\neq j} \text{Cov}(f(X_i),(X_j)) \right)

Since the XiX_i’s are independent, Cov(f(Xi),(Xj))=0\text{Cov}(f(X_i),(X_j))=0 well i≠ji\neq j.

E[(f^n−E^[f(X)])2]=1nE[(f(X1)−E[f^(X)])2].\mathbb{E}\left[(\hat{f}_n-\hat{E}[f(X)])^2\right] =\frac{1}{n} \mathbb{E}\left[\left(f(X_1)-\mathbb{E}[\hat{f}(X)]\right)^2\right].

If we take square root

E[(f^n−E^[f(X)])2]=1nE[(f(X1)−E[f^(X)])2],\sqrt{\mathbb{E}\left[(\hat{f}_n-\hat{E}[f(X)])^2\right]} =\frac{1}{\sqrt{n}} \sqrt{\mathbb{E}\left[\left(f(X_1)-\mathbb{E}[\hat{f}(X)]\right)^2\right]},

we see that the average error will decay like 1n\frac{1}{\sqrt{n}}.

This one over square root nn error is universal in Monte Carlo. It is both the advantage and the curse of Monte Carlo.

Suppose we are trying to compute an integral in dd dimensions using a ppth order rule using nn points. The error is

O(nd−p)=O(n−p/d),O\left( \sqrt[d]{n}^{-p}\right) = O\left( n^{-p/d}\right),

which will be very slow when dd is large. This will be a lot smaller than the error of Monte Carlo when d>2pd>2p

O(n−1/2).O\left(n^{-1/2} \right).

Importance sampling

Suppose we want to compute the integral of a highly concentrated function

∫−∞∞e−104x2dx=π104\int_{-\infty}^\infty e^{-10^4x^2} dx = \frac{\sqrt{\pi}}{10^4}

We shall truncate to the interval [−1,1][-1,1], since the function is very small there.

<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

We can increase the accuracy, we should use Monte Carlo samples where ff is large. If the samples XiX_i are drawn from the pobability distribution pp, then we compensate for this by

∫−∞∞f(x)dx=∫−∞∞f(x)p(x)p(x)dx≈1n∑if(xi)p(xi)\int_{-\infty}^\infty f(x)dx = \int_{-\infty}^\infty \frac{f(x)}{p(x)} p(x)dx \approx \frac{1}{n} \sum_{i}\frac{f(x_i)}{p(x_i)}

We shall choose pp to be the density for a normal distrubtion: p(x)=12πσ2exp⁡(−x22σ2)p(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp(-\frac{x^2}{2\sigma^2})

<Figure size 640x480 with 1 Axes>

We see that concentrating the samples at the peak of the function significantly decreases the error. This method is called “importance sampling”.

Markov Chain Monte Carlo (MCMC)

Sometimes we wish to compute

∫f(x)p(x)dx\int f(x) p(x) dx

but we can’t directly sample from pp. The idea will be to choose our samples XiX_i to be samples from a Markov chain. i.e. Xi+1X_{i+1} is randomly generated from XiX_i. If we run it long enough, then the XiX_i’s will be distributed according to the stationary distribution of the Markov chain.

Our task is therefore to design a Markov chain whose stationary distribution is pp.

Metropolis-Hastings

We shall generate a sample Yi+1Y_{i+1} from XiX_i. i.e. Yi+1=Xi+ΔiY_{i+1}=X_i + \Delta_i.

Then we set Xi+1=Yi+1X_{i+1}=Y_{i+1} with probability min(p(Yi+1)/p(Xi),1)\text{min}(p(Y_{i+1})/p(X_i),1) and set Xi+1=XiX_{i+1}=X_i otherwise.

You can check that the stationary probability is pp.

Note that we don’t need the value of pp, we only need the ratio, so pp need not be normalized.

Let’s use this to compute

∫−∞∞x2Z−1e−V(x)dx,\int_{-\infty}^\infty x^2 Z^{-1}e^{-V(x)} dx,

where V(x)=x2V(x)=x^2 and Z=∫e−V(x)Z=\int e^{-V(x)}. The corresponding p(x)=Z−1e−V(x)p(x) = Z^{-1} e^{-V(x)}.

Note that the samples are no longer independent. The covariance between samples will lower the effective number of samples, but the estimator will still converge.

<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

Such examples occur in statistical physics.