<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/using-the-tidyverse-for-more-than-data-manipulation-estimating-pi-with-monte-carlo-methods/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2018-12-21-tidyverse_pi/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><a rel="nofollow" href="https://www.youtube.com/watch?v=kZJY15dyMig" target="_blank"><br>
<img width="400" title="Audentes Fortuna Iuvat" src="https://i0.wp.com/www.brodrigues.co/blog/2018-12-21-tidyverse_pi/img/casino.jpg?w=456" data-recalc-dims="1"></a></p>
</div>
<p></p>
<p>This blog post is an excerpt of my ebook <em>Modern R with the tidyverse</em> that you can read for<br>
free <a rel="nofollow" href="https://b-rodrigues.github.io/modern_R/" target="_blank">here</a>. This is taken from Chapter 5, which presents<br>
the <code>{tidyverse}</code> packages and how to use them to compute descriptive statistics and manipulate data.<br>
In the text below, I show how you can use the <code>{tidyverse}</code> functions and principles for the<br>
estimation of <span>\(\pi\)</span> using Monte Carlo simulation.</p>
<div>
<h2>Going beyond descriptive statistics and data manipulation</h2>
<p>The <code>{tidyverse}</code> collection of packages can do much more than simply data manipulation and<br>
descriptive statisics. You can use the principles we have covered and the functions you now know<br>
to do much more. For instance, you can use a few <code>{tidyverse}</code> functions to do Monte Carlo simulations,<br>
for example to estimate <span>\(\pi\)</span>.</p>
<p>Draw the unit circle inside the unit square, the ratio of the area of the circle to the area of the<br>
square will be <span>\(\pi/4\)</span>. Then shot K arrows at the square; roughly <span>\(K*\pi/4\)</span> should have fallen<br>
inside the circle. So if now you shoot N arrows at the square, and M fall inside the circle, you have<br>
the following relationship <span>\(M = N*\pi/4\)</span>. You can thus compute <span>\(\pi\)</span> like so: <span>\(\pi = 4*M/N\)</span>.</p>
<p>The more arrows N you throw at the square, the better approximation of <span>\(\pi\)</span> you’ll have. Let’s<br>
try to do this with a tidy Monte Carlo simulation. First, let’s randomly pick some points inside<br>
the unit square:</p>
<pre><code>library(tidyverse)
library(brotools)</code></pre>
<pre><code>n &lt;- 5000

set.seed(2019)
points &lt;- tibble(&quot;x&quot; = runif(n), &quot;y&quot; = runif(n))</code></pre>
<p>Now, to know if a point is inside the unit circle, we need to check wether <span>\(x^2 + y^2 &lt; 1\)</span>. Let’s<br>
add a new column to the <code>points</code> tibble, called <code>inside</code> equal to 1 if the point is inside the<br>
unit circle and 0 if not:</p>
<pre><code>points &lt;- points %>% 
    mutate(inside = map2_dbl(.x = x, .y = y, ~ifelse(.x**2 + .y**2 &lt; 1, 1, 0))) %>% 
    rowid_to_column(&quot;N&quot;)</code></pre>
<p>Let’s take a look at <code>points</code>:</p>
<pre><code>points</code></pre>
<pre><code>## # A tibble: 5,000 x 4
##        N       x      y inside
##    </code></pre></div>