---
title: "Homework: Sample Size Calculation and Coverage"
format:
  html:
    theme: [default, "class.scss"]
    callout-appearance: simple
    callout-icon: false
tlda-answer-baseline: "homework-sample-size-coverage.qmd.support/baseline.txt"
filters:
  - "solution-callout.lua"
  - "homework-sample-size-coverage.qmd.support/answer-placement-warning.lua"
---

<!-- Sources:
  Problem 1 (Interval Calibration): adapted from exams/practice-midterm-1.qmd Problem 3 (lines 206-299)
  Problem 2 (Sample Size): adapted from chapters/chapter-variance-and-sample-size.qmd sample size section
  Problem 3 (Coverage Checking): pattern from homework/homework-calibration.qmd, homework/enrichment-probability.qmd
  Design notes from scratch/homework-sample-size-coverage.qmd
-->

{{< include shared-code.qmd >}}
{{< include ga-turnout.qmd >}}

# Problem 1: Interval Calibration

Suppose we're estimating the proportion $\theta$ of Georgia's registered voters who will turn out for an election, using a sample of size $n=625$ drawn with replacement. Below are three sampling distributions corresponding to three different values of $\theta$: 0.6, 0.7, and 0.8. On each, I've drawn a 95% confidence interval centered at 0.7.

```{r}
#| label: plot-coverage-across-theta
#| layout-ncol: 3
#| fig-width: 3.5
#| fig-cap:
#|   - "θ = 0.6"
#|   - "θ = 0.7"
#|   - "θ = 0.8"
#| fig-alt:
#|   - "Normal approximation to the sampling distribution when theta is 0.6, with the true value marked in green and a fixed interval centered at 0.7."
#|   - "Normal approximation to the sampling distribution when theta is 0.7, with the true value marked in green at the center of the fixed interval."
#|   - "Normal approximation to the sampling distribution when theta is 0.8, with the true value marked in green and a fixed interval centered at 0.7."

n = 625
w = 1.96 * sqrt(0.7 * 0.3 / n)
interval.center = 0.7
x = seq(0, 1, length.out = 1000)

scale = list(scale_x_continuous(limits=c(0.5, 0.9), breaks=seq(0.5, 0.9, by=0.1)),
             scale_y_continuous(limits=c(0, 25)),
             labs(x='', y=''))

theta = 0.6
ggplot() +
  geom_line(aes(x=x, y=dnorm(x, mean=theta, sd=sqrt(theta*(1-theta)/n)))) +
  geom_vline(xintercept=theta, color='green', alpha=.5) +
  geom_pointrange(aes(x=interval.center, xmin=interval.center-w/2, xmax=interval.center+w/2, y=1)) +
  scale

theta = 0.7
ggplot() +
  geom_line(aes(x=x, y=dnorm(x, mean=theta, sd=sqrt(theta*(1-theta)/n)))) +
  geom_vline(xintercept=theta, color='green', alpha=.5) +
  geom_pointrange(aes(x=interval.center, xmin=interval.center-w/2, xmax=interval.center+w/2, y=1)) +
  scale

theta = 0.8
ggplot() +
  geom_line(aes(x=x, y=dnorm(x, mean=theta, sd=sqrt(theta*(1-theta)/n)))) +
  geom_vline(xintercept=theta, color='green', alpha=.5) +
  geom_pointrange(aes(x=interval.center, xmin=interval.center-w/2, xmax=interval.center+w/2, y=1)) +
  scale
```

The [green vertical line]{.target} marks the true value of $\theta$ in each case.

::: {#exr-hw4-1a .callout-exercise}
### Part A
For which of these three values of $\theta$ does the interval contain the true value? Looking at the sampling distributions, roughly what fraction of intervals constructed this way (centered at $\hat\theta$ with the same width) would contain $\theta$ in each case?
:::

::: {#ans-exr-hw4-1a .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

::: {#exr-hw4-1b .callout-exercise}
### Part B
If you wanted to have 95% coverage no matter which of these three $\theta$ values was true, would you need to make your interval wider, narrower, or keep it the same? Explain briefly.
:::

::: {#ans-exr-hw4-1b .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

::: {#exr-hw4-1c .callout-exercise}
### Part C
Suppose you wanted to achieve 99% coverage instead of 95%. By what factor would you need to multiply the interval width?

*Hint*: For a normal distribution, 95% of the probability is within $\pm 1.96$ standard deviations of the mean, and 99% is within $\pm 2.58$ standard deviations.
:::

::: {#ans-exr-hw4-1c .callout-answer .callout-note title="Template"}

*(your answer here)*

:::


# Problem 2: Sample Size Calculation

Suppose you're planning a survey to estimate the proportion of Georgia's registered voters who will turn out for an election. You want your 95% confidence interval to have a width of no more than $\pm .02$, i.e. $\pm$ 2 percentage points.

::: {#exr-hw4-2a .callout-exercise}
### Part A
Using the normal approximation, how large a sample do you need? You don't know $\theta$, so use the worst case.
:::

::: {#ans-exr-hw4-2a .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

::: {#exr-hw4-2b .callout-exercise}
### Part B
Suppose you've already done a pilot study with $n=625$ people and found that your 95% interval has a half-width of about $.04$. You want to cut the half-width in half, to $.02$. How large a sample do you need?

Do this the easy way: compare the width you have to the width you want.
:::

::: {#ans-exr-hw4-2b .callout-answer .callout-note title="Template"}

*(your answer here)*

:::


# Problem 3: Checking Coverage

Now let's verify that all this works in practice.

::: {#exr-hw4-3a .callout-exercise}
### Part A
Write a function `interval` that takes a sample `Y` and returns a 95% confidence interval for $\theta$ using the normal approximation. It should return a vector of length 2: the lower and upper bounds.
:::

::: {#ans-exr-hw4-3a .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

::: {#exr-hw4-3b .callout-exercise}
### Part B
Using the GA turnout population, draw 10,000 samples of size $n=625$ with replacement. For each sample, compute your interval. What fraction of these intervals contain the true value of $\theta$?
:::

::: {#ans-exr-hw4-3b .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

::: {#exr-hw4-3c .callout-exercise}
### Part C
Repeat Part B, but now also compute a 95% bootstrap interval for each sample. Compare the coverage of the normal approximation interval and the bootstrap interval.
:::

::: {#ans-exr-hw4-3c .callout-answer .callout-note title="Template"}

*(your answer here)*

:::
