stat 579

Homework: week 10

Due in class, Thursday/Monday Nov 10/11.

Buffon's Needle

Buffon's Needle Problem is one of the first known applications of geometric probability. It can be used to determine the value of pi.
The setup of the problem is simple: a needle of length 1 is thrown onto a piece of lined paper n times (with distance between lines equal to 1). Let x be the number of times (out of n) that the needle crosses a line. Then pi can be approximated by the value 2*n/x (for an explanation of the mathematical background see e.g. .)
  1. Write a function rneedle(n) that simulates n drops of a needle onto paper and returns a data frame with n rows and 3 columns, where the columns are location (x,y) of the midpoint of the needle, where y gives the location of the midpoint of the needle between two lines (random uniform [0,1]), and x is a random uniform value between 0 and 5; and angle theta, the sharp angle - measured in radians - of the needle and the lines (random uniform between -pi and pi).
  2. Write a function plotneedle that takes a data frame with columns x,y,theta and shows the n needles (use ggplot2 as the basis, use + geom_hline(yintercept=c(0,1)) to additionally visualize the two lines)
    You might want to make use of helper functions to solve this problem. Why do some lines appear longer than other lines? How can you fix this? Comment.
  3. Compute an estimate for pi, based on the number of line crossings (wrap this into a function buffon.
  4. Run the following code to test your functions:
    X <- rneedle(50)
    Report the output.
  5. Repeat the above calculation. If you use the same seed, you will get the same answer. Notice how the estimate varies, if the seed is not set, or set to a different value.

    In order to get an estimate for the variability inherent to the simulation process, we need to repeat the above calculation a good number of times.
    Repeat the calculation of pi (which is based on 50 needles) another 500 times. Report average and variability of your estimate and discuss.


Submit an Rmd script of your commented code, your functions and the discussion.

Great Answers: