Monte Carlo

The use of random numbers explains the Monte Carlo part of the name. Monte Carlo methods are a very broad family of algorithms that use random sampling to compute or simulate a given process. Monte Carlo is a very famous casino located in the Principality of Monaco. One of the developers of the Monte Carlo method, Stanislaw Ulam, had an uncle who used to gamble there. The key idea Stan had was that while many problems are difficult to solve or even formulate in an exact way, they can be effectively studied by taking samples from them. In fact, as the story goes, the motivation was to answer questions about the probability of getting a particular hand in a game of Solitary. One way to solve this problem is to follow the analytical combinatorial problem. Another way, Stan argued, is to play several games of Solitaire and count how many of the hands we play match the particular hand we are interested in! Maybe this sounds obvious to you, or at least pretty reasonable; you may even have used re-sampling methods to solve statistical problems. But, remember this, mental experiment was performed about 70 years ago, a time when the first practical computers began to be developed!

The first application of the Monte Carlo method was to solve a problem of nuclear physics, a hard-to-tackle problem using the tools at that time. Nowadays, even personal computers are powerful enough to solve many interesting problems using the Monte Carlo approach; hence, these methods are applied to a wide variety of problems in science, engineering, industry, and the arts. A classic pedagogical example of using a Monte Carlo method to compute a quantity of interest is the numerical estimation of the number . In practice, there are better methods for this particular computation, but its pedagogical value still remains.

We can estimate the value of  with the following procedure:

  1. Throw points at random into a square of side .
  2. Draw a circle of radius   inscribed in the square and count the number of points that are inside that circle.
  3. Estimate  as the ratio, .

Here are a few notes:

  • The area of the circle and square are proportional to the number of points inside the circle and the total points, respectively.
  • We know a point is inside a circle if the following relation holds, .
  • The area of the square is   and the area of the circle is . Thus we know that the ratio of the area of the square to the area of the circle is .

Using a few lines of Python, we can run this simple Monte Carlo simulation and compute π, and also the relative error of our estimate compared to the true value of :

N = 10000

x, y = np.random.uniform(-1, 1, size=(2, N))
inside = (x**2 + y**2) <= 1
pi = inside.sum()*4/N
error = abs((pi - np.pi) / pi) * 100

outside = np.invert(inside)

plt.figure(figsize=(8, 8))
plt.plot(x[inside], y[inside], 'b.')
plt.plot(x[outside], y[outside], 'r.')
plt.plot(0, 0, label=f'π*= {pi:4.3f} error = {error:4.3f}', alpha=0)
plt.axis('square')
plt.xticks([])
plt.yticks([])
plt.legend(loc=1, frameon=True, framealpha=0.9);

Figure 8.4

In the preceding code, we can see that the outside variable is only used to get the plot; we do not need it to compute . Also notice that because our computation is restricted to the unit circle; we can omit computing the square root when computing the inside variable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.143.65