From a1643ae35e0fe6d448c3bc41d14d497d45b890c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Juaristi?= <127882282+juaristi22@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:36:34 +0000 Subject: [PATCH 1/2] Total the UK budget headline from the government accounts The economy output's tax and benefit figures were the changes in household_tax and household_benefits for every country. policyengine-uk's household_tax holds only the taxes households pay, so it leaves out employer National Insurance; under the model's default incidence an employer NI change moves gross pay, and the headline saw only the tax on that pay change. A cut in the employer rate showed as a saving. The UK now totals gov_tax and gov_spending (gov_balance), named per country in the contract's budget_total_variables. UK runs request them as extra output variables before they run: in the annual-impact runtime, the Stage 12 output plan and the UK precompute hook. UK reports store their tax and benefit budget rows under the new names. Fixes #1170 Co-Authored-By: Claude Opus 5.5 --- changelog.d/1170.fixed.md | 1 + .../macro_output.py | 27 ++++++++ .../report_run_results.py | 23 +++++-- .../tests/test_report_run_results.py | 18 +++-- .../simulation_output_budget.py | 46 +++++++++++-- .../simulation_runtime.py | 5 ++ .../stage12_cache/levels_uk.py | 10 ++- .../stage12_runtime/output_planning.py | 4 ++ .../tests/test_simulation_output_budget.py | 68 +++++++++++++++++++ .../tests/test_simulation_output_fakes.py | 6 ++ ...test_simulation_output_household_bounds.py | 3 + .../tests/test_simulation_output_impacts.py | 19 +++++- .../test_simulation_output_uk_local_areas.py | 10 ++- .../tests/test_stage12_output_planning.py | 15 ++++ 14 files changed, 232 insertions(+), 23 deletions(-) create mode 100644 changelog.d/1170.fixed.md diff --git a/changelog.d/1170.fixed.md b/changelog.d/1170.fixed.md new file mode 100644 index 000000000..4c49ffcc2 --- /dev/null +++ b/changelog.d/1170.fixed.md @@ -0,0 +1 @@ +The UK budget headline now totals the model's government accounts, `gov_tax` and `gov_spending`, which count employer National Insurance, instead of `household_tax` and `household_benefits`. A cut in the employer NI rate showed as a saving because `household_tax` saw only the extra tax on the higher pay the model passes the cut into. UK reports store their tax and benefit budget rows under the new names. diff --git a/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/macro_output.py b/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/macro_output.py index 2681faaa5..8e456efc0 100644 --- a/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/macro_output.py +++ b/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/macro_output.py @@ -2,6 +2,7 @@ from __future__ import annotations +from types import MappingProxyType from typing import Generic, Literal, TypeVar from pydantic import BaseModel, ConfigDict, Field, RootModel @@ -41,6 +42,32 @@ class BudgetaryImpact(MacroOutputModel): BudgetaryOutput = BudgetaryImpact +# The household variables whose reform-minus-baseline totals are a country's +# ``tax_revenue_impact`` and ``benefit_spending_impact``. The UK's are +# policyengine-uk's government accounts, whose difference is its +# ``gov_balance``. Its ``household_tax`` holds only the taxes households pay, +# so it leaves out employer National Insurance: under the model's default +# incidence an employer NI change moves gross pay, and ``household_tax`` sees +# only the income tax and employee NI on that pay change, which has the +# opposite sign to the change in receipts. +HOUSEHOLD_BUDGET_TOTAL_VARIABLES = ("household_tax", "household_benefits") +BUDGET_TOTAL_VARIABLES = MappingProxyType( + { + "us": HOUSEHOLD_BUDGET_TOTAL_VARIABLES, + "uk": ("gov_tax", "gov_spending"), + } +) + + +def budget_total_variables(country: str) -> tuple[str, str]: + """Return the household variables a country's tax and benefit figures total. + + The pair is (tax revenue, benefit spending). A country without an entry + in ``BUDGET_TOTAL_VARIABLES`` totals ``household_tax`` and + ``household_benefits``. + """ + return BUDGET_TOTAL_VARIABLES.get(country.lower(), HOUSEHOLD_BUDGET_TOTAL_VARIABLES) + class DetailedBudgetProgramOutput(MacroOutputModel): """Baseline and reform totals for one program. diff --git a/libs/policyengine-stage12-persistence/src/policyengine_stage12_persistence/report_run_results.py b/libs/policyengine-stage12-persistence/src/policyengine_stage12_persistence/report_run_results.py index d3ce95106..d3d430480 100644 --- a/libs/policyengine-stage12-persistence/src/policyengine_stage12_persistence/report_run_results.py +++ b/libs/policyengine-stage12-persistence/src/policyengine_stage12_persistence/report_run_results.py @@ -20,7 +20,10 @@ from typing import TYPE_CHECKING, Any, Literal, NamedTuple from uuid import uuid4 -from policyengine_simulation_contract.macro_output import SingleYearMacroOutput +from policyengine_simulation_contract.macro_output import ( + SingleYearMacroOutput, + budget_total_variables, +) from policyengine_simulation_persistence.result_tables import ( BudgetSummary, CongressionalDistrictImpact, @@ -56,7 +59,10 @@ # The household variable each headline budget figure totals, and the column # its figure goes in. ``budgetary_impact`` is tax minus benefits, as the -# economy output computes it, so it needs no row of its own. +# economy output computes it, so it needs no row of its own. The tax and +# benefit figures total the country's ``budget_total_variables``, which +# replace the names here where they differ: the UK's are ``gov_tax`` and +# ``gov_spending``. BUDGET_FIGURES = ( ("tax_revenue_impact", "household_tax", "change"), ("benefit_spending_impact", "household_benefits", "change"), @@ -409,14 +415,21 @@ def _budget_rows( ) -> tuple[dict[str, Any], ...]: """Return the headline budget figures; state income tax only for the US. - Today's output reports 0.0 state income tax for a country without it, so a - reader without that row reports 0.0 again. + Each row names the variable its figure totals, so a UK report's tax and + benefit rows are ``gov_tax`` and ``gov_spending``. Today's output reports + 0.0 state income tax for a country without it, so a reader without that + row reports 0.0 again. """ + tax_variable, benefit_variable = budget_total_variables(country) + variables = { + "tax_revenue_impact": tax_variable, + "benefit_spending_impact": benefit_variable, + } return tuple( { "id": uuid4(), **keys, - "variable_name": variable, + "variable_name": variables.get(figure, variable), "entity": "household", column: getattr(economy.budget, figure), } diff --git a/libs/policyengine-stage12-persistence/tests/test_report_run_results.py b/libs/policyengine-stage12-persistence/tests/test_report_run_results.py index f7abcfe52..07cb0e9a3 100644 --- a/libs/policyengine-stage12-persistence/tests/test_report_run_results.py +++ b/libs/policyengine-stage12-persistence/tests/test_report_run_results.py @@ -18,6 +18,7 @@ uk_output_with_withheld_codes, us_output_with_every_part, ) +from policyengine_simulation_contract.macro_output import budget_total_variables from policyengine_simulation_contract.stage12_execution import ReportRunIds from policyengine_simulation_persistence import CliffImpact from policyengine_simulation_persistence.result_tables import BudgetSummary @@ -100,13 +101,15 @@ def _rebuilt_district(row): return f"{state}-{number:02d}" -def _rebuilt_budget(seeder, report_run_id): +def _rebuilt_budget(seeder, report_run_id, country="us"): rows = { row["variable_name"]: row for row in seeder.rows("budget_summary", report_run_id=report_run_id) } - tax = rows["household_tax"]["change"] - benefits = rows["household_benefits"]["change"] + # Each country's tax and benefit rows name the variables they total. + tax_variable, benefit_variable = budget_total_variables(country) + tax = rows[tax_variable]["change"] + benefits = rows[benefit_variable]["change"] # Only the US models state income tax; today's output reports 0.0 elsewhere. state_tax = rows.get("household_state_income_tax", {"change": 0.0})["change"] return { @@ -421,12 +424,17 @@ def test_the_uk_output_uses_its_own_poverty_types_and_wealth_deciles( rebuilt = _rebuilt_deciles(rows) assert rebuilt["average"] == output[part]["average"] assert rebuilt["relative"] == pytest.approx(output[part]["relative"], rel=1e-12) - assert "household_state_income_tax" not in { + budget_variables = { row["variable_name"] for row in seeder.rows("budget_summary", report_run_id=run_ids.report_run_id) } + assert "household_state_income_tax" not in budget_variables + # The UK's tax and benefit figures total the model's government accounts, + # which count employer National Insurance; household_tax does not. + assert {"gov_tax", "gov_spending"} <= budget_variables + assert not {"household_tax", "household_benefits"} & budget_variables assert ( - _rebuilt_budget(seeder, run_ids.report_run_id) + _rebuilt_budget(seeder, run_ids.report_run_id, country="uk") == (uk_economy_output()["budget"]) ) (inequality,) = seeder.rows( diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_budget.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_budget.py index ed7407536..5b387e7d2 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_budget.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_budget.py @@ -5,6 +5,11 @@ import math from typing import TYPE_CHECKING +from policyengine_simulation_contract.macro_output import ( + HOUSEHOLD_BUDGET_TOTAL_VARIABLES, + budget_total_variables, +) + from policyengine_simulation_executor.simulation_macro_output import ( BudgetaryImpact, DetailedBudgetOutput, @@ -85,21 +90,48 @@ def _require_plausible_households(country: str, households: float) -> None: raise ImplausibleEntityCountError(msg) +def configure_budget_total_variables(country: str, *simulations: Simulation) -> None: + """Add the country's budget totals to each simulation's output variables. + + policyengine's default outputs hold ``household_tax`` and + ``household_benefits`` but not the UK's ``gov_tax`` and ``gov_spending``, + which ``build_budgetary_impact`` totals. Call this before the simulations + run (``ensure()``), as policyengine's own + ``configure_budgetary_impact_variables`` is called for the US. A country + that totals the default variables needs nothing added. + """ + variables = budget_total_variables(country) + if variables == HOUSEHOLD_BUDGET_TOTAL_VARIABLES: + return + from policyengine.outputs.extra_variables import ( # noqa: PLC0415 # policyengine stays out of this module's import-time imports + add_extra_variables, + ) + + for simulation in simulations: + add_extra_variables(simulation, {"household": list(variables)}) + + def build_budgetary_impact( country: str, baseline: Simulation, reform: Simulation ) -> BudgetaryImpact: - """Compute headline budget figures from household tax and benefit changes. - - ``budgetary_impact`` is the tax revenue change minus the benefit spending - change. State income tax is only modelled for the US; elsewhere its impact - is 0.0. A household count outside the country's plausible range raises + """Compute headline budget figures from the country's tax and benefit totals. + + ``tax_revenue_impact`` and ``benefit_spending_impact`` are the changes in + the household variables ``budget_total_variables`` names for the country: + ``household_tax`` and ``household_benefits`` for the US, and the UK + model's government accounts ``gov_tax`` and ``gov_spending``, which count + employer National Insurance. ``budgetary_impact`` is the tax revenue + change minus the benefit spending change. State income tax is only + modelled for the US; elsewhere its impact is 0.0. A household count + outside the country's plausible range raises ``ImplausibleEntityCountError``. """ + tax_variable, benefit_variable = budget_total_variables(country) tax_revenue_impact = _change_output_variable( - baseline, reform, "household_tax", entity="household" + baseline, reform, tax_variable, entity="household" ) benefit_spending_impact = _change_output_variable( - baseline, reform, "household_benefits", entity="household" + baseline, reform, benefit_variable, entity="household" ) state_tax_revenue_impact = ( _change_output_variable( diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py index 709711dfe..dbc341414 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_runtime.py @@ -44,6 +44,9 @@ parse_year, wrapper_policy, ) +from policyengine_simulation_executor.simulation_output_budget import ( + configure_budget_total_variables, +) from policyengine_simulation_executor.simulation_output_builder import ( SimulationOutputBuilder, ) @@ -610,6 +613,8 @@ def _run_simulation_impl_core( policy=reform_policy, region=region_resolution, ) + # Before either runs: the UK's budget totals are not default outputs. + configure_budget_total_variables(country, baseline, reform) _report_calculation_start(baseline, progress, JobStage.BASELINE) _report_calculation_start(reform, progress, JobStage.REFORM) diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_cache/levels_uk.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_cache/levels_uk.py index bc42af07f..6ff993aac 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_cache/levels_uk.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_cache/levels_uk.py @@ -12,6 +12,9 @@ from policyengine.core.scoping_strategy import RegionGroupStrategy +from policyengine_simulation_executor.simulation_output_budget import ( + configure_budget_total_variables, +) from policyengine_simulation_executor.stage12_cache.levels import ( BaselineScope, BaselineTarget, @@ -167,7 +170,12 @@ def uk_level_of(run: RunScope) -> DatasetLevel | None: def _prepare_uk_baseline_outputs(baseline: Simulation) -> None: - """Add nothing: UK analyses request no extra baseline outputs.""" + """Add the budget totals every UK request adds before ``ensure()``. + + A precomputed baseline must carry them, or every request would fail the + artifact's column check and recompute. + """ + configure_budget_total_variables(UK, baseline) def uk_prebuilt() -> CountryPrebuilt: diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/output_planning.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/output_planning.py index bb0d58056..cb96801f0 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/output_planning.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/stage12_runtime/output_planning.py @@ -28,6 +28,9 @@ ) from policyengine_simulation_observability.stages import Stage +from policyengine_simulation_executor.simulation_output_budget import ( + configure_budget_total_variables, +) from policyengine_simulation_executor.uk_local_authority_metadata import ( UK_AREA_DATASET_COLUMNS, uk_area_codes_required, @@ -199,6 +202,7 @@ def _configure_country_outputs( ) configure_budgetary_impact_variables(baseline, reform) + configure_budget_total_variables(country, baseline, reform) return labor_supply_active diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_budget.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_budget.py index d61140bdb..3edf3648b 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_budget.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_budget.py @@ -11,6 +11,7 @@ from policyengine_simulation_executor.simulation_output_budget import ( build_budgetary_impact, build_detailed_budget, + configure_budget_total_variables, ) COMMON_LOGGER = "policyengine_simulation_executor.simulation_output_common" @@ -66,6 +67,73 @@ def test_households_counts_each_weight_once_on_weighted_frames(): assert budget.state_tax_revenue_impact == 8.0 +def _employer_ni_cut(): + """Two households under a cut in the employer NI rate, as the UK model scores it. + + Each household's employer NI (10 and 20) goes to zero and, as the model's + default incidence has it, into gross pay. ``household_tax``, the income + tax and employee NI the household pays, rises by the tax on that pay (3 + and 6); ``gov_tax``, which also holds employer NI, falls. Universal Credit + falls a little with the higher pay. + """ + baseline = _simulation( + { + "household_weight": [1.0, 2.0], + "household_net_income": [100.0, 200.0], + "household_tax": [20.0, 40.0], + "household_benefits": [5.0, 10.0], + "gov_tax": [30.0, 60.0], + "gov_spending": [5.0, 10.0], + } + ) + reform = _simulation( + { + "household_weight": [1.0, 2.0], + "household_net_income": [106.0, 212.0], + "household_tax": [23.0, 46.0], + "household_benefits": [4.0, 8.0], + "gov_tax": [23.0, 46.0], + "gov_spending": [4.0, 8.0], + } + ) + return baseline, reform + + +def test_uk_budget_totals_the_government_accounts_which_count_employer_ni(): + baseline, reform = _employer_ni_cut() + + budget = build_budgetary_impact("uk", baseline, reform) + + # From household_tax and household_benefits the cut would read as a + # saving: 1 * 3 + 2 * 6 more tax and 1 * 1 + 2 * 2 less spending, +20. + # gov_tax also loses the employer NI, so the cut costs 30. + assert budget.tax_revenue_impact == 1.0 * (23.0 - 30.0) + 2.0 * (46.0 - 60.0) + assert budget.benefit_spending_impact == 1.0 * (4.0 - 5.0) + 2.0 * (8.0 - 10.0) + assert budget.budgetary_impact == -30.0 + assert budget.state_tax_revenue_impact == 0.0 + assert budget.households == 3.0 + assert budget.baseline_net_income == 500.0 + + +def test_uk_runs_request_the_government_accounts_and_others_nothing(): + uk_baseline = SimpleNamespace(extra_variables={"person": ["age"]}) + uk_reform = SimpleNamespace(extra_variables=None) + us = SimpleNamespace(extra_variables={}) + + configure_budget_total_variables("uk", uk_baseline, uk_reform) + configure_budget_total_variables("UK", uk_baseline) + configure_budget_total_variables("us", us) + + # Added once each, beside the extras a simulation already had. + assert uk_baseline.extra_variables == { + "person": ["age"], + "household": ["gov_tax", "gov_spending"], + } + assert uk_reform.extra_variables == {"household": ["gov_tax", "gov_spending"]} + # household_tax and household_benefits are policyengine's default outputs. + assert us.extra_variables == {} + + def _mismatched_weight_warnings(caplog: pytest.LogCaptureFixture) -> list[str]: return [ record.getMessage() diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_fakes.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_fakes.py index 5e80312e6..48fb8eb6d 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_fakes.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_fakes.py @@ -100,6 +100,8 @@ def macro_baseline_reform(): net income of 1000. Unweighted sums of the same rows would give 120, 40, 24 and 1200, and summing the weights through the weighted frame (#767) would give 2.5 households, so the golden result only matches a correct build. + The UK's totals, ``gov_tax`` and ``gov_spending``, add an unchanged 10 of + employer NI to the tax, so they change by the same 100 and 30. """ baseline = FakeSimulation( weighted_entity_frame( @@ -109,6 +111,8 @@ def macro_baseline_reform(): "household_tax": [60.0, 40.0], "household_benefits": [20.0, 30.0], "household_state_income_tax": [5.0, 5.0], + "gov_tax": [70.0, 50.0], + "gov_spending": [20.0, 30.0], } ) ) @@ -120,6 +124,8 @@ def macro_baseline_reform(): "household_tax": [140.0, 80.0], "household_benefits": [50.0, 40.0], "household_state_income_tax": [21.0, 13.0], + "gov_tax": [150.0, 90.0], + "gov_spending": [50.0, 40.0], } ) ) diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_household_bounds.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_household_bounds.py index 1e4c40b06..bc6393427 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_household_bounds.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_household_bounds.py @@ -20,6 +20,9 @@ def _simulation(weights: list[float]): "household_tax": [20.0, 40.0], "household_benefits": [5.0, 10.0], "household_state_income_tax": [2.0, 3.0], + # The UK's budget totals. + "gov_tax": [30.0, 60.0], + "gov_spending": [5.0, 10.0], } ) return SimpleNamespace( diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_impacts.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_impacts.py index 638687417..f0cb240d0 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_impacts.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_impacts.py @@ -164,7 +164,7 @@ def economic_impact_analysis( assert metadata_builder._data_version() == "metadata-version" -def test_builder_budgetary_impact_uses_materialized_columns_and_uk_state_tax_zero(): +def test_builder_budgetary_impact_uses_materialized_columns_and_each_countrys_totals(): baseline = FakeSimulation( weighted_entity_frame( { @@ -173,6 +173,8 @@ def test_builder_budgetary_impact_uses_materialized_columns_and_uk_state_tax_zer "household_tax": [20.0, 40.0], "household_benefits": [5.0, 10.0], "household_state_income_tax": [2.0, 3.0], + "gov_tax": [30.0, 60.0], + "gov_spending": [6.0, 12.0], } ) ) @@ -184,6 +186,8 @@ def test_builder_budgetary_impact_uses_materialized_columns_and_uk_state_tax_zer "household_tax": [25.0, 50.0], "household_benefits": [4.0, 8.0], "household_state_income_tax": [4.0, 6.0], + "gov_tax": [33.0, 70.0], + "gov_spending": [5.0, 9.0], } ) ) @@ -205,7 +209,18 @@ def test_builder_budgetary_impact_uses_materialized_columns_and_uk_state_tax_zer "households": 1.0 + 2.0, "baseline_net_income": 1.0 * 100.0 + 2.0 * 200.0, } - assert uk_budget.state_tax_revenue_impact == 0.0 + # The UK totals the model's government accounts instead, and has no + # state income tax. + uk_tax_revenue_impact = 1.0 * (33.0 - 30.0) + 2.0 * (70.0 - 60.0) + uk_benefit_spending_impact = 1.0 * (5.0 - 6.0) + 2.0 * (9.0 - 12.0) + assert uk_budget.model_dump(mode="json") == { + "tax_revenue_impact": uk_tax_revenue_impact, + "state_tax_revenue_impact": 0.0, + "benefit_spending_impact": uk_benefit_spending_impact, + "budgetary_impact": uk_tax_revenue_impact - uk_benefit_spending_impact, + "households": 1.0 + 2.0, + "baseline_net_income": 1.0 * 100.0 + 2.0 * 200.0, + } def test_builder_budgetary_impact_propagates_required_calculation_errors(monkeypatch): diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_uk_local_areas.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_uk_local_areas.py index bb3e22de5..477e6a2cd 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_uk_local_areas.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_uk_local_areas.py @@ -69,7 +69,7 @@ def serialize(self): monkeypatch.setattr( "policyengine_simulation_executor.simulation_runtime._build_simulation", lambda *_args, **_kwargs: SimpleNamespace( - on_first_ensure=lambda _callback: None + on_first_ensure=lambda _callback: None, extra_variables={} ), ) monkeypatch.setattr( @@ -213,7 +213,9 @@ def load_dataset(self, _params, *, selection, country_module, data_folder=None): return self.national_dataset def build_simulation(self, _params, *, dataset, dataset_selection, policy, region): - simulation = SimpleNamespace(on_first_ensure=lambda _callback: None) + simulation = SimpleNamespace( + on_first_ensure=lambda _callback: None, extra_variables={} + ) self.run.builds.append( { "dataset": dataset, @@ -428,7 +430,9 @@ def build_simulation(_params, *, dataset, dataset_selection, policy, region): "scoping_strategy": region.scoping_strategy, } ) - return SimpleNamespace(on_first_ensure=lambda _callback: None) + return SimpleNamespace( + on_first_ensure=lambda _callback: None, extra_variables={} + ) class RecordingBreakdownBuilder: def __init__(self, **kwargs): diff --git a/projects/policyengine-simulation-executor/tests/test_stage12_output_planning.py b/projects/policyengine-simulation-executor/tests/test_stage12_output_planning.py index 6134db782..784d14d8d 100644 --- a/projects/policyengine-simulation-executor/tests/test_stage12_output_planning.py +++ b/projects/policyengine-simulation-executor/tests/test_stage12_output_planning.py @@ -169,6 +169,21 @@ def test_uk_plan_uses_uk_defaults_without_us_budget_variables(monkeypatch) -> No assert _dataset_variables(plan, "household").issubset(_variables(plan, "household")) +def test_uk_plan_adds_the_government_accounts_its_budget_totals(monkeypatch) -> None: + _route_uk_area_regions(monkeypatch, routed=False) + plan = resolve_report_output_plan(_report("uk")) + + # The UK budget totals gov_tax and gov_spending, which count employer NI; + # policyengine's UK defaults hold household_tax and household_benefits. + assert {"gov_tax", "gov_spending"}.issubset(_variables(plan, "household")) + assert {"gov_tax", "gov_spending"}.issubset( + _additional_variables(plan, "household") + ) + assert "gov_tax" not in _variables( + resolve_report_output_plan(_report()), "household" + ) + + @pytest.mark.parametrize( ("region", "routed", "planned"), [ From 321296675b322fb51bcc79813eca961b9d442b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Juaristi?= <127882282+juaristi22@users.noreply.github.com> Date: Wed, 2 Dec 2026 13:18:34 +0000 Subject: [PATCH 2/2] Total the UK's spending from its terms, so benefit uprating counts policyengine-uk's gov_spending leaves out what the benefit uprating reforms pay, which household_benefits counts, so totalling it would make such a reform look free. The UK headline's spending is now household_benefits plus the two terms gov_spending adds to it, the Lifetime ISA bonus and other_public_spending_budget_change. Its tax stays gov_tax. Decision: fix the headline here, on the policyengine-uk and policyengine.py the service already runs, so that the app is right as soon as possible. Fixing gov_spending first would need a UK data build certified on the fixed policyengine-uk, and moving the rule into policyengine.py first would need a release of it. This deploys before the API moves to .py 6.20.4 (PolicyEngine/policyengine-api#4377), so UK reports compute again when they are next opened. Follow-ups: policyengine-uk counts the uprating in gov_spending; the rule moves into policyengine.py (PolicyEngine/policyengine.py#780) and this service calls it and moves the pin; then both read gov_spending. Co-Authored-By: Claude Opus 5.5 --- changelog.d/1170.fixed.md | 2 +- .../macro_output.py | 5 +- .../simulation_output_budget.py | 68 ++++--- .../tests/test_simulation_output_budget.py | 185 +++++++++++++++++- .../tests/test_simulation_output_fakes.py | 30 ++- ...test_simulation_output_household_bounds.py | 7 +- .../tests/test_simulation_output_impacts.py | 24 ++- .../tests/test_stage12_output_planning.py | 20 +- 8 files changed, 280 insertions(+), 61 deletions(-) diff --git a/changelog.d/1170.fixed.md b/changelog.d/1170.fixed.md index 4c49ffcc2..f4cfd4e97 100644 --- a/changelog.d/1170.fixed.md +++ b/changelog.d/1170.fixed.md @@ -1 +1 @@ -The UK budget headline now totals the model's government accounts, `gov_tax` and `gov_spending`, which count employer National Insurance, instead of `household_tax` and `household_benefits`. A cut in the employer NI rate showed as a saving because `household_tax` saw only the extra tax on the higher pay the model passes the cut into. UK reports store their tax and benefit budget rows under the new names. +The UK budget headline now totals the government's accounts instead of `household_tax` and `household_benefits`. Its tax is `gov_tax`, which counts employer National Insurance: a cut in the employer NI rate showed as a saving because `household_tax` saw only the extra tax on the higher pay the model passes the cut into. Its spending is `household_benefits` plus the two terms the model's `gov_spending` adds to it, the Lifetime ISA bonus and `other_public_spending_budget_change`. UK reports store their tax and benefit budget rows as `gov_tax` and `gov_spending`. diff --git a/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/macro_output.py b/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/macro_output.py index 8e456efc0..072cef3ba 100644 --- a/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/macro_output.py +++ b/libs/policyengine-simulation-contract/src/policyengine_simulation_contract/macro_output.py @@ -49,7 +49,10 @@ class BudgetaryImpact(MacroOutputModel): # so it leaves out employer National Insurance: under the model's default # incidence an employer NI change moves gross pay, and ``household_tax`` sees # only the income tax and employee NI on that pay change, which has the -# opposite sign to the change in receipts. +# opposite sign to the change in receipts. The benefit figure is the +# government's spending, which the executor totals from its terms +# (``UK_SPENDING_TERMS``) because ``gov_spending`` leaves out the benefit +# uprating reforms. HOUSEHOLD_BUDGET_TOTAL_VARIABLES = ("household_tax", "household_benefits") BUDGET_TOTAL_VARIABLES = MappingProxyType( { diff --git a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_budget.py b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_budget.py index 5b387e7d2..05533e1be 100644 --- a/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_budget.py +++ b/projects/policyengine-simulation-executor/src/policyengine_simulation_executor/simulation_output_budget.py @@ -5,10 +5,7 @@ import math from typing import TYPE_CHECKING -from policyengine_simulation_contract.macro_output import ( - HOUSEHOLD_BUDGET_TOTAL_VARIABLES, - budget_total_variables, -) +from policyengine_simulation_contract.macro_output import budget_total_variables from policyengine_simulation_executor.simulation_macro_output import ( BudgetaryImpact, @@ -33,6 +30,20 @@ # households in the UK 2023). MAX_PLAUSIBLE_HOUSEHOLDS = {"us": 2e8, "uk": 6e7} +# The terms a UK report's ``gov_spending`` row totals, with the entity each is +# on. policyengine-uk's ``gov_spending`` leaves out what the benefit uprating +# reforms pay (``gov.contrib.benefit_uprating``), which ``household_benefits`` +# counts, so a benefit uprating reform would cost nothing. Until it counts them, +# this totals the same spending itself: the household benefits, plus the two +# terms ``gov_spending`` adds to them. A test checks that those are all it +# adds, so once ``gov_spending`` counts the uprating this is ``gov_spending`` +# and can read it instead. +UK_SPENDING_TERMS = ( + ("household", "household_benefits"), + ("person", "lifetime_isa_bonus"), + ("household", "other_public_spending_budget_change"), +) + class ImplausibleEntityCountError(ValueError): """A published entity count is negative, not finite or above its bound.""" @@ -91,24 +102,27 @@ def _require_plausible_households(country: str, households: float) -> None: def configure_budget_total_variables(country: str, *simulations: Simulation) -> None: - """Add the country's budget totals to each simulation's output variables. - - policyengine's default outputs hold ``household_tax`` and - ``household_benefits`` but not the UK's ``gov_tax`` and ``gov_spending``, - which ``build_budgetary_impact`` totals. Call this before the simulations - run (``ensure()``), as policyengine's own - ``configure_budgetary_impact_variables`` is called for the US. A country - that totals the default variables needs nothing added. + """Add the UK's budget totals to each simulation's output variables. + + Other countries total ``household_tax`` and ``household_benefits``, which + are policyengine's default outputs. The UK totals ``gov_tax`` and + ``UK_SPENDING_TERMS``, of which only ``household_benefits`` is a default + output. Call this before the simulations run (``ensure()``), as + policyengine's own ``configure_budgetary_impact_variables`` is called for + the US. """ - variables = budget_total_variables(country) - if variables == HOUSEHOLD_BUDGET_TOTAL_VARIABLES: + if country.lower() != "uk": return from policyengine.outputs.extra_variables import ( # noqa: PLC0415 # policyengine stays out of this module's import-time imports add_extra_variables, ) + extras: dict[str, list[str]] = {"household": ["gov_tax"]} + for entity, variable in UK_SPENDING_TERMS: + if variable != "household_benefits": + extras.setdefault(entity, []).append(variable) for simulation in simulations: - add_extra_variables(simulation, {"household": list(variables)}) + add_extra_variables(simulation, extras) def build_budgetary_impact( @@ -118,21 +132,27 @@ def build_budgetary_impact( ``tax_revenue_impact`` and ``benefit_spending_impact`` are the changes in the household variables ``budget_total_variables`` names for the country: - ``household_tax`` and ``household_benefits`` for the US, and the UK - model's government accounts ``gov_tax`` and ``gov_spending``, which count - employer National Insurance. ``budgetary_impact`` is the tax revenue - change minus the benefit spending change. State income tax is only - modelled for the US; elsewhere its impact is 0.0. A household count - outside the country's plausible range raises + ``household_tax`` and ``household_benefits`` for the US. For the UK, they + are the model's government accounts: ``gov_tax``, which counts employer + National Insurance, and the government's spending (``UK_SPENDING_TERMS``). + ``budgetary_impact`` is the tax revenue change minus the benefit spending + change. State income tax is only modelled for the US; elsewhere its impact + is 0.0. A household count outside the country's plausible range raises ``ImplausibleEntityCountError``. """ tax_variable, benefit_variable = budget_total_variables(country) tax_revenue_impact = _change_output_variable( baseline, reform, tax_variable, entity="household" ) - benefit_spending_impact = _change_output_variable( - baseline, reform, benefit_variable, entity="household" - ) + if country.lower() == "uk": + benefit_spending_impact = sum( + _change_output_variable(baseline, reform, variable, entity=entity) + for entity, variable in UK_SPENDING_TERMS + ) + else: + benefit_spending_impact = _change_output_variable( + baseline, reform, benefit_variable, entity="household" + ) state_tax_revenue_impact = ( _change_output_variable( baseline, diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_budget.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_budget.py index 3edf3648b..e15310a05 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_budget.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_budget.py @@ -6,22 +6,43 @@ from types import SimpleNamespace import numpy as np +import policyengine_uk import pytest from fixtures.weighted_frames import weighted_entity_frame from policyengine_simulation_executor.simulation_output_budget import ( + UK_SPENDING_TERMS, build_budgetary_impact, build_detailed_budget, configure_budget_total_variables, ) +from policyengine_uk.variables.gov.gov_spending import GOV_SPENDING_VARIABLES +from policyengine_uk.variables.household.income.household_benefits import ( + HOUSEHOLD_BENEFIT_VARIABLES, +) COMMON_LOGGER = "policyengine_simulation_executor.simulation_output_common" -def _simulation(columns: dict[str, list[float]]): - """Build a simulation whose household output is a policyengine MicroDataFrame.""" - household = weighted_entity_frame(columns) +def _simulation( + columns: dict[str, list[float]], + lifetime_isa_bonus: tuple[float, ...] = (0.0, 0.0), +): + """Build a simulation whose outputs are policyengine MicroDataFrames. + + ``columns`` are the household output. People are weighted 1 and 2 and hold + ``lifetime_isa_bonus``, which the UK's spending counts. + """ + rows = len(next(iter(columns.values()))) + household = weighted_entity_frame( + {"other_public_spending_budget_change": [0.0] * rows, **columns} + ) + person = weighted_entity_frame( + {"lifetime_isa_bonus": list(lifetime_isa_bonus)}, entity="person" + ) return SimpleNamespace( - output_dataset=SimpleNamespace(data=SimpleNamespace(household=household)) + output_dataset=SimpleNamespace( + data=SimpleNamespace(household=household, person=person) + ) ) @@ -83,7 +104,6 @@ def _employer_ni_cut(): "household_tax": [20.0, 40.0], "household_benefits": [5.0, 10.0], "gov_tax": [30.0, 60.0], - "gov_spending": [5.0, 10.0], } ) reform = _simulation( @@ -93,7 +113,6 @@ def _employer_ni_cut(): "household_tax": [23.0, 46.0], "household_benefits": [4.0, 8.0], "gov_tax": [23.0, 46.0], - "gov_spending": [4.0, 8.0], } ) return baseline, reform @@ -115,6 +134,150 @@ def test_uk_budget_totals_the_government_accounts_which_count_employer_ni(): assert budget.baseline_net_income == 500.0 +def test_uk_spending_adds_the_terms_gov_spending_adds_to_household_benefits(): + columns = { + "household_weight": [1.0, 2.0], + "household_net_income": [100.0, 200.0], + "household_tax": [20.0, 40.0], + "household_benefits": [5.0, 10.0], + "gov_tax": [30.0, 60.0], + } + baseline = _simulation(columns, lifetime_isa_bonus=(1.0, 0.0)) + reform = _simulation( + {**columns, "other_public_spending_budget_change": [3.0, 4.0]}, + lifetime_isa_bonus=(2.0, 0.5), + ) + + budget = build_budgetary_impact("uk", baseline, reform) + + # Households and people are both weighted 1 and 2. The household benefits + # don't change; the public spending and the Lifetime ISA bonus do. + assert budget.benefit_spending_impact == pytest.approx( + 1.0 * 3.0 + 2.0 * 4.0 + 1.0 * (2.0 - 1.0) + 2.0 * (0.5 - 0.0) + ) + assert budget.tax_revenue_impact == 0.0 + assert budget.budgetary_impact == -budget.benefit_spending_impact + + +def test_uk_spending_terms_are_what_gov_spending_adds_to_household_benefits(): + # The spending total rests on these being the only terms ``gov_spending`` + # adds to the household benefits. If policyengine-uk's lists change, this + # fails, so that the total is updated with them. + assert set(HOUSEHOLD_BENEFIT_VARIABLES) <= set(GOV_SPENDING_VARIABLES) + beyond = set(GOV_SPENDING_VARIABLES) - set(HOUSEHOLD_BENEFIT_VARIABLES) + assert beyond == { + variable + for _, variable in UK_SPENDING_TERMS + if variable != "household_benefits" + } + + +def _uk_model_simulation(reform: dict[str, float] | None): + """Return the installed UK model's outputs for two made-up households. + + A worker with a child, who gets Child Benefit and has a Lifetime ISA, and a + pensioner, in 2026, weighted 1 and 2. The person outputs are weighted as + the household they are in. + """ + year = 2026 + model = policyengine_uk.Simulation( + situation={ + "people": { + "worker": { + "age": {year: 40}, + "employment_income": {year: 30_000.0}, + "lifetime_isa_payments": {year: 4_000.0}, + }, + "child": {"age": {year: 6}}, + "pensioner": { + "age": {year: 75}, + "state_pension_reported": {year: 11_500.0}, + }, + }, + "benunits": { + "family": {"members": ["worker", "child"]}, + "older": {"members": ["pensioner"]}, + }, + "households": { + "family": {"members": ["worker", "child"]}, + "older": {"members": ["pensioner"]}, + }, + }, + reform=reform, + ) + values = { + variable: [float(value) for value in model.calculate(variable, year)] + for variable in ( + "household_net_income", + "household_tax", + "household_benefits", + "gov_tax", + # Not read by the total. The tests compare the total with it, and + # a total that did read it fails on its value. + "gov_spending", + "other_public_spending_budget_change", + ) + } + people = model.calculate("lifetime_isa_bonus", year) + person = weighted_entity_frame( + { + "lifetime_isa_bonus": [float(value) for value in people], + "person_weight": [1.0, 1.0, 2.0], + }, + entity="person", + ) + household = weighted_entity_frame({"household_weight": [1.0, 2.0], **values}) + return SimpleNamespace( + output_dataset=SimpleNamespace( + data=SimpleNamespace(household=household, person=person) + ) + ) + + +def _weighted_change(before, after, variable: str) -> float: + households = [side.output_dataset.data.household for side in (before, after)] + return float((households[1][variable] - households[0][variable]).sum()) + + +@pytest.mark.parametrize( + "reform", + [ + {"gov.hmrc.child_benefit.amount.eldest": 40.0}, + {"gov.hmrc.isa.lifetime.bonus_rate": 0.5}, + ], + ids=["child_benefit", "lifetime_isa_bonus"], +) +def test_uk_spending_is_the_models_gov_spending_without_an_uprating_reform(reform): + # The lists can agree while a formula adds something beyond them, as + # ``household_benefits`` does, so compare the totals themselves. + before = _uk_model_simulation(None) + after = _uk_model_simulation(reform) + spent = _weighted_change(before, after, "gov_spending") + assert spent > 0 + + budget = build_budgetary_impact("uk", before, after) + + assert budget.benefit_spending_impact == pytest.approx(spent, abs=0.01) + + +@pytest.mark.parametrize("rate", ["all", "non_sp"]) +def test_uk_headline_counts_what_a_benefit_uprating_reform_pays(rate): + # household_benefits adds the uprating rates a reform sets to its benefits, + # and gov_spending doesn't. A spending total without them would make a + # benefit uprating reform free. + before = _uk_model_simulation(None) + after = _uk_model_simulation({f"gov.contrib.benefit_uprating.{rate}": 0.05}) + paid = _weighted_change(before, after, "household_benefits") + assert paid > 0 + + budget = build_budgetary_impact("uk", before, after) + + assert budget.benefit_spending_impact == pytest.approx(paid, abs=0.01) + assert budget.budgetary_impact == pytest.approx( + budget.tax_revenue_impact - paid, abs=0.01 + ) + + def test_uk_runs_request_the_government_accounts_and_others_nothing(): uk_baseline = SimpleNamespace(extra_variables={"person": ["age"]}) uk_reform = SimpleNamespace(extra_variables=None) @@ -125,11 +288,15 @@ def test_uk_runs_request_the_government_accounts_and_others_nothing(): configure_budget_total_variables("us", us) # Added once each, beside the extras a simulation already had. + uk_extras = { + "household": ["gov_tax", "other_public_spending_budget_change"], + "person": ["lifetime_isa_bonus"], + } assert uk_baseline.extra_variables == { - "person": ["age"], - "household": ["gov_tax", "gov_spending"], + "person": ["age", *uk_extras["person"]], + "household": uk_extras["household"], } - assert uk_reform.extra_variables == {"household": ["gov_tax", "gov_spending"]} + assert uk_reform.extra_variables == uk_extras # household_tax and household_benefits are policyengine's default outputs. assert us.extra_variables == {} diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_fakes.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_fakes.py index 48fb8eb6d..0e0fdca62 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_fakes.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_fakes.py @@ -33,14 +33,26 @@ ) +def fake_people(lifetime_isa_bonus=(0.0, 0.0)): + """Return people weighted 1 and 2, with a bonus on their Lifetime ISAs. + + The UK's spending counts the Lifetime ISA bonus, which is on people. + """ + return weighted_entity_frame( + {"lifetime_isa_bonus": list(lifetime_isa_bonus)}, entity="person" + ) + + class FakeOutputDataset: - def __init__(self, household): - self.data = SimpleNamespace(household=household) + def __init__(self, household, person=None): + self.data = SimpleNamespace( + household=household, person=fake_people() if person is None else person + ) class FakeSimulation: - def __init__(self, household): - self.output_dataset = FakeOutputDataset(household) + def __init__(self, household, person=None): + self.output_dataset = FakeOutputDataset(household, person) def ensure(self): msg = "test data is already materialized" @@ -100,8 +112,10 @@ def macro_baseline_reform(): net income of 1000. Unweighted sums of the same rows would give 120, 40, 24 and 1200, and summing the weights through the weighted frame (#767) would give 2.5 households, so the golden result only matches a correct build. - The UK's totals, ``gov_tax`` and ``gov_spending``, add an unchanged 10 of - employer NI to the tax, so they change by the same 100 and 30. + The UK's tax total, ``gov_tax``, adds an unchanged 10 of employer NI to the + household tax, so it changes by the same 100. Its spending total is + ``household_benefits`` plus the Lifetime ISA bonus and the reform-only + public spending term, which don't change here, so it changes by the same 30. """ baseline = FakeSimulation( weighted_entity_frame( @@ -112,7 +126,7 @@ def macro_baseline_reform(): "household_benefits": [20.0, 30.0], "household_state_income_tax": [5.0, 5.0], "gov_tax": [70.0, 50.0], - "gov_spending": [20.0, 30.0], + "other_public_spending_budget_change": [0.0, 0.0], } ) ) @@ -125,7 +139,7 @@ def macro_baseline_reform(): "household_benefits": [50.0, 40.0], "household_state_income_tax": [21.0, 13.0], "gov_tax": [150.0, 90.0], - "gov_spending": [50.0, 40.0], + "other_public_spending_budget_change": [0.0, 0.0], } ) ) diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_household_bounds.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_household_bounds.py index bc6393427..6f308c0ad 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_household_bounds.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_household_bounds.py @@ -10,6 +10,7 @@ ImplausibleEntityCountError, build_budgetary_impact, ) +from test_simulation_output_fakes import fake_people def _simulation(weights: list[float]): @@ -22,11 +23,13 @@ def _simulation(weights: list[float]): "household_state_income_tax": [2.0, 3.0], # The UK's budget totals. "gov_tax": [30.0, 60.0], - "gov_spending": [5.0, 10.0], + "other_public_spending_budget_change": [0.0, 0.0], } ) return SimpleNamespace( - output_dataset=SimpleNamespace(data=SimpleNamespace(household=household)) + output_dataset=SimpleNamespace( + data=SimpleNamespace(household=household, person=fake_people()) + ) ) diff --git a/projects/policyengine-simulation-executor/tests/test_simulation_output_impacts.py b/projects/policyengine-simulation-executor/tests/test_simulation_output_impacts.py index f0cb240d0..7cf773d2c 100644 --- a/projects/policyengine-simulation-executor/tests/test_simulation_output_impacts.py +++ b/projects/policyengine-simulation-executor/tests/test_simulation_output_impacts.py @@ -21,6 +21,7 @@ from test_simulation_output_fakes import ( BuilderOptions, FakeSimulation, + fake_people, local_authority_impact_record, macro_baseline_reform, make_output_builder, @@ -174,9 +175,10 @@ def test_builder_budgetary_impact_uses_materialized_columns_and_each_countrys_to "household_benefits": [5.0, 10.0], "household_state_income_tax": [2.0, 3.0], "gov_tax": [30.0, 60.0], - "gov_spending": [6.0, 12.0], + "other_public_spending_budget_change": [0.0, 1.0], } - ) + ), + person=fake_people(lifetime_isa_bonus=(0.0, 0.5)), ) reform = FakeSimulation( weighted_entity_frame( @@ -187,9 +189,10 @@ def test_builder_budgetary_impact_uses_materialized_columns_and_each_countrys_to "household_benefits": [4.0, 8.0], "household_state_income_tax": [4.0, 6.0], "gov_tax": [33.0, 70.0], - "gov_spending": [5.0, 9.0], + "other_public_spending_budget_change": [0.0, 4.0], } - ) + ), + person=fake_people(lifetime_isa_bonus=(1.0, 2.0)), ) us_budget = make_output_builder("us", baseline, reform)._build_budgetary_impact() @@ -209,10 +212,17 @@ def test_builder_budgetary_impact_uses_materialized_columns_and_each_countrys_to "households": 1.0 + 2.0, "baseline_net_income": 1.0 * 100.0 + 2.0 * 200.0, } - # The UK totals the model's government accounts instead, and has no - # state income tax. + # The UK totals the model's government accounts instead: gov_tax, and the + # terms of its spending, which are the household benefits, the Lifetime ISA + # bonus (people are weighted 1 and 2 as households are) and the reform-only + # public spending. It has no state income tax. uk_tax_revenue_impact = 1.0 * (33.0 - 30.0) + 2.0 * (70.0 - 60.0) - uk_benefit_spending_impact = 1.0 * (5.0 - 6.0) + 2.0 * (9.0 - 12.0) + uk_benefit_spending_impact = ( + benefit_spending_impact + + 1.0 * (1.0 - 0.0) + + 2.0 * (2.0 - 0.5) + + 2.0 * (4.0 - 1.0) + ) assert uk_budget.model_dump(mode="json") == { "tax_revenue_impact": uk_tax_revenue_impact, "state_tax_revenue_impact": 0.0, diff --git a/projects/policyengine-simulation-executor/tests/test_stage12_output_planning.py b/projects/policyengine-simulation-executor/tests/test_stage12_output_planning.py index 784d14d8d..f2165e148 100644 --- a/projects/policyengine-simulation-executor/tests/test_stage12_output_planning.py +++ b/projects/policyengine-simulation-executor/tests/test_stage12_output_planning.py @@ -173,15 +173,17 @@ def test_uk_plan_adds_the_government_accounts_its_budget_totals(monkeypatch) -> _route_uk_area_regions(monkeypatch, routed=False) plan = resolve_report_output_plan(_report("uk")) - # The UK budget totals gov_tax and gov_spending, which count employer NI; - # policyengine's UK defaults hold household_tax and household_benefits. - assert {"gov_tax", "gov_spending"}.issubset(_variables(plan, "household")) - assert {"gov_tax", "gov_spending"}.issubset( - _additional_variables(plan, "household") - ) - assert "gov_tax" not in _variables( - resolve_report_output_plan(_report()), "household" - ) + # The UK budget totals gov_tax, which counts employer NI, and the terms of + # its spending. policyengine's UK defaults hold household_tax and + # household_benefits, so the rest is added. + household = {"gov_tax", "other_public_spending_budget_change"} + assert household.issubset(_variables(plan, "household")) + assert household.issubset(_additional_variables(plan, "household")) + assert {"lifetime_isa_bonus"}.issubset(_variables(plan, "person")) + assert {"lifetime_isa_bonus"}.issubset(_additional_variables(plan, "person")) + us_plan = resolve_report_output_plan(_report()) + assert "gov_tax" not in _variables(us_plan, "household") + assert "lifetime_isa_bonus" not in _variables(us_plan, "person") @pytest.mark.parametrize(