====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gibson:teaching:spring-2016:math445:lab8 [2016/03/21 20:20] gibson |
gibson:teaching:spring-2016:math445:lab8 [2016/03/24 07:02] (current) gibson [Math 445 Lab 8: Predicting Presidential elections with Monte Carlo methods] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Math 445 Lab 8: Predicting Presidential elections with Monte Carlo methods====== | ====== Math 445 Lab 8: Predicting Presidential elections with Monte Carlo methods====== | ||
+ | |||
+ | Necessary Matlab concepts | ||
+ | |||
+ | * ''rand'' random number generation | ||
+ | * ''if-else'' statements | ||
+ | * ''for'' loops | ||
+ | * ''histogram'' plots | ||
====Background==== | ====Background==== | ||
Line 8: | Line 15: | ||
In the 2012 elections Silver's projections received an enormous amount of attention, and quite a bit of criticism, too. Political pundits derided his work as meaningless number crunching and his 2008 results as lucky. But this time, Silver predicted the Presidential election correctly in all 50 states, and 31 of 33 Senate elections. | In the 2012 elections Silver's projections received an enormous amount of attention, and quite a bit of criticism, too. Political pundits derided his work as meaningless number crunching and his 2008 results as lucky. But this time, Silver predicted the Presidential election correctly in all 50 states, and 31 of 33 Senate elections. | ||
+ | Further reading on Nate Silver, fivethirtyeight.com, and the mathematics of election prediction, and the Electoral College system: | ||
+ | |||
+ | * [[http://en.wikipedia.org/wiki/FiveThirtyEight | FiveThirtyEight, Wikipedia]] | ||
+ | * [[http://cosmiclog.nbcnews.com/_news/2012/10/30/14809227-political-forecasts-stir-up-a-storm?lite|NBC News story on Nate Silver, just prior to 2012 election]], | ||
+ | * [[http://www.dailykos.com/story/2012/11/01/1153661/-Nate-Silver-s-Math-Based-Math|DailyKos article on Nate Silver, just prior to 2012 election]] | ||
+ | * [[https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=nate%20silver%20controversy | google:"Nate Silver controversy" ]] | ||
+ | * [[https://en.wikipedia.org/wiki/Electoral_College_(United_States) | Electoral College, Wikipedia]] | ||
==== This lab ==== | ==== This lab ==== | ||
In this lab we will simulate the 2012 Presidential elections based on last-minute polling data. | In this lab we will simulate the 2012 Presidential elections based on last-minute polling data. | ||
Essentially, we're pretending that it's 2012 Election Day and we're trying to figure out the odds | Essentially, we're pretending that it's 2012 Election Day and we're trying to figure out the odds | ||
- | that either Obama or Romney will win. The first part of Silver's model (constructing unbiased composite polls) is too complicated for us to do here, so we'll just do the second part (running | + | that Obama or Romney be declared the winner after the polls close. The first part of Silver's model (constructing unbiased composite polls) is too complicated for us to do here, so we'll just do the second part (running a bunch of simulated elections based on the unbiased poll data). |
- | a bunch of simulated elections based on the unbiased poll data). | + | |
- | ---- | + | ==== Problem 1: Predicting the 2012 New Hampshire Presidential election==== |
- | **Problem 1: Predicting the New Hampshire Presidential election.** Just prior to the 2012 Presidential election, polls showed that 51.5% of New Hampshire voters planned to vote for Obama, and 47.8% planned to vote for Romney. Thus it looked likely that New Hampshire's four Electoral College delegates would go to Obama. | + | Just prior to the 2012 Presidential election, polls showed that 51.5% of New Hampshire voters planned to vote for Obama, and 47.8% planned to vote for Romney. Thus it looked likely that New Hampshire's four Electoral College delegates would go to Obama. |
But polling data is not entirely certain. The margin of error on the New | But polling data is not entirely certain. The margin of error on the New | ||
Line 32: | Line 45: | ||
a random percentage of voters between -3.4% and 3.4%, and add that percent to | a random percentage of voters between -3.4% and 3.4%, and add that percent to | ||
Obama while subtracting it from Romney. Compare the resulting percentages | Obama while subtracting it from Romney. Compare the resulting percentages | ||
- | and declare the winner of the simulated election to whomever hads the larger | + | and declare the winner of the simulated election to whomever has the larger |
percentage. | percentage. | ||
Line 40: | Line 53: | ||
Turn in your code for simulating the New Hampshire election (probably a Matlab | Turn in your code for simulating the New Hampshire election (probably a Matlab | ||
- | script) and the percentage likelihoods that the election would go to Obama or | + | script) and the percentage likelihoods that Obama or Romney would win New Hampshire. |
- | Romney. | + | |
+ | ====Problem 2: Simulating the 2012 Electoral College votes of a few swing states==== | ||
- | ----- | ||
- | |||
- | **Problem 2: Simulating the Electoral College votes of a few swing states.** | ||
Problem 1 demonstrated that New Hampshire was a swing state in the 2012 | Problem 1 demonstrated that New Hampshire was a swing state in the 2012 | ||
election --it could have gone either way. Colorado was another swing state. | election --it could have gone either way. Colorado was another swing state. | ||
Line 71: | Line 81: | ||
- | ---- | + | ==== Problem 3: Simulating the elections in all fifty states and the likelihood that Obama or Romney wins the general election.==== |
+ | |||
+ | |||
+ | Now we're ready to tackle the original problem: a Monte Carlo simulation of | ||
+ | the 2012 Presidential election. | ||
+ | Specifically, given a list of states, the number of their electoral votes, the composite | ||
+ | polling percentages for each candidate, and the margins of error those | ||
+ | polling percentages, you are to run a large number of simulations of the | ||
+ | election and determine the likelihood that either candidate will win based | ||
+ | on the results of those simulations. For each state, start by assigning the | ||
+ | specified composite polling percentages to the two candidates. Then transfer from one | ||
+ | candidate to the other a different random number in the range between | ||
+ | ''-margin'' and ''+margin''. Compare the two percentages and award that state's | ||
+ | electoral votes to the candidate with the larger percentage of votes. Do this | ||
+ | for all fifty states (plus DC), add up all the electoral votes for each candidate, | ||
+ | and award the ''n''th election to the candidate with the majority of electoral votes. | ||
+ | |||
+ | Run a large number (say, 10000) of such simulated elections, keeping track of the number of | ||
+ | electoral votes for each candidate in each election. Make a histogram that | ||
+ | shows the statistical distribution of total electoral votes for one of the | ||
+ | candidates, using bins of width 10 between 0 and 540 (0-9.99 for bin 1, 10-14.99 for | ||
+ | bin 2, etc). If you can figure out how, color the bins corresponding to Romney | ||
+ | wins red and the bins corresponding to Obama wins blue, or else just draw a vertical | ||
+ | line at the magic number of 270 electoral votes needed to win the election outright. | ||
+ | |||
+ | === Questions === | ||
+ | |||
+ | Then answer the following questions (again, pretending that it's still Tuesday, November 6th 2012 and the real outcome is unknown). | ||
+ | |||
+ | - Who is most likely to win the presidential election? | ||
+ | - What is the probability that the most likely winner will actually win? | ||
+ | - What is the most likely range of electoral votes for the winner? (among the bins of width 10 specified above) | ||
+ | - What is the likelihood of a 269-269 electoral vote tie? | ||
+ | |||
+ | Turn in print-outs of your codes, your histogram, and your answers to the above questions. | ||
- | **Problem 3: Simulating the elections in all fifty states and the likelihood that Obama wins the general election.** | + | === Tips === |
- | ...to be continued... | + | * Start with a small number of simulated elections (say 100) and then increase to a large number (say 10,000) when you're confident your code is working correctly. |
+ | * Try to use as few for-loops as possible. If you are really on fire, you can do it with just one for-loop that loops over the number of trials. | ||
+ | * Changing the colors of histogram bins in Matlab is not as easy as one might hope. You'll need to take data returned from the **hist** function and replot it with the **bar** command. See http://www.mathworks.com/matlabcentral/newsreader/view_thread/290534 for an example of how to do this. | ||
+ | === Data === | ||
+ | Here's the 2012 polling data, taken from [[http://fivethirtyeight.blogs.nytimes.com]] on 2012-11-01. You can load this into Matlab as a matrix ''P'' by cutting and pasting the data into a text file ''P.asc'' and running ''load P.asc'' within Matlab. If you don't believe this polling data, feel free to use something you trust more. | ||
+ | <file matlab P.asc> | ||
+ | % Composite 2012 Presidential election polling numbers | ||
+ | % from http://fivethirtyeight.blogs.nytimes.com | ||
+ | % 2012-11-01 1am | ||
+ | % | ||
+ | % O == Obama percentage | ||
+ | % R == Romney percentage | ||
+ | % M == margin of error | ||
+ | % EV == electoral votes | ||
+ | % | ||
+ | % O R M EV state | ||
+ | 36.8 62.7 3.8 9 % AL | ||
+ | 38.8 59.7 6.0 3 % AK | ||
+ | 46.2 53.0 3.3 11 % AZ | ||
+ | 38.7 59.7 3.8 6 % AR | ||
+ | 58.2 40.5 2.9 55 % CA | ||
+ | 50.9 48.2 3.0 9 % CO | ||
+ | 56.7 42.4 3.3 7 % CT | ||
+ | 59.6 39.7 5.5 3 % DE | ||
+ | 93.1 6.3 3.2 3 % DC | ||
+ | 49.9 49.7 2.7 29 % FL | ||
+ | 45.5 54.1 2.7 16 % GA | ||
+ | 66.5 32.6 3.9 4 % HA | ||
+ | 32.2 66.1 4.4 4 % ID | ||
+ | 59.9 39.5 3.0 20 % IL | ||
+ | 45.3 53.9 3.0 11 % IN | ||
+ | 51.2 47.8 3.2 6 % IA | ||
+ | 38.0 61.0 6.1 6 % KA | ||
+ | 40.4 58.7 4.5 8 % KY | ||
+ | 39.4 59.8 3.5 8 % LA | ||
+ | 56.1 42.7 3.7 4 % ME | ||
+ | 61.0 38.0 3.0 10 % MD | ||
+ | 59.1 39.8 3.7 11 % MA | ||
+ | 53.1 45.8 2.7 16 % MI | ||
+ | 53.8 45.0 2.9 10 % MN | ||
+ | 39.4 60.1 5.3 6 % MS | ||
+ | 45.6 53.6 2.8 10 % MO | ||
+ | 45.3 53.1 3.9 3 % MT | ||
+ | 40.5 58.8 3.3 5 % NE | ||
+ | 51.9 47.2 2.9 6 % NV | ||
+ | 51.5 47.8 3.4 4 % NH | ||
+ | 55.6 43.4 3.3 14 % NJ | ||
+ | 54.2 44.6 3.6 5 % NM | ||
+ | 62.5 36.9 2.8 29 % NY | ||
+ | 48.9 50.5 2.6 15 % NC | ||
+ | 42.1 56.5 3.9 3 % ND | ||
+ | 51.4 47.6 2.7 18 % OH | ||
+ | 33.9 65.8 3.8 7 % OK | ||
+ | 53.7 44.0 3.6 7 % OR | ||
+ | 52.6 46.5 2.6 20 % PA | ||
+ | 61.9 36.3 4.3 4 % RI | ||
+ | 43.3 56.0 4.6 9 % SC | ||
+ | 42.6 56.1 4.2 3 % SD | ||
+ | 41.4 57.7 3.9 11 % TN | ||
+ | 41.3 58.1 3.1 38 % TX | ||
+ | 27.8 70.5 4.1 6 % UT | ||
+ | 50.8 48.6 2.5 13 % VA | ||
+ | 66.3 32.5 4.8 3 % VT | ||
+ | 56.2 42.5 3.5 12 % WA | ||
+ | 41.4 57.4 4.7 5 % WV | ||
+ | 52.5 46.8 2.9 10 % WI | ||
+ | 30.9 67.6 6.0 3 % WY | ||
+ | </file> |