Chapter 5.1 Exercise 15

Write a program for the user to input n, p, j and have the program print out the exact value of \(b(n, p, k)\) and the Poisson approximation to this value.

binom_poisson <- function(n,p,j){
  bin <- choose(n,j)*p^j*(1-p)^(n-j)
  lam <- p*n
  poiss <- (exp(1)^(-lam)*lam^j)/factorial(j)
  print(paste("The exact value of b(", n, ", ", p, ", ", j, 
              ") is: ", round(bin, 6), sep = ""))
  print(paste("The Poisson approximation to this value is: ",
              round(poiss, 6), sep = ""))
  cat("\n")
}
for(j in seq(0,8)){
  binom_poisson(100, .01, j)
}
## [1] "The exact value of b(100, 0.01, 0) is: 0.366032"
## [1] "The Poisson approximation to this value is: 0.367879"
## 
## [1] "The exact value of b(100, 0.01, 1) is: 0.36973"
## [1] "The Poisson approximation to this value is: 0.367879"
## 
## [1] "The exact value of b(100, 0.01, 2) is: 0.184865"
## [1] "The Poisson approximation to this value is: 0.18394"
## 
## [1] "The exact value of b(100, 0.01, 3) is: 0.060999"
## [1] "The Poisson approximation to this value is: 0.061313"
## 
## [1] "The exact value of b(100, 0.01, 4) is: 0.014942"
## [1] "The Poisson approximation to this value is: 0.015328"
## 
## [1] "The exact value of b(100, 0.01, 5) is: 0.002898"
## [1] "The Poisson approximation to this value is: 0.003066"
## 
## [1] "The exact value of b(100, 0.01, 6) is: 0.000463"
## [1] "The Poisson approximation to this value is: 0.000511"
## 
## [1] "The exact value of b(100, 0.01, 7) is: 0.000063"
## [1] "The Poisson approximation to this value is: 0.000073"
## 
## [1] "The exact value of b(100, 0.01, 8) is: 0.000007"
## [1] "The Poisson approximation to this value is: 0.000009"

The cheating way to do it…

bin_poiss <- function(n,p,j){
  bin <- dbinom(j, n, p) 
  lam <- p*n
  poiss <- dpois(j, lam)
  print(paste("The exact value of b(", n, ", ", p, ", ", j, 
              ") is: ", round(bin, 6), sep = ""))
  print(paste("The Poisson approximation to this value is: ",
              round(poiss, 6), sep = ""))
  cat("\n")
}
for(j in seq(0,8)){
  bin_poiss(100, .01, j)
}
## [1] "The exact value of b(100, 0.01, 0) is: 0.366032"
## [1] "The Poisson approximation to this value is: 0.367879"
## 
## [1] "The exact value of b(100, 0.01, 1) is: 0.36973"
## [1] "The Poisson approximation to this value is: 0.367879"
## 
## [1] "The exact value of b(100, 0.01, 2) is: 0.184865"
## [1] "The Poisson approximation to this value is: 0.18394"
## 
## [1] "The exact value of b(100, 0.01, 3) is: 0.060999"
## [1] "The Poisson approximation to this value is: 0.061313"
## 
## [1] "The exact value of b(100, 0.01, 4) is: 0.014942"
## [1] "The Poisson approximation to this value is: 0.015328"
## 
## [1] "The exact value of b(100, 0.01, 5) is: 0.002898"
## [1] "The Poisson approximation to this value is: 0.003066"
## 
## [1] "The exact value of b(100, 0.01, 6) is: 0.000463"
## [1] "The Poisson approximation to this value is: 0.000511"
## 
## [1] "The exact value of b(100, 0.01, 7) is: 0.000063"
## [1] "The Poisson approximation to this value is: 0.000073"
## 
## [1] "The exact value of b(100, 0.01, 8) is: 0.000007"
## [1] "The Poisson approximation to this value is: 0.000009"