Skip to content

Instantly share code, notes, and snippets.

@johnmackintosh
Created October 30, 2016 08:53
Show Gist options
  • Save johnmackintosh/e7a50839faf864e8abc0042bb5cdd8b3 to your computer and use it in GitHub Desktop.
Save johnmackintosh/e7a50839faf864e8abc0042bb5cdd8b3 to your computer and use it in GitHub Desktop.
Probability of n consecutive heads in a row from successive coin flips
library(ggplot2)
library(ggrepel)
library(scales)
library(ggExtra)
library(ggthemes)
n<- c(1:10)
flips<- 2*(2^n-1) #number of coin flips required
prob <- 0.5^n # probability of achieving n in a row
percentage<-(round(prob,4)*100)
data<- cbind.data.frame(n,flips,prob,percentage)
p<- ggplot(data,aes(n,flips))+
geom_bar(stat="identity",colour= "steelblue",fill="steelblue")+
scale_x_discrete(limits= n)+
scale_y_discrete(limits="")+
theme_tufte(base_size=10)+
removeGridX()+
ggtitle( "Number of coin flips required to see N heads in a row")+
labs(x= "N (Number of consecutive heads desired)",y="Number of coin flips")+
geom_text_repel(data=data,aes(n,flips),label=flips,size=3)
p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment