import numpy as np import pytest from election_data.cells import AgeLeanModel, fit_age_lean, ipf, village_cells from election_data.elections import PARTY_A, PARTY_B, PARTY_C from election_data.lean import LEAN_KEYS, LeanParams, Observation, base_turnout, lean_shares pytestmark = pytest.mark.unit AGES = ("20-29", "30-39", "40-49", "50-64", "65+") def obs(a: int, b: int, c: int, other: int = 0, weight: float = 1.0, contested=None) -> Observation: valid = a + b + c + other return Observation( weight=weight, valid=valid, ballots=valid, electors=int(valid / 0.7), votes={PARTY_A: a, PARTY_B: b, PARTY_C: c, "其他": other}, contested=frozenset(contested or {PARTY_A, PARTY_B, PARTY_C}), ) class TestLean: def test_shares_sum_to_one(self) -> None: shares = lean_shares([obs(400, 350, 200, 50), obs(450, 400, 100, 50)]) assert shares is not None assert set(shares) == set(LEAN_KEYS) assert sum(shares.values()) == pytest.approx(1.0) def test_core_never_exceeds_mean(self) -> None: shares = lean_shares([obs(500, 300, 200)]) assert shares is not None assert shares["a_core"] <= 0.5 + 1e-9 assert shares["a_core"] + shares["a_lean"] < 0.5 def test_volatility_lowers_core(self) -> None: steady = lean_shares([obs(500, 400, 100), obs(500, 400, 100)]) swingy = lean_shares([obs(700, 200, 100), obs(300, 600, 100)]) assert steady is not None and swingy is not None assert swingy["a_core"] < steady["a_core"] def test_uncontested_party_is_ignored(self) -> None: # 第三勢力只在第二次選舉參選:平均只看那一次(0.2),而不是兩次平均(0.1)。 # 甲、乙平均各 0.45,三者合計 1.1 > 1,所以等比例縮放成 0.2 / 1.1。 shares = lean_shares( [obs(500, 500, 0, contested={PARTY_A, PARTY_B}), obs(400, 400, 200)], LeanParams(third_to_middle=0.0), ) assert shares is not None assert shares["c_lean"] == pytest.approx(0.2 / 1.1, abs=1e-6) def test_no_valid_votes(self) -> None: assert lean_shares([Observation(1.0, 0, 0, 0, {}, frozenset())]) is None def test_base_turnout_weighted(self) -> None: o1 = Observation(3.0, 70, 70, 100, {}, frozenset()) o2 = Observation(1.0, 50, 50, 100, {}, frozenset()) assert base_turnout([o1, o2]) == pytest.approx(0.65) class TestIpf: def test_matches_marginals(self) -> None: seed = np.ones((2, 3)) rows = np.array([30.0, 70.0]) cols = np.array([20.0, 50.0, 30.0]) fitted = ipf(seed, rows, cols) assert fitted.sum(axis=1) == pytest.approx(rows) assert fitted.sum(axis=0) == pytest.approx(cols) def test_rejects_inconsistent_targets(self) -> None: with pytest.raises(ValueError): ipf(np.ones((2, 2)), np.array([1.0, 1.0]), np.array([1.0, 2.0])) class TestEcologicalRegression: def test_recovers_known_association(self) -> None: rng = np.random.default_rng(7) leans = LEAN_KEYS truth = rng.dirichlet(np.ones(len(leans)) * 3, size=len(AGES)) ages, lean_rows = [], [] for _ in range(400): counts = rng.integers(50, 500, size=len(AGES)).astype(float) share = counts / counts.sum() lean = share @ truth ages.append(dict(zip(AGES, counts, strict=True))) lean_rows.append(dict(zip(leans, lean, strict=True))) model = fit_age_lean(ages, lean_rows, AGES, leans) assert np.abs(model.matrix - truth).max() < 0.02 assert model.matrix.sum(axis=1) == pytest.approx(np.ones(len(AGES))) def test_ridge_shrinks_toward_overall_mean(self) -> None: rng = np.random.default_rng(3) leans = LEAN_KEYS ages, lean_rows = [], [] for _ in range(200): counts = rng.integers(50, 500, size=len(AGES)).astype(float) lean = rng.dirichlet(np.ones(len(leans)) * 5) ages.append(dict(zip(AGES, counts, strict=True))) lean_rows.append(dict(zip(leans, lean, strict=True))) loose = fit_age_lean(ages, lean_rows, AGES, leans, ridge=0.0) tight = fit_age_lean(ages, lean_rows, AGES, leans, ridge=1e6) spread = lambda m: (m.matrix.max(axis=0) - m.matrix.min(axis=0)).max() # noqa: E731 assert spread(tight) < 0.01 assert spread(tight) < spread(loose) def test_village_cells_respect_marginals(self) -> None: model = AgeLeanModel(AGES, LEAN_KEYS, np.full((5, 6), 1 / 6)) ages = dict(zip(AGES, [100, 200, 150, 250, 300], strict=True)) lean = dict(zip(LEAN_KEYS, [0.2, 0.1, 0.25, 0.1, 0.25, 0.1], strict=True)) cells = np.array(village_cells(model, ages, lean)).reshape(5, 6) assert cells.sum(axis=1) == pytest.approx(list(ages.values()), abs=0.05) assert cells.sum(axis=0) == pytest.approx([x * 1000 for x in lean.values()], abs=0.05) def test_empty_village(self) -> None: model = AgeLeanModel(AGES, LEAN_KEYS, np.full((5, 6), 1 / 6)) assert village_cells(model, dict.fromkeys(AGES, 0), dict.fromkeys(LEAN_KEYS, 1 / 6)) == [0.0] * 30