Statistics 999 Final Project A Study of Comte de Buffon’s Needle Experiment Student Anonymous December 5, 2005 Statistics 999 Final Project Student Anonymous Fall 2005 The Background ____________________________________________________________ 1 The Experiment _____________________________________________________________ 2 Summary ___________________________________________________________________ 5 Appendix A: SAS Code ______________________________________________________ 7 Figure 1: Buffon and Laplace Estimates of pi ______________________________________ 3 Figure 2: Buffon and Laplace Estimated Errors ____________________________________ 4 Figure 3: Histograms of 10,000 Estimated pi Values ________________________________ 6 Table I: Input Values for Each Experiment ________________________________________ 2 i Statistics 999 Final Project Student Anonymous Fall 2005 The Background Buffon’s Needle Experiment is a simple Monte Carlo method for estimating the value of pi, 3.14159265. The problem was first posed by the French naturalist Georges Louis Leclerc Comte de Buffon in 1733. Buffon reproduced the problem himself, with a solution, in 1777. The idea is simple. Suppose you have a tabletop (or floor) with a number of parallel lines drawn on it. These lines are equally spaced at a distance, D. Suppose you also have a pin or needle of length L, where L ≤ D. If you drop the needle on the table, you will find that one of two things happens: 1. The needle crosses or touches one of the lines, or 2. The needle crosses no lines. The idea is to keep dropping this needle over and over on the table, and to record the statistics. Namely, we want to keep track of both the total number of times that the needle is randomly dropped on the table (call this n), and the number of times that it intersects a line (call this R). It can be shown that, given L and D, where L ≤ D and p is the probability that a dropped needle crosses one of the lines, 2L Dp (1) By dropping a needle of length L a total of n times and counting the number of intersections, R, we can estimate p as pˆ R n (2) With this estimate of p, we can then estimate the value of pi with equation 1. Repeating this M times, we have M estimates of pi. For sufficiently large M, ˆ 0 NOR( ,Var (ˆ 0 )) 1 Statistics 999 Final Project Student Anonymous Fall 2005 When L = D, Var (ˆ 0 ) 5.63 n Laplace proposed an alternative experiment in which the parallel lines are replaced by a grid of rectangles with sides a and b. In Laplace’s experiment, the M estimates of pi are ˆ1 NOR( ,Var(ˆ1 )) When L = a = b, Var (ˆ1 ) 0.47 n The Experiment Appendix A contains the SAS code used to explore Buffon’s and Laplace’s needle experiments. A macro is used generate the estimates of pi. The code is run using the inputs shown in table I. Buffon’s method is selected to create the first data set of pi estimates. After generating Laplace’s data set, the two data sets are merged into a single data set of 20,000 estimates of pi. This merged data set is used to explore the results of estimating pi. Table I: Input Values for Each Experiment Input Buffon Laplace 0 0 n needle drops 10,000 10,000 M iterations 10,000 10,000 D 2 1.5 L 2 N/A a N/A 2 b N/A 2 Seed (estimates of pi) 2 Statistics 999 Final Project Student Anonymous Fall 2005 Figure 1 shows the plot of each method’s 10,000 estimates of pi. A reference line shown in blue at 3.141592 is barely discernable in figure 1. It is easy to see that Buffon’s estimates are centered about the actual value of pi while Laplace’s method tends to estimate pi approximately 5% greater than 3.141592. However, Buffon’s method also results in greater variability in the estimated value of pi as predicted by the theory. E s t i ma t i 3. 3. 3. 3. 3. 3. on 37 36 35 34 33 32 3. 3. 3. 3. 3. 3. 3. 31 30 29 28 27 26 25 3. 3. 3. 3. 3. 3. 3. 24 23 22 21 20 19 18 3. 3. 3. 3. 3. 3. 3. 17 16 15 14 13 12 11 3. 3. 3. 3. 3. 3. 10 09 08 07 06 05 Bu f f o n 0 1000 2000 3000 4000 5000 Lapl ace 6000 7000 8000 9000 10000 I t er at i on Figure 1: Buffon and Laplace Estimates of pi The reduced variability of Laplace’s method is more apparent in Figure 2. This figure is a plot of the error of each estimate of pi from the “true” value of 3.14159265. It’s easy to see from Figure 2 that variability of Laplace’s method is approximately two-thirds that of the Buffon method. A reference line, shown in blue, at zero also shows that Buffon’s method provides an estimated value closer to the “true” value of pi. 3 Statistics 999 Final Project Student Anonymous Fall 2005 Er r 0. 0. 0. 0. 0. 0. or 23 22 21 20 19 18 0. 0. 0. 0. 0. 0. 0. 17 16 15 14 13 12 11 0. 0. 0. 0. 0. 0. 0. 10 09 08 07 06 05 04 0. 0. 0. 0. - 0. - 0. - 0. 03 02 01 00 01 02 03 - 04 05 06 07 08 09 0. 0. 0. 0. 0. 0. Bu f f o n 0 1000 2000 3000 4000 5000 Lapl ace 6000 7000 8000 9000 10000 I t er at i on Figure 2: Buffon and Laplace Estimated Errors A summary of each method’s estimate, including the mean estimated value of pi, the sample variance, and the minimum and maximum values of pi, are shown below. These results are consistent with the graphical results. Buffon’s method provides a point estimate of pi of 3.1402619. This is 0.00133 less than the “true” value of pi (3.14159265) or 99.6% of the “true” value. Summary of Estimated pi Values for Buffon The MEANS Procedure Analysis Variable : pi 95th N Mean Variance Minimum Maximum 5th Ptcl Pctl ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 10000 3.1403 0.000573 3.0539 3.2289 3.1013 3.1807 ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Summary of Estimated pi Values for Laplace Method The MEANS Procedure 4 Statistics 999 Final Project Student Anonymous Fall 2005 Analysis Variable : pi 95th N Mean Variance Minimum Maximum 5th Ptcl Pctl ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 10000 3.3004 0.000381 3.2293 3.3700 3.2683 3.3326 ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ The Laplace method results in an estimated value of pi 0.1589 greater than the “true” value of pi (3.14159265) or 105.1% of the true value. Similar to the graphical results, the summary statistics indicate that the variability of the Laplace method is less than Buffon’s method. The summary statistics reveal that 90% of the Laplace estimated values fall in a range of 0.0643. Buffon’s methods results in 90% of the estimated values falling in a range of 0.0794. Summary The results of this simulation show that Buffon’s Needle Experiment results in a reasonable approximation of pi. Buffon’s method provides a point estimate closer to the “true” value of pi while the Laplace method results in less variability in the estimate. Simulated variability from Buffon’s method was very close to the variability predicted by theory of Var (ˆ 0 ) 5.63 when the needle length is equal to the distance between lines. n The point estimate and variability obtained from the Laplace method did not agree well with the values predicted by theory. Point estimates were consistently greater than the “true” value of pi by fairly significant amounts (e.g., 0.3 to 0.4). The greatest deviation from the theory was seen in the variability. While the variability of the Laplace method was less than the variability in Buffon’s method, the Laplace variability was far from the Var (ˆ 1 ) 0.47 predicted n by theory. This is likely due to an error in the SAS code rather than the Laplace method itself. Figure 3 displays histograms of Buffon and Laplace data sets side by side on identical horizontal scales. These histograms clearly show the poor point estimate of Laplace’s method and the greater variability found in Buffon’s method. 5 Statistics 999 Final Project Student Anonymous Fall 2005 pi Sum ‚ * 12000 ˆ * ‚ * ‚ * ‚ * 10000 ˆ * * ‚ * * ‚ * * ‚ * * 8000 ˆ * * * * ‚ * * * * * * ‚ * * * * * * ‚ * * * * * * 6000 ˆ * * * * * * ‚ * * * * * * ‚ * * * * * * ‚ * * * * * * 4000 ˆ * * * * * * ‚ * * * * * * ‚ * * * * * * * ‚ * * * * * * * * 2000 ˆ * * * * * * * * * ‚ * * * * * * * * * * ‚ * * * * * * * * * * ‚ * * * * * * * * * * * * Šƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 pi Midpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 ‚ƒƒƒƒƒƒƒƒƒƒƒƒ Buffon ƒƒƒƒƒƒƒƒƒƒƒ‚ ‚ƒƒƒƒƒƒƒƒƒƒƒ Laplace ƒƒƒƒƒƒƒƒƒƒƒ‚ Figure 3: Histograms of 10,000 Estimated pi Values 6 meth Statistics 999 Final Project Student Anonymous Fall 2005 Appendix A: SAS Code %MACRO PI(seed, size, obs, data, L, D, a, b, method = 'Buffon'); DATA WORK.&&data (DROP = j X A R p rho psi); DO i = 1 TO &obs; R = 0; DO j = 1 TO &size; IF &method = 'Buffon' THEN DO; X = (&D / 2) * RANUNI(&seed); A = COS(1.57 * RANUNI(&seed)); END; IF &method = 'Laplace' THEN DO; X = (&a / 2) * RANUNI(&seed); A = COS(3.14 * RANUNI(&seed)); END; IF ((X / A) LT (&L / 2)) THEN R + 1; END; p = R / &size; IF &method = 'Buffon' THEN DO; meth = 1; rho = (&L / &D); psi = p / (2 * rho); pi = 1 / psi; END; IF &method = 'Laplace' THEN DO; meth = 2; rho = ((2 * &L * (&a + &b)) - (&L * &L)) / (&a * &b); psi = p / rho; pi = 1 / psi; END; OUTPUT; END; RUN; %MEND PI; /**************************************************************************/ 7 Statistics 999 Final Project Student Anonymous Fall 2005 /* Set options and create formats. */ /**************************************************************************/ OPTIONS LINESIZE = 80 NOBYLINE NODATE PAGENO = 1; GOPTIONS COLORS = (RED GREEN); AXIS1 ORDER = (0 TO 10000 BY 1000); LEGEND1 LABEL = NONE MODE = SHARE POSITION = (top center inside); SYMBOL1 CV = GREEN VALUE = DOT HEIGHT = 0.15; SYMBOL2 CV = RED VALUE = DOT HEIGHT = 0.15; PROC FORMAT; VALUE m 1 = 'Buffon' 2 = 'Laplace'; RUN; /**************************************************************************/ /* Call the macro PI to estimate pi. Pass the following /* arguments: */ /* */ */ /* Seed for random number generator. */ /* Sample size (n). */ /* Number of estimates of pi (M). */ /* Output data set name. */ /* Length of needle (L). */ /* Distance between lines (D). */ /* Length of rectangle side (a). */ /* Length of rectangle side (b). */ /* Method (Buffon or Laplace). */ /**************************************************************************/ /* Call PI using Buffon method.*/ %PI(161049191, 10000, 10000, B, 2, 2, 1, 1, method = 'Buffon'); /*Call PI using Laplace method.*/ 8 Statistics 999 Final Project Student Anonymous Fall 2005 %PI(161049191, 10000, 10000, L, 2, 2, 2, 2, method = 'Laplace'); /* Combine data sets and calculate error.*/ DATA TWOPI; SET B L; dev = pi - 3.14159265; RUN; /* Create plot of estimated values of pi from each method.*/ PROC GPLOT DATA = TWOPI; PLOT pi * i = meth / NOFRAME LEGEND = LEGEND1 HAXIS = AXIS1 CVREF = 'BLUE' VREF = 3.141593 VAXIS = 'PI' CAXIS = 'BLACK' CTEXT = 'BLACK'; FORMAT meth m.; LABEL meth = 'Method'; LABEL i = 'Iteration'; LABEL pi = 'Estimation'; RUN; /* Create plot of errors from each method.*/ PROC GPLOT DATA = TWOPI; PLOT dev * i = meth / LEGEND = LEGEND1 HAXIS = AXIS1 CVREF = 'BLUE' VREF = 0 VAXIS = 'PI' NOFRAME CAXIS = 'BLACK' CTEXT = 'BLACK'; FORMAT meth m.; LABEL meth = 'Method'; LABEL i = 'Iteration'; LABEL pi = 'Estimation'; LABEL dev = 'Error'; RUN; QUIT; OPTIONS LINESIZE = 120; /* Create histograms of estimated data sets.*/ PROC CHART DATA = TWOPI; VBAR pi / SYMBOL = '*' GROUP = meth TYPE = SUM 9 Statistics 999 Final Project Student Anonymous Fall 2005 SUMVAR = pi LEVELS = 17 MIDPOINTS = 3.02 3.04 3.06 3.08 3.10 3.12 3.14 3.16 3.18 3.2 3.22 3.24 3.26 3.28 3.30 3.32 3.34; FORMAT meth m.; RUN; OPTIONS LINESIZE = 100; /* Calculate summary statistics.*/ PROC MEANS DATA =TWOPI N MEAN VAR MIN MAX P5 P95 FW = 8; BY meth; VAR PI; FORMAT meth m. MEAN 5.2 VAR 5.2; TITLE 'Summary of Estimated pi Values for #BYVAL(meth) Method'; RUN; 10