Calculating pi with Monte Carlo integration

Click For Summary

Discussion Overview

The discussion revolves around the use of Monte Carlo integration to approximate the value of pi. Participants explore different methods of implementing this technique, share code snippets, and address potential errors in the calculations. The scope includes computational methods, programming practices, and the theoretical underpinnings of Monte Carlo simulations.

Discussion Character

  • Exploratory, Technical explanation, Debate/contested, Mathematical reasoning

Main Points Raised

  • One participant initially approximated pi using the integral of \(\sqrt{1-x^2}\) and reported a value of around 3.49 with 10 random numbers.
  • The same participant later implemented a Monte Carlo method in Java, generating 100,000 random numbers, but initially obtained a value around 3.415, suggesting a systematic error.
  • Another participant pointed out that using two random numbers per iteration was unnecessary and suggested generating one random number and squaring it instead.
  • Concerns were raised about the clarity of the original participant's approach, with a suggestion that it did not align with traditional Monte Carlo simulations, which involve generating random x and y values to simulate dart throws in a quarter circle.
  • One participant defended the original method, stating it qualifies as a Monte Carlo integration technique by estimating the expected value of a random variable defined by the integral.
  • There was a discussion about the efficiency of different methods, with one participant asserting that the original method could be more efficient than the rejection technique described by another participant.

Areas of Agreement / Disagreement

Participants express differing views on the appropriateness of the original method as a Monte Carlo simulation. While some argue it is valid, others contest its classification and suggest alternative approaches. No consensus is reached on the best method to approximate pi using Monte Carlo integration.

Contextual Notes

Participants have not fully resolved the implications of their differing approaches, and there are unresolved questions regarding the efficiency and accuracy of the methods discussed.

mjordan2nd
Messages
173
Reaction score
1
Hello!

I am taking a computational physics class this semester and just got out of a test. One of the questions provided us with 10 random numbers and asked us to approximate pi. The method I came up with was realizing that

[tex]\int_0^1 \sqrt{1-x^2} dx = \frac{\pi}{4},[/tex]

and then performing this integral numerically. To perform the integral numerically, I plugged in the random values in place of x and evaluated the integrand. I then summed up the integrand values for each of the 10 random numbers and divided this value by 10 to give me pi/4. This method game me a value of pi of around 3.49. I figured this was good enough since I only had 10 values. However, I decided to come back and actually code this into the computer. Using java, I wrote the following code:

Code:
import java.io.*;
import java.util.*;
import java.math.*;

public class Test
{
	public static void main(String [] args)
	{
		double sum = 0;
		for(int i = 0; i< 100000; i++)
		{
			sum+=Math.sqrt(1-(Math.random() * Math.random()));
		}
		sum = (sum / 100000) * 4;
		System.out.println(sum);
	}
}

However, even with a 100000 random numbers I'm still only getting values of around 3.415. There seems to be some kind of systematic error in my logic, however I can't pinpoint what it is. Any help clearing up this matter would be appreciated.

Thanks.
 
Technology news on Phys.org
Never mind, I found my error. The math.random() * math.random() should read Math.pow(Math.random(),2). This fixed it, and gave me a value of 3.14158
 
First, I don't see any point in generating two random numbers per iteration. Just generate one and square it.

Second, I generally try to avoid using pow() when all I'm doing is squaring or cubing a number. After all, x*x produces the same result as pow(x, 2) without the overhead of a function call.

Third, it's not clear to me what you are doing, but it doesn't seem like a Monte Carlo simulation. In a Monte Carlo simulation of the value of ##\pi## you would be simulating throwing darts at a quarter circle of radius 1 that sits inside a unit square. In the simulation you generate x and y values at random (in the range 0 to 1). If the point lies inside the circle, count it. If it lies outside the circle, don't count it. With a large number of darts, the ratio of points inside the circle to the total number of darts thrown should be close to ##\pi/4##.

Whatever it is that your code is doing, here's how I would modify your code, with an eye to speeding it up.
Code:
for(int i = 0; i< 100000; i++)
{
   x = Math.random();
   sum += Math.sqrt(1.0 - x*x); 
}
 
Mark44 said:
Whatever it is that your code is doing ...

What his code is doing is using random sampling to estimate the expected value of the random variable Y, where [itex]Y = \sqrt{1-X^2}[/itex] and X is U(0,1). Since the expected value is an integral (in this case, [itex]E(Y) = \int_0^1 \sqrt{1-x^2}\,dx[/itex]), I would say this qualifies as a monte carlo integration technique -- and it is more efficient than the rejection technique you described.
 
D H said:
What his code is doing is using random sampling to estimate the expected value of the random variable Y, where [itex]Y = \sqrt{1-X^2}[/itex] and X is U(0,1). Since the expected value is an integral (in this case, [itex]E(Y) = \int_0^1 \sqrt{1-x^2}\,dx[/itex]), I would say this qualifies as a monte carlo integration technique -- and it is more efficient than the rejection technique you described.
OK, good to know. It bothered me that the result was not too far off from ##\pi##.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K