Skip to content

Instantly share code, notes, and snippets.

@daviddalpiaz
Last active October 21, 2016 14:01
Show Gist options
  • Save daviddalpiaz/2f46d9f90814c01e86064b73f9ffeeef to your computer and use it in GitHub Desktop.
Save daviddalpiaz/2f46d9f90814c01e86064b73f9ffeeef to your computer and use it in GitHub Desktop.
Plots to show Poisson approximation to binomial.
plot_pois = function(lambda = 1.4) {
stop_graph = max(15, ceiling(lambda + 3 * sqrt(lambda)))
x = 0:stop_graph
fx = dpois(x, lambda = lambda)
barplot(fx, names.arg = x, space = 0,
xlab = "values", ylab = "probabilites",
col = "darkorange", border = "dodgerblue",
main = "Poisson Probabilities")
}
plot_binom = function(n = 10, p = 0.5) {
stop_graph = max(15, ceiling(n * p + 3 * sqrt(n * p)))
x = 0:stop_graph
fx = dbinom(x, size = n, prob = p)
barplot(fx, names.arg = x, space = 0,
xlab = "values", ylab = "probabilites",
col = "darkorange", border = "dodgerblue",
main = "Binomial Probabilities")
}
n = 50
p = 0.01
lambda = n * p
par(mfrow = c(2, 1))
plot_pois(lambda = lambda)
plot_binom(n = n, p = p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment