Skip to content
Draft
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
1 change: 1 addition & 0 deletions changelog.d/779.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`pe.uk.calculate_budgetary_impact` and `pe.uk.BudgetaryImpact` give a UK reform's budgetary impact from the government's accounts: the change in `gov_tax`, which counts employer National Insurance, less the change in the government's spending. Spending is `household_benefits` plus the two terms policyengine-uk's `gov_spending` adds to it, because `gov_spending` leaves out what the benefit uprating reforms pay. `pe.uk.configure_budgetary_impact_variables` adds what isn't a default UK output to any number of simulations before they run. The UK `economic_impact_analysis` is unchanged.
9 changes: 9 additions & 0 deletions docs/impact-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ A `PolicyReformAnalysis` with:
| `wealth_decile_impacts` | `OutputCollection[DecileImpact]` | UK only: household net income impacts grouped by `household_wealth_decile` |
| `intra_wealth_decile_impacts` | `OutputCollection[IntraDecileImpact]` | UK only: within-wealth-decile distribution of household net income changes |
| `program_statistics` | `OutputCollection[ProgramStatistics]` | Totals, counts, winners/losers per program |
| `budgetary_impact` | `BudgetaryImpact` | US only: the reform's budgetary impact as `federal`, `state`, `unattributed` and their `total`, positive when the government is better off |
| `baseline_poverty` | `OutputCollection[Poverty]` | Baseline rates by measure and demographic group |
| `reform_poverty` | `OutputCollection[Poverty]` | Reform rates, same schema as baseline |
| `baseline_inequality` | `Inequality` | Gini plus top / bottom income shares (baseline) |
Expand All @@ -56,6 +57,14 @@ if hasattr(analysis, "wealth_decile_impacts"):
analysis.reform_inequality.gini - analysis.baseline_inequality.gini
```

For a UK reform's budgetary impact, call `pe.uk.calculate_budgetary_impact`. It returns the changes in the government's tax revenue (`gov_tax`, employer National Insurance included) and spending (the household benefits, the Lifetime ISA bonus and any public spending a reform sets), and their difference, `total`, positive when the government is better off. Not all of that is in the default UK output, so configure the simulations before they run:

```python
pe.uk.configure_budgetary_impact_variables(baseline, reformed)
budget = pe.uk.calculate_budgetary_impact(baseline, reformed)
budget.total
```

## When to call it

- Producing a reform brief covering multiple metrics
Expand Down
16 changes: 16 additions & 0 deletions src/policyengine/outputs/change_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,19 @@ def run(self):
self.result = filtered_change.sum()
elif self.aggregate_type == ChangeAggregateType.MEAN:
self.result = filtered_change.mean()


def sum_change(
baseline_simulation: Simulation,
reform_simulation: Simulation,
variable: str,
) -> float:
"""Return the reform-minus-baseline weighted total of a variable."""
aggregate = ChangeAggregate(
baseline_simulation=baseline_simulation,
reform_simulation=reform_simulation,
variable=variable,
aggregate_type=ChangeAggregateType.SUM,
)
aggregate.run()
return float(aggregate.result)
10 changes: 9 additions & 1 deletion src/policyengine/tax_benefit_models/uk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
from policyengine.core import Dataset
from policyengine.outputs import LaborSupplyResponse, ProgramStatistics

from .analysis import economic_impact_analysis
from .analysis import (
BudgetaryImpact,
calculate_budgetary_impact,
configure_budgetary_impact_variables,
economic_impact_analysis,
)
from .datasets import (
PolicyEngineUKDataset,
UKYearData,
Expand Down Expand Up @@ -56,6 +61,9 @@
"uk_latest",
"calculate_household",
"economic_impact_analysis",
"calculate_budgetary_impact",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calculate_budgetary_impact is exported from pe.uk, but configure_budgetary_impact_variables isn't, so callers (and the docs example) import it from the module path. They're always used together, so export it too?

"configure_budgetary_impact_variables",
"BudgetaryImpact",
"ProgramStatistics",
"LaborSupplyResponse",
]
Expand Down
119 changes: 118 additions & 1 deletion src/policyengine/tax_benefit_models/uk/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import annotations

from pydantic import BaseModel
from pydantic import BaseModel, Field, computed_field

from policyengine.core import OutputCollection, Simulation
from policyengine.outputs import (
Expand All @@ -20,10 +20,12 @@
configure_labor_supply_response_variables,
validate_program_statistics_config,
)
from policyengine.outputs.change_aggregate import sum_change
from policyengine.outputs.decile_impact import (
DecileImpact,
calculate_decile_impacts,
)
from policyengine.outputs.extra_variables import add_extra_variables
from policyengine.outputs.inequality import (
Inequality,
calculate_uk_inequality,
Expand Down Expand Up @@ -61,6 +63,121 @@
"state_pension": {"is_tax": False},
}

# The UK model's government accounts, which name the two sides of the budgetary
# impact: ``gov_tax`` (every tax the government raises in respect of a
# household, employer National Insurance included) and ``gov_spending`` (every
# benefit and other programme it pays for). Their difference is ``gov_balance``.
BUDGETARY_IMPACT_VARIABLES: tuple[str, str] = ("gov_tax", "gov_spending")

# What the spending side totals. 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.
SPENDING_TERMS: tuple[str, ...] = (
"household_benefits",
"lifetime_isa_bonus",
"other_public_spending_budget_change",
)

# ``household_benefits`` is in the default UK output. The rest is not, so it is
# materialized before the simulations run.
_BUDGETARY_IMPACT_EXTRA_VARIABLES: dict[str, list[str]] = {
"household": ["gov_tax", "other_public_spending_budget_change"],
"person": ["lifetime_isa_bonus"],
}


class BudgetaryImpact(BaseModel):
"""A UK reform's budgetary impact: the change in the government's balance.

Sign convention: ``total`` is positive when the government is better off
(more tax revenue or less spending) and negative when the reform costs it
money. ``tax_revenue`` and ``spending`` are the reform-minus-baseline
changes in the government's tax revenue and spending, so more spending is
a positive ``spending``. See :func:`calculate_budgetary_impact`.
"""

tax_revenue: float = Field(
...,
description=(
"Change in government tax revenue (gov_tax), GBP, employer "
"National Insurance included."
),
)
spending: float = Field(
...,
description=(
"Change in government spending (gov_spending, and what the benefit "
"uprating reforms pay), GBP; positive means more spending."
),
)

@computed_field # type: ignore[prop-decorator]
@property
def total(self) -> float:
"""Budgetary impact, GBP: tax revenue change minus spending change."""
return self.tax_revenue - self.spending


def configure_budgetary_impact_variables(*simulations: Simulation) -> None:
"""Materialize what the budgetary impact totals in the simulations' outputs.

``gov_tax`` and two of ``SPENDING_TERMS`` are not in the default UK output,
so they are added as extra output variables before the simulations run. Call
this before :meth:`Simulation.ensure`, so that they are present when
:func:`calculate_budgetary_impact` reads them. It takes any number of
simulations: a precomputed baseline has no reform to go with it.
"""
for simulation in simulations:
add_extra_variables(simulation, _BUDGETARY_IMPACT_EXTRA_VARIABLES)


def calculate_budgetary_impact(
baseline_simulation: Simulation,
reform_simulation: Simulation,
) -> BudgetaryImpact:
"""Return a UK reform's budgetary impact from the government's accounts.

``tax_revenue`` is the reform-minus-baseline weighted total of ``gov_tax``,
and ``spending`` that of the government's spending; ``total`` is their
difference, the change in the government's balance.

The totals are the government's, not the household totals
``household_tax`` and ``household_benefits``. They differ in these terms:

- ``gov_tax`` counts employer National Insurance, which employers pay and
``household_tax`` leaves out. Under policyengine-uk's default incidence an
employer NI change moves employees' pay, so a total of household taxes
would count the income tax and NI on that pay change but not the employer
NI receipts behind it. ``household_tax`` holds shares of the change
instead, which are not revenue of their own.
- ``gov_tax`` counts the Lifetime ISA withdrawal charge, and spending the
Lifetime ISA bonus.
- Spending counts ``other_public_spending_budget_change``, which only a
reform sets.

``gov_tax`` holds VAT and private school VAT as levels where
``household_tax`` holds their changes from baseline, which are the same
change.

Spending is totalled from ``SPENDING_TERMS`` rather than read from
``gov_spending``, which leaves out what the benefit uprating reforms pay.

``gov_tax`` and the terms are not all in the default UK output: call
:func:`configure_budgetary_impact_variables` before the simulations run.
:func:`economic_impact_analysis` calls neither, because it also runs on
stored outputs that hold none of them.
"""
tax_variable, _ = BUDGETARY_IMPACT_VARIABLES
return BudgetaryImpact(
tax_revenue=sum_change(baseline_simulation, reform_simulation, tax_variable),
spending=sum(
sum_change(baseline_simulation, reform_simulation, variable)
for variable in SPENDING_TERMS
),
)


class PolicyReformAnalysis(BaseModel):
"""Complete policy reform analysis result."""
Expand Down
35 changes: 8 additions & 27 deletions src/policyengine/tax_benefit_models/us/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
configure_labor_supply_response_variables,
validate_program_statistics_config,
)
from policyengine.outputs.change_aggregate import (
ChangeAggregate,
ChangeAggregateType,
)
from policyengine.outputs.change_aggregate import sum_change
from policyengine.outputs.decile_impact import (
DecileImpact,
calculate_decile_impacts,
Expand Down Expand Up @@ -103,22 +100,6 @@ def total(self) -> float:
return self.federal + self.state + self.unattributed


def _sum_change(
baseline_simulation: Simulation,
reform_simulation: Simulation,
variable: str,
) -> float:
"""Reform minus baseline total for a variable."""
agg = ChangeAggregate(
baseline_simulation=baseline_simulation,
reform_simulation=reform_simulation,
variable=variable,
aggregate_type=ChangeAggregateType.SUM,
)
agg.run()
return float(agg.result)


# Budgetary-impact variables that are not in the default US output
# (household_tax, household_benefits, and the three tax variables are). They
# must be materialized before the reform simulations run.
Expand Down Expand Up @@ -212,16 +193,16 @@ def calculate_budgetary_impact(
:func:`configure_budgetary_impact_variables` to materialize them before the
simulations run. Callers using this helper directly must do the same.
"""
household_tax_change = _sum_change(
household_tax_change = sum_change(
baseline_simulation, reform_simulation, "household_tax"
)
household_benefits_change = _sum_change(
household_benefits_change = sum_change(
baseline_simulation, reform_simulation, "household_benefits"
)
federal_benefit_cost_change = _sum_change(
federal_benefit_cost_change = sum_change(
baseline_simulation, reform_simulation, "federal_benefit_cost"
)
state_benefit_cost_change = _sum_change(
state_benefit_cost_change = sum_change(
baseline_simulation, reform_simulation, "state_benefit_cost"
)

Expand All @@ -233,12 +214,12 @@ def calculate_budgetary_impact(
total -= federal_benefit_cost_change + state_benefit_cost_change

federal = (
_sum_change(baseline_simulation, reform_simulation, "income_tax")
+ _sum_change(baseline_simulation, reform_simulation, "employee_payroll_tax")
sum_change(baseline_simulation, reform_simulation, "income_tax")
+ sum_change(baseline_simulation, reform_simulation, "employee_payroll_tax")
- federal_benefit_cost_change
)
state = (
_sum_change(baseline_simulation, reform_simulation, "state_income_tax")
sum_change(baseline_simulation, reform_simulation, "state_income_tax")
- state_benefit_cost_change
)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_budgetary_impact.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Tests for federal/state/unattributed budgetary impact partitioning.

The unit tests patch ``_sum_change`` and feed mocked reform-minus-baseline
The unit tests patch ``sum_change`` and feed mocked reform-minus-baseline
deltas, so they exercise the partition arithmetic in isolation. The
integration test at the bottom runs ``calculate_budgetary_impact`` against a
tiny hand-built US output simulation so that every variable name it references
Expand Down Expand Up @@ -41,7 +41,7 @@


def _fake_sum_change_factory(variable_to_delta: dict[str, float]):
"""Return a fake _sum_change that looks up deltas by variable name."""
"""Return a fake sum_change that looks up deltas by variable name."""

def fake_sum_change(baseline_sim, reform_sim, variable):
return variable_to_delta.get(variable, 0.0)
Expand All @@ -54,7 +54,7 @@ def _budgetary_impact_from_deltas(
) -> BudgetaryImpact:
with (
patch(
"policyengine.tax_benefit_models.us.analysis._sum_change",
"policyengine.tax_benefit_models.us.analysis.sum_change",
side_effect=_fake_sum_change_factory(deltas),
),
patch(
Expand Down
Loading
Loading