Problem Set 5: Expectation and Variance

Author

POL 211

Published

October 27, 2026

Due: Monday, November 2 by 11:59 PM. Email both your .qmd and your rendered .html to Max Welch (mwelch@ucdavis.edu).

TipReview Quant Camp as you go

This set leans on the R you learned at Quant Camp. When you get stuck on filtering, grouping, or plotting, go back to the Quant Camp materials. They’re the fastest way to get unstuck.

ImportantUsing AI

Use AI the way people used Google, R’s help pages, and Stack Overflow before AI: ask precise, limited questions to get unstuck on part of a question. Passing off AI-generated work wholesale as your own is not allowed. See the syllabus and the Department’s policy on generative AI.

Format: Write your answers in one Quarto document, saved in the sets/ folder of your course project. Start it with this header, so the HTML renders as one self-contained file:

---
title: "Problem Set 5"
author: "Your name"
format:
  html:
    embed-resources: true
---

Your document should read as a short report: brief prose explaining your reasoning, not just code and output.

Libraries you will need:

library(tidyverse)
library(juanr)
WarningMethods subfield

Parts marked Methods portfolio are required if you plan to choose methods as a subfield. They’re optional, and recommended, for everyone else.

Question 1: How many voters does a canvass need?

A campaign wants to know whether knocking on doors makes voters like its candidate (a Democrat) more. It plans an experiment: flip a coin for each voter on its list, canvass the voters who come up heads, then survey everyone with a feeling thermometer, a 0–100 rating of how warmly they feel toward the candidate.

Here is the DGP. Since we wrote it, we know the true effect of canvassing: 5 points.

Variable What it is How it’s made
dem 1 if the voter is a registered Democrat, 0 if not A coin flip: a 50% chance of 1
canvassed 1 if a canvasser knocked on the voter’s door, 0 if not A separate coin flip: a 50% chance of 1
therm The voter’s feeling thermometer score \(40 + 5 \times \texttt{canvassed} + 20 \times \texttt{dem} + \text{noise}\), where the noise is drawn from a normal distribution with mean 0 and SD 20

(A few scores will land below 0 or above 100. Don’t worry about it.)

  1. Build the DGP. Write a function, make_canvass(n), that takes a number of voters, n, and returns a tibble with one row per voter and the three variables above. Model it on make_resumes_n() in this week’s class script (05-code.R, section 4). Run it once with 1,000 voters, show the first few rows, and compute the difference in mean thermometer scores between canvassed and uncanvassed voters. How close is it to 5? Why isn’t it exactly 5?

  2. Two sampling distributions. Write a second function, run_canvass(n), that runs the study once with n voters and returns just the difference in means (model it on run_resumes_n()). Then simulate 1,000 studies with 100 voters and 1,000 studies with 1,000 voters, as in section 4 of the class script, and plot the two sampling distributions. For each n, report the center and the standard error. What changes between the two, and what doesn’t?

  3. How often will we be very wrong? With 100 voters, what share of the 1,000 studies came out negative: estimates that would tell the campaign that canvassing hurts?

WarningMethods portfolio
  1. How big is big enough? Use simulation to find the smallest number of voters for which the center is at least two standard errors away from zero. At what sample size does the center minus two standard errors first land above zero?

Question 2: A survey question as a random variable

A survey asks: “How much do you agree with this statement: Elections in this country are free and fair.” Answers are coded 1 to 5. Let \(X\) be the answer of one randomly chosen respondent. Its probability distribution:

\(x\) Answer \(P(X = x)\)
1 Strongly disagree 0.05
2 Disagree 0.10
3 Neither agree nor disagree 0.15
4 Agree 0.40
5 Strongly agree 0.30

Do (a) and (b) by hand, writing your work in math mode (see the box below). You can use R as a calculator for the arithmetic, but show each step in math.

NoteWriting math in Quarto

Quarto writes math with LaTeX commands. Wrap math that sits inside a sentence in single dollar signs, and math that gets its own line in double dollar signs. For example, this:

For a fair coin, $P(Y = 1) = 0.5$. The expected value is

$$
E[Y] = \sum_y y \cdot P(Y = y) = 0(0.5) + 1(0.5) = 0.5
$$

renders as: For a fair coin, \(P(Y = 1) = 0.5\). The expected value is

\[ E[Y] = \sum_y y \cdot P(Y = y) = 0(0.5) + 1(0.5) = 0.5 \]

A few commands you’ll want: \sum for \(\sum\), x^2 for \(x^2\), \mu_X for \(\mu_X\), \frac{a}{b} for \(\frac{a}{b}\), \sqrt{x} for \(\sqrt{x}\), \times for \(\times\), and \text{Var}(X) for \(\text{Var}(X)\). To line up the steps of a long calculation, put them inside \begin{aligned} ... \end{aligned}, with &= where the lines should line up and \\ at the end of each line.

To find a command you don’t know: look it up in Overleaf’s guides to math expressions and symbols, or draw the symbol in Detexify and it will tell you the command. “What’s the LaTeX command for …?” is also a good question for AI: precise and limited.

  1. Find \(E[X]\).

  2. Find \(\text{Var}(X)\) and \(\text{SD}(X)\).

  3. In words: what do \(E[X]\) and \(\text{SD}(X)\) tell you about how people answer this question?

  4. Check your answers by simulation: draw 100,000 answers with sample() and its prob argument (Problem Set 4), and compare their mean() and var() to (a) and (b).

Question 3: When the canvassers go off script

Back to Question 1’s canvass. Imagine the canvassers go off script: instead of knocking on the doors the coin flip picked, they knock mostly on the doors of registered Democrats, who are friendlier to talk to. Let’s say canvassers knock on the door of a registered Democrat with an 80% chance, and on the door of everyone else with a 20% chance. Everything else about the DGP stays the same, including the true effect of 5 points.

  1. Before you simulate, why does this behavior break randomization? How does it introduce confounding? Finally, sign the bias. Will the difference in means between canvassed and uncanvassed voters overstate or understate the effect of canvassing? Why?

  2. Write make_canvass_conf(n), a version of make_canvass() where canvassing depends on dem. One way to make canvassing depend on dem:

    rbinom(n, size = 1, prob = if_else(dem == 1, 0.8, 0.2))

    Simulate 1,000 studies with 1,000 voters each, and plot the sampling distribution of the difference in means with a vertical line at the true effect. Where does it center? Did you sign the bias right?

  3. Unbiased for what? Explain why the estimator is unbiased for the difference in means but biased for the effect of canvassing.

Question 4: Loaded dice

Three six-sided dice:

  • \(D_{\text{fair}}\), a regular die: each face has a \(1/6\) chance
  • \(D_{\text{middle}}\), a die loaded toward the middle: it lands on 3 or 4 half the time, \(P(D_{\text{middle}} = 3) = P(D_{\text{middle}} = 4) = 0.25\), and each of the other four faces has an equal chance
  • \(D_{\text{edges}}\), a die loaded toward the edges: it lands on 1 or 6 half the time, \(P(D_{\text{edges}} = 1) = P(D_{\text{edges}} = 6) = 0.25\), and each of the other four faces has an equal chance
  1. Before doing any math, guess: rank the three dice from smallest to largest variance. Why? There’s no wrong answer here; you’ll be graded on giving a reason.

  2. Find the expected value and the variance of each die by hand, in math mode.

  3. All three dice have the same expected value. So why are their variances different? Explain why the ranking makes sense. Was your guess in (a) right?

Question 5: Watching the news

Let \(X\) be the hours of TV news a respondent watches per week, and \(Y\) the hours of online news. Suppose \(\text{Var}(X) = 4\) and \(\text{Var}(Y) = 9\).

NoteShortcut to covariance

(Hint: in week 3, the correlation was the covariance divided by the two SDs, \(r = \text{Cov}(X, Y) / [\text{SD}(X)\,\text{SD}(Y)]\). Use this to get the covariance from a correlation.)

  1. Say we add up all the news the respondent watches (online or TV). What is the variance of total news consumption? Assume online and TV news consumption are independent.

  2. Now, what is the variance of total news consumption if people who consume more of one tend to consume more of the other? Let’s say \(\text{Cor}(X, Y) = 0.5\).

  3. Now, what is the variance of total news consumption if people who consume more of one tend to consume less of the other? Let’s say \(\text{Cor}(X, Y) = -0.5\). In substantive terms, why is the answer smaller?