<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/amazing-things-that-happen-when-you-toss-a-coin-12-times/"></div></div><div><a data-url="http://www.r-bloggers.com/amazing-things-that-happen-when-you-toss-a-coin-12-times/" data-counturl="http://www.r-bloggers.com/amazing-things-that-happen-when-you-toss-a-coin-12-times/" data-count="horizontal" data-text="Amazing Things That Happen When You Toss a Coin 12 Times" 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/06/19/amazing-things-that-happens-when-you-toss-a-coin-12-times/"> Ripples</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<blockquote><p>If there is a God, he’s a great mathematician (Paul Dirac)</p>
</blockquote>
<p>Imagine you toss a coin 12 times and you count how many heads and tails you are obtaining after each throwing (the coin is equilibrated so the probability of head or tail is the same). At some point, it can happen that number of heads and number of tails are the same. For example, if you obtain the sequence T-H-T-T-H-T-H-H-T-T-H-H, after the second throwing, number of heads is equal to number of tails (and both equal to one). It happens again after the 8th throwing and after last one. In this example, the last throwing where <em>equallity occurs</em> is the number 12. Obviously, equallity can only be observed in even throwings.</p>
<p>If you repeat the experiment 10.000 times you will find something like this if you draw the relative frequency of the last throwing where cumulated number of heads is equal to the one of tails:</p>
<p><img alt="Coin12Times" class="wp-image-2891" src="https://aschinchon.files.wordpress.com/2016/06/coin12times1.png?w=604"><br>
From my point of view there are three amazing things in this plot:</p>
<ol>
<li>It is symmetrical, so <code>prob(n)=prob(12-n)</code></li>
<li>The least likely throwing to obtain the last equality is the central one.</li>
<li>As a corollary, the most likely is <em>not obtaining any equality</em> (number of heads never are the same than number of tails) or <em>obtaining last equality in the last throwing</em>: two extremely different scenarios with the same chances to be observed.</li>
</ol>
<p>Behind the simplicity of tossing coins there is a beautiful universe of mathematical surprises.</p>
<pre>
library(dplyr)
library(ggplot2)
library(scales)
tosses=12
iter=10000
results=data.frame(nmax=numeric(0), count=numeric(0), iter=numeric(0))
tmp=data.frame(nmax=numeric(0))
for (j in 1:iter)
{
data.frame(x=sample(c(-1,1), size=tosses, replace=TRUE)) %>%
add_rownames(var = &quot;n&quot;) %>%
mutate(cumsum = cumsum(x)) %>% filter(cumsum==0) %>%
summarize(nmax=max(as.numeric(n))) %>% rbind(tmp)->tmp
}
tmp %>%
group_by(nmax) %>%
summarize(count=n()) %>%
mutate(nmax=ifelse(is.finite(nmax), nmax, 0), iter=iter) %>%
rbind(results)->results
opts=theme(
panel.background = element_rect(fill=&quot;darkolivegreen1&quot;),
panel.border = element_rect(colour=&quot;black&quot;, fill=NA),
axis.line = element_line(size = 0.5, colour = &quot;black&quot;),
axis.ticks = element_line(colour=&quot;black&quot;),
panel.grid.major = element_line(colour=&quot;white&quot;, linetype = 1),
panel.grid.minor = element_blank(),
axis.text.y = element_text(colour=&quot;black&quot;),
axis.text.x = element_text(colour=&quot;black&quot;),
text = element_text(size=20),
legend.key = element_blank(),
plot.title = element_text(size = 30)
)
ggplot(results, aes(x=nmax, y=count/iter)) +
geom_line(size=2, color=&quot;green4&quot;)+
geom_point(size=8, fill=&quot;green4&quot;, colour=&quot;darkolivegreen1&quot;,pch=21)+
scale_x_continuous(breaks = seq(0, tosses, by=2))+
scale_y_continuous(labels=percent, limits=c(0, .25))+
labs(title=&quot;What happens when you toss a coin 12 times?&quot;,
x=&quot;Last throwing where cumulated #tails = #heads&quot;,
y=&quot;Probability (estimated)&quot;)+opts
</pre>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aschinchon.wordpress.com/2880/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/aschinchon.wordpress.com/2880/" border="0"></a> <img height="1" alt="" width="1" src="https://pixel.wp.com/b.gif?host=aschinchon.wordpress.com&blog=62377824&%23038;post=2880&%23038;subd=aschinchon&%23038;ref=&%23038;feed=1" border="0"></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/amazing-things-that-happen-when-you-toss-a-coin-12-times/"></div></div><div><a data-url="http://www.r-bloggers.com/amazing-things-that-happen-when-you-toss-a-coin-12-times/" data-counturl="http://www.r-bloggers.com/amazing-things-that-happen-when-you-toss-a-coin-12-times/" data-count="vertical" data-text="Amazing Things That Happen When You Toss a Coin 12 Times" 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/06/19/amazing-things-that-happens-when-you-toss-a-coin-12-times/"> 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/ZOuQ4akI06M"></a></div>