<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/visualizing-stirlings-approximation-with-highcharts/"></div></div><div><a data-url="http://www.r-bloggers.com/visualizing-stirlings-approximation-with-highcharts/" data-counturl="http://www.r-bloggers.com/visualizing-stirlings-approximation-with-highcharts/" data-count="horizontal" data-text="Visualizing Stirling’s Approximation With Highcharts" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://aschinchon.wordpress.com/2016/07/05/visualizing-stirlings-approximation-with-highcharts/"> Ripples</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<blockquote><p>I said, “Wait a minute, Chester, you know I’m a peaceful man”, He said, “That’s okay, boy, won’t you feed him when you can” (The Weight, The Band)</p>
</blockquote>
<p>It is quite easy to calculate the probability of obtaining the same number of heads and tails when tossing a coin N times, and N is even. There are <img title="2^{N}" alt="2^{N}" class="latex" src="https://s0.wp.com/latex.php?latex=2%5E%7BN%7D&bg=ffffff&%23038;fg=141412&%23038;s=0"> possible outcomes and only <img title="C_{N/2}^{N}" alt="C_{N/2}^{N}" class="latex" src="https://s0.wp.com/latex.php?latex=C_%7BN%2F2%7D%5E%7BN%7D&bg=ffffff&%23038;fg=141412&%23038;s=0"> are favorable so the exact probability is the quotient of these numbers (# of favorable divided by # of possible).</p>
<p>There is another way to approximate this number incredibly well: to use the Stirling’s formula, which is <img title="1/\sqrt{\pi\cdot N/2}" alt="1/\sqrt{\pi\cdot N/2}" class="latex" src="https://s0.wp.com/latex.php?latex=1%2F%5Csqrt%7B%5Cpi%5Ccdot+N%2F2%7D&bg=ffffff&%23038;fg=141412&%23038;s=0"></p>
<p>This plot represents both calculations for N from 2 to 200:</p>
<p><img alt="Stirling" class="wp-image-3065" src="http://i1.wp.com/aschinchon.files.wordpress.com/2016/07/stirling.jpeg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Although for small values of N, Stirling’s approximation tends to overestimate probability …</p>
<p><img alt="Stirling 2" class="wp-image-3063" src="http://i2.wp.com/aschinchon.files.wordpress.com/2016/07/stirling-2.jpeg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>… is extremely precise as N becomes bigger:</p>
<p><img alt="Stirling 3" class="wp-image-3064" src="http://i1.wp.com/aschinchon.files.wordpress.com/2016/07/stirling-3.jpeg?w=456&ssl=1" data-recalc-dims="1"></p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/James_Stirling_(mathematician)">James Stirling</a> published this amazing formula in 1730. It simplifies the calculus to the extreme and also gives a quick way to obtain the answer to a very interesting question: How many tosses are needed to be sure that the probability of obtaining the same number of heads and tails is under any given threshold? Just solve the formula for N and you will obtain the answer. And, also, the formula is another example of the presence of <img title="pi" alt="pi" class="latex" src="https://s0.wp.com/latex.php?latex=pi&bg=ffffff&%23038;fg=141412&%23038;s=0"> in the most unexpected places, as happens <a target="_blank" href="https://aschinchon.wordpress.com/2014/01/08/what-the-hell-is-pi-doing-here/">here</a>.</p>
<p>Just another thing: the more I use <code>highcharter</code> package the more I like it.</p>
<p>This is the code:</p>
<pre>
library(highcharter)
library(dplyr)
data.frame(N=seq(from=2, by=2, length.out = 100)) %>%
  mutate(Exact=choose(N,N/2)/2**N, Stirling=1/sqrt(pi*N/2))->data
hc &lt;- highchart() %>% 
  hc_title(text = &quot;Stirling's Approximation&quot;) %>% 
  hc_subtitle(text = &quot;How likely is getting 50% heads and 50% tails tossing a coin N times?&quot;) %>% 
  hc_xAxis(title = list(text = &quot;N: Number of tosses&quot;), categories = data$N) %>% 
  hc_yAxis(title = list(text = &quot;Probability&quot;), labels = list(format = &quot;{value}%&quot;, useHTML = TRUE)) %>% 
  hc_add_series(name = &quot;Stirling&quot;, data = data$Stirling*100,  marker = list(enabled = FALSE), color=&quot;blue&quot;) %>% 
  hc_add_series(name = &quot;Exact&quot;, data = data$Exact*100,  marker = list(enabled = FALSE), color=&quot;lightblue&quot;) %>% 
  hc_tooltip(formatter = JS(&quot;function(){return ('&lt;b>Number of tosses: &lt;/b>'+this.x+'&lt;br>&lt;b>Probability: &lt;/b>'+Highcharts.numberFormat(this.y, 2)+'%')}&quot;)) %>%
  hc_exporting(enabled = TRUE) %>%
  hc_chart(zoomType = &quot;xy&quot;)
hc
</pre>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aschinchon.wordpress.com/3012/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/aschinchon.wordpress.com/3012/" border="0"></a> <img alt="" src="http://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/visualizing-stirlings-approximation-with-highcharts/"></div></div><div><a data-url="http://www.r-bloggers.com/visualizing-stirlings-approximation-with-highcharts/" data-counturl="http://www.r-bloggers.com/visualizing-stirlings-approximation-with-highcharts/" data-count="vertical" data-text="Visualizing Stirling’s Approximation With Highcharts" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://aschinchon.wordpress.com/2016/07/05/visualizing-stirlings-approximation-with-highcharts/"> Ripples</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/P-GRQqqoqQM"></a></div>