Created
November 5, 2020 10:10
-
-
Save alexlyzhov/011c52afaf004553042b8d72a537f8a6 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
import pandas as pd | |
import itertools | |
five_csv = pd.read_csv('C:\\Users\\Alexander\\Documents\\downloads\\presidential_state_toplines_2020.csv') | |
states = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'] | |
five = [0.1514, 0.0164, 0.0095, 0.7112, 0.998, 0.9644, 0.9993, 1.0, 1.0, 0.6817, 0.5744, 0.9933, 0.3753, 0.0058, 0.9985, 0.0451, 0.0291, 0.0153, 0.027, 0.9995, 0.9995, 0.9068, 0.9506, 0.9585, 0.0711, 0.0858, 0.1565, 0.6461, 0.0226, 0.0058, 0.8886, 0.994, 0.9766, 0.873, 0.9999, 0.4913, 0.0056, 0.9782, 0.8726, 0.9993, 0.0916, 0.0524, 0.0294, 0.3945, 0.0421, 0.9899, 0.9951, 0.9917, 0.939, 0.0086, 0.0016] | |
econ = [0.0432, 0.0, 0.0, 0.7215, 1.0, 0.9983, 1.0, 1.0, 1.0, 0.7368, 0.534, 1.0, 0.3694, 0.0, 1.0, 0.0002, 0.0015, 0.0, 0.0004, 1.0, 1.0, 0.9984, 0.9759, 0.9824, 0.0155, 0.0034, 0.0094, 0.6486, 0.0, 0.0001, 0.9756, 1.0, 0.9965, 0.934, 1.0, 0.3337, 0.0, 1.0, 0.9309, 1.0, 0.0176, 0.0, 0.0, 0.263, 0.0, 0.9955, 1.0, 1.0, 0.9614, 0.0, 0.0] | |
scores = {'AK': 0, 'AL': 0, 'AR': 0, 'AZ': 0.5, 'CA': 1, 'CO': 1, 'CT': 1, 'DC': 1, 'DE': 1, 'FL': 0, 'GA': 0.5, 'HI': 1, 'IA': 0, 'ID': 0, 'IL': 1, 'IN': 0, 'KS': 0, 'KY': 0, 'LA': 0, 'MA': 1, 'MD': 1, 'ME': 1, 'MI': 1, 'MN': 1, 'MO': 0, 'MS': 0, 'MT': 0, 'NC': 0.5, 'ND': 0, 'NE': 0, 'NH': 1, 'NJ': 1, 'NM': 1, 'NV': 0.5, 'NY': 1, 'OH': 0, 'OK': 0, 'OR': 1, 'PA': 0.5, 'RI': 1, 'SC': 0, 'SD': 0, 'TN': 0, 'TX': 0, 'UT': 0, 'VA': 1, 'VT': 1, 'WA': 1, 'WI': 1, 'WV': 0, 'WY': 0,} | |
state_list = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY',] | |
def calc_briers(scores): | |
five_brier = 0 | |
econ_brier = 0 | |
for i, state in enumerate(state_list): | |
score = scores[state] | |
assert score != 0.5 | |
cur_five_brier = (five[i] - score)**2 + ((1-five[i]) - (1-score))**2 | |
cur_econ_brier = (econ[i] - score)**2 + ((1-econ[i]) - (1-score))**2 | |
five_brier += cur_five_brier / len(state_list) | |
econ_brier += cur_econ_brier / len(state_list) | |
return five_brier, econ_brier | |
swing_states = ['AZ', 'GA', 'NC', 'NV', 'PA'] | |
all_outcomes = list(itertools.product([0, 1], repeat=5)) | |
print(len(all_outcomes)) | |
for outcome in all_outcomes: | |
new_scores = scores.copy() | |
for i, swing_state in enumerate(swing_states): | |
new_scores[swing_state] = outcome[i] | |
five_brier, econ_brier = calc_briers(new_scores) | |
for i, state in enumerate(swing_states): | |
if outcome[i]: | |
print(state, end=' ') | |
print(f'{econ_brier-five_brier:.5f}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment