Skip to content
Merged
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/2774.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When a caller sets an input for an earlier year after a calculation, recalculate the later years the default branch had grown, carried over or calculated from earlier inputs, as a named branch and a new simulation do.
15 changes: 13 additions & 2 deletions docs/book/usage/scenarios.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,24 @@ calculations retain them too. To undo an override, call
variable's formula or default runs again. A reset alone no longer undoes an
input supplied after construction.

Every top-level `set_input` on a built simulation queues dependent-cache cleanup
before its next outermost read. This includes simulations with no modifier: a
Every top-level `Simulation.set_input` on a built simulation queues
dependent-cache cleanup before its next outermost read. This includes simulations with no modifier: a
caller can read Child Benefit, add an amount and then obtain fresh income and
response values. Inputs stored only on another clone or branch do not become
the parent's inputs. Formula and framework writes during a calculation keep
their existing selective invalidation.

In that cleanup, as in `reset_calculations`, the simulation keeps only the
years of each input variable that the data, a modifier or a caller supplied.
It calculates the other years again, whether it grew them from an earlier
input by the variable's uprating index, carried an earlier input into them,
ran the variable's formula for them or, for a stored household total, grew
them from an earlier total. So when a caller sets an input for an earlier year
after a calculation, the later years the simulation had already filled follow
it, as they do on a named branch and in a new simulation that gets the same
inputs. The simulation keeps a later year that the data or `set_input`
supplied.

Direct edits of the parameter tree require a separate array-cache boundary.
After calling a parameter's `update` method on a built simulation, call
`simulation.drop_computed_arrays()` before reading its results. This public
Expand Down
14 changes: 7 additions & 7 deletions policyengine_uk/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
from policyengine_uk.utils.data_source import built_from_data
from policyengine_uk.utils.input_dataframe_export import input_dataframe
from policyengine_uk.utils.measurement_branches import (
drop_calculated_totals,
drop_calculated_values,
drop_calculated_years,
retained_input_variables,
)
from policyengine_uk.utils.supplied_inputs import (
Expand Down Expand Up @@ -815,10 +815,9 @@ def _stored_household_total(self, variable, period):
continue
native = self.tax_benefit_system.variables[part].definition_period
if part_source.start > source.start or part_source.unit != native:
# Person balances stored for a later year than the total are
# the newer observation, and Core carries a balance stored in
# another unit flat rather than uprating it, so their sum
# stands.
# Person balances stored for a later year than the total give
# the newer observation, and Core doesn't uprate a balance
# stored in another unit, so their sum stands.
return None
try:
index_name = stored_total_uprating(self.tax_benefit_system, variable.name)
Expand Down Expand Up @@ -1119,8 +1118,9 @@ def reset_calculations(self):
for variable in self.tax_benefit_system.variables:
if variable not in retained:
self.delete_arrays(variable)
elif variable in STORED_HOUSEHOLD_TOTALS:
drop_calculated_totals(self, variable)
drop_calculated_years(
self, retained.intersection(self.tax_benefit_system.variables)
)

def get_known_variables(self):
variables = []
Expand Down
Loading
Loading