Thursday, July 9, 2009

How do you calculate the variance and standard deviation of PI using C++?

after setting a C++ program to estimate the value of PI using the Monte Carlo random numbers method, how can you modify the code to calculate the variance and the standard deviation of PI?

How do you calculate the variance and standard deviation of PI using C++?
From this site:


http://www.eveandersson.com/pi/monte-car...





Using the Monte Carlo method you end up with this calculation:





pi = (4*M)/N





where





M = number of points inside the circle


N = number of total points used





From Wikipedia:


http://en.wikipedia.org/wiki/Standard_de...





Suppose we wished to find the standard deviation of the set of the numbers 4 and 8:





Step 1: find the arithmetic mean (or average) of 4 and 8


(4 + 8) / 2 = 6


Step 2: find the difference between each number and the mean


4 − 6 = −2


8 − 6 = 2


Step 3: square each of the differences


(−2)^2 = 4


(2)^2 = 4


Step 4: sum the obtained squares


4 + 4 = 8.


Step 5: divide the sum by the count of numbers (here we have two numbers)


8 / 2 = 4


-- So, the variance is 4 --


Step 6: take the non-negative square root of the quotient


sqrt(4)=2


-- So, the standard deviation is 2 --





The standard deviation is the square root of the variance, so step 5 produces the variance and step 6 produces the standard devation.





What you need to do:


1) Calculate the value of PI using increasing numbers of points and store each result. So, try 100 points, then 200 points, then 300 points, etc... and build up an array of different values for pi.


2) Write a function to calculate the variance and standard deviation. You'll need to pass in your pi array and then follow the above formula in a loop for each value.





Hope this helps!

gifts

No comments:

Post a Comment