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/declared-current-law-reads.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added declared current-law reads: `Population.current_law` for variables that set `current_law_read = True`, `Simulation.compares_with_current_law`, `Simulation.current_law_system` and `Simulation.current_law_simulation()`, each reported to the tracer through `record_current_law_call`.
1 change: 1 addition & 0 deletions changelog.d/declared-current-law-reads.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A reform simulation no longer branches its current-law copy while it is constructed: the copy is built from the inputs held when first needed, without calculated values, and the first declared current-law call makes it read-only; `Simulation.baseline` is deprecated and assigning it replaces the copy.
12 changes: 12 additions & 0 deletions docs/python_api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ have changed.
:show-inheritance:
```

## UndeclaredCurrentLawReadError

Raised by `Population.current_law` for a variable that does not declare
`current_law_read = True` in the current-law tax-benefit system.

```{eval-rst}
.. autoclass:: policyengine_core.errors.undeclared_current_law_read_error.UndeclaredCurrentLawReadError
:members:
:undoc-members:
:show-inheritance:
```

## VariableNameConflictError

```{eval-rst}
Expand Down
34 changes: 34 additions & 0 deletions docs/python_api/simulations.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ keeps values it does not hold yet.

The method's docstring lists what is not detected.

### Current-law reads

A simulation built with a reform compares with current law. Formulas read
current law through declared calls, each reported to the simulation's tracer
(`record_current_law_call`, kept as `TraceNode.current_law_calls` by the full
tracer):

- `population.current_law(variable, period)` returns the variable's value under
current law, for a variable that declares `current_law_read = True` in the
current-law tax-benefit system; any other variable raises
`UndeclaredCurrentLawReadError`. Without a reform it returns the
simulation's own value.
- `simulation.compares_with_current_law` says whether the simulation compares
with current law.
- `simulation.current_law_system` is the current-law tax-benefit system.
- `simulation.current_law_simulation()` is the simulation that calculates
current-law values: the simulation itself without a reform.

Construction no longer branches a copy under current law. The copy is built the
first time something needs it, by the overridable
`_build_current_law_simulation()`: a branch named `baseline`, under
`default_tax_benefit_system_instance`, holding the inputs the simulation holds
then and none of its calculated values. `compares_with_current_law` and
`current_law_system` never build it. The first declared call that reaches it
makes it read-only (see above). A read-only simulation registers the copy it
builds in `branches`, unlike its other branches. Branches read their parent's
copy; `clone()` and `subsample()` keep a copy only if one was built.

`simulation.baseline` is deprecated. It returns the copy (built if needed, and
left writable) or `None`, and each read is reported to the tracer. Assigning a
simulation to it makes that simulation the copy, kept as it is given;
assigning `None` stops the comparison. Assigning it on a read-only simulation
raises `ReadOnlySimulationError`.

## Microsimulation

```{eval-rst}
Expand Down
1 change: 1 addition & 0 deletions policyengine_core/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
from .read_only_simulation_error import ReadOnlySimulationError
from .situation_parsing_error import SituationParsingError
from .spiral_error import SpiralError
from .undeclared_current_law_read_error import UndeclaredCurrentLawReadError
from .variable_name_conflict_error import VariableNameConflictError
from .variable_not_found_error import VariableNotFoundError
8 changes: 8 additions & 0 deletions policyengine_core/errors/undeclared_current_law_read_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UndeclaredCurrentLawReadError(ValueError):
"""A formula read the current-law value of a variable not declared for it.

Raised by ``Population.current_law`` when the variable's
``current_law_read`` attribute, in the current-law tax-benefit system, is
not true. Declaring the reads lets core know in advance which current-law
values a simulation may need.
"""
58 changes: 58 additions & 0 deletions policyengine_core/populations/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,64 @@ def __call__(

# Helpers

@projectors.projectable
def current_law(
self,
variable_name: str,
period: Period = None,
options: Optional[Container[str]] = None,
):
"""Calculate ``variable_name`` under current law, for this entity.

In a simulation that compares with current law (one built with a
reform, or one of its branches), the value its current-law copy
calculates (see ``Simulation.current_law_simulation``), which this
builds if there is none yet and makes read-only (unless it is a
simulation assigned to ``baseline``, which is kept as it was given). Otherwise the
simulation is current law itself, and this is the same as calling
the population. ``period`` and ``options`` are as for a call.

The variable must declare ``current_law_read = True`` in the
current-law tax-benefit system; otherwise this raises
``UndeclaredCurrentLawReadError``. Each call is reported to the
simulation's tracer, with the variable and period.

Example, for a variable ``income_tax`` declaring
``current_law_read = True``:

>>> person.current_law('income_tax', '2017-04')
>>> array([45.])

Called through a projection (``person.household.current_law(...)``),
the result is projected as a call's would be. Once the copy is
read-only, the array returned from it is non-writable, as every array
a read-only simulation stores: a formula that writes into what it
read must copy it first.
"""
from policyengine_core.errors import UndeclaredCurrentLawReadError

# Before anything is built, so a missing period names the caller.
self.check_period_validity(variable_name, period)
simulation = self.simulation
variable = simulation._current_law_system().get_variable(
variable_name, check_existence=True
)
if not variable.current_law_read:
raise UndeclaredCurrentLawReadError(
f'The variable "{variable_name}" does not declare '
"current_law_read = True, so formulas may not read its "
"current-law value with current_law(). Declare it on the "
"variable."
)
simulation._report_current_law_call("current_law", variable_name, period)
source = simulation._current_law_source()
if not source.compares:
return self(variable_name, period, options)
current_law_simulation = source.get(make_read_only=True)
return current_law_simulation.populations[self.entity.key](
variable_name, period, options
)

def get_holder(self, variable_name: str) -> Holder:
self.entity.check_variable_defined_for_entity(variable_name)
variable = self.entity.get_variable(variable_name)
Expand Down
Loading
Loading