Chapter DISTRIBUTIONS AND DENSITIES
Exercise 15 Page 199
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.
Solution
Binomial probabilities are represented as
\[ f(j,n,p) = Pr(j;n,p) = Pr(X = j) = {n \choose j}p^{j}(1 - p)^{n - j} \]
Where \[ {n \choose j} = n!/j!(n - j)! \]
In R, this is calculated by pbinom() function
And, Poisson approximation is represented as
\[ P(X \le j) = \sum_{j=0}^{j} \frac{e^{-j} j^{j}}{j!} \] In R, this is calculated by ppois() function
Solution
cal_bin_pois <- function(n,p,j) { # Function to calculate poisson approximation of binomial probabilities
# n independent trials in an experiment
# The probability of a successful trial is p
# j is number of successful events
pbio <- round(pbinom(j, n, p),4)
pois <- round(ppois(j, lambda = (n * p)),4)
me <- n * p
se <- sqrt(n * (1 - p) * j)
nprob <- round(pnorm(j, mean = me, sd = se),4)
print(paste("Calculated normal approximation", nprob))
print(paste("Calculated binomial probability", pbio))
print(paste("Calculated poisson approximation", pois))
}
Testing
## [1] "Calculated normal approximation 0.4856"
## [1] "Calculated binomial probability 0.2189"
## [1] "Calculated poisson approximation 0.2202"
## [1] "Calculated normal approximation 0.5"
## [1] "Calculated binomial probability 0.6063"
## [1] "Calculated poisson approximation 0.6063"
## [1] "Calculated normal approximation 0.4022"
## [1] "Calculated binomial probability 0.0023"
## [1] "Calculated poisson approximation 0.0029"