---
title: "Homework — Random Variables, Moments, and the Standard Deviation"
execute:
  eval: true
format:
  html:
    theme: [default, "class.scss"]
    callout-appearance: simple
    callout-icon: false
tlda-answer-baseline: "homework-random-variables-and-moments.qmd.support/baseline.txt"
filters:
  - "solution-callout.lua"
  - "homework-random-variables-and-moments.qmd.support/answer-placement-warning.lua"
---
{{< include shared-code.qmd >}}

{{< include _submit-reminder.qmd >}}

```{r}
#| label: setup
#| echo: false
library(ggplot2)
library(purrr)
library(tidyverse)
library(scales)
theme_set(theme_minimal())
```

```{r}
#| label: setup-latex-device
#| echo: false
if (knitr::is_latex_output()) {
    knitr::opts_chunk$set(dev = "cairo_pdf")
}
theme_set(theme_minimal())
```

::: hidden
$$
\newcommand{\thetaprior}{\theta^{\text{prior}}}
\newcommand{\nprior}{n^{\text{prior}}}
\newcommand{\yprior}{y^{\text{prior}}}
$$
:::

<!-- ============================================================================
  HOMEWORK STUB for the RV/Moments week. Created 2026-06-19.
  Rationale + portfolio logic: scratch/hw-rv-moments-PLAN.md
  Design: scratch/design-probability-week-rv-moments.md

  STRUCTURE: floor -> ceiling. Routine fluency first (keeps the bottom third afloat),
  the conceptually hard / fun problems last. Skip's instinct = the lecture; the homework
  carries the intro-class ballast (fluency, repetition) that the lecture skips.

  This is a SKELETON stub: HOLE = new writing; PULL = lift from hw5 when we reshape it
  (the kept hw5 sections move here; Markov/Chebyshev derivations went up to Ch2).
============================================================================ -->

# Warm-up: Computing Moments

<!-- MECHANICAL FLOOR — ONE UNIFIED CONTEXT (Skip 2026-06-19, refine into final voice).
     Structure (Skip): don't scatter a separate bait per problem — run the WHOLE set through
     ONE rich context, and let the with-vs-without-replacement CONTRAST surface every trap by
     itself. The setup contains the cue ("without replacement") but never flags it; noticing the
     dependence is the skill. So the floor discriminates without being mindless, and the student
     stays in one world.
     NO "name the distribution" anywhere (Skip: naming is in-name garbage / vocabulary-as-
     gatekeeping — the class rewards the reasoning, never the label). Frame everything as
     BEHAVIOR (do the variances add or not?), never as names.

  THE CONTEXT: "Draw 3 cards from a deck. Do everything twice — once WITH replacement
  (shuffle each card back), once WITHOUT (a normal deal). Let X = the number of hearts."

  DRAFT sub-exercises (one deck, both draw-modes):
  (1) INDICATOR + LINEARITY: write X = sum of three heart-indicators. Find E[X] for both
      draw-modes. (Same answer — linearity needs no independence. That sameness is the cue.)
  (2) VARIANCE — THE TRAP: find Var[X] WITH replacement by adding the indicators' variances.
      Try the same WITHOUT replacement. Which is legitimate? What did "without replacement"
      change, and what term is missing? (independent -> variances add; dependent -> they don't,
      and the gap is a covariance.)
  (3) E[X^2] vs (E[X])^2: back E[X^2] out of Var = E[X^2] - (E[X])^2 using (1),(2). Note they
      differ — the mini "flaw of averages."
  (4) FACTORIZATION: is E[(heart_1)(heart_2)] = E[heart_1] E[heart_2]? True WITH replacement,
      false WITHOUT — compute both; the difference IS the covariance from (2).
  (5) MARKOV/CHEBYSHEV floor rep: bound P(X >= 3) (all hearts). Sets up the flush problem later.
  (6) PROBABILITY <-> INDICATOR: (a) write P(X > 0) as an expectation of an indicator;
      (b) if E[1_{X=3}] = p, what is P(X=3)?; (c) one sentence: why is P(A) = E[1_A] not a trick?

  CAPSTONE (rigor) for the variance trap: the existing without-replacement covariance derivation,
  homework/homework-comparing-estimators.qmd @exr-covariance-with-replacement — derive
  E[(Y_i-E Y_i)(Y_j-E Y_j)] = -theta(1-theta)/(m-1) and the full Var = theta(1-theta)/n * (m-n)/(m-1).
  This is the "we'll leave deriving this for homework" promise the Ch1 variance section makes.
  ARC: discover the trap (deck) -> name the covariance (lecture) -> derive it (week3). -->

::: {#exr-deck-moments .callout-exercise}
## One Deck, Two Sampling Mechanisms

Draw three cards from a standard deck. Do every calculation twice: first replace and reshuffle
each card before the next draw; then deal three cards without replacement. Let $I_i$ indicate
that card $i$ is a heart, and let $H=I_1+I_2+I_3$.

1. Calculate $\E[H]$ in both designs. Explain why the answer is the same even though only one
   design has independent indicators.
2. Calculate $\Var(H)$ with replacement by centering and squaring out the sum.
3. Without replacement, calculate $\E[I_1I_2]$ and compare it with
   $\E[I_1]\E[I_2]$. Use the difference to calculate $\Cov(I_1,I_2)$.
4. Use the three variances and six ordered cross-terms to calculate $\Var(H)$ without
   replacement. Which design is more precise, and where does the difference enter?
5. Recover $\E[H^2]$ in both designs from your means and variances.
6. In each design, use Markov and Chebyshev to bound the probability of drawing three hearts.
   Then calculate the exact probability. What information does the exact calculation use that
   the two moment bounds discard?
:::

::: {#ans-exr-deck-moments .callout-answer .callout-note title="Template"}

*(your answer here)*

:::


# Recognition

<!-- Engine note: quick reps that keep the vocabulary live. Course-original. DRAFT: -->

<!-- DRAFT R1 (independent or not? — one-line each, say why):
     (a) The two numbers shown on a roll of two dice.
     (b) A card drawn from a deck, and the next card drawn without replacing the first.
     (c) Two separate fair coin flips.
     (d) Two players' picks in odds-and-evens when each flips a SEPARATE coin.
     (e) Two players' picks in odds-and-evens when both read the SAME coin.
     (Callbacks to the Ch1 independence example — (d)/(e) are the fair-vs-cheating pair.) -->

::: {#exr-recognize-independence .callout-exercise}
## What Is Independent?

For each pair, say whether the two random variables are independent and identify the random
mechanism that makes your answer true.

1. The numbers shown on two separately rolled dice.
2. Two successive cards dealt from one shuffled deck.
3. Two separate fair coin flips.
4. Two odds-and-evens players who each flip a separate independent fair coin.
5. Two odds-and-evens players when the second player copies the first.
6. The sum of two dice and the number on the first die.
:::

::: {#ans-exr-recognize-independence .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

<!-- R2 (name the distribution) — CUT (Skip 2026-06-19): naming distributions is in-name
     garbage / vocabulary-as-gatekeeping. The class rewards reasoning, never the label. The
     real skill (independent or not? does the variance shrink?) lives in R1 + the deck floor. -->

# Same Move, Many Surfaces

<!-- Engine 1 of repetition done right (Skip 2026-06-19): the SAME core move restaged across
     different stories, so the student practices RECOGNIZING the move, not arithmetic.
     Course-original. DRAFT: -->

<!-- DRAFT S1 (linearity on indicators — expected count, dependence-free; same one-line move x3):
     Find the expected count three ways, each by writing the count as a sum of indicators:
     (a) POLL: expected number of "yes" answers in n=625 calls, turnout theta.
     (b) CARDS: expected number of hearts in a 5-card hand. (cf. the card problem)
     (c) DICE: expected number of 6s in 10 rolls.
     Then: "You used the same one-line move all three times. In which of (a)-(c) were the
     terms independent, and did it matter for the answer?" (Punchline: linearity didn't care.) -->

<!-- DRAFT S2 (center-and-square-out — variance, same procedure x2):
     (a) Number of heads in n fair flips: center and square out to get Var = n*theta*(1-theta).
     (b) Sample mean of n iid draws with variance sigma^2: get Var = sigma^2 / n. (callback)
     Note where the cross-terms vanish (independence) — same story both times. -->

::: {#exr-variance-sums-conventional .callout-exercise}
## Variance of a Sum, the Conventional Way

Let $Y_1 \ldots Y_n$ be independent random variables with means $\mu_1 \ldots \mu_n$ and
finite variances.

1. Center $\sum_iY_i$, square out the centered sum, and take expectations term by term.
2. Explain exactly where independence is used and why every off-diagonal term is zero.
3. Conclude that $\Var(\sum_iY_i)=\sum_i\Var(Y_i)$.
4. Compare this proof with the independent-copy proof from class. Which step is genuinely
   different, and which algebra merely appears in a different place?
5. If $A,B,C$ are independent, use the operational rule for independence to explain why
   $A+B$ and $C$ are independent.
:::

::: {#ans-exr-variance-sums-conventional .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

<!-- DRAFT S3 (two moments -> a bound, same move x2):
     (a) MARKOV: X >= 0 with E[X] = 4. Bound P(X >= 16).
     (b) CHEBYSHEV: X with mean mu, sd sigma. Bound P(|X-mu| >= 2.5 sigma), and say whether
         you'd trust this bound more for a "typical" deviation or a "rare" one. (Ties to the flush.) -->


# Indicator Counting (Cards)

The indicator-counting technique in this section follows Blitzstein and Hwang, *Introduction
to Probability*, 2nd ed. (2019), Chapter 4.

::: {#exr-card-indicator-count .callout-exercise}
## Count Without Finding the Distribution

Deal five cards from a shuffled standard deck without replacement.

1. Let $H$ be the number of hearts in the hand. Write $H$ as a sum of five indicators.
2. Use linearity to calculate $\E[H]$. Why does the dependence among the five indicators not
   matter for this calculation?
3. Repeat the calculation for the number of aces.
4. Generalize both answers to a hand of $k$ cards.
5. Which corresponding variance calculation would require information that linearity alone
   does not provide?
:::

::: {#ans-exr-card-indicator-count .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

<!-- *** CITATION REQUIRED (Skip 2026-06-19): these problems ADAPT material from
     Blitzstein & Hwang, "Introduction to Probability" (Ch. 4 — indicator random variables,
     the matching problem). Cite it: add Blitzstein-Hwang to references.qmd and footnote this
     section, e.g. "The indicator-counting technique and the matching problem follow the
     treatment in Blitzstein & Hwang (2019), Ch. 4. The matching problem is classically due to
     de Montmort." Adapt the wording into Skip's voice; do NOT reproduce Blitzstein's text. ***

     The on-brand technique: write a COUNT as a sum of indicators, take linearity, IGNORE the
     dependence. Callback to the Lec3 card warm-ups (hearts/pairs), now abstract. The lesson =
     LINEARITY NEEDS NO INDEPENDENCE: expected count of dependent events for free, never
     touching the joint. DRAFT problems below. -->

<!-- PROVENANCE NOTE for the rest of this homework: the mechanical-floor drills (W1-W4), the
     same-move-many-surfaces sets, the recognition items, the storytelling problem, and the
     bias/pseudo-observations + coverage-comparison material are ORIGINAL to this course
     (no Blitzstein citation needed). Only the card indicator/matching problems above are
     Blitzstein-adapted. The flush problem reuses our own H and is original. -->


<!-- DRAFT — Problem A (expected count; dependence irrelevant):
     Deal a 5-card hand from a standard 52-card deck.
     (a) Let H = the number of hearts in the hand. Write H as a sum of indicators
         H = sum_{i=1}^5 1_{card i is a heart}.
     (b) Use linearity to find E[H]. (Note: the cards are drawn WITHOUT replacement, so the
         indicators are NOT independent. Does linearity care?)
     (c) Repeat for the number of aces. (d) Generalize to a k-card hand.
     PAYOFF to state: E[H] = 5 * 13/52, computed without ever finding the distribution of H. -->

<!-- DRAFT — Problem B (the matching problem, capstone):
     Shuffle a 52-card deck and lay the cards in a row. Say card i "matches" if the card in
     position i is the i-th card of an ordered reference deck.
     (a) Let M = number of matches; write M as a sum of indicators.
     (b) Find P(card i matches) and use linearity to get E[M].
     (c) Marvel: E[M] = 1, no matter the deck size, despite wild dependence among the matches.
     OPTIONAL stretch: why is Var[M] also ~ 1? (here the dependence DOES matter — sets up that
     expectation is easy but variance needs the cross-terms / covariances). -->

<!-- DRAFT — Problem C (FLUSH: Chebyshev is terrible at rare events). Bridges cards -> Chebyshev.
     Reuse H = # hearts in a 5-card hand (E[H]=1.25, Var[H] ~ 0.86, hypergeometric).
     (a) A heart flush is H=5. Compute P(H=5) EXACTLY: C(13,5)/C(52,5) ~ 0.0005 (~0.05%).
     (b) Now bound it with Chebyshev: P(|H-1.25| >= 3.75) <= Var[H]/3.75^2 ~ 6%.
     (c) Compare: Chebyshev says "<= ~6%", truth is ~0.05% — off by ~100x. Why so bad?
     THE ESSENTIAL FEELING TO PLANT (Skip 2026-06-19): a flush is FIVE things happening at once
     (a 5-way coincidence); variance is built from PAIRS (2nd-order info, E[Y_i Y_j]). You can't
     see a five-way coincidence through a two-way lens. The ORDER of the event must match the
     order of the information. Chebyshev nails the BULK (low-order/typical), goes silent on rare
     (high-order) events. Foreshadows the moment hierarchy (Paley-Zygmund needs the 4th moment;
     rarer tails need more). The flush is the ideal case because the truth is EXACTLY computable,
     so the gap is visible with your own eyes. -->

# Tell a Story

<!-- HOLE (storytelling — the most "Skip" problem, 2026-06-19): "Here is a (joint)
     distribution. Tell a story in which a real random variable has it." Drill that carries
     an idea — builds the y_j-vs-Y_i / what-IS-a-random-variable intuition in reverse.
     Could reuse the odds-and-evens / checkerboard joint from the Ch1 independence example. -->

::: {#exr-joint-story .callout-exercise}
## Tell a Story About a Joint Distribution

The random pair $(A,B)$ takes values in $\{1,2\}\times\{1,2\}$. Its joint probabilities are
$$
P(A=1,B=1)=P(A=2,B=2)=\frac12,
\qquad
P(A=1,B=2)=P(A=2,B=1)=0.
$$

1. Tell a concrete story whose random pair has this joint distribution.
2. Calculate the marginal distributions of $A$ and $B$.
3. Someone sees only the two margins and says both variables look fair. What did they miss?
4. Give a second joint distribution with the same margins in which $A$ and $B$ are independent.
:::

::: {#ans-exr-joint-story .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

<!-- PULL from hw5 (stays this week, Skip 2026-06-19): "Biased Estimators" +
     "Bias, Variance, and Mean Squared Error" (lines 162-345). The Bernoulli
     pseudo-observations estimator; E, bias, sd; bias/variance/MSE; optimal n_prior. -->

# Bias and Consistency

## Using Prior Information
So far, we've exclusively talked about *unbiased estimators*. That is, estimators with the property that
their expected value is equal to the estimation target.
$$
\hat\theta \qqtext{ is called unbiased if } \E[\hat{\theta}] = \theta.
$$
We say an estimator is *biased* if this isn't true. To get a sense of what that means, let's consider a simple
example of a biased estimator.
$$
\tilde{Y}_1 = \frac{1}{n+1}\cdot \{\frac{1}{2} + \sum_{i=1}^{n}Y_i\}
$$

::: {#exr-bias-1 .callout-exercise}
Calculate the expected value of the estimator $\hat\theta =\tilde{Y}_1$.
Then calculate its bias $\E[\hat\theta] - \theta$ and its
standard deviation $\sqrt{\Var[\hat\theta]}$. Report all three.
:::

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

*(your answer here)*

:::

This is a specific example of a more general estimator that integrates information from a prior study. Maybe a real study or maybe a study we're imagining. Suppose we have $\nprior$ observations $\yprior_1 \ldots \yprior_{\nprior}$ from this study^[Sometimes we call these *pseudo-observations*. This particular interpretation, in which we think of them as coming from a prior study, can be used to derive this estimator from Bayesian principles.]
$$
\thetaprior = \frac{1}{\nprior}\sum_{i=1}^{\nprior}\yprior_i.
$$

Then averaging all our observations --- from our current study and this prior one --- gives us the following estimator.

$$
\begin{aligned}
\tilde{Y}_{\nprior}
&= \frac{1}{\nprior + n} \cb*{\sum_{i=1}^{\nprior}\yprior_i + \sum_{i=1}^{n} Y_i} \\
&= \frac{1}{\nprior + n}  \cb*{ \nprior\thetaprior + n\bar Y }
\end{aligned}
$$

We treat these prior observations, and therefore their mean $\thetaprior$, as deterministic.
The simple example we started with, $\tilde{Y}_1$, is a special case where we have a single prior observation $\yprior_1 = \frac{1}{2}$.

::: {#exr-bias-general .callout-exercise}
Repeat @exr-bias-1 for $\tilde{Y}_{\nprior}$.
:::

::: {#ans-exr-bias-general .callout-answer .callout-note title="Template"}

*(your answer here)*

:::


## Visualizating the Impact of Prior Information

Lets's try to get a sense of how using prior information like this
impacts our inference. We'll do it visually. The plot below shows the sampling distributions of three estimators of a population mean $\theta$ at three sample sizes $n$: 10, 40, and 160. These are the estimators.

1. The estimator $\hat\theta_1 = \tilde{Y}_{\nprior}$ for $\thetaprior=3/4$ and $\nprior=10$ prior observations.
2. The estimator $\hat\theta_2 = \tilde{Y}_{n}$ for $\thetaprior=3/4$ and $\nprior=n$ prior observations---the bigger the sample, the more prior observations we incorporate.
3. The sample mean, $\hat\theta_3 = \bar Y$.

As usual, the estimation target $\theta$ is indicated by a green line, the sampling distribution's mean by a solid blue line, and its mean plus and minus two standard deviations by dotted blue lines. In our first exercise, we'll match estimators to pictures.

::: {#exr-bias-visualize .callout-exercise}
What is the correspondence between $\theta_1$, $\theta_2$, and $\theta_3$ described above
and 'estimator a', 'estimator b', and 'estimator c' from the plot?
:::

::: {#ans-exr-bias-visualize .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

::: {#fig-3-sampling-distributions}
```{r}
#| label: plot-prior-information-grid
#| echo: false
#| out-height: 4in
#| fig-alt: "Nine sampling distributions arranged by sample size (10, 40, and 160) and estimator (a, b, and c). A green line marks the target, a solid blue line marks each distribution's mean, and dotted blue lines mark two standard deviations on either side."
set.seed(1234)
n.prior = 10
theta.prior = .75
theta = .5

error = 2 * rnorm(100000)
bias = list(a = function(n) 0,
            b = function(n) n.prior * (theta.prior - theta) / (n.prior + n),
            c = function(n) n * (theta.prior-theta) / (n + n))                # n.prior = n
variance = list(a = function(n) theta*(1-theta)/n,
                b = function(n) theta*(1-theta) * n / (n.prior + n)^2,
                c = function(n) theta*(1-theta) * n / (n + n)^2)


plotdata = expand_grid(estimator = names(bias), n = c(10, 40, 160)) %>%
    pmap(function(estimator, n) {
        thebias = bias[[estimator]](n)
        thevar = variance[[estimator]](n)
        data.frame(estimator = estimator, n = n, muhat = thebias + error * sqrt(thevar))
    }) |> bind_rows()

summaries = plotdata |>
    group_by(n, estimator) |>
    summarize(mean = mean(muhat), sd = sd(muhat))

breaks = seq(-2/3, 2/3, by = 1/3)
signs = case_when(
    breaks < 0 ~ "-",
    breaks == 0 ~ "",
    breaks > 0 ~ "+"
)
offsets = case_when(
    breaks != 0 ~ sprintf("%d/3", 3*abs(breaks)),
    breaks == 0 ~ ""
)
labels.abstract = sprintf("θ %s %s", signs, offsets)

ggplot(plotdata) +
    geom_vline(aes(xintercept = theta), data = data.frame(theta=0), color='green',alpha=.4, linewidth=1.5) +
    geom_histogram(aes(x = muhat, y = after_stat(density)), bins = 100, alpha = .2) +
    geom_vline(aes(xintercept = mean), data = summaries, alpha=.4, color='blue') +
    geom_vline(aes(xintercept = mean + 2 * sd), data = summaries, linetype = "dotted", color='blue',alpha=.4) +
    geom_vline(aes(xintercept = mean - 2 * sd), data = summaries, linetype = "dotted", color='blue',alpha=.4) +
    scale_x_continuous(breaks = breaks, labels = labels.abstract, limits = c(-3/4,3/4)) +
    theme(axis.text.x = element_text(angle = -90, vjust = 0.5, hjust = 1)) +
    xlab("") +
    ylab("") +
    facet_grid(n ~ estimator, labeller = labeller(n = label_both, estimator = label_both))
```

:::

# Convergence in Probability

Now let's think about another notion of convergence. If an estimator's sampling distribution
ends up in the right location ($\text{bias} \to 0$) with arbitrarily little spread ($\text{variance} \to 0$),
then it makes sense that *any draw from it* will be close to the estimation target $\mu$.

Recall that you can visualize your estimator as a single dot drawn
from its sampling distribution. Like the black one below. And if you want to think of what happens if you were to repeat your survey 100 times, you can visualize the results by plotting 100 dots. Like the purple ones.
```{r}
#| label: plot-convergence-in-probability
#| fig-alt: "Sampling distribution for estimator b at sample size 40, with one black draw and 99 purple draws plotted over the histogram; a vertical line marks the distribution's mean."
K <- 100
plotdata$height <- runif(nrow(plotdata), 0, 1.2)
dotplotdata <- plotdata |> filter(n == 40 & estimator == "b")
dotplotsummaries <- summaries |> filter(n == 40 & estimator == "b")

ggplot(dotplotdata) +
    geom_histogram(aes(x = muhat, y = after_stat(density)), bins = 100, alpha = .4) +
    geom_vline(aes(xintercept = mean), data = dotplotsummaries) +
    geom_point(aes(x = muhat, y = height),
        alpha = .2,
        color = "purple", data = dotplotdata[2:K, ]
    ) +
    geom_point(aes(x = muhat, y = height),
        alpha = 1,
        color = "black", data = dotplotdata[1, ]
    ) +
    scale_x_continuous(breaks = breaks, labels = labels.abstract, limits = range(breaks)) +
    theme(axis.text.x = element_text(angle = -90, vjust = 0.5, hjust = 1)) +
    xlab("") +
    ylab("")

```


We formalize this idea by talking about *convergence in probability*. We say an estimator $\hat\mu$ converges in probability to $\mu$ if, for anyone's idea of `sufficiently close', the probability that $\hat\mu$ is sufficiently close to $\mu$ goes to one. Or equivalently, the probability that it isn't sufficiently close goes to zero.
$$
P(\lvert\hat\mu - \mu\rvert \ge \epsilon) \to 0 \qqtext{ as } n \to \infty
$$

Convergence in mean square *implies* convergence in probability. Let's see why. We'll use *Markov's inequality*.

## Markov's Inequality

Markov's inequality says that the probability a nonnegative random variable $X$
exceeds some threshold $t$ is bounded by the ratio of its expected value and $t$, i.e.,
$$ P(X \geq t) \leq \frac{\E[X]}{t}$$
Often, instead of using this to bound the random variable we're interested in directly,
e.g. $X=\lvert\hat\mu - \mu\rvert$, we use it to bound its square. $X \ge t$ if and only if $X^2 \ge t^2$, so the probability that $X \ge t$ is the same as the probability that $X^2 \ge t^2$. Applying Markov's inequality to the random variable $X^2$ gives us a bound in terms of $X$'s mean square.
$$
P(X \ge t) = P(X^2 \ge t^2) \leq \frac{\E[X^2]}{t^2} \qqtext{ e.g.}
P(\lvert\hat\mu - \mu\rvert \ge \epsilon) = P((\hat\mu - \mu)^2 \ge \epsilon^2) \leq \frac{\E[(\hat\mu - \mu)^2]}{\epsilon^2}
$$

This tells us, in terms of the mean squared error of $\hat\mu$, how likely it is that $\hat\mu$ is far from $\mu$. And, in particular, it tells us that if the mean squared error goes to zero, then the probability that $\hat\mu$ is any distance $\epsilon$ away from $\mu$ goes to zero.

Now we're going to do two things. First, we're going to prove Markov's inequality. It's good practice working with expectations and probabilities. Second, we're going to look at what Markov's inequality tells us about
how close $\hat\mu$ is to $\mu$, i.e., we're going to use it to calculate the coverage of an interval estimate.

First we define the following two functions, $f$ and $g$, with $t, x\ge 0$.
$$
f(x,t) = \frac{x}{t}
$$
$$
g(x,t) = 1_{\ge t}(x) = \begin{cases}
1 & \text{ if } x \ge t \\
0 & \text{ otherwise }
\end{cases}
$$

::: {#exr-markov-proof .callout-exercise}
1. We pick $t=3$, plot the two functions above on the same coordinate with $x$ on the x axis. Based on the graph, what can we infer about the relationship between $f(x,t)$ and $g(x,t)$?

2. It turns out that your inference is correct! For all $t, x\ge 0$, $\frac{x}{t} \ge 1_{\ge t}(x)$. Use the fact to prove *Markov's inequality*.

   *Hint 1*. $\E[Y]\ge \E[Z]$ if $Y\ge Z$

   *Hint 2*. Knowing that $\frac{x}{t} \ge 1_{\ge t}(x)$, we can infer that $\frac{X}{t} \ge 1_{\ge t}(X)$ for $X$ being a non-negative random variable.
:::

::: {#ans-exr-markov-proof .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

## Markov's Inequality and Interval Estimation

So far, when we've calculated the coverage of an interval estimate in terms of its mean and variance, we've relied on normal approximation. That is, we've been using a different formula for
$P(\lvert\hat\mu - \mu\rvert \ge \epsilon)$ based on the idea that $\hat\mu-\mu$ has approximately the same distribution as a normally-distributed random variable $Z$ with the same mean---zero---and variance $\sigma_{\hat\mu}^2=\Var[\hat\mu-\mu]$. We can use this approximation, in combination with our knowledge of the probability density of the normal approximation, to get a bound on the probability that $\lvert\hat\mu - \mu\rvert \ge \epsilon$.

$$
P(\abs{\hat\mu - \mu} \ge \epsilon) \approx P(\abs{Z} \ge \epsilon) = 1-P(\abs{Z} < \epsilon) = 1- \int_{-\epsilon}^{\epsilon} f_{\sigma_{\hat\mu}}(z) dz \qfor f_{\sigma}(z) = \frac{1}{\sqrt{2\pi}\sigma} e^{-z^2/(2\sigma^2)}
$$

The reason we're always talking about intervals of the form $\hat\mu \pm 2 \sigma_{\hat\mu}$ is that, using this approximation, that yields 95% coverage. That is, $P(\abs{Z} < 2 \sigma_{\hat\mu}) \approx .95$. To see why we tend to use normal approximation instead of Markov's inequality, let's see what Markov's inequality tells us about how close an estimator is to its estimation target and about the coverage of this interval.

::: {#exr-markov-coverage .callout-exercise}
1. To see what Markov's inequality can tell us, first we can apply the property to the squared deviation from the mean:

   $$
   X = \{\hat{\theta}-\E[\hat{\theta}]\}^2
   $$

   $$
   P(X\ge t)\le \frac{\E[\{\hat{\theta}-\E[\hat{\theta}]\}^2]}{t} = \frac{\Var(\hat{\theta})}{t}
   $$

   When we do this, what does Markov's inequality tell us about how close our estimator is to its mean? What is the probability that it's within 1, 2, 3 standard deviations?

2. We can use this to calibrate an interval. What does it tell us about the probability that $\hat{\theta} \pm 2 \sigma_{\hat{\theta}}$ contains $\E[\hat{\theta}]$? How wide must an interval of width $w$ be so that $\hat{\theta} \pm \frac{w}{2}$ has 95% coverage?

   *Hint*. $\hat{\theta} \pm \frac{w}{2}$ contains $\theta$ if and only if $|\hat{\theta}-\theta| \le \frac{w}{2}$. Therefore, $P(\hat{\theta} \pm \frac{w}{2} \text{ contains } \theta) = P(|\hat{\theta}-\theta| \le \frac{w}{2})$.
:::

::: {#ans-exr-markov-coverage .callout-answer .callout-note title="Template"}

*(your answer here)*

:::


# Comparing Interval Calibration Methods

<!-- PULL from hw5 (stays): "Comparing Interval Calibration Methods" (lines 348-482).
     The programming exercise: empirical coverage of bootstrap vs normal vs Markov;
     measure coverage & width; choose; does it change with n? This is the empirical
     coverage calculation Skip wants paired with the biased-estimation material. -->

::: {#exr-chebyshev-binomial-comparison .callout-exercise}
## Exact and Guaranteed Intervals

Return to the binary population and sample size from the earlier calibration homework. Use
the exact Binomial method from that homework and the Chebyshev method from this week's chapter.

1. Derive the value $t$ for which Chebyshev guarantees
   $P(|\hat\theta-\theta|\le t\sigma)\ge .95$.
2. On the same axis, draw the exact middle-95% interval for $\hat\theta$ and the guaranteed
   interval $\theta\pm t\sigma$.
3. Compare their widths over a grid of values of $\theta$. Which method needs the full
   distribution, and which needs only the first two moments?
4. Explain why the guaranteed interval is useful even though it is much wider.
5. Save this comparison. After normal approximation, add the normal interval to the same
   object rather than starting a new exercise.
:::

::: {#ans-exr-chebyshev-binomial-comparison .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

::: {#exr-calibration-method-simulation .callout-exercise}
## Coverage Is an Empirical Question

Let $\theta=.7$ and draw binary samples of size $n=625$. For each sample, calculate both the
sample mean $\bar Y$ and the pseudo-observation estimator $\tilde Y_{\nprior}$ from
@exr-bias-general with $\nprior=100$ and $\thetaprior=.5$.

1. For each estimator, construct three intervals centered at the observed point estimate: a
   95% bootstrap interval, a 95% normal-approximation interval, and an interval whose radius
   is $\sqrt{20}$ times the estimated standard deviation. For the pseudo-observation
   estimator, estimate its standard deviation by replacing $\theta$ with $\bar Y$ in the
   variance formula you derived earlier. The last radius is Chebyshev-calibrated around the
   estimator's expectation; for the biased estimator, it does not guarantee 95% coverage of
   $\theta$.
2. Repeat the experiment 1,000 times. For each of the six estimator-and-calibration pairs,
   report empirical coverage and average width.
3. Which calibration method would you use for each point estimator? Separate the question
   “is the interval narrow?” from “does it cover $\theta$ 95% of the time?”
4. Repeat with $n=2500$. Explain why increasing the amount of real data changes both the bias
   and the variance advantage of the pseudo-observations.
:::

::: {#ans-exr-calibration-method-simulation .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

::: {#exr-bernoulli-third-absolute-moment .callout-exercise}
## One More Moment

Let $X$ equal one with probability $p$ and zero with probability $1-p$.

1. List the two possible values of $|X-p|^3$ and their probabilities.
2. Calculate $\E|X-p|^3$.
3. For $n$ independent copies, calculate the sum of the $n$ individual third absolute centered
   moments.
4. The later normal-approximation result uses this sum. Why is that easier than calculating
   the distribution—or the third absolute centered moment—of the sum itself?
:::

::: {#ans-exr-bernoulli-third-absolute-moment .callout-answer .callout-note title="Template"}

*(your answer here)*

:::

# Paley–Zygmund: The Other Half

<!-- HOLE (the conceptual ceiling — new writing, exists nowhere): the LOWER bound.
     Chebyshev only capped sigma from above; show the spread is genuinely there, so sigma
     is pinned from BOTH sides ("the SD is standard, for real"). The 4th moment
     E[(Y-mu)^4] is the vehicle — frame as DISCOVERING the other half of the story. -->

::: {#exr-paley-zygmund-bonus .callout-exercise}
## Bonus: A Lower Bound on Spread

For a nonnegative random variable $Z$ and $0<a<1$, the Paley–Zygmund inequality says
$$
P(Z\ge a\E Z)\ge (1-a)^2\frac{(\E Z)^2}{\E[Z^2]}.
$$
Let $Y$ have mean $\mu$, positive variance $\sigma^2$, and finite fourth centered moment. Apply the
inequality with $Z=(Y-\mu)^2$ and $a=1/2$.

1. Rewrite the event $Z\ge a\E Z$ as a statement about $|Y-\mu|$ and $\sigma$.
2. Express the resulting lower bound using $\sigma^4$ and $\E[(Y-\mu)^4]$.
3. Chebyshev says there cannot be too much probability far from $\mu$. Explain how this result
   says that the standard deviation cannot be describing spread that is not really there.
:::

::: {#ans-exr-paley-zygmund-bonus .callout-answer .callout-note title="Template"}

*(your answer here)*

:::
