Question
Problem 4: Pollster errors Now that we can represent election results and polling data in Python, we can begin to implement Nate Silver's algorithm. A
Problem 4: Pollster errors Now that we can represent election results and polling data in Python, we can begin to implement Nate Silver's algorithm. A first step is to write a function that computes the rank (average error) of a pollster's predictions. Implement the function average_error, which computes the average error of pollster edges. In each state, the error of a predicted edge is the absolute value of the difference between the predicted edge and actual edge. The average error of a collection of pollster edges is the average of these individual errors. Hint: Not all pollsters conduct polls in every state. When computing an average error, be sure to divide by the number of states in which a pollster made a prediction, not by the total number of states. Next, use average_error to implement the function pollster_errors. Again, refer to the data type reference below for more information about parameter and return types. Once completed, pollster_errors provides a quantitative method for measuring the accuracy of a pollster, based on their past predictions.
- Python
- Following the following functions:
For the following test values:
# 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. IT IT 11 - def pollster_errors (pollster_predictions, state_edges_actual): Given pollster predictions and actual state edges, retuns pollster errors. #TODO: Implement this function pass -def test_average_error(): state_edges_pred_1 = {'WA': 1.0, 'CA': -2.3, 'ID': -20.1} state_edges_act_1 = { 'WA': 2.1, 'CA': -1.4, 'ID': -19.1} assert average_error(state_edges_pred_1, state_edges_act_1) == 1.0 state_edges_pred_2 = {'WA': 1.0, 'CA': 2.0} state_edges_act_2 = { 'WA': 2.0, 'CA': 1.0} assert average_error(state_edges_pred_2, state_edges_act_2) == 1.0 state_edges_pred_3 = {'WA': 1.0, 'CA': 2.0} state_edges_act_3 = {'WA': 2.0, 'CA': 1.0, 'MA': 2.4, TOR': -3.9} assert average_error(state_edges_pred_3, state_edges_act_3) == 1.0 state_edges_pred_4 = { 'WA': 1.0} state_edges_act_4 = {'WA': 0.0} assert average_error(state_edges_pred_4, state_edges_act_4) == 1.0 - def test_pollster_errors(): predictions = { PPP': {'WA': 1.0, 'CA': -2.0, 'ID': -20.0}, 'ISPOP': {'WA': 2.0, 'ID': -19.0} } actual = {'WA': 2.0, 'CA': -1.0, 'ID': -19.0, 'OR': 2.2, 'DC': 0.1} assert pollster_errors (predictions, actual) == {'PPP': 1.0, 'ISPOP': 0.0}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started