Created
October 24, 2020 17:39
-
-
Save CamDavidsonPilon/7982d0ce06e2dc8711af62d9f8563343 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
votes | scaled_prob | |
---|---|---|
64.02877697841728 | 0 | |
79.85611510791367 | 0 | |
93.52517985611513 | 0 | |
107.19006190396522 | 0.0 | |
120.14388489208633 | 0 | |
135.97122302158274 | 0 | |
151.07913669064752 | 0 | |
159.70386481512466 | 0.011627906976744207 | |
173.36038146227202 | 0.02906976744186074 | |
181.2698678266689 | 0.03488372093023262 | |
189.882047850092 | 0.06395348837209314 | |
197.05537895265184 | 0.09302325581395365 | |
202.79404383469966 | 0.11627906976744207 | |
209.96319223690813 | 0.15116279069767447 | |
217.14907144052202 | 0.16279069767441867 | |
225.7737995649992 | 0.17441860465116288 | |
234.394344989125 | 0.1918604651162792 | |
241.5718587920361 | 0.2151162790697676 | |
247.3063409737327 | 0.2441860465116279 | |
253.04082315542917 | 0.2732558139534884 | |
257.3406391166137 | 0.2965116279069768 | |
261.6404550777982 | 0.31976744186046524 | |
264.4972394177681 | 0.34883720930232565 | |
267.34984105738664 | 0.38372093023255816 | |
270.2024426970052 | 0.41860465116279066 | |
271.6120127154091 | 0.4593023255813954 | |
275.17567341475655 | 0.5058139534883721 | |
278.0450058557805 | 0.5174418604651162 | |
280.9310690982098 | 0.5058139534883721 | |
283.1018905805588 | 0.4883720930232558 | |
285.27271206290783 | 0.4709302325581395 | |
287.43935084490545 | 0.4593023255813954 | |
290.3254140873348 | 0.4476744186046512 | |
293.9225363894931 | 0.4476744186046512 | |
297.51129329094863 | 0.4593023255813954 | |
301.10005019240424 | 0.4709302325581395 | |
303.24995817299646 | 0.4825581395348837 | |
306.115107913669 | 0.5000000000000001 | |
307.537226033127 | 0.5232558139534883 | |
309.67876861301653 | 0.5465116279069767 | |
311.09670403212317 | 0.5755813953488372 | |
313.2340639116614 | 0.6046511627906976 | |
315.3714237911996 | 0.633720930232558 | |
318.2449389325749 | 0.6395348837209301 | |
321.1310021750042 | 0.627906976744186 | |
324.01288271708216 | 0.622093023255814 | |
326.17115609837714 | 0.622093023255814 | |
329.0363058390497 | 0.6395348837209301 | |
329.74318219842735 | 0.6569767441860465 | |
331.1736657185879 | 0.6686046511627907 | |
332.59578383804586 | 0.6918604651162791 | |
334.0262673582065 | 0.7034883720930232 | |
336.16780993809607 | 0.7267441860465116 | |
339.0413250794713 | 0.7325581395348837 | |
341.2037811611176 | 0.7267441860465116 | |
342.65099548268364 | 0.7151162790697674 | |
344.0982098042496 | 0.7034883720930232 | |
345.54542412581566 | 0.6918604651162791 | |
347.7162456081646 | 0.6744186046511628 | |
352.0327923707546 | 0.6744186046511628 | |
355.6299146729128 | 0.6744186046511628 | |
357.80491885561315 | 0.6511627906976744 | |
359.26468127823324 | 0.622093023255814 | |
361.4438681612849 | 0.5930232558139534 | |
362.8994478835536 | 0.569767441860465 | |
364.3592103061737 | 0.5406976744186046 | |
366.53421448887406 | 0.5174418604651162 | |
369.40354692989797 | 0.5290697674418605 | |
371.54508950978754 | 0.5523255813953488 | |
372.9588422285428 | 0.5872093023255813 | |
374.38096034800066 | 0.6104651162790697 | |
377.2461100886732 | 0.627906976744186 | |
379.4211142713736 | 0.6046511627906976 | |
380.8766939936423 | 0.5813953488372092 | |
382.332273715911 | 0.5581395348837209 | |
383.7878534381797 | 0.5348837209302325 | |
385.2476158607997 | 0.5058139534883721 | |
385.9879538229881 | 0.4767441860465117 | |
387.4393508449055 | 0.4593023255813954 | |
389.61017232725453 | 0.44186046511627897 | |
392.48368746862974 | 0.4476744186046512 | |
394.6252300485193 | 0.4709302325581395 | |
396.0389827672745 | 0.5058139534883721 | |
397.44855278567843 | 0.5465116279069767 | |
398.866488204785 | 0.5755813953488372 | |
400.2760582231889 | 0.6162790697674418 | |
401.6772628408901 | 0.6686046511627907 | |
403.0826501589426 | 0.7151162790697674 | |
404.47548937594115 | 0.7790697674418604 | |
405.8808766939937 | 0.8255813953488371 | |
407.2820813116948 | 0.8779069767441859 | |
409.40271038982763 | 0.9302325581395348 | |
410.8206458089343 | 0.9593023255813953 | |
412.24276392839215 | 0.9825581395348836 | |
415.1204617701188 | 0.9825581395348836 | |
416.5760414923875 | 0.9593023255813953 | |
418.04416931571023 | 0.9186046511627906 | |
419.51229713903297 | 0.8779069767441859 | |
420.26100050192406 | 0.8372093023255813 | |
421.0055211644638 | 0.8023255813953487 | |
421.7500418270035 | 0.7674418604651163 | |
422.4945624895433 | 0.7325581395348837 | |
423.2432658524344 | 0.6918604651162791 | |
423.9919692153254 | 0.6511627906976744 | |
424.74903797891926 | 0.5988372093023255 | |
425.4977413418103 | 0.5581395348837209 | |
426.24226200435 | 0.5232558139534883 | |
426.9867826668898 | 0.4883720930232558 | |
427.7271206290781 | 0.4593023255813954 | |
429.19524845240085 | 0.41860465116279066 | |
429.94395181529194 | 0.37790697674418605 | |
431.40371423791197 | 0.34883720930232565 | |
432.14405220010036 | 0.31976744186046524 | |
433.59963192236904 | 0.2965116279069768 | |
435.0552116446378 | 0.2732558139534884 | |
436.5107913669065 | 0.25 | |
437.9621883888238 | 0.2325581395348837 | |
439.4135854107412 | 0.2151162790697676 | |
440.8649824326585 | 0.1976744186046513 | |
442.31219675422454 | 0.18604651162790709 | |
444.4788355362222 | 0.17441860465116288 | |
445.92604985778814 | 0.16279069767441867 | |
448.0926886397858 | 0.15116279069767447 | |
450.2551447214321 | 0.14534883720930236 | |
452.42178350342977 | 0.13372093023255838 | |
454.5842395850761 | 0.12790697674418605 | |
457.46612012715406 | 0.12209302325581395 | |
460.35218336958343 | 0.11046511627906996 | |
463.23824661201274 | 0.09883720930232553 | |
465.40488539401036 | 0.08720930232558155 | |
468.28676593608833 | 0.08139534883720945 | |
471.17282917851765 | 0.06976744186046524 | |
476.21716580224194 | 0.058139534883721034 | |
481.25731972561476 | 0.05232558139534893 | |
488.4599297306342 | 0.04069767441860472 | |
494.9431152752217 | 0.02906976744186074 | |
503.5845742011043 | 0.01744186046511631 | |
512.9370921867159 | 0.01744186046511631 | |
523.7326417935419 | 0.011627906976744207 | |
534.5240087000168 | 0.011627906976744207 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = pd.read_csv("/Users/camerondavidson-pilon/code/election2020/fivethirtyeight.csv", sep=", ") | |
def integrate(x, y, mask): | |
return trapz(y[mask], x[mask])/trapz(y, x) | |
def percent_chance(x, y, mask): | |
return str(round(integrate(x,y, mask) * 100, 1)) + "%" | |
BIDEN_WIN = df['votes'] > 269 | |
TRUMP_WIN = df['votes'] < 269 | |
## fig1 | |
fig = plt.figure(figsize=(12, 4)) | |
ax = fig.subplots() | |
plt.axvline(269, c="k", alpha=0.2) | |
plt.axhline(0, c="k", alpha=0.2) | |
plt.plot(df['votes'], df['scaled_prob'], c="#5768ac"); | |
plt.title("Data extracted from FiveThirtyEight (Oct. 24, 2020)") | |
ax.set_yticklabels([]) | |
plt.savefig("/Users/camerondavidson-pilon/code/election2020/fig1.png") | |
## fig2 | |
## compare probabilities of winning | |
fig = plt.figure(figsize=(12, 4)) | |
ax = fig.subplots() | |
plt.axvline(269, c="k", alpha=0.2) | |
plt.axhline(0, c="k", alpha=0.2) | |
plt.plot(df['votes'], df['scaled_prob'], c="#5768ac"); | |
plt.fill_between(x=df['votes'].loc[BIDEN_WIN], y1=df['scaled_prob'].loc[BIDEN_WIN], color="#5768ac", alpha=0.2) | |
plt.text(350, 0.3, percent_chance(df['votes'], df['scaled_prob'], BIDEN_WIN)) | |
plt.title("Probability of Biden win is equal to the area under the curve") | |
ax.set_yticklabels([]) | |
plt.savefig("/Users/camerondavidson-pilon/code/election2020/fig2.png") | |
## fig3 | |
## show that height only matters if width is large too | |
PEAK = (395 < df['votes']) & (df['votes'] < 430) | |
fig = plt.figure(figsize=(12, 4)) | |
ax = fig.subplots() | |
plt.axvline(269, c="k", alpha=0.2) | |
plt.axhline(0, c="k", alpha=0.2) | |
plt.plot(df['votes'], df['scaled_prob'], c="#5768ac"); | |
plt.fill_between(x=df['votes'].loc[PEAK], y1=df['scaled_prob'].loc[PEAK], color="#6d904f", alpha=0.3) | |
plt.text(400, 0.3, percent_chance(df['votes'], df['scaled_prob'], PEAK)) | |
plt.title("Probability of this blowout peak is " + percent_chance(df['votes'], df['scaled_prob'], PEAK)) | |
ax.set_yticklabels([]) | |
plt.savefig("/Users/camerondavidson-pilon/code/election2020/fig3.png") | |
## fig4 | |
## you know what else is about equal to that area? | |
NARROW_BIDEN_WIN = (269 < df['votes']) & (df['votes'] < 322) | |
fig = plt.figure(figsize=(12, 4)) | |
ax = fig.subplots() | |
plt.axvline(269, c="k", alpha=0.2) | |
#plt.axvline(322, c="k", alpha=0.2) | |
#plt.text(315, 0.05, "322") | |
plt.plot(df['votes'], df['scaled_prob'], c="#5768ac"); | |
plt.fill_between(x=df['votes'].loc[PEAK], y1=df['scaled_prob'].loc[PEAK], color="#6d904f", alpha=0.3) | |
plt.text(400, 0.3, percent_chance(df['votes'], df['scaled_prob'], PEAK)) | |
plt.fill_between(x=df['votes'].loc[NARROW_BIDEN_WIN], y1=df['scaled_prob'].loc[NARROW_BIDEN_WIN], color="#e5ae38", alpha=0.3) | |
plt.text(285, 0.3, percent_chance(df['votes'], df['scaled_prob'], NARROW_BIDEN_WIN)) | |
plt.title("You know what else is equally as likely? \nA universe with a narrow Biden win of less than 322...") | |
ax.set_yticklabels([]) | |
ax.set_xticks([100, 200, 322, 400, 500],) | |
plt.tight_layout() | |
plt.savefig("/Users/camerondavidson-pilon/code/election2020/fig4.png") | |
## fig5 | |
NARROWER_BIDEN_WIN = (269 < df['votes']) & (df['votes'] < 292) | |
fig = plt.figure(figsize=(12, 4)) | |
ax = fig.subplots() | |
plt.axvline(269, c="k", alpha=0.2) | |
plt.axhline(0, c="k", alpha=0.2) | |
plt.plot(df['votes'], df['scaled_prob'], c="#5768ac"); | |
plt.fill_between(x=df['votes'].loc[PEAK], y1=df['scaled_prob'].loc[PEAK], color="#6d904f", alpha=0.3) | |
plt.text(400, 0.3, percent_chance(df['votes'], df['scaled_prob'], PEAK)) | |
plt.fill_between(x=df['votes'].loc[NARROWER_BIDEN_WIN], y1=df['scaled_prob'].loc[NARROWER_BIDEN_WIN], color="#5768ac", alpha=0.3) | |
plt.text(270, 0.3, percent_chance(df['votes'], df['scaled_prob'], NARROWER_BIDEN_WIN)) | |
plt.fill_between(x=df['votes'].loc[TRUMP_WIN], y1=df['scaled_prob'].loc[TRUMP_WIN], color="#fc4f30", alpha=0.2) | |
plt.text(230, 0.1, percent_chance(df['votes'], df['scaled_prob'], TRUMP_WIN)) | |
plt.title("...or a universe where Trump has a 62% chance of winning") | |
ax.set_yticklabels([]) | |
plt.savefig("/Users/camerondavidson-pilon/code/election2020/fig5.png") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment