Skip to content

A simulation given an input for a year before its data holds no identifiers there #2353

Open

Description

@MaxGhenis

A simulation given an input for a year before its data holds no identifiers in that year. When its exported frame is loaded back, the new simulation holds the real identifiers there, so formulas that read identifiers give different values in that year.

Reproduction

On #2349's head (a9664455e):

import numpy as np, pandas as pd
from policyengine_uk import Simulation

frame = pd.DataFrame({
    "person_id__2025": [1, 2], "benunit_id__2025": [1, 1], "household_id__2025": [1, 1],
    "age__2025": [10, 10], "household_weight__2025": [1, 1],
})
s = Simulation(dataset=frame)
s.set_input("hours_worked", 2023, np.array([0, 0]))
r = Simulation(dataset=s.to_input_dataframe())

s.calculate("person_id", 2023)                   # [0, 0]
r.calculate("person_id", 2023)                   # [1, 2]
s.calculate("months_since_last_birthday", 2023)  # [3., 9.]
r.calculate("months_since_last_birthday", 2023)  # [9., 3.]

Why

  • policyengine-core carries an input forward but never back. Simulation._calculate returns the default when the only stored periods are later ones (core 3.32.27 simulations/simulation.py:1199-1250).
  • So in 2023 the identifiers are zero.
  • The loader treats every whole year a frame gives values for as a dataset year, and each year's tables hold all five identifiers (policyengine_uk/data/input_dataframe.py, dataset_from_input_dataframe). So the reloaded simulation holds the real identifiers in 2023.
  • The frame still round-trips, but these formulas read identifiers, so their values differ in that year:
    • is_benunit_eldest_child
    • months_since_last_birthday
    • uc_deduction_random_draw
    • uc_deduction_type_random_draw

Found while fixing #2333 in #2349.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions