Add files via upload

This commit is contained in:
Peter Norvig 2019-01-24 00:32:01 -08:00 committed by GitHub
parent 7cab8134f8
commit 61dc724159
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,9 +17,9 @@
"source": [
"import urllib.request\n",
"import re\n",
"import collections \n",
"from collections import namedtuple, defaultdict\n",
"\n",
"Row = collections.namedtuple('Row', 'state delta jan17app jan17dis jan17err app dis err')\n",
"Row = namedtuple('Row', 'state delta jan17app jan17dis jan17err app dis err')\n",
"\n",
"def read_data(url='https://morningconsult.com/tracking-trump/'):\n",
" \"Fetch data from the website and return a list of `Row` objects.\"\n",
@ -35,16 +35,16 @@
"\n",
"def net(row): \"Approval minus disapproval.\"; return row.app - row.dis \n",
"\n",
"votes = dict(AL=9, AK=3, AZ=11, AR=6, CA=55, CO=9, CT=7, DE=3, DC=3, FL=29, GA=16, HI=4, ID=4,\n",
" IL=20, IN=11, IA=6, KS=6, KY=8, LA=8, ME=4, MD=10, MA=11, MI=16, MN=10, MS=6, MO=10, \n",
" MT=3, NE=5, NV=6, NH=4, NJ=14, NM=5, NY=29, NC=15, ND=3, OH=18, OK=7, OR=7, PA=20, \n",
" RI=4, SC=9, SD=3, TN=11, TX=38, UT=6, VT=3, VA=13, WA=12, WV=5, WI=10, WY=3)\n",
"\n",
"rows = read_data()\n",
"\n",
"def EV(swing=0, rows=rows):\n",
" \"How many electoral votes would Trump win today, given a swing.\"\n",
" return sum(votes[row.state] for row in rows if net(row) + swing > 0)"
" return sum(votes[row.state] for row in rows if net(row) + swing > 0)\n",
"\n",
"votes = dict(AL=9, AK=3, AZ=11, AR=6, CA=55, CO=9, CT=7, DE=3, DC=3, FL=29, GA=16, HI=4, ID=4,\n",
" IL=20, IN=11, IA=6, KS=6, KY=8, LA=8, ME=4, MD=10, MA=11, MI=16, MN=10, MS=6, MO=10, \n",
" MT=3, NE=5, NV=6, NH=4, NJ=14, NM=5, NY=29, NC=15, ND=3, OH=18, OK=7, OR=7, PA=20, \n",
" RI=4, SC=9, SD=3, TN=11, TX=38, UT=6, VT=3, VA=13, WA=12, WV=5, WI=10, WY=3)"
]
},
{
@ -73,7 +73,7 @@
"source": [
"What that says is that if an election were held today, January 23, 2019, and if Trump won those states in which he has a net positive approval (i.e. more approval than disapproval), **he would get 164 electoral votes** (that's 9 worse than McCain in 2008). You need **270** to win.\n",
"\n",
"But things can change. The election is a long ways away, and we don't know who's running. In the table below, I list the number of electoral votes Trump would get assuming he gets an increase of net approval of 0 to 9 percentage points across the board in every state. We see he would need a **7** percent upswing in order to win."
"But things can change. The election is a long ways away, and we don't know who's running, and we don't know if there is systematic bias in the polling data. In the table below, I list the number of electoral votes Trump would get assuming he gets an increase of net approval of 0 to 9 percentage points across the board in every state. We see he would need a **7** percent upswing in order to win."
]
},
{
@ -104,6 +104,84 @@
"source": [
"{m: EV(m) for m in range(10)}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here are the states sorted by current net approval:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(-62, 'DC', 3),\n",
" (-31, 'VT', 3),\n",
" (-29, 'CA', 55),\n",
" (-29, 'MA', 11),\n",
" (-28, 'HI', 4),\n",
" (-28, 'MD', 10),\n",
" (-23, 'NY', 29),\n",
" (-23, 'WA', 12),\n",
" (-22, 'OR', 7),\n",
" (-21, 'IL', 20),\n",
" (-20, 'CT', 7),\n",
" (-19, 'NM', 5),\n",
" (-19, 'RI', 4),\n",
" (-18, 'NJ', 14),\n",
" (-16, 'CO', 9),\n",
" (-16, 'DE', 3),\n",
" (-14, 'MN', 10),\n",
" (-12, 'MI', 16),\n",
" (-12, 'WI', 10),\n",
" (-11, 'NV', 6),\n",
" (-10, 'IA', 6),\n",
" (-8, 'AZ', 11),\n",
" (-8, 'NH', 4),\n",
" (-6, 'ME', 4),\n",
" (-6, 'PA', 20),\n",
" (-3, 'FL', 29),\n",
" (-3, 'GA', 16),\n",
" (-3, 'NC', 15),\n",
" (-3, 'OH', 18),\n",
" (-3, 'VA', 13),\n",
" (2, 'KS', 6),\n",
" (3, 'MO', 10),\n",
" (3, 'UT', 6),\n",
" (4, 'AK', 3),\n",
" (4, 'MT', 3),\n",
" (4, 'TX', 38),\n",
" (8, 'IN', 11),\n",
" (8, 'NE', 5),\n",
" (9, 'ND', 3),\n",
" (9, 'SC', 9),\n",
" (11, 'AR', 6),\n",
" (11, 'ID', 4),\n",
" (13, 'LA', 8),\n",
" (13, 'OK', 7),\n",
" (15, 'KY', 8),\n",
" (15, 'SD', 3),\n",
" (16, 'TN', 11),\n",
" (17, 'MS', 6),\n",
" (21, 'AL', 9),\n",
" (26, 'WV', 5),\n",
" (32, 'WY', 3)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted((net(row), row.state, votes[row.state]) \n",
" for row in rows)"
]
}
],
"metadata": {