Skip to content

Commit b46780b

Browse files
fix: clear caches after parameter updates in simulation modifier
The simulation modifier now clears both parameter caches and variable holder caches after updating parameter values. This ensures that subsequent calculations use the updated parameter values instead of stale cached values. Previously, when a simulation was created and then modified with parameter updates, the cached calculations would still use the old parameter values, causing reforms to have no effect.
1 parent 00794f9 commit b46780b

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

src/policyengine/utils/parametric_reforms.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ def modifier(simulation):
3434
start=start_period,
3535
stop=stop_period,
3636
)
37+
38+
# Clear caches so calculations use updated parameter values
39+
simulation.tax_benefit_system.reset_parameter_caches()
40+
41+
# Clear computed variable caches (but preserve input variables)
42+
# Variables with formulas are computed; those without are inputs
43+
for population in simulation.populations.values():
44+
for var_name, holder in population._holders.items():
45+
variable = simulation.tax_benefit_system.get_variable(var_name)
46+
if variable.formulas: # Only clear computed variables
47+
holder.delete_arrays()
48+
3749
return simulation
3850

3951
return modifier

tests/test_parametric_reforms.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Test that parameter reforms are applied correctly."""
2+
3+
from datetime import datetime
4+
5+
from policyengine.core.policy import ParameterValue as PEParameterValue
6+
from policyengine.core.policy import Policy as PEPolicy
7+
from policyengine.tax_benefit_models.uk import uk_latest
8+
from policyengine.tax_benefit_models.uk.analysis import (
9+
UKHouseholdInput,
10+
calculate_household_impact,
11+
)
12+
13+
14+
def test_parameter_reform_affects_calculation():
15+
"""Test that modifying a parameter actually changes the calculation result."""
16+
param_lookup = {p.name: p for p in uk_latest.parameters}
17+
pe_param = param_lookup["gov.dwp.universal_credit.standard_allowance.amount.SINGLE_OLD"]
18+
19+
pe_input = UKHouseholdInput(
20+
people=[{"age": 30, "employment_income": 0}],
21+
benunit={},
22+
household={},
23+
year=2026,
24+
)
25+
26+
# Baseline
27+
baseline = calculate_household_impact(pe_input, policy=None)
28+
baseline_uc = baseline.benunit[0]["universal_credit"]
29+
30+
# Reform - increase standard allowance
31+
pv = PEParameterValue(
32+
parameter=pe_param,
33+
value=533.0,
34+
start_date=datetime(2026, 1, 1),
35+
end_date=None,
36+
)
37+
policy = PEPolicy(name="Test", description="Test", parameter_values=[pv])
38+
reform = calculate_household_impact(pe_input, policy=policy)
39+
reform_uc = reform.benunit[0]["universal_credit"]
40+
41+
# UC should increase
42+
assert reform_uc > baseline_uc
43+
assert abs(reform_uc - 533 * 12) < 1
44+
45+
46+
def test_parameter_reform_preserves_inputs():
47+
"""Test that input variables are preserved when applying a reform."""
48+
param_lookup = {p.name: p for p in uk_latest.parameters}
49+
pe_param = param_lookup["gov.dwp.universal_credit.standard_allowance.amount.SINGLE_OLD"]
50+
51+
pe_input = UKHouseholdInput(
52+
people=[{"age": 30, "employment_income": 5000}],
53+
benunit={},
54+
household={},
55+
year=2026,
56+
)
57+
58+
pv = PEParameterValue(
59+
parameter=pe_param,
60+
value=533.0,
61+
start_date=datetime(2026, 1, 1),
62+
end_date=None,
63+
)
64+
policy = PEPolicy(name="Test", description="Test", parameter_values=[pv])
65+
reform = calculate_household_impact(pe_input, policy=policy)
66+
67+
assert reform.person[0]["employment_income"] == 5000
68+
assert reform.person[0]["age"] == 30

0 commit comments

Comments
 (0)