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

Release/2.2.4 🎉 #139

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

# 2.2.4 (11/5/2024)
- fix: truncation can fail catastrophically when % reporting is too low [#138](https://github.com/washingtonpost/elex-live-model/pull/138)

# 2.2.3 (11/5/2024)
- chore: adding additional log [#135](https://github.com/washingtonpost/elex-live-model/pull/135)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
LONG_DESCRIPTION = f.read()

# The full version, including alpha/beta/rc tags
RELEASE = "2.2.3"
RELEASE = "2.2.4"
# The short X.Y version
VERSION = ".".join(RELEASE.split(".")[:2])

Expand Down
4 changes: 2 additions & 2 deletions src/elexmodel/models/BootstrapElectionModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,10 @@ def _generate_nonreporting_bounds(

# if percent reporting is 0 or 1, don't try to compute anything and revert to naive bounds
lower_bound[
np.isclose(nonreporting_expected_vote_frac, 0) | np.isclose(nonreporting_expected_vote_frac, 1)
(nonreporting_expected_vote_frac < 0.5) | np.isclose(nonreporting_expected_vote_frac, 1)
] = unobserved_lower_bound
upper_bound[
np.isclose(nonreporting_expected_vote_frac, 0) | np.isclose(nonreporting_expected_vote_frac, 1)
(nonreporting_expected_vote_frac < 0.5) | np.isclose(nonreporting_expected_vote_frac, 1)
] = unobserved_upper_bound

return lower_bound.values.reshape(-1, 1), upper_bound.values.reshape(-1, 1)
Expand Down
62 changes: 25 additions & 37 deletions tests/models/test_bootstrap_election_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_estimate_strata_dist(bootstrap_election_model, rng):

def test_generate_nonreporting_bounds(bootstrap_election_model, rng):
nonreporting_units = pd.DataFrame(
[[0.1, 1.2, 75], [0.8, 0.8, 24], [0.1, 0.01, 0], [-0.2, 0.8, 99], [-0.3, 0.9, 100]],
[[0.1, 1.2, 75], [0.1, 0.01, 0], [-0.2, 0.8, 99], [-0.3, 0.9, 100]],
columns=["results_normalized_margin", "turnout_factor", "percent_expected_vote"],
)

Expand All @@ -162,35 +162,29 @@ def test_generate_nonreporting_bounds(bootstrap_election_model, rng):
assert lower[0] == pytest.approx(-0.175)
assert upper[0] == pytest.approx(0.325)

assert lower[1] == pytest.approx(-0.568)
assert upper[1] == pytest.approx(0.952)

# if expected vote is close to 0 or 1 we set the bounds to be the extreme case
assert lower[2] == bootstrap_election_model.y_unobserved_lower_bound
assert upper[2] == bootstrap_election_model.y_unobserved_upper_bound
assert lower[1] == bootstrap_election_model.y_unobserved_lower_bound
assert upper[1] == bootstrap_election_model.y_unobserved_upper_bound

assert lower[3] == pytest.approx(-0.208)
assert upper[3] == pytest.approx(-0.188)
assert lower[2] == pytest.approx(-0.208)
assert upper[2] == pytest.approx(-0.188)

assert lower[4] == bootstrap_election_model.y_unobserved_lower_bound
assert upper[4] == bootstrap_election_model.y_unobserved_upper_bound
assert lower[3] == bootstrap_election_model.y_unobserved_lower_bound
assert upper[3] == bootstrap_election_model.y_unobserved_upper_bound

lower, upper = bootstrap_election_model._generate_nonreporting_bounds(nonreporting_units, "turnout_factor")

assert lower[0] == pytest.approx(0.96)
assert upper[0] == pytest.approx(4.8)

assert lower[1] == pytest.approx(1.081081081)
assert upper[1] == pytest.approx(80) # this is 80 since we divide 0.8 / 0.01 (it's clipped)

assert lower[2] == bootstrap_election_model.z_unobserved_lower_bound
assert upper[2] == bootstrap_election_model.z_unobserved_upper_bound
assert lower[1] == bootstrap_election_model.z_unobserved_lower_bound
assert upper[1] == bootstrap_election_model.z_unobserved_upper_bound

assert lower[3] == pytest.approx(0.536912752)
assert upper[3] == pytest.approx(1.632653061)
assert lower[2] == pytest.approx(0.536912752)
assert upper[2] == pytest.approx(1.632653061)

assert lower[4] == bootstrap_election_model.z_unobserved_lower_bound
assert upper[4] == bootstrap_election_model.z_unobserved_upper_bound
assert lower[3] == bootstrap_election_model.z_unobserved_lower_bound
assert upper[3] == bootstrap_election_model.z_unobserved_upper_bound

# changing parameters
bootstrap_election_model.y_unobserved_lower_bound = -0.8
Expand All @@ -202,17 +196,14 @@ def test_generate_nonreporting_bounds(bootstrap_election_model, rng):
assert lower[0] == pytest.approx(-0.125)
assert upper[0] == pytest.approx(0.275)

assert lower[1] == pytest.approx(-0.416)
assert upper[1] == pytest.approx(0.8)
assert lower[1] == bootstrap_election_model.y_unobserved_lower_bound
assert upper[1] == bootstrap_election_model.y_unobserved_upper_bound

assert lower[2] == bootstrap_election_model.y_unobserved_lower_bound
assert upper[2] == bootstrap_election_model.y_unobserved_upper_bound
assert lower[2] == pytest.approx(-0.206)
assert upper[2] == pytest.approx(-0.19)

assert lower[3] == pytest.approx(-0.206)
assert upper[3] == pytest.approx(-0.19)

assert lower[4] == bootstrap_election_model.y_unobserved_lower_bound
assert upper[4] == bootstrap_election_model.y_unobserved_upper_bound
assert lower[3] == bootstrap_election_model.y_unobserved_lower_bound
assert upper[3] == bootstrap_election_model.y_unobserved_upper_bound

bootstrap_election_model.y_unobserved_lower_bound = 0.8
bootstrap_election_model.y_unobserved_upper_bound = 1.2
Expand All @@ -222,17 +213,14 @@ def test_generate_nonreporting_bounds(bootstrap_election_model, rng):
assert lower[0] == pytest.approx(1.411764706)
assert upper[0] == pytest.approx(1.846153846)

assert lower[1] == pytest.approx(2.352941176)
assert upper[1] == pytest.approx(5.714285714)

assert lower[2] == bootstrap_election_model.z_unobserved_lower_bound
assert upper[2] == bootstrap_election_model.z_unobserved_upper_bound
assert lower[1] == bootstrap_election_model.z_unobserved_lower_bound
assert upper[1] == bootstrap_election_model.z_unobserved_upper_bound

assert lower[3] == pytest.approx(0.733944954)
assert upper[3] == pytest.approx(0.898876404)
assert lower[2] == pytest.approx(0.733944954)
assert upper[2] == pytest.approx(0.898876404)

assert lower[4] == bootstrap_election_model.z_unobserved_lower_bound
assert upper[4] == bootstrap_election_model.z_unobserved_upper_bound
assert lower[3] == bootstrap_election_model.z_unobserved_lower_bound
assert upper[3] == bootstrap_election_model.z_unobserved_upper_bound


def test_strata_pit(bootstrap_election_model, rng):
Expand Down
Loading