Add files via upload

This commit is contained in:
Peter Norvig 2020-02-02 20:11:34 -08:00 committed by GitHub
parent 95aec859f7
commit 81ef969e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 34 deletions

File diff suppressed because one or more lines are too long

View File

@ -57,7 +57,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Additional data: the variable `state_data` contains the [electoral votes by state](https://www.britannica.com/topic/United-States-Electoral-College-Votes-by-State-1787124) and the [partisan lean by state](https://github.com/fivethirtyeight/data/tree/master/partisan-lean) (how much more Republican (plus) or Democratic (minus) leaning the state is compared to the country as a whole, across recent elections). The variable `net_usa` has the [country-wide net presidential approval](https://projects.fivethirtyeight.com/trump-approval-ratings/) by month."
"Additional data: the variable `state_data` contains the [electoral votes by state](https://www.britannica.com/topic/United-States-Electoral-College-Votes-by-State-1787124) and the [partisan lean by state](https://github.com/fivethirtyeight/data/tree/master/partisan-lean) (how much more Republican (plus) or Democratic (minus) leaning the state is compared to the country as a whole, across recent elections). \n",
"\n",
"The variable `net_usa` has the [country-wide net presidential approval](https://projects.fivethirtyeight.com/trump-approval-ratings/) by month."
]
},
{
@ -67,7 +69,7 @@
"outputs": [],
"source": [
"# From https://github.com/fivethirtyeight/data/tree/master/partisan+lean\n",
"# a dict of {\"state name\": (electoral_votes, partisan_lean)}\n",
"# A dict of {\"state name\": (electoral_votes, partisan_lean)}\n",
"state_data = { \n",
" \"Alabama\": (9, +27), \"Alaska\": (3, +15), \"Arizona\": (11, +9), \n",
" \"Arkansas\": (6, +24), \"California\": (55, -24), \"Colorado\": (9, -1), \n",
@ -88,10 +90,10 @@
" \"West Virginia\": (5, +30), \"Wisconsin\": (10, +1), \"Wyoming\": (3, +47)}\n",
"\n",
"# From https://projects.fivethirtyeight.com/trump-approval-ratings/\n",
"# A dict of {'date': country-wide-net-approval}\n",
"# A dict of {'date': country-wide-net-approval}, taken from 1st of month.\n",
"net_usa = {\n",
" 'Jan-17': 10, 'Jan-18': -18, 'Jan-19': -12, \n",
" 'Feb-17': 0, 'Feb-18': -15, 'Feb-19': -16, \n",
" 'Jan-17': 10, 'Jan-18': -18, 'Jan-19': -12, 'Jan-20': -11,\n",
" 'Feb-17': 0, 'Feb-18': -15, 'Feb-19': -16, 'Feb-20': -10,\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",
@ -168,15 +170,16 @@
"metadata": {},
"outputs": [],
"source": [
"def labels(xlab, ylab): plt.xlabel(xlab); plt.ylabel(ylab); plt.legend()\n",
"\n",
"def grid(): plt.minorticks_on(); plt.grid(which='minor', ls=':', alpha=0.7)\n",
" \n",
"def header(head) -> str: return head + '\\n' + '-'.join('|' * head.count('|'))\n",
"\n",
"def markdown(fn) -> callable: return lambda *args: display(Markdown('\\n'.join(fn(*args))))\n",
"\n",
"def parp(state, date=now) -> int: return net(state, date) - state.lean "
"def parp(state, date=now) -> int: return net(state, date) - state.lean\n",
"\n",
"def grid(dates, xlab, ylab): \n",
" plt.minorticks_on(); plt.grid(which='minor', axis='y', ls=':', alpha=0.7)\n",
" plt.xticks(range(len(dates)), dates, rotation=90)\n",
" plt.xlabel(xlab); plt.ylabel(ylab); plt.legend()"
]
},
{
@ -192,11 +195,11 @@
" N = len(dates)\n",
" err = [[EV(states, date) - EV(states, date, -swing) for date in dates],\n",
" [EV(states, date, +swing) - EV(states, date) for date in dates]]\n",
" grid()\n",
" plt.plot(range(N), [270] * N, color='darkorange', label=\"270 EVs\", lw=2)\n",
" plt.errorbar(range(N), [EV(states, date) for date in dates], fmt='D-',\n",
" yerr=err, ecolor='grey', capsize=7, label='Trump EVs ±3% swing', lw=2)\n",
" labels('Months into term', 'Electoral Votes')"
" grid(dates, 'Date', 'Electoral Vptes')\n",
" #labels('Date', 'Electoral Votes')"
]
},
{
@ -210,11 +213,10 @@
" plt.rcParams[\"figure.figsize\"] = [10, 7]\n",
" plt.style.use('fivethirtyeight')\n",
" N = len(dates)\n",
" grid()\n",
" plt.plot(range(N), [0] * N, label='Net zero', color='darkorange')\n",
" plt.plot(range(N), [-margin(states, date) for date in dates], 'D-', label='Margin to 270')\n",
" plt.plot(range(N), [net_usa[date] for date in dates], 'go-', label='Country-wide Net')\n",
" labels('Months into term', 'Net popularity')"
" grid(dates, 'Date', 'Net popularity')"
]
},
{
@ -223,7 +225,7 @@
"metadata": {},
"outputs": [],
"source": [
"def show_swings(swings=range(8)):\n",
"def show_swings(swings=range(10)):\n",
" print('Swing EV Range')\n",
" for swing in swings:\n",
" s = swing + 0.5\n",
@ -273,7 +275,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Tests** (I really should have more)."
"**Tests** (I really should have more):"
]
},
{

File diff suppressed because one or more lines are too long