|
| 1 | +from unittest.mock import MagicMock |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import pytest |
| 5 | + |
| 6 | +from policyengine.core import OutputCollection |
| 7 | +from policyengine.outputs import ( |
| 8 | + CliffImpact, |
| 9 | + CliffImpactInSimulation, |
| 10 | + LaborSupplyResponse, |
| 11 | + ProgramStatistics, |
| 12 | +) |
| 13 | +from policyengine.outputs.inequality import Inequality |
| 14 | +from policyengine.tax_benefit_models.uk import analysis as uk_analysis |
| 15 | +from policyengine.tax_benefit_models.us import analysis as us_analysis |
| 16 | + |
| 17 | + |
| 18 | +def _empty_collection() -> OutputCollection: |
| 19 | + return OutputCollection(outputs=[], dataframe=pd.DataFrame()) |
| 20 | + |
| 21 | + |
| 22 | +def _empty_labor_supply_response() -> LaborSupplyResponse: |
| 23 | + return LaborSupplyResponse.model_construct() |
| 24 | + |
| 25 | + |
| 26 | +def _empty_inequality(simulation) -> Inequality: |
| 27 | + return Inequality.model_construct( |
| 28 | + simulation=simulation, |
| 29 | + income_variable="household_net_income", |
| 30 | + gini=0.0, |
| 31 | + top_10_share=0.0, |
| 32 | + top_1_share=0.0, |
| 33 | + bottom_50_share=0.0, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def _make_simulation(simulation_id: str, events: list[str]) -> MagicMock: |
| 38 | + simulation = MagicMock() |
| 39 | + simulation.id = simulation_id |
| 40 | + simulation.dataset.data.household = pd.DataFrame({"household_id": range(101)}) |
| 41 | + simulation.tax_benefit_model_version.get_variable.return_value.entity = "household" |
| 42 | + simulation.ensure.side_effect = lambda: events.append(f"{simulation_id}.ensure") |
| 43 | + return simulation |
| 44 | + |
| 45 | + |
| 46 | +def _patch_analysis_dependencies( |
| 47 | + monkeypatch, |
| 48 | + analysis_module, |
| 49 | + *, |
| 50 | + country_code: str, |
| 51 | + events: list[str], |
| 52 | + fail_on_cliff: bool, |
| 53 | + cliff_result: CliffImpact | None = None, |
| 54 | +) -> None: |
| 55 | + class DummyProgramStatistics(ProgramStatistics): |
| 56 | + def run(self): |
| 57 | + self.baseline_total = 0.0 |
| 58 | + self.reform_total = 0.0 |
| 59 | + self.change = 0.0 |
| 60 | + self.baseline_count = 0.0 |
| 61 | + self.reform_count = 0.0 |
| 62 | + self.winners = 0.0 |
| 63 | + self.losers = 0.0 |
| 64 | + |
| 65 | + def fake_program_statistics(**kwargs): |
| 66 | + return DummyProgramStatistics.model_construct(**kwargs) |
| 67 | + |
| 68 | + monkeypatch.setattr( |
| 69 | + analysis_module, |
| 70 | + "_validate_program_statistics_config", |
| 71 | + lambda baseline_simulation, reform_simulation: None, |
| 72 | + ) |
| 73 | + monkeypatch.setattr(analysis_module, "ProgramStatistics", fake_program_statistics) |
| 74 | + monkeypatch.setattr( |
| 75 | + analysis_module, |
| 76 | + "configure_labor_supply_response_variables", |
| 77 | + lambda baseline_simulation, reform_simulation, country_code: None, |
| 78 | + ) |
| 79 | + monkeypatch.setattr( |
| 80 | + analysis_module, |
| 81 | + "calculate_labor_supply_response", |
| 82 | + lambda baseline_simulation, reform_simulation, country_code: ( |
| 83 | + _empty_labor_supply_response() |
| 84 | + ), |
| 85 | + ) |
| 86 | + monkeypatch.setattr( |
| 87 | + analysis_module, |
| 88 | + "calculate_decile_impacts", |
| 89 | + lambda **kwargs: _empty_collection(), |
| 90 | + ) |
| 91 | + |
| 92 | + if country_code == "uk": |
| 93 | + monkeypatch.setattr( |
| 94 | + analysis_module, |
| 95 | + "compute_intra_decile_impacts", |
| 96 | + lambda **kwargs: _empty_collection(), |
| 97 | + ) |
| 98 | + monkeypatch.setattr( |
| 99 | + analysis_module, |
| 100 | + "calculate_uk_poverty_rates", |
| 101 | + lambda simulation: _empty_collection(), |
| 102 | + ) |
| 103 | + monkeypatch.setattr( |
| 104 | + analysis_module, |
| 105 | + "calculate_uk_inequality", |
| 106 | + _empty_inequality, |
| 107 | + ) |
| 108 | + else: |
| 109 | + monkeypatch.setattr( |
| 110 | + analysis_module, |
| 111 | + "calculate_us_poverty_rates", |
| 112 | + lambda simulation: _empty_collection(), |
| 113 | + ) |
| 114 | + monkeypatch.setattr( |
| 115 | + analysis_module, |
| 116 | + "calculate_us_inequality", |
| 117 | + lambda simulation, preset: _empty_inequality(simulation), |
| 118 | + ) |
| 119 | + |
| 120 | + if fail_on_cliff: |
| 121 | + |
| 122 | + def unexpected_cliff_call(*args, **kwargs): |
| 123 | + raise AssertionError("cliff helpers should not run by default") |
| 124 | + |
| 125 | + monkeypatch.setattr( |
| 126 | + analysis_module, |
| 127 | + "configure_cliff_impact_variables", |
| 128 | + unexpected_cliff_call, |
| 129 | + ) |
| 130 | + monkeypatch.setattr( |
| 131 | + analysis_module, |
| 132 | + "calculate_cliff_impact", |
| 133 | + unexpected_cliff_call, |
| 134 | + ) |
| 135 | + return |
| 136 | + |
| 137 | + def fake_configure_cliff_impact_variables( |
| 138 | + baseline_simulation, |
| 139 | + reform_simulation, |
| 140 | + ): |
| 141 | + events.append("configure_cliff") |
| 142 | + |
| 143 | + def fake_calculate_cliff_impact( |
| 144 | + baseline_simulation, |
| 145 | + reform_simulation, |
| 146 | + ): |
| 147 | + events.append("calculate_cliff") |
| 148 | + return cliff_result |
| 149 | + |
| 150 | + monkeypatch.setattr( |
| 151 | + analysis_module, |
| 152 | + "configure_cliff_impact_variables", |
| 153 | + fake_configure_cliff_impact_variables, |
| 154 | + ) |
| 155 | + monkeypatch.setattr( |
| 156 | + analysis_module, |
| 157 | + "calculate_cliff_impact", |
| 158 | + fake_calculate_cliff_impact, |
| 159 | + ) |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.parametrize( |
| 163 | + ("analysis_module", "country_code"), |
| 164 | + [(us_analysis, "us"), (uk_analysis, "uk")], |
| 165 | +) |
| 166 | +def test_economic_impact_analysis_defaults_cliff_impact_to_none( |
| 167 | + monkeypatch, |
| 168 | + analysis_module, |
| 169 | + country_code, |
| 170 | +): |
| 171 | + events: list[str] = [] |
| 172 | + baseline = _make_simulation("baseline", events) |
| 173 | + reform = _make_simulation("reform", events) |
| 174 | + _patch_analysis_dependencies( |
| 175 | + monkeypatch, |
| 176 | + analysis_module, |
| 177 | + country_code=country_code, |
| 178 | + events=events, |
| 179 | + fail_on_cliff=True, |
| 180 | + ) |
| 181 | + |
| 182 | + result = analysis_module.economic_impact_analysis(baseline, reform) |
| 183 | + |
| 184 | + assert result.cliff_impact is None |
| 185 | + assert events == ["baseline.ensure", "reform.ensure"] |
| 186 | + |
| 187 | + |
| 188 | +@pytest.mark.parametrize( |
| 189 | + ("analysis_module", "country_code"), |
| 190 | + [(us_analysis, "us"), (uk_analysis, "uk")], |
| 191 | +) |
| 192 | +def test_economic_impact_analysis_can_include_cliff_impacts( |
| 193 | + monkeypatch, |
| 194 | + analysis_module, |
| 195 | + country_code, |
| 196 | +): |
| 197 | + events: list[str] = [] |
| 198 | + baseline = _make_simulation("baseline", events) |
| 199 | + reform = _make_simulation("reform", events) |
| 200 | + cliff_result = CliffImpact( |
| 201 | + baseline=CliffImpactInSimulation(cliff_gap=1.0, cliff_share=0.1), |
| 202 | + reform=CliffImpactInSimulation(cliff_gap=2.0, cliff_share=0.2), |
| 203 | + ) |
| 204 | + _patch_analysis_dependencies( |
| 205 | + monkeypatch, |
| 206 | + analysis_module, |
| 207 | + country_code=country_code, |
| 208 | + events=events, |
| 209 | + fail_on_cliff=False, |
| 210 | + cliff_result=cliff_result, |
| 211 | + ) |
| 212 | + |
| 213 | + result = analysis_module.economic_impact_analysis( |
| 214 | + baseline, |
| 215 | + reform, |
| 216 | + include_cliff_impacts=True, |
| 217 | + ) |
| 218 | + |
| 219 | + assert result.cliff_impact == cliff_result |
| 220 | + assert events == [ |
| 221 | + "configure_cliff", |
| 222 | + "baseline.ensure", |
| 223 | + "reform.ensure", |
| 224 | + "calculate_cliff", |
| 225 | + ] |
0 commit comments