Tuesday 14 March 2017

Pi Day – Floor Pie and Floor Pi



Pi Day – Floor Pie and Floor Pi

Fans of the Simpsons know the term “floor pie” – a notable episode showed pie obsessed Homer being caught in Bart’s diabolical floor pie trap.  Hilarity then ensued.



But over the years pi obsessed math types have also had a version of “floor pi”, known as Buffon’s needle.  The idea is that it would be possible to estimate pi, via a process of randomly scattering needle-like objects on a surface with parallel horizontal lines inscribed on it, and counting the number of times the needles intersect the parallel lines.  In a case where the needle length does not exceed the distance between the parallel lines, pi can be estimated from a Monte Carlo method.

After some trigonometry and a bit of calculus, it can be shown that pi is approximated by:
Pi ~ (2L*n)/T*h, where
L=length of needles
n=total number of needles dropped
T=distance between lines
H=number of needles that crossed lines
In real life, that would mean something like scattering nails a hardwood floor and counting those that cross the lines, a pretty tedious process.  In computer life, though, a relatively short bit of code in a computing language, or an excel model, should do the trick.  I chose the latter.

Basically, my excel spreadsheet just created a large number of virtual needles of length L, centered at random x and y co-ordinates, within a given area having spacing T between lines, and tested to see which of them had a top or bottom above or below the lines, after a random rotation. 

As long as excel’s random number generator is actually reasonably random (sometimes I think it is a little fat in the tails), this should be a reliable simulation.  There are some sine and cosine calls, so floating point cut-offs would eventually limit the accuracy of the simulation.   I am not sure when these factors would kick in, but given the slow convergence of my Monte Carlo, it could be a moot point.


Of course a physical experiment would have its own uncertainties, such as whether a given needle actually crossed a line or not.  If one attempted to use this method to calculate pi to arbitrary accuracy, surely there would be ambiguous cases, which would limit the accuracy of the experiment.

My results showed that the process does approximate pi, but the convergence is slow, to say the least.  Even with 10,000 throws, a typical simulation only came within about 0.1% of pi.  Here are some results.  The numbers across the top show the number of needles cast in the simulation, and the numbers along the side show different runs of the simulation.  So, for example, this table shows 10 trials for each count of needles cast (10 needles, 100 needles, 1000 needles, 10,000 needles) :



10
100
1000
10000
1
0.00000
3.52941
3.31492
3.147954
2
3.00000
3.75000
3.27869
3.12989
3
3.00000
3.15789
3.01508
3.09598
4
6.00000
4.00000
2.63158
3.25556
5
3.00000
2.50000
3.14136
3.06748
6
6.00000
2.60870
3.29670
3.15956
7
3.00000
4.00000
2.84360
3.05033
8
3.00000
3.33333
3.19149
3.12826
9
3.00000
3.52941
3.09278
3.14795
10
6.00000
2.14286
3.48837
3.15956

3.60000
3.25516
3.12946
3.13425





Diff from Pi
0.45841
0.11357
-0.01214
-0.00734
Pct Diff
14.6%
3.6%
-0.4%
-0.2%

As you can see the accuracy of the estimate of pi tends to go up with the number of needles cast.  For the 10,000 needle Monte Carlo, the 10 trial overall average for the estimate of pi is accurate to within 0.2%.  That’s still not very close, though.

The Monte Carlo is set up so that the length of the needle compared to the distance between the lines can be varied.  There appears to be some evidence that the simulation works best if the length of the needle is about half the spacing between the lines.  I am not sure if that’s a real effect, though.  It might just be normal statistical variation.  It’s hard to see why that should matter in a computer simulation, though it might make a physical simulation easier to perform, in practice.


L/T
Est
Pi
Diff
Pct Diff
0.1
3.144704
3.141593
0.003112
0.10%
0.2
3.135268
3.141593
-0.00632
-0.20%
0.3
3.187073
3.141593
0.04548
1.45%
0.4
3.141697
3.141593
0.000104
0.00%
0.5
3.140686
3.141593
-0.00091
-0.03%
0.6
3.143699
3.141593
0.002107
0.07%
0.7
3.14176
3.141593
0.000168
0.01%
0.8
3.131017
3.141593
-0.01058
-0.34%
0.9
3.148454
3.141593
0.006861
0.22%



An Italian mathematician, Mario Lazzarini, claimed to have performed the experiment in 1901.  He got pi to an amazing accuracy, 355/113, which is accurate to 5 decimal places. To be charitable, he was awfully lucky.  There is a lot of doubt about whether the experiment was actually performed, and if so, whether the results were cooked ahead of time.  The last link below has a detailed analysis of his results, including some examination of whether his results were “too good”.

Sources:
The Pleasures of Probability, Richard Issac, Springer


---------------------------------------------------------------------------------------------------------------

So, now that you have done some math, you should read a science fiction book, or even better, a whole series.  Book 1 of the Witches’ Stones series even includes a reference to pi.:

Kati of Terra

How about trying Kati of Terra, the 3-novel story of a feisty young Earth woman, making her way in that big, bad, beautiful universe out there. 

The Witches’ Stones

Or, you might prefer, the trilogy of the Witches’ Stones (they’re psychic aliens, not actual witches), which follows the interactions of a future Earth confederation, an opposing galactic power, and the Witches of Kordea.  It features Sarah Mackenzie, another feisty young Earth woman (they’re the most interesting type – the novelist who wrote the books is pretty feisty, too).

The Magnetic Anomaly: A Science Fiction Story

“A geophysical crew went into the Canadian north. There were some regrettable accidents among a few ex-military who had become geophysical contractors after their service in the forces. A young man and young woman went temporarily mad from the stress of seeing that. They imagined things, terrible things. But both are known to have vivid imaginations; we have childhood records to verify that. It was all very sad. That’s the official story.”






No comments:

Post a Comment