Question 3
To draw a graph of the binomial distribution and add vertical lines at points μ±kσ for (k = 1, 2, 3), we need to calculate the mean (\mu) and standard deviation (\sigma) of the distribution:
For a binomial distribution with parameters (n) and (p):
[ \mu = np ]
[ \sigma = \sqrt{np(1-p)} ]
Here, we assume (n = 50) and (p = 0.5), but you need to substitute these with the parameters from your specific context if different.
Steps:
- Calculate (\mu) and (\sigma).
- Generate the binomial distribution probabilities for (X = 5) to (X = 35).
- Plot these probabilities.
- Add vertical lines at (\mu \pm \sigma), (\mu \pm 2\sigma), and (\mu \pm 3\sigma).
# Required libraries
library(ggplot2)
library(dplyr)
# Parameters
n <- 50 # Replace with your value if different
p <- 0.5 # Replace with your value if different
# Calculations
mu <- n * p
sigma <- sqrt(n * p * (1 - p))
x_values <- 5:35
probs <- dbinom(x_values, size = n, prob = p)
# Create data frame
data <- data.frame(X = x_values, Probability = probs)
# Plot
ggplot(data, aes(x = X, y = Probability)) +
geom_segment(aes(xend = X, yend = 0)) +
geom_vline(xintercept = mu + sigma, linetype = "solid", color = "red") +
geom_vline(xintercept = mu - sigma, linetype = "solid", color = "red") +
geom_vline(xintercept = mu + 2 * sigma, linetype = "dashed", color = "blue") +
geom_vline(xintercept = mu - 2 * sigma, linetype = "dashed", color = "blue") +
geom_vline(xintercept = mu + 3 * sigma, linetype = "dotted", color = "green") +
geom_vline(xintercept = mu - 3 * sigma, linetype = "dotted", color = "green") +
ggtitle("Binomial Distribution with Vertical Lines at $\mu \pm k\sigma$") +
xlab("X") + ylab("Probability")
Question 4
4A: Probability of Observing 30 Heads or Fewer
# Parameters
n <- 100
p <- 0.5
# Cumulative probability P(X <= 30)
prob_at_most_30_heads <- pbinom(30, size = n, prob = p)
prob_at_most_30_heads
4B: Evidence Against the Coin Being Fair
Response: If the computed probability in 4A is very low (close to 0), it suggests that observing 30 or fewer heads in 100 tosses is a rare event under the assumption that the coin is fair. Since we observed 35 heads, which is still lower than the expected 50 heads for a fair coin, this provides evidence against the coin being fair and suggests that the coin might be biased.
Question 5
5A: Create Tibble
library(tibble)
# Range of n
n_values <- seq(1000, 25000, 5)
p <- 0.25
# Function to compute the required fields
compute_bin_stats <- function(n, p) {
mu <- n * p
sigma <- sqrt(n * p * (1 - p))
q_90 <- qbinom(0.90, size = n, prob = p)
z <- (q_90 - mu) / sigma
prob <- 1 - pbinom(q_90 - 1, size = n, prob = p)
return(c(n, mu, sigma, q_90, z, prob))
}
# Create tibble
binomial_tibble <- tibble::tibble(
n = n_values,
stats = purrr::map(n_values, ~ compute_bin_stats(.x, p))
)
# Unnest the stats column
binomial_tibble <- binomial_tibble %>%
unnest_wider(stats) %>%
rename(mu = `2`, sigma = `3`, q.90 = `4`, z = `5`, prob = `6`)
# Filter for multiples of 5000
binomial_tibble %>%
filter(n %% 5000 == 0)
5B: Line Plot for Relationship between (n) and (z)
# Plot
ggplot(binomial_tibble, aes(x = n, y = z)) +
geom_line() +
geom_smooth(se = FALSE) +
ggtitle("Relationship between n and z") +
xlab("n") + ylab("z")
Response: The plot shows that as (n) increases, (z) stabilizes to a particular value. This suggests that for larger (n), the standardized z-score becomes more consistent.
5C: Average Z-Score for (n > 15,000)
# Calculate average z for n > 15000
mean(binomial_tibble %>% filter(n > 15000) %>% pull(z))
5D: Plot Right Tail Probability
# Plot
ggplot(binomial_tibble, aes(x = n, y = prob)) +
geom_line() +
ggtitle("Right Tail Probability vs n") +
xlab("n") + ylab("Right Tail Probability")
5E: Average Right Tail Probability for (n > 15,000)
# Calculate average right tail probability for n > 15000
mean(binomial_tibble %>% filter(n > 15000) %>% pull(prob))