1 Factorials

factorial (10)
## [1] 3628800

Find number of combindations of choosing 40 out of 400 ##########

choose (400,40)
## [1] 1.970337e+55
choose (49, 6)
## [1] 13983816

Find number of permuation of choosing 40 out of 400 #############

choose (400, 40) * factorial (40)
## [1] 1.607628e+103

2 Binomial probability distribution

n <- 20; p <- 0.1
x <- 0:n

binom.x <- dbinom (x, size = n, p = p) # compute binomial probability
binom.x
##  [1] 1.215767e-01 2.701703e-01 2.851798e-01
##  [4] 1.901199e-01 8.977883e-02 3.192136e-02
##  [7] 8.867045e-03 1.970454e-03 3.557765e-04
## [10] 5.270763e-05 6.442043e-06 6.507115e-07
## [13] 5.422595e-08 3.707758e-09 2.059865e-10
## [16] 9.154957e-12 3.178805e-13 8.310600e-15
## [19] 1.539000e-16 1.800000e-18 1.000000e-20
breaks <-  c(x - 0.5, n + 0.5)
breaks
##  [1] -0.5  0.5  1.5  2.5  3.5  4.5  5.5  6.5
##  [9]  7.5  8.5  9.5 10.5 11.5 12.5 13.5 14.5
## [17] 15.5 16.5 17.5 18.5 19.5 20.5
hist.binom <- list (breaks = breaks, counts = binom.x) 
class (hist.binom) <- "histogram"
plot (hist.binom, main = sprintf("Binomial (n=%g, p=%.1f)", n,p), xlab = "x")

n <- 20; p <- 0.3
x <- 0:n


binom.x <- dbinom (x, size = n, p = p) # compute binomial probability
binom.x
##  [1] 7.979227e-04 6.839337e-03 2.784587e-02
##  [4] 7.160367e-02 1.304210e-01 1.788631e-01
##  [7] 1.916390e-01 1.642620e-01 1.143967e-01
## [10] 6.536957e-02 3.081708e-02 1.200665e-02
## [13] 3.859282e-03 1.017833e-03 2.181070e-04
## [16] 3.738977e-05 5.007558e-06 5.049639e-07
## [19] 3.606885e-08 1.627166e-09 3.486784e-11
breaks <-  c(x - 0.5, n + 0.5)
breaks
##  [1] -0.5  0.5  1.5  2.5  3.5  4.5  5.5  6.5
##  [9]  7.5  8.5  9.5 10.5 11.5 12.5 13.5 14.5
## [17] 15.5 16.5 17.5 18.5 19.5 20.5
hist.binom <- list (breaks = breaks, counts = binom.x) 
class (hist.binom) <- "histogram"
plot (hist.binom, main = sprintf("Binomial (n=%g, p=%.1f)", n,p), xlab = "x")

n <- 20; p <- 0.5
x <- 0:n

binom.x <- dbinom (x, size = n, p = p) # compute binomial probability
binom.x
##  [1] 9.536743e-07 1.907349e-05 1.811981e-04
##  [4] 1.087189e-03 4.620552e-03 1.478577e-02
##  [7] 3.696442e-02 7.392883e-02 1.201344e-01
## [10] 1.601791e-01 1.761971e-01 1.601791e-01
## [13] 1.201344e-01 7.392883e-02 3.696442e-02
## [16] 1.478577e-02 4.620552e-03 1.087189e-03
## [19] 1.811981e-04 1.907349e-05 9.536743e-07
breaks <-  c(x - 0.5, n + 0.5)
breaks
##  [1] -0.5  0.5  1.5  2.5  3.5  4.5  5.5  6.5
##  [9]  7.5  8.5  9.5 10.5 11.5 12.5 13.5 14.5
## [17] 15.5 16.5 17.5 18.5 19.5 20.5
hist.binom <- list (breaks = breaks, counts = binom.x) 
class (hist.binom) <- "histogram"
plot (hist.binom, main = sprintf("Binomial (n=%g, p=%.1f)", n,p), xlab = "x")

n <- 20; p <- 0.7
x <- 0:n

binom.x <- dbinom (x, size = n, p = p) # compute binomial probability
binom.x
##  [1] 3.486784e-11 1.627166e-09 3.606885e-08
##  [4] 5.049639e-07 5.007558e-06 3.738977e-05
##  [7] 2.181070e-04 1.017833e-03 3.859282e-03
## [10] 1.200665e-02 3.081708e-02 6.536957e-02
## [13] 1.143967e-01 1.642620e-01 1.916390e-01
## [16] 1.788631e-01 1.304210e-01 7.160367e-02
## [19] 2.784587e-02 6.839337e-03 7.979227e-04
breaks <-  c(x - 0.5, n + 0.5)
breaks
##  [1] -0.5  0.5  1.5  2.5  3.5  4.5  5.5  6.5
##  [9]  7.5  8.5  9.5 10.5 11.5 12.5 13.5 14.5
## [17] 15.5 16.5 17.5 18.5 19.5 20.5
hist.binom <- list (breaks = breaks, counts = binom.x) 
class (hist.binom) <- "histogram"
plot (hist.binom, main = sprintf("Binomial (n=%g, p=%.1f)", n,p), xlab = "x")

3 Hypergeometric Distribution for 6/49

Calculate the PMFs in three ways

lotto.hyper <- dhyper(0:6, 6, 43, 6) # Exact Method
lotto.binomial <- dbinom (0:6, 6, 6/49)
lotto.poisson <- dpois(0:6, lambda = 6*6/49)

Comparision of probability functions

round(data.frame (x=0:6,  lotto.hyper, lotto.binomial, lotto.poisson), digits = 8)
##   x lotto.hyper lotto.binomial lotto.poisson
## 1 0  0.43596498     0.45670341    0.47965227
## 2 1  0.41301945     0.38235634    0.35239759
## 3 2  0.13237803     0.13338012    0.12945217
## 4 3  0.01765040     0.02481491    0.03170257
## 5 4  0.00096862     0.00259691    0.00582292
## 6 5  0.00001845     0.00014494    0.00085561
## 7 6  0.00000007     0.00000337    0.00010477
plot(0:6, lotto.hyper, type = "h", ylim = c(0, 0.5),
     main = "Distribution of the Number of Winning Numbers in 6/49",
     xlab = "Number of Winning Numbers")
lines(0:6+0.1, lotto.binomial, type = "h", col = "red")
lines(0:6+0.2, lotto.poisson, type = "h", col = "blue")

legend("topright",
       legend = c("Hypergeometric Calculation",
                  "Binomial Calculation", 
                  "Poisson Calculation"), 
       lty = c(1,1,1),
       col = c(1,2,"blue"))