library(tidyverse)

# Example - Normal Approximation to a Binomial Distribution
########################################################################################

num_rolls <- 60   # Compare with 10, where the normal curve is not a good fit

# Probabilities for the number of 1's in num_rolls of a fair die
rolls <- tibble(
  outcomes = 0:num_rolls,
  prob = dbinom(0:num_rolls, size=num_rolls, prob=1/6)
)

# Normal distribution with the same mean and standard deviation as the binomial
mu <- num_rolls*(1/6)
sigma <- sqrt(num_rolls*(1/6)*(5/6))

ggplot(data = rolls, mapping = aes(x = outcomes, y = prob)) +
  geom_col() +
  stat_function(fun = dnorm, args = list(mean = mu, sd = sigma)) +
  labs(y = "Probability", x = paste("X = Number of 1's in", num_rolls, "rolls of a die"))

# Conditions for the normal approximation: both should be at least 10
num_rolls*(1/6)   # np
num_rolls*(5/6)   # n(1-p)


# Example - CLT for Proportions: Rolling a Die 60 Times
########################################################################################

SE <- sqrt(((1/6)*(5/6))/60)
SE

# z-score for a lucky game with 15 1's in 60 rolls: p_hat = 15/60 = .25
(.25 - 1/6)/SE
