# Statistics bootcamp: estimating pi with R and Buffon’s needle

Written by Eric Wika on . Posted in The Statistics Dictionary Creative applications of statistics abound in virtually every discipline. But coming up with tangible examples to explain core concepts to those new to the field can be quite a challenge. That’s how it was for me as a graduate student in the Department of Biostatistics at the University of Kansas Medical Center.

My professor was looking to pilot a statistics bootcamp for incoming graduate students, but instead of drill sergeant-mandated push-ups, pull-ups and 5am wake-up calls, current graduate students would be responsible for planning and teaching lectures for incoming recruits, giving entry-level refreshers on multivariable calculus, matrix and linear algebra, basic probability theory, etc.

My lecture was meant to address the basics of importing/exporting data and data manipulation in R. Exactly how could I communicate the information in a creative way; one that would adequately prepare our new recruits, while at the same time being mindful of the diverse academic backgrounds of our incoming students?

The answer, it turns out, dates back to the 18th century when Georges-Louis Leclerc, Comte de Buffon (pictured), proposed an innovative approach for the estimation of pi (π) - the ratio of a circle’s circumference to its diameter - using nothing more than a collection of identical needles, a flat surface with carefully drawn vertical lines and, of course, a little probability theory.

This has since become known as ‘Buffon’s needle problem’, and it was explained brilliantly and simply in a YouTube video I stumbled upon by accident. Novice statisticians might want to review the video now before continuing to the section headed What's R got to do with it?. Those who are comfortable following the math can read on.

How it works

‘Buffon’s needle’ begins with a board and a large number of identical needles, each with length L. Parallel vertical lines are drawn on the board, spaced twice the length of the needle (2L) from each other. In doing this, a random toss of all the needles on to the board ensures that every needle will land in such a way that the center of the needle will be at most one needle’s length from a line.

As the needles are tossed randomly on to the board, it is assumed that the distance from the needle’s center to the nearest line is uniform, with any distance between 0 and L being equally probable. Also, it is also assumed that a needle can fall at any angle with respect to a vertical line, θ, with equal probability. As depicted in Figure 1, the probability of a needle crossing a line is based on whether the distance from the center of the needle to the nearest line, X, is shorter than half the distance of the component of the needle perpendicular to the line; or remembering the grade school mnemonic so-cah-toa for calculating the sides of a right triangle.

Figure 1: Illustration of the conditions under which a needle crosses a line in terms of X, θ, and L

Utilizing the fact that X and θ are independent uniform random variables with marginal probability density functions – Px = 1/L and Pθ = 2/π respectively – an expression for the earlier probability statement involves integrating the product of the marginal probability density functions above with respect to X and θ; X integrated between 0 and , and θ integrated between 0 and π/2.

At this point we need only apply concepts learned in introductory calculus, where it can be easily shown that the probability of a needle crossing a vertical line is equal to 1/π. Thus, if N denotes the total number of needles tossed onto the board and n the number of needles that intersect with one of the vertical lines, then the probability of a needle crossing a line can be estimated by . Taking the reciprocal of this equation gives us as an unbiased estimator for π. In other words, an estimate of π is obtained simply by taking the ratio of the total number of needles tossed to the number of needles that cross a line.

What’s R got to do with it?

The next day, during one of many summer planning meetings, I mentioned the YouTube video to my professor. This is exactly what I should do for my teaching session, he said: “The students will have plenty of time to practice importing, exporting, and manipulating data in R; show them what a modern day approach to the Buffon’s needle approximation looks like using statistical computing.”

Not only would this serve to familiarize students with some of the commonly used functions and operations in R (i.e., sample, if and for statements), but it represented a fun and practical example to teach simulation study, and how it can be used to illustrate statistical properties and concepts.

Over lunch, I sat down with my laptop and opened up R. As the needles were tossed on this virtual board they would assume a random position and would fall at random angles (Figure 2). The graphical capabilities of R easily drew the board to my specifications. After drafting my R script, the first series of needles flew across the board and the first estimate of π was calculated: 3.15 when N = 1000. While it took an hour to program the R script that tossed the first set of needles, the next and all future throws came in milliseconds. R could very easily draw the resulting density plot as well as calculate the 95% confidence interval. Figure 2 contains an example of representative output for this experiment. In 24 hours I had gone from not knowing π could be estimated by throwing needles to being able to throw an endless number of them – virtually, of course.

Figure 2: (Top panel) Typical output using N = 400 needles. Red indicates needles that intersect with a vertical line. In this particular sample, n = 119 needles crossed a line, giving an estimate N/n = 3.36. (Middle panel) A representative distribution of  for 1000 bootstrap samples at N = 1000. (Bottom panel) Estimates of π for increasing number of needles tossed (N). For each N, was taken to be the average over 1000 bootstrap samples

Throwing needles in R was so quick and easy, I could use it to demonstrate concepts that are central to statistics. For example, I could show that each needle throw is a Bernoulli random variable with a probability of success of 1/π. As the number of needles goes up, the sample average gets closer to the expected value - giving better and better approximations of π. This is an excellent way to demonstrate the law of large numbers.

Furthermore, I could discuss the bootstrap as well as the bootstrap estimate of the variance of the estimator. If I wanted to stay in the land of pencil and paper theory, I could show that the estimator is a function of the sample average  and, as such, to find the variability of the estimator I could apply the delta method. Or I could do both and see how well the bootstrap agrees with the delta method.

As the theory underpinning Buffon’s needle involves little more than introductory calculus and a basic understanding of probability theory it could be used at both the undergraduate and graduate levels and provides a practical and fun application calculus. There is something here for all levels of statistics students.

## Acknowledgements

I would like to give special thanks to Dr. Devin Koestler, Assistant Professor of Biostatistics at the University of Kansas Medical Center, whose encouragement and feedback were instrumental in the preparation of this article.

We use cookies to understand how you use our site and to improve your experience. By continuing to use our site, you accept our use of cookies and Terms of Use.