# Probability foundation

## The binomial distribution

Random draw from a binomial distribution.

# Generate 10 separate random flips with probability .3
rbinom(10, 1, .3)
##  [1] 0 0 0 1 0 0 1 1 0 1

100 random draws with 10 flips within each.

# Generate 100 occurrences of flipping 10 coins, each with 30% probability
rbinom(100, 10, .3)
##   [1] 2 6 4 6 2 3 2 4 5 2 3 3 5 1 5 2 2 5 3 3 2 4 2 5 2 3 2 4 2 5 3 3 3 1 3
##  [36] 2 4 5 2 3 4 3 4 5 2 4 3 3 3 4 5 5 4 3 1 3 4 6 4 4 4 2 3 4 0 3 3 2 3 2
##  [71] 1 1 5 3 1 1 4 2 2 4 1 4 4 3 5 2 4 1 4 2 5 2 3 6 3 2 1 4 3 1

$X_1\dotso n\sim\text{Binomial}(\text{size},\text{p})$

The probability of binomial random variable X with parameter 10 flips has an outcome of 5 heads.

$X\sim\text{Binomial}(10,.5)\\ Pr(X=5)$

### Calculating density of a binomial

If you flip 10 coins each with a 30% probability of coming up heads, what is the probability exactly 2 of them are heads?

# Calculate the probability that 2 are heads using dbinom
dbinom(2, 10, .3)
## [1] 0.2335
# Confirm your answer with a simulation using rbinom
mean(rbinom(10000, 10, .3) == 2)
## [1] 0.2386

### Calculating cumulative density of a binomial

If you flip ten coins that each have a 30% probability of heads, what is the probability at least five are heads?

# Calculate the probability that at least five coins are heads
1 - pbinom(4, 10, .3)
## [1] 0.1503
# Confirm your answer with a simulation of 10,000 trials
mean(rbinom(10000, 10, .3) >= 5 )
## [1] 0.151

How many trials do you really need?

# Try now with 100, 1000, 10,000, and 100,000 trials
mean(rbinom(100, 10, .3) >= 5)
## [1] 0.15
mean(rbinom(1000, 10, .3) >= 5)
## [1] 0.138
mean(rbinom(10000, 10, .3) >= 5)
## [1] 0.1539
mean(rbinom(100000, 10, .3) >= 5)
## [1] 0.1508

The more trials the better then diminishing returns sets in.

### Expected value

The expected value is the mean of the distribution.

$X\sim\text{Binomial}(\text{size},\text{p})\\ E[X]=\text{size}\cdot\text{p}\\ Var(X)=\text{size}\cdot\text{p}\cdot(1-p)$

### Calculating the expected value

What is the expected value of a binomial distribution where 25 coins are flipped, each having a 30% chance of heads?

# Calculate the expected value using the exact formula
25*.3
## [1] 7.5
# Confirm with a simulation using rbinom
mean(rbinom(10000,25,.3))
## [1] 7.505

### Calculating the variance

What is the variance of a binomial distribution where 25 coins are flipped, each having a 30% chance of heads?

# Calculate the variance using the exact formula
25*.3*(1-.3)
## [1] 5.25
# Confirm with a simulation using rbinom
var(rbinom(10000, 25,.3) )
## [1] 5.255
sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.1 LTS
##
## Matrix products: default
## BLAS: /home/michael/anaconda3/lib/R/lib/libRblas.so
## LAPACK: /home/michael/anaconda3/lib/R/lib/libRlapack.so
##
## locale:
##  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
##  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
##  [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8
##  [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C
## [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base
##
## other attached packages:
## [1] RevoUtils_11.0.1     RevoUtilsMath_11.0.0
##
## loaded via a namespace (and not attached):
##  [1] bookdown_0.7     Rcpp_0.12.18     codetools_0.2-15 digest_0.6.15
##  [5] rprojroot_1.3-2  backports_1.1.2  magrittr_1.5     evaluate_0.11
##  [9] blogdown_0.9.8   stringi_1.2.4    rmarkdown_1.10   tools_3.5.1
## [13] stringr_1.3.1    xfun_0.4.11      yaml_2.2.0       compiler_3.5.1
## [17] htmltools_0.3.6  knitr_1.20

# References

R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.