Skip to content
Nemenman Lab
Menu ☰

Archive · Physics 212, 2019

Lecture 20

Archived from the old lab wiki. Some external links may no longer work.

In today’s lecture, we will introduce the first example of a Monte-Carlo simulation.

Follow the Random Numbers notebook in this lecture.

Monte-Carlo area estimation

We use random numbers to perform computations that are fundamentally random, and we will focus on those in the upcoming weeks. However, there are computations that are fundamentally deterministic, but may be easier to perform with random numbers. Suppose we want to calculate an integral under a curve y=y(x)y=y(x), with the limits x∈[a,b]x\in [a,b]. We can, of course, do it analytically for some functions yy or numerically by subdividing the interval of xx into small segments Δx\Delta x and adding up the areas of all small rectangles, y(x)×Δxy(x)\times \Delta x. An alternative way is as follows. Suppose we know that y(x)y(x) is positive and smaller than fmaxf_{\rm max}. Then the area under the curve can be written down as (b−a)fmax∗α(b-a)f_{\rm max}*\alpha, where α\alpha is the fraction of the square b−ab-a by fmaxf_{\rm max} that is actually under the curve. How do we estimate this fraction? Suppose I generate random points (dart throws), uniformly distributed over the square b−ab-a by fmaxf_{\rm max}. The fraction of those that end up under the curve (and it’s easy to check which one does!) is an estimate of α\alpha. The provided script performs such calculation for an arbitrary y=y(x)y=y(x). This is called Monte-Carlo integration. Note that our implementation is incomplete — we don’t check if the function ever is below 0 or ever is above fmaxf_{\rm max}, which a better implementation should do.

How precise are MC approaches?

A problem, of course, is that such calculation is probabilistic — every time we perform it, the results are different. But how different? We can verify, first of all, that our probabilistic estimate of the area converges to to true one, where true is known, e.g, when we integrate a function x2x^2. We can also show the distribution of the estimates of the area when we perform the are estimate with NN dart throws. We see that the larger NN is, the more concentrated the estimates are. To quantify this, we use the provided script to calculate the standard deviation of the estimates as a function of NN. We find that σ2=A/N\sigma^2 = A/N, where AA is some constant. The claim is that the general 1/N1/N law holds true for essentially any function being integrated. It’s only the constant that changes — and we can verify this by integrating other functions.

Your work

Verify that MC integration of different function still converges as 1/N1/\sqrt{N}.

Scripts

Random Numbers — the Random Numbers notebook, which we will be exploring over multiple lectures.