Homework 3: Election Prediction
Credits: This assignment is modified form of https://courses.cs.washington.edu/courses/cse140/13wi/homework/hw3/homework3.html
Due:
Submit only main.cpp via LMS
In this assignment, you will practice use of vectors, maps, and tuples.
Introduction and background
Election prediction: pundits vs. statisticians
In recent decades there has been a shift to a more scientific approach, in which election results
are predicted statistically using a poll. A small random sample of voters is asked how they will
vote, and from that the result of the entire election is extrapolated.
In the 2012 presidential election, statistician Nate Silver correctly predicted the outcome in
every state, whereas pundits’ predictions varied significantly.
How did Nate Silver do it? In this assignment, you will find out, and you will replicate his results
by using polling data to predict the outcome of the 2012 US presidential election.
Election polls: Which ones to trust?
An election poll is a survey that asks a small sample of voters how they plan to vote. If the
sample of voters is representative of the voting population at large, then the poll predicts the
result of the entire election. In practice, a poll’s prediction must be taken with a grain of salt,
because the sample is only approximately representative of the voting population.
How can you decide which polls to rely upon?
Depending on which poll you trust, you might make a very different prediction.
One approach is to average together the different polls. This is better than trusting any one of
them, but it is still rather crude as some of them may be biased.
Silver’s approach is very sophisticated, but its key idea is to combine different polls using a
weighted average. In a normal average, each data point contributes equally to the result. In a
weighted average, some data points contribute more than others. Silver examined how well
each polling organization had predicted previous elections, and then weighted their polls
according to their accuracy: more biased pollsters had less effect on the weighted average.
The general structure of FiveThirtyEight’s algorithm is:
1. Calculate the average error of each pollster’s predictions for previous elections. This is
known as the pollster’s rank. A smaller rank indicates a more accurate pollster.
2. Transform each rank into a weight (for use in a weighted average). A larger weight
indicates a more accurate pollster. Silver considers a number of factors when
computing a weight, including rank, sample size, and when a poll was conducted. For
this assignment, we simply set weight to equal the inverse square of rank (weight =
rank^-2).
3. In each state, perform a weighted average of predictions made by pollsters. This
predicts the winner in that state.
4. Calculate the outcome of the Electoral College, using the per-state predictions. The
candidate with the most electoral votes wins the election.
The US Electoral College
Why polls are approximate
Recall that a poll sample is only approximately representative of the voting population. There
are two reasons for this: sampling error and pollster bias.
1. Sampling error: If you randomly choose a sample from a population, then random
chance may cause the sample to differ from the population. The US population is 50.7%
female and 49.3% male, but a random sample of 1000 individuals might include 496 females
and 504 males.
Sampling error is unavoidable, but it can be reduced by increasing the sample size.
2. Pollster bias or house effects: These are systematic inaccuracies caused by faulty
methodology essentially, the pollster has not chosen a random sample of US voters.
Suppose that a pollster sampled only Mormons or only African-Americans; it would be
meaningless to predict the overall vote from these biased samples. Here are some
examples:
a. Not all Americans vote, so each polling firm should adjust its sampling to select
not among all Americans, but among likely voters. Poor people and young
people are are less likely to vote.
b. Survey response rates are typically lowest in urban areas, so unweighted
samples routinely under-represent black and Hispanic Americans who frequently
live in urban areas.
c. Some telephone polls call only landline numbers, but 1/3 of Americans rely on
cellphones and they are younger, more urban, poorer, and more likely to be
black and Hispanic, all of which correlate with Democratic voting.