Skip to content
Open
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/2916.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a single, optional warning when dataset-built microsimulations omit formula-less take-up inputs that default to true, without changing calculation results or household simulation behavior.
19 changes: 18 additions & 1 deletion docs/book/usage/simulations.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,24 @@ print(f"Total UK income tax revenue: £{total_income_tax:.1f}bn")

## Microsimulation analysis

The `Microsimulation` class extends `Simulation` with survey weights for population estimates:
The `Microsimulation` class extends `Simulation` with survey weights for population estimates.

When constructed from a dataset, `Microsimulation` emits one `UserWarning`
listing omitted Boolean `would_claim_*` inputs that have no formula and default
to `True`, such as `would_claim_uc` and `would_claim_carers_allowance`.
These calculator defaults do not establish benefit eligibility or certify
population take-up rates. Supply take-up assumptions appropriate to the dataset,
or suppress the diagnostic explicitly:

```python
sim = Microsimulation(dataset=dataset, warn_on_missing_takeup_inputs=False)
```

The check counts an input as supplied if it has at least one genuine input
period, including an explicit scenario override. It does not check every year
or record, and it does not verify calibration. The warning leaves inputs and
calculation results unchanged. Plain `Simulation` and situation-built
`Microsimulation` do not emit this diagnostic.

```python
from policyengine_uk import Microsimulation
Expand Down
12 changes: 12 additions & 0 deletions docs/engineering/skills/dataset-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,18 @@ with a reporting family, so Child Tax Credit, Working Tax Credit and Income
Support take-up also follow reported claims there. Explicit inputs override
these defaults, including a future calibrated dataset column (uk-data#448).

Dataset-built `Microsimulation` objects also emit one construction-time
`UserWarning` naming formula-less Boolean `would_claim_*` inputs that default
to `True` and have no genuine supplied input period. The check uses the final
variable registry and readable holder input periods, so formulas, neutralized
definitions, explicit scenario inputs and DataFrame period replays are
respected; calculated caches do not count as supplied inputs. One supplied
period is enough: this is an omission-by-variable diagnostic, not a check of
every year or record or a certification of take-up calibration. It changes no
defaults, inputs or results. Pass `warn_on_missing_takeup_inputs=False` to
`Microsimulation` to suppress it. Plain `Simulation` and situation-built
`Microsimulation` do not emit this diagnostic.

Do not rebuild an existing data-built simulation by calling core's
`SimulationBuilder.build_from_dict` directly. In core 3.33.2 this leaves stale
dataset provenance, which copies inherit. Use UK `build_from_situation` instead.
Expand Down
29 changes: 29 additions & 0 deletions policyengine_uk/microsimulation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Standard library imports
import warnings
from typing import List, Optional

# Third-party imports
Expand All @@ -9,6 +10,7 @@
from policyengine_core.tracers import SimpleTracer

from .simulation import Simulation
from .utils.data_source import built_from_data


class Microsimulation(Simulation):
Expand All @@ -18,6 +20,33 @@ class Microsimulation(Simulation):
estimates and statistics.
"""

def __init__(self, *args, warn_on_missing_takeup_inputs: bool = True, **kwargs):
"""Initialize a weighted simulation and its optional take-up diagnostic."""
super().__init__(*args, **kwargs)

if warn_on_missing_takeup_inputs and built_from_data(self):
missing_inputs = sorted(
name
for name, variable in self.tax_benefit_system.variables.items()
if name.startswith("would_claim_")
and variable.value_type is bool
and not variable.formulas
and variable.default_value is True
and not variable.is_neutralized
and not self.get_holder(name).get_input_periods(self.branch_name)
)
if missing_inputs:
warnings.warn(
"Dataset-built Microsimulation is using calculator take-up "
"defaults of True for inputs without supplied input periods: "
f"{', '.join(missing_inputs)}. "
"These defaults do not establish benefit eligibility or "
"certify take-up rates. Supply take-up inputs or set "
"warn_on_missing_takeup_inputs=False to suppress this warning.",
UserWarning,
stacklevel=2,
)

def get_weights(
self, variable_name: str, period: str, map_to: Optional[str] = None
) -> np.ndarray:
Expand Down
Loading
Loading