Site icon R-bloggers

I’m late for π day

[This article was first published on Back Side Smack » R Stuff, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

It is officially no longer pi day, but I didn’t see this Drew Conway post about estimating pi until just a few minutes ago. Because Google Reader doesn’t show github embeds, I also got to try it without seeing Drew’s solution. The estimation method relies on exploiting the area of a circle.

We can use R to generate random numbers for our and coordinates and count up the number of pairs inside the circle (or quarter of a circle, in our case). Because is the area of our quarter circle, the ratio of the 4 times the number of random coordinates within the quarter circle to the total number of random coordinates should converge on . This is a very simple Monte Carlo integration. So what do we get?

From pi day

Gets pretty close! The final error was , not too shabby! I’m computing the running sample average, so it isn’t a true Monte Carlo, but it converges well enough. Code is below:

To leave a comment for the author, please follow the link and comment on their blog: Back Side Smack » R Stuff.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.