Add files via upload
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -12,12 +12,12 @@
|
||||
" gives state-by-state, month-by-month presidential approval poll data. Within the web page there is some Javascript from which\n",
|
||||
" we can extract the data we need. It looks like this:\n",
|
||||
"\n",
|
||||
" var mc_state_trend = [[\"Demographic\",\"January 1, 2017\",\"February 1, 2017\", ...]\n",
|
||||
" var mc_state_trend = [[\"Demographic\",\"Jan-17\",\"\",\"Feb-17\",\"\", ...]\n",
|
||||
" [\"Alabama\",\"62\",\"26\",\"65\",\"29\", ...], \n",
|
||||
" ... ]\n",
|
||||
" \n",
|
||||
"The first row is a header (each date is a day at which polls were aggregated).\n",
|
||||
"The subsequent rows each start with the state name, followed by the approval and disapproval percentages for each date. That is, if there are 34 dates, there will by 68 numbers. The row shown above is saying that on January 1, 2017, 62% of Alabamans approved and 26% disapproved; then on February 1, 2017, 65% approved and 29% disapproved, and so on. Our job is to extract this data and find ways to visualize and understand it.\n",
|
||||
"The first row is a header (each date is a month at which polls were aggregated).\n",
|
||||
"The subsequent rows each start with the state name, followed by the approval and disapproval percentages for each date. That is, if there are 34 dates, there will by 68 numbers. The row shown above is saying that in January, 2017, 62% of Alabamans approved and 26% disapproved; then in February, 2017, 65% approved and 29% disapproved, and so on. Our job is to extract this data and find ways to visualize and understand it.\n",
|
||||
"\n",
|
||||
"First fetch the page and save it locally:"
|
||||
]
|
||||
@@ -90,18 +90,18 @@
|
||||
"# From https://projects.fivethirtyeight.com/trump-approval-ratings/\n",
|
||||
"# A dict of {'date': country-wide-net-approval}\n",
|
||||
"net_usa = {\n",
|
||||
" 'January 2017': 10, 'January 2018': -18, 'January 2019': -12, \n",
|
||||
" 'February 2017': 0, 'February 2018': -15, 'February 2019': -16, \n",
|
||||
" 'March 2017': -6, 'March 2018': -14, 'March 2019': -11, \n",
|
||||
" 'April 2017': -13, 'April 2018': -13, 'April 2019': -11, \n",
|
||||
" 'May 2017': -11, 'May 2018': -12, 'May 2019': -12, \n",
|
||||
" 'June 2017': -16, 'June 2018': -11, 'June 2019': -12, \n",
|
||||
" 'July 2017': -15, 'July 2018': -10, 'July 2019': -11, \n",
|
||||
" 'August 2017': -19, 'August 2018': -12, 'August 2019': -10, \n",
|
||||
" 'September 2017': -20, 'September 2018': -14, 'September 2019': -13, \n",
|
||||
" 'October 2017': -17, 'October 2018': -11, 'October 2019': -13, \n",
|
||||
" 'November 2017': -19, 'November 2018': -11, 'November 2019': -13,\n",
|
||||
" 'December 2017': -18, 'December 2018': -10, 'December 2019': -12,\n",
|
||||
" 'Jan-17': 10, 'Jan-18': -18, 'Jan-19': -12, \n",
|
||||
" 'Feb-17': 0, 'Feb-18': -15, 'Feb-19': -16, \n",
|
||||
" 'Mar-17': -6, 'Mar-18': -14, 'Mar-19': -11, \n",
|
||||
" 'Apr-17': -13, 'Apr-18': -13, 'Apr-19': -11, \n",
|
||||
" 'May-17': -11, 'May-18': -12, 'May-19': -12, \n",
|
||||
" 'Jun-17': -16, 'Jun-18': -11, 'Jun-19': -12, \n",
|
||||
" 'Jul-17': -15, 'Jul-18': -10, 'Jul-19': -11, \n",
|
||||
" 'Aug-17': -19, 'Aug-18': -12, 'Aug-19': -10, \n",
|
||||
" 'Sep-17': -20, 'Sep-18': -14, 'Sep-19': -13, \n",
|
||||
" 'Oct-17': -17, 'Oct-18': -11, 'Oct-19': -13, \n",
|
||||
" 'Nov-17': -19, 'Nov-18': -11, 'Nov-19': -13,\n",
|
||||
" 'Dec-17': -18, 'Dec-18': -10, 'Dec-19': -12,\n",
|
||||
" }"
|
||||
]
|
||||
},
|
||||
@@ -146,7 +146,7 @@
|
||||
"\n",
|
||||
"def margin(states, date=now) -> int:\n",
|
||||
" \"What's the least swing that would lead to a majority?\"\n",
|
||||
" return min(swing for swing in range(-50, 50) if EV(states, date, swing) >= 270)\n",
|
||||
" return min(swing for swing in range(-50, 50) if EV(states, date, swing+0.1) >= 270)\n",
|
||||
"\n",
|
||||
"def net(state, date=now) -> int: return state.approvals[date] - state.disapprovals[date]\n",
|
||||
"def undecided(state, date=now) -> int: return 100 - state.approvals[date] - state.disapprovals[date]\n",
|
||||
@@ -164,7 +164,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -181,7 +181,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -201,7 +201,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -217,6 +217,19 @@
|
||||
" labels('Months into term', 'Net popularity')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def show_swings(swings=range(8)):\n",
|
||||
" print('Swing EV Range')\n",
|
||||
" for swing in swings:\n",
|
||||
" s = swing + 0.5\n",
|
||||
" print(f'±{s:3.1f}% {EV(states, swing=-s):3} to {EV(states, swing=s):3}')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
@@ -226,7 +239,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"@markdown\n",
|
||||
"def show_states(states=states, d=now, ref='January 2017'):\n",
|
||||
"def show_states(states=states, d=now, ref='Jan-17'):\n",
|
||||
" \"A table of states, sorted by net approval, with electoral votes.\"\n",
|
||||
" total = 0\n",
|
||||
" yield header(f'|State|Net|Move|EV|ΣEV|+|−|?|𝝈|')\n",
|
||||
@@ -246,7 +259,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"@markdown\n",
|
||||
"def show_parp(states=states, dates=(now, 'January 2019', 'January 2018', 'January 2017')):\n",
|
||||
"def show_parp(states=states, dates=(now, 'Jan-19', 'Jan-18', 'Jan-17')):\n",
|
||||
" \"A table of states, sorted by Popularity Above Replacement President.\"\n",
|
||||
" def year(date): return '' if date == now else \"'\" + date[-2:]\n",
|
||||
" fields = [f\"PARP{year(date)}|(Net)\" for date in dates]\n",
|
||||
|
||||
813
ipynb/evs.html
813
ipynb/evs.html
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user