stat 579
Homework: week 11
Due in class, Tuesday/Thursday Nov 9/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.
http://mste.illinois.edu/reese/buffon/buffon.html#intro .)
- 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).
-
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_vline(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.
-
Compute an estimate for pi, based on the number of line crossings (wrap this into a function
buffon.
-
Run the following code to test your functions:
set.seed(1415)
X <- rneedle(50)
plotneedle(X)
buffon(X)
Report the output.
-
Repeat the calculation of pi based on Buffon's Needle 500 times. Report average and variability of your estimate and discuss.
Deliverables:
Submit an R script of your commented code and an Sweave file that contains calls to your functions and the discussion. Also submit a pdf with the result of your Sweave file.
Great Answers: