-
Notifications
You must be signed in to change notification settings - Fork 1
/
election.py
267 lines (216 loc) · 8.41 KB
/
election.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
# Name: Chris Edwards
# Evergreen Login: edwchr30
# Programming as a Way of Life
# Homework 5: Election prediction
import csv
import os
import time
def read_csv(path):
"""
Reads the CSV file at path, and returns a list of rows. Each row is a
dictionary that maps a column name to a value in that column, as a string.
"""
output = []
for row in csv.DictReader(open(path)):
output.append(row)
return output
################################################################################
# Problem 1: State edges
################################################################################
def row_to_edge(row):
"""
Given an election result row or poll data row, returns the Democratic edge
in that state.
"""
return float(row["Dem"]) - float(row["Rep"])
def state_edges(election_result_rows):
"""
Given a list of election result rows, returns state edges.
The input list does has no duplicate states;
that is, each state is represented at most once in the input list.
"""
"""
make a dictionary
add values to a list
put specific list values into dictionary
return dictionary
"""
################################################################################
# Problem 2: Find the most recent poll row
################################################################################
def earlier_date(date1, date2):
"""
Given two dates as strings (formatted like "Oct 06 2012"), returns True if
date1 is after date2.
"""
return (time.strptime(date1, "%b %d %Y") < time.strptime(date2, "%b %d %Y"))
def most_recent_poll_row(poll_rows, pollster, state):
"""
Given a list of poll data rows, returns the most recent row with the
specified pollster and state. If no such row exists, returns None.
"""
#TODO: Implement this function
pass
################################################################################
# Problem 3: Pollster predictions
################################################################################
def unique_column_values(rows, column_name):
"""
Given a list of rows and the name of a column (a string), returns a set
containing all values in that column.
"""
#TODO: Implement this function
pass
def pollster_predictions(poll_rows):
"""
Given a list of poll data rows, returns pollster predictions.
"""
#TODO: Implement this function
pass
################################################################################
# Problem 4: Pollster errors
################################################################################
def average_error(state_edges_predicted, state_edges_actual):
"""
Given predicted state edges and actual state edges, returns
the average error of the prediction.
"""
#TODO: Implement this function
pass
def pollster_errors(pollster_predictions, state_edges_actual):
"""
Given pollster predictions and actual state edges, retuns pollster errors.
"""
#TODO: Implement this function
pass
################################################################################
# Problem 5: Pivot a nested dictionary
################################################################################
def pivot_nested_dict(nested_dict):
"""
Pivots a nested dictionary, producing a different nested dictionary
containing the same values.
The input is a dictionary d1 that maps from keys k1 to dictionaries d2,
where d2 maps from keys k2 to values v.
The output is a dictionary d3 that maps from keys k2 to dictionaries d4,
where d4 maps from keys k1 to values v.
For example:
input = { "a" : { "x": 1, "y": 2 },
"b" : { "x": 3, "z": 4 } }
output = {'y': {'a': 2},
'x': {'a': 1, 'b': 3},
'z': {'b': 4} }
"""
#TODO: Implement this function
pass
################################################################################
# Problem 6: Average the edges in a single state
################################################################################
def average_error_to_weight(error):
"""
Given the average error of a pollster, returns that pollster's weight.
The error must be a positive number.
"""
return error ** (-2)
# The default average error of a pollster who did no polling in the
# previous election.
DEFAULT_AVERAGE_ERROR = 5.0
def pollster_to_weight(pollster, pollster_errors):
""""
Given a pollster and a pollster errors, return the given pollster's weight.
"""
if pollster not in pollster_errors:
weight = average_error_to_weight(DEFAULT_AVERAGE_ERROR)
else:
weight = average_error_to_weight(pollster_errors[pollster])
return weight
def weighted_average(items, weights):
"""
Returns the weighted average of a list of items.
Arguments:
items is a list of numbers.
weights is a list of numbers, whose sum is nonzero.
Each weight in weights corresponds to the item in items at the same index.
items and weights must be the same length.
"""
assert len(items) > 0
assert len(items) == len(weights)
#TODO: Implement this function
pass
def average_edge(pollster_edges, pollster_errors):
"""
Given pollster edges and pollster errors, returns the average of these edges
weighted by their respective pollster errors.
"""
#TODO: Implement this function
pass
################################################################################
# Problem 7: Predict the 2012 election
################################################################################
def predict_state_edges(pollster_predictions, pollster_errors):
"""
Given pollster predictions from a current election and pollster errors from
a past election, returns the predicted state edges of the current election.
"""
#TODO: Implement this function
pass
################################################################################
# Electoral College, Main Function, etc.
################################################################################
def electoral_college_outcome(ec_rows, state_edges):
"""
Given electoral college rows and state edges, returns the outcome of
the Electoral College, as a map from "Dem" or "Rep" to a number of
electoral votes won. If a state has an edge of exactly 0.0, its votes
are evenly divided between both parties.
"""
ec_votes = {} # maps from state to number of electoral votes
for row in ec_rows:
ec_votes[row["State"]] = float(row["Electors"])
outcome = {"Dem": 0, "Rep": 0}
for state in state_edges:
votes = ec_votes[state]
if state_edges[state] > 0:
outcome["Dem"] += votes
elif state_edges[state] < 0:
outcome["Rep"] += votes
else:
outcome["Dem"] += votes/2.0
outcome["Rep"] += votes/2.0
return outcome
def print_dict(dictionary):
"""
Given a dictionary, prints its contents in sorted order by key.
Rounds float values to 8 decimal places.
"""
for key in sorted(dictionary.keys()):
value = dictionary[key]
if type(value) == float:
value = round(value, 8)
print key, value
def main():
"""
Main function, which is executed when election.py is run as a Python script.
"""
# Read state edges from the 2008 election
edges_2008 = state_edges(read_csv("data/2008-results.csv"))
# Read pollster predictions from the 2008 and 2012 election
polls_2008 = pollster_predictions(read_csv("data/2008-polls.csv"))
polls_2012 = pollster_predictions(read_csv("data/2012-polls.csv"))
# Compute pollster errors for the 2008 election
error_2008 = pollster_errors(polls_2008, edges_2008)
# Predict the 2012 state edges
prediction_2012 = predict_state_edges(polls_2012, error_2008)
# Obtain the 2012 Electoral College outcome
ec_2012 = electoral_college_outcome(read_csv("data/2012-electoral-college.csv"),
prediction_2012)
print "Predicted 2012 election results:"
print_dict(prediction_2012)
print
print "Predicted 2012 Electoral College outcome:"
print_dict(ec_2012)
print
# If this file, election.py, is run as a Python script (such as by typing
# "python election.py" at the command shell), then run the main() function.
if __name__ == "__main__":
main()