Write a program for the user to input values for \(n\), \(p\), and \(k\), and have the program print out the exact value of \(b(n,p,k)\) and the Poisson approximation to this value.
binomial_coef <- function(n, k) {
return(factorial(n) / (factorial(k) * factorial(n - k)))
}
binomial_dist <- function(n, p, k) {
return(binomial_coef(n, k) * p^k * (1 - p)^(n - k))
}
poisson_approximation <- function(n, p, k) {
lambda <- n * p
return(exp(-lambda) * (lambda^k) / factorial(k))
}
poisson_funct <- function(n, p, k) {
b <- binomial_dist(n, p, k)
cat("Exact b(n, p, k): ", b, "\n")
poisson <- poisson_approximation(n, p, k)
cat("Poisson approx: ", poisson, "\n")
}
poisson_funct(10, 0.5, 3)
## Exact b(n, p, k): 0.1171875
## Poisson approx: 0.1403739
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.