5
$\begingroup$

I'm trying to figure out which is the proper way to estimate $\pi$ using the Monte Carlo method randomly distributing points in a square that also contains an inscribed circle.

Some sources say to use the comparison of $\sqrt{x^2+y^2}\le 1$, while others use $\sqrt{x^2+y^2}<1$.

                                          

Here's some example code from a wikipedia article:

def monte_carlo_pi(nsamples):
    acc = 0
    for i in range(nsamples):
        x = random.random()
        y = random.random()
        if (x**2 + y**2) < 1.0:
            acc += 1
    return 4.0 * acc / nsamples

Instead of posting a long list of websites that use $\le 1$ or $< 1$, I've made the list and stored it on the following websites: See either: socrates.io or markdown.press or markdownshare for examples using less than and less than or equal to.

$\endgroup$
1
  • $\begingroup$ I know you are trying to have a logical, math, reason which one, but that asides your first list of resources referring to <= is way more legit that the other list. I have also found in books, articles, university lectures mostly <=, like in Oak Ridge National Lab: olcf.ornl.gov/tutorials/monte-carlo-pi. End of the way it is method that randomly samples, and there might be that there is no math-proven right or wrong here, but rather you gotta pick the one that converge faster; maybe. $\endgroup$ May 9, 2019 at 5:55

1 Answer 1

6
$\begingroup$

Short answer: Both formulations lead to the same answer.


Mathematical explanation:

In order to understand that let us look at two similar problems. Imagine we want to integrate a function $f(x)=x^2$ on two intervals $I_1=[0,1]$ (including both bounds) and $I_2=(0,1)$ (excluding both bounds).

For $I_1$ we have

$$\int_0^1 x^2~dx=1/3.$$

For the second interval, we need to introduce a positive dummy parameter $\varepsilon$ then we can calculate the integral as

$$\lim_{\varepsilon\to 0}\int_{0+\varepsilon}^{1-\varepsilon}x^2~dx=1/3.$$

So the line of separation between both intervals does not contribute to the integral (area) because it has an infinitesimal width. The same argument can be applied to the circle area. But including the line of the circle will make the calculations easier because we do not need to introduce a dummy variable.


Numerical explanation:

Because of the numerical precision of your computer, it will be very unlikely that the generated random numbers will lead to points that are really on the line of the circle. Numerically it will be impossible to obtain any value in which at least one of the coordinates is irrational. Only if both values are rational and if $x^2+y^2=1$ they can lie on the circle. But this case is also very unlikely.

$\endgroup$
5
  • $\begingroup$ When you say that it is easier without the dummy variable $\epsilon$ that approaches 0, does this only apply to the math? There is no dummy variable in the Monte Carlo simulation, after all. $\endgroup$ May 9, 2019 at 6:34
  • $\begingroup$ @I_Don't_Code Yes, this is only related to the mathematical derivation. $\endgroup$ May 9, 2019 at 6:37
  • $\begingroup$ So if I'm reading this correctly, it mathematically doesn't make a difference using $\le1$ or $< 1$, so it wouldn't affect the accuracy of the simulation, even if my computer had an infinite decimal precision. $\endgroup$ May 9, 2019 at 6:39
  • $\begingroup$ @I_Don't_Code: If the number of samples goes to infinity it will not make any difference. Remember that you are trying to approximate the area of the circle with points. It will always make a difference for a finite number of points. But with an increasing number of data points the difference will become smaller. $\endgroup$ May 9, 2019 at 6:40
  • 3
    $\begingroup$ @I_Don't_Code: You could also count the number of points that for which $0.99999999999 \leq \sqrt{x^2+y^2}\leq 1.00000000001$ (You can use the numerical precision of your programming language for the bounds). To see the number of values which lie very close to the line of the circle. Then you can directly see the bias. $\endgroup$ May 9, 2019 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.