policy <- readRDS ("HW - policy.rds")
n <- 2000
x <- 5
p0 <- 0.30
N_pop <- 43000


cat("Part (a): Binomial Model Assumptions\n")
## Part (a): Binomial Model Assumptions
cat("1) Fixed number of trials: n =", n, "\n")
## 1) Fixed number of trials: n = 2000
cat("2) Two outcomes per trial (problem / no problem)\n")
## 2) Two outcomes per trial (problem / no problem)
cat("3) Constant probability of problem: p =", p0, "\n")
## 3) Constant probability of problem: p = 0.3
cat("4) Independent trials (sampling fraction < 10%)\n\n")
## 4) Independent trials (sampling fraction < 10%)
sampling_fraction <- n / N_pop

cat("Sampling fraction =", round(sampling_fraction, 4), "→ Independence assumption satisfied.\n\n")
## Sampling fraction = 0.0465 → Independence assumption satisfied.
mu <- n * p0
sigma <- sqrt(n * p0 * (1 - p0))
z <- (x + 0.5 - mu) / sigma
p_binom <- pbinom(x, n, p0)
p_norm <- pnorm(z)

cat("Part (b): Probability of finding 5 or fewer cars with defects if p = 0.30\n")
## Part (b): Probability of finding 5 or fewer cars with defects if p = 0.30
cat("Mean =", mu, " SD =", round(sigma, 2), "\n")
## Mean = 600  SD = 20.49
cat("Observed =", x, " → Z =", round(z, 2), "\n")
## Observed = 5  → Z = -29.01
cat("Approx. P(X ≤ 5) =", format(p_binom, scientific = TRUE), "\n")
## Approx. P(X ≤ 5) = 6.060865e-298
cat("Normal approximation =", format(p_norm, scientific = TRUE), "\n\n")
## Normal approximation = 2.560565e-185
test_exact <- binom.test(x, n, p0)
p_hat <- x / n

cat("Part (c): Test whether 30% is plausible given the sample\n")
## Part (c): Test whether 30% is plausible given the sample
cat("Sample proportion =", round(p_hat * 100, 3), "%\n")
## Sample proportion = 0.25 %
cat("Exact p-value =", format(test_exact$p.value, scientific = TRUE), "\n")
## Exact p-value = 9.385763e-298
cat("95% Confidence Interval for true p =", paste0(round(100 * test_exact$conf.int[1:2], 3), "%"), "\n")
## 95% Confidence Interval for true p = 0.081% 0.582%
cat("Conclusion: The observed rate (", round(p_hat * 100, 3), "%) is FAR below 30%.\n")
## Conclusion: The observed rate ( 0.25 %) is FAR below 30%.
cat("We reject the claim that 30% of cars have wiring problems.\n")
## We reject the claim that 30% of cars have wiring problems.