Skip to content

Instantly share code, notes, and snippets.

@joftius
Created August 25, 2022 17:36
Show Gist options
  • Save joftius/d595109f0404a8fad08aadfb5bafb062 to your computer and use it in GitHub Desktop.
Save joftius/d595109f0404a8fad08aadfb5bafb062 to your computer and use it in GitHub Desktop.
Visualizing the Normal approximation to the Binomial
library(tidyverse)
df <- data.frame(x = 0:20, y = dbinom(0:20, size = 20, prob = .4))
# Plot pdf of Bin(20, 0.4)
ggplot(df, aes(x, y)) +
geom_bar(stat = "identity", alpha = .4) +
geom_bar(stat = "identity", alpha = .9, data = subset(df, x > 4 & x <= 7)) +
ylab("Binomial probability") +
theme_minimal()
# Calculate binomial probability
pbinom(7, 20, .4) - pbinom(4, 20, .4)
# Normal approximation to Binomial
ggplot(df, aes(x, y)) +
stat_function(fun = dnorm, args = list(mean = 20*.4, sd = sqrt(20*.4*.6))) +
geom_bar(stat = "identity", alpha = .4) + ylab("Probability") +
stat_function(fun = dnorm, args = list(mean = 20*.4, sd = sqrt(20*.4*.6)),
xlim = c(4.5, 7.5), geom = "area", alpha = .6, fill = "blue") +
theme_minimal() + ggtitle("Normal approximation (blue area)")
# Calculate normal probability
pnorm(7.5, mean = 20*.4, sd = sqrt(20*.4*.6)) -
pnorm(4.5, mean = 20*.4, sd = sqrt(20*.4*.6))
# Visualize both
ggplot(df, aes(x, y)) +
stat_function(fun = dnorm, args = list(mean = 20*.4, sd = sqrt(20*.4*.6))) +
geom_bar(stat = "identity", alpha = .4) + ylab("Probability") +
geom_bar(stat = "identity", fill = "red", alpha = .9, data = subset(df, x > 4 & x <= 7)) +
stat_function(fun = dnorm, args = list(mean = 20*.4, sd = sqrt(20*.4*.6)),
xlim = c(4.5, 7.5), geom = "area", alpha = .6, fill = "blue") +
theme_minimal() + ggtitle("Normal approximation (blue area) to Binomial (red area)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment