Experimenting with Pi Value
One thing I have been picking during this safe-distancing time is doing hard core programming. I think if given an opportunity, everyone should learn programming; its not about how well you can code or if you can build an app or algorithm, but more about how you can think and solve a problem logically. During this process of learning, where I scaled up my Python programming, I got really fascinated by exercises involving estimation of Pi value.
The number Pi is literally infinitely long and non-repeating number. In its endless stream of digits, pi is believed to contain the numbers 0 through 9 in every conceivable combination, forming every possible string of digit. Thus, you can find your birthday date and possibly even works of Shakespeare in Pi value.
Here’s the video of final results from an experiment I undertook. Though I spent most time on learning Python programming, this was using MS Excel and VBA programming. Actually, this experiment can be replicated much more efficiently in Python.
How do you estimate Pi? Pi is circle’s circumference divided by its diameter. Lets say we have a circle with radius R within a square with side 2R. Further, lets say R is 1 unit, then the Area of square is 2R^2 = 4 units and area of circle is Pi*R^2 with an unknown Pi value. Ratio Area of circle/ Area of square = Pi/4, essentially that means Pi = Ratio * 4.
Illustration A:
The left chart in the video has about 1000 random points in a square (within is a circle with diameter same as square’s side). Origin for both, square & circle, is (0,0) coordinates on Cartesian plane (see illustration A). Points x & y random coordinates are generated using xls randbetween function. To know if a point falls within the circle, we use Pythagoras theorem i.e if X^2 + Y^2 is less than or equal to R^2 then its inside the circle.
Illustration B:
Why do we need such complex method and simulation:
One may ask why put so much effort and not just measure the size of circumference and diameter of a circle manually. Or why not measure % of area of circle/area of square using a ruler. Well you can do so, but as we know Pi is endless, we would only be able to get an approximate value and remember we are doing an experiment. Further, instead of manual methods, which are prone to errors and not efficient, we utilize a computer; i.e have a programming way to detect whether or not a randomly placed point on the image is in a circle or not.
So then why do we need Monte Carlo Simulation? Monte carlo simulation has much more sophisticated applications, but in this case we needed random points to generate for very high number of iteration to converge Pi value to its actual value. Lets say if you could draw a lot of points, randomly, within the square, then you know that the odds of any single point within the square is also within the circle is equal to (area of circle)/(area of square) and since we fixed the area of the square to 1, you can then calculate Pi by multiplying the fraction of points within the square that is also within the circle by 4 to get an estimate of Pi.
Coming back to the final results; now the magic happens as you can see in the video. I used VBA codes to refresh random numbers to generate 1000 sets of x & y coordinates and populated value of Pi in a new cell for every new simulation. In the video, the chart on right is a histogram of thousands of averages of all Pi values Monte Carlo simulation produces. You can see with increase in simulation iterations, the Average in the histogram (which charts frequency distribution of estimated Pi value) moves closure to an actual Pi value and the distribution takes a shape of a normal distribution.
A bespoke implementer of Flipped Classroom using power of visual design, success at higher education & post grad levels
4 年This looks excellent.? Another reference that we can make out here is how quickly the digits take their actual position. I think right from the first set the digit 3 was fixed. Then the first decimal 1 was fixed followed by 4. So as we continue to increase the number of set trials, more decimal places would get fixed. Just curious ... Can we get a rough estimate to this phenomenon - the relation between exact value of PI till n decimal places and the number of trials? So to phrase it ?- if we want to get the exact value of PI till 10 decimal places, then we would require at least x random trails.? This is so interesting and beautiful. Great work !