← Back to index

Monte Carlo Calculation of Pi

Original notebook: monte_carlo_pi.ipynb


Goals

Background theory

More on the background theory.

Tasks and exercises

  1. What is meant by a uniformly distributed random number?

    Solution

    If a discrete random number xx is distributed according to the uniform distribution, x∼U{a,b}x \sim \mathcal{U}\{a,b\}, this means that the probability of xx taking on an integer value between the two integers aa and bb (both inclusive) is simply 1/n1/n where b−a+1=nb-a+1=n. For example, if b=5b=5 and a=2a=2, then xx may assume the values 2,3,4,5, each with probability 15−2+1=1/4\frac{1}{5-2+1}=1/4. In the case that xx is continuous, uniformly distributed takes on an analogous meaning. Now however, we must talk of xx assuming a value in some range dxdx. Then “x is continuously, uniformly distributed on the interval (a,b)(a,b), means that the probability of finding xx in some range (x0,x0+dx)(x_0,x_0+dx) in that interval is simply dxb−a\frac{dx}{b-a}. In other words, there is a uniform probability of finding xx at any point within the interval (a,b)(a,b).

  2. In this notebook, we are using pseudorandom numbers. Why is it that the generated numbers we use are not true random numbers?

    Solution

    In computational science, “random” numbers are generated by some algorithm. Ultimately, such algorithms are intrinsically deterministic: after having specified an input to the algorithm, referred to as a seed, the sequence of generated “random” numbers is fully determined. This seed is typically chosen in a way so as to try and introduce as much randomness into the simulation in possible. Examples include the current time, or physical quantities which possess some inherent noise like temperature, etc. So, in summary, we cannot have a truly random number generator for this notebook.

  3. How could one obtain a better estimate for the value of Pi from the simulation?

    Solution

    The accuracy of the simulation largely depends on the quality of the random numbers employed and the total number of samples used in the computation. Using the quasi-random number approach can improve the quality of the random numbers and thus convergence of the calculation. On the other hand, one can also improve the accuracy of the result by increasing the number of samples generated. Of course, the latter implies that longer simulation times shall be necessary and thus we have a time-accuracy trade-off.

Interactive visualization

Monte Carlo calculation of π

Number of inside coins:

Number of outside coins:

Legend

(How to use the interactive visualization)

The left panel shows the simulated square referred to in the theory notebook. The light blue area shows the quarter circle. When the coin is dropped inside the circle, it will show up as a blue dot in the figure. Otherwise, it is indicated by a red dot. The right panel shows the calculated value of Pi as a function of the number of dropped coins.

You can choose how many coins to drop at each time from the slider. Use the Quasi-Monte Carlo method (using quasi-random numbers) by ticking the checkbox. The “Throw” button is used to throw the coins into the square. You can clear the simulation by clicking the “Clear” button.