-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll_predict.py
338 lines (263 loc) · 10.4 KB
/
poll_predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python
#poll_data.py
from fnmatch import fnmatch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import requests
from pattern import web
from matplotlib import rcParams
import re
import random
#colors from colorbrewer2.org
dark2_colors = [(0.10588235294117647, 0.6196078431372549, 0.4666666666666667),
(0.8509803921568627, 0.37254901960784315, 0.00784313725490196),
(0.4588235294117647, 0.4392156862745098, 0.7019607843137254),
(0.9058823529411765, 0.1607843137254902, 0.5411764705882353),
(0.4, 0.6509803921568628, 0.11764705882352941),
(0.9019607843137255, 0.6705882352941176, 0.00784313725490196),
(0.6509803921568628, 0.4627450980392157, 0.11372549019607843),
(0.4, 0.4, 0.4)]
#rcParams settings code from CS109
rcParams['figure.figsize'] = (10, 6)
rcParams['figure.dpi'] = 150
rcParams['axes.color_cycle'] = dark2_colors
rcParams['lines.linewidth'] = 2
rcParams['axes.grid'] = True
rcParams['axes.facecolor'] = '#eeeeee'
rcParams['font.size'] = 14
rcParams['patch.edgecolor'] = 'none'
#retrieves xml for RCP data given the poll id
def get_poll_xml(poll_id):
xml = 'http://charts.realclearpolitics.com/charts/' + str(poll_id) + '.xml'
return requests.get(xml).text
#CS109
def _strip(s):
return re.sub(r'[\W_]+', '', s)
#code provided by CS109
def plot_colors(xml):
dom = web.Element(xml)
result = {}
for graph in dom.by_tag('graph'):
title = _strip(graph.attributes['title'])
result[title] = graph.attributes['color']
return result
#extracts poll data from an XML string and converts to a DataFrame
def rcp_poll_data(xml):
dom = web.Element(xml)
result = {}
# extract dates
series = dom.by_tag('series')
date_value = series[0].by_tag('value')
date = []
for d in date_value:
date.append(pd.to_datetime(d.content))
result['date'] = date
#extract result data and titles
graphs_tag = dom.by_tag('graphs')
graph_tags = graphs_tag[0].by_tag('graph')
for graph in graph_tags:
title = graph.attributes['title']
values = []
for value in graph.by_tag('value'):
try:
values.append(float(value.content))
except ValueError:
values.append(np.nan)
result[title] = values
result = pd.DataFrame(result)
return result
#from CS109 plots RCP poll data given the poll id
def poll_plot(poll_id):
xml = get_poll_xml(poll_id)
data = rcp_poll_data(xml)
colors = plot_colors(xml)
#remove characters like apostrophes
data = data.rename(columns = {c: _strip(c) for c in data.columns})
#normalize poll numbers so they add to 100%
norm = data[colors.keys()].sum(axis=1) / 100
for c in colors.keys():
data[c] /= norm
for label, color in colors.items():
plt.plot(data.date, data[label], color=color, label=label)
plt.xticks(rotation=70)
plt.legend(loc='best')
plt.xlabel("Date")
plt.ylabel("Normalized Poll Percentage")
"""
Finds and returns links to RCP races on a page like
http://www.realclearpolitics.com/epolls/2010/governor/2010_elections_governor_map.html
"""
def find_governor_races(html):
dom = web.Element(html)
option_tags = dom.by_tag('option')
gov_links = []
#iterate through option tags
for op in option_tags:
value = op.attributes['value']
# only append governor links
if re.search("2010/governor", value):
gov_links.append(value)
return gov_links
#finds race result from RCP url
def race_result(url):
html = requests.get(url).text
dom = web.Element(html)
result = {}
#find tags unique to candidate names
tr_tags = dom.by_tag('tr.omit')
th_tags = tr_tags[0].by_tag('th')
#extract candidate names
candidate = []
#add names to candidate list without additional chars
for tags in th_tags[3:-1]:
if re.search("\(", tags.content):
candidate.append(tags.content[:-4])
else:
candidate.append(tags.content)
#find tags unique to final polling results
td_tags = tr_tags[0].next.by_tag('td')
# extract percentages
percentage = []
for tags in td_tags[3:-1]:
percentage.append(float(tags.content))
result = dict(zip(candidate, percentage))
return result
#CS109
def id_from_url(url):
"""Given a URL, look up the RCP identifier number"""
return url.split('-')[-1].split('.html')[0]
#CS109
def plot_race(url):
"""Make a plot summarizing a senate race
Overplots the actual race results as dashed horizontal lines
"""
id = id_from_url(url)
xml = get_poll_xml(id)
colors = plot_colors(xml)
if len(colors) == 0:
return
result = race_result(url)
poll_plot(id)
plt.xlabel("Date")
plt.ylabel("Polling Percentage")
for r in result:
plt.axhline(result[r], color=colors[_strip(r)], alpha=0.6, ls='--')
#CS109
def party_from_color(color):
if color in ['#0000CC', '#3B5998']:
return 'democrat'
if color in ['#FF0000', '#D30015']:
return 'republican'
return 'other'
def error_data(url):
"""
Provided by CS109
Given a Governor race URL, download the poll data and race result,
and construct a DataFrame with the following columns:
candidate: Name of the candidate
forecast_length: Number of days before the election
percentage: The percent of poll votes a candidate has.
Normalized to that the canddidate percentages add to 100%
error: Difference between percentage and actual race reulst
party: Political party of the candidate
The data are resampled as necessary, to provide one data point per day
"""
id = id_from_url(url)
xml = get_poll_xml(id)
colors = plot_colors(xml)
if len(colors) == 0:
return pd.DataFrame()
df = rcp_poll_data(xml)
result = race_result(url)
#remove non-letter characters from columns
df = df.rename(columns={c: _strip(c) for c in df.columns})
for k, v in result.items():
result[_strip(k)] = v
candidates = [c for c in df.columns if c is not 'date']
#turn into a timeseries...
df.index = df.date
#...so that we can resample at regular, daily intervals
df = df.resample('D')
df = df.dropna()
#compute forecast length in days
#(assuming that last forecast happens on the day of the election, for simplicity)
forecast_length = (df.date.max() - df.date).values
forecast_length = forecast_length / np.timedelta64(1, 'D') # convert to number of days
#compute forecast error
errors = {}
normalized = {}
poll_lead = {}
for c in candidates:
#turn raw percentage into percentage of poll votes
corr = df[c].values / df[candidates].sum(axis=1).values * 100.
err = corr - result[_strip(c)]
normalized[c] = corr
errors[c] = err
n = forecast_length.size
result = {}
result['percentage'] = np.hstack(normalized[c] for c in candidates)
result['error'] = np.hstack(errors[c] for c in candidates)
result['candidate'] = np.hstack(np.repeat(c, n) for c in candidates)
result['party'] = np.hstack(np.repeat(party_from_color(colors[_strip(c)]), n) for c in candidates)
result['forecast_length'] = np.hstack(forecast_length for _ in candidates)
result = pd.DataFrame(result)
return result
#collects error data for all races found on RCP url
def all_error_data():
page = requests.get('http://www.realclearpolitics.com/epolls/2010/governor/2010_elections_governor_map.html').text.encode('ascii', 'ignore')
races = find_governor_races(page)
#must skip url for first link, because access denied
df = error_data(races[1])
#add error data for each governor race to a dataframe
for url in races[2:]:
new = error_data(url)
df = pd.DataFrame(df.append(new, ignore_index=True))
return df
#makes prediction of current governor races given previous polling data
def bootstrap_estimate(errors,n,url):
#get current RCP polling average for candidate of interest
current_data = race_result(url)
candidate1_perc = current_data[current_data.keys()[0]]
candidate2_perc = current_data[current_data.keys()[1]]
cand_1_wins = 0
for i in xrange(n):
#randomly select a number from errors dataframe
er = random.choice(errors.error)
#estimate outcome of race by adding/subtracting error from current data
estimate_1 = candidate1_perc + er
estimate_2 = candidate2_perc - er
if estimate_1 > estimate_2:
cand_1_wins += 1
cand1_win_percentage = cand_1_wins/1000.0*100
print "The likelihood that " + current_data.keys()[0] + " wins the election is: " + str(cand1_win_percentage) + "%"
return cand1_win_percentage
#test functions
if __name__ == '__main__':
errors = all_error_data()
poll_plot(1044)
plt.title("Obama Job Approval")
page = requests.get('http://www.realclearpolitics.com/epolls/2010/governor/2010_elections_governor_map.html').text.encode('ascii', 'ignore')
#plot of polling data leading up to election
for race in find_governor_races(page):
plot_race(race)
plt.show()
#histogram showing polling errors from governor races
errors.error.hist(bins=50)
plt.xlabel("Polling Error")
plt.ylabel('N')
#standard deviation of governor polling data
std = np.std(errors.error)
print "the standard deviation of the polling errors is: " + str(std)
errors_week = []
#shows standard deviation of errors within one week of the election
errors_week = errors[errors.forecast_length < 7]
std_week = np.std(errors_week.error)
print "Standard deviation of polling errors within one week of polling: " + str(std_week)
#shows standard deviation of errors within one month of the election
errors_month = errors[errors.forecast_length < 30]
std_month = np.std(errors_month.error)
print "Standard deviation of polling errors within one month of polling: " + str(std_month)
#make prediction of current governor races using bootsrap strategy
bootstrap_estimate(errors, 1000, 'http://www.realclearpolitics.com/epolls/2013/governor/va/virginia_governor_cuccinelli_vs_mcauliffe-3033.html')
bootstrap_estimate(errors, 1000, 'http://www.realclearpolitics.com/epolls/2013/governor/nj/new_jersey_governor_christie_vs_buono-3411.html')