Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elex 4455 add race call contest agg #101

Merged
merged 24 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Parameters for the CLI tool:
| aggregates | list | list of geographies for which to calculate predictions beyond the original `postal_code`, `county_fips`, `district`, `county_classification` |
| pi_method | string | method for constructing prediction intervals (`nonparametric` or `gaussian`) |
| model_parameters | dict | dictionary of model specific parameters e.g. `--model_parameters='{"robust":True}'` |
| called_contests | dict | a dictionary of called contests. specific to Bootstrap model for now. e.g. `--called_contests='{"VA": -1}'` |
| save_output | list | `results`, `data`, `config` |
| unexpected_units | int | number of unexpected units to simulate; only used for testing and does not work with historical run |

Expand Down
3 changes: 3 additions & 0 deletions src/elexmodel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def type_cast_value(self, ctx, value):
cls=PythonLiteralOption,
help="A dictionary of model parameters",
)
@click.option(
"--called_contests", "called_contests", default="{}", cls=PythonLiteralOption, help="A dictionary with race calls"
dmnapolitano marked this conversation as resolved.
Show resolved Hide resolved
)
@click.option(
"--percent_reporting",
"percent_reporting",
Expand Down
7 changes: 5 additions & 2 deletions src/elexmodel/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def get_aggregate_list(self, office, aggregate):
raw_aggregate_list = base_aggregate + [aggregate]
return sorted(list(set(raw_aggregate_list)), key=lambda x: AGGREGATE_ORDER.index(x))

def get_national_summary_votes_estimates(self, nat_sum_data_dict=None, called_states={}, base_to_add=0, alpha=0.99):
return self.model.get_national_summary_estimates(nat_sum_data_dict, called_states, base_to_add, alpha)
def get_national_summary_votes_estimates(self, nat_sum_data_dict=None, base_to_add=0, alpha=0.99):
return self.model.get_national_summary_estimates(nat_sum_data_dict, base_to_add, alpha)

def get_estimates(
self,
Expand Down Expand Up @@ -200,6 +200,7 @@ def get_estimates(
aggregates = kwargs.get("aggregates", DEFAULT_AGGREGATES[office])
fixed_effects = kwargs.get("fixed_effects", {})
pi_method = kwargs.get("pi_method", "nonparametric")
called_contests = kwargs.get("called_contests", None)
save_output = kwargs.get("save_output", ["results"])
save_results = "results" in save_output
save_data = "data" in save_output
Expand Down Expand Up @@ -359,6 +360,7 @@ def get_estimates(
results_handler.unexpected_units,
aggregate_list,
estimand,
called_contests=called_contests,
)
alpha_to_agg_prediction_intervals = {}
for alpha in prediction_intervals:
Expand All @@ -370,6 +372,7 @@ def get_estimates(
alpha,
alpha_to_unit_prediction_intervals[alpha],
estimand,
called_contests=called_contests,
)
if isinstance(self.model, ConformalElectionModel):
self.all_conformalization_data_agg_dict[alpha][
Expand Down
4 changes: 2 additions & 2 deletions src/elexmodel/models/BaseElectionModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_aggregate_predictions(
unexpected_units: pd.DataFrame,
aggregate: list,
estimand: str,
*kwargs,
**kwargs,
) -> pd.DataFrame:
"""
Aggregate predictions and results by aggregate (ie. postal_code, county_fips etc.). Add results from reporting
Expand Down Expand Up @@ -158,7 +158,7 @@ def get_aggregate_prediction_intervals(
unexpected_units: pd.DataFrame,
aggregate: list,
alpha: float,
*kwargs,
**kwargs,
) -> PredictionIntervals:
"""
Generates and returns aggregate prediction intervals for arbitrary aggregates
Expand Down
234 changes: 134 additions & 100 deletions src/elexmodel/models/BootstrapElectionModel.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/elexmodel/models/GaussianElectionModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def get_aggregate_prediction_intervals(
alpha: float,
unit_prediction_intervals: PredictionIntervals,
estimand: str,
**kwargs,
) -> PredictionIntervals:
"""
Get aggregate prediction intervals. Adjust aggregate prediction intervals based on Gaussian models
Expand Down
1 change: 1 addition & 0 deletions src/elexmodel/models/NonparametricElectionModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def get_aggregate_prediction_intervals(
alpha: float,
unit_prediction_intervals: PredictionIntervals,
estimand: str,
**kwargs,
) -> PredictionIntervals:
"""
Get aggregate prediction intervals. In the non-parametric case prediction intervals just sum.
Expand Down
Loading
Loading