PolicyEngine / PolicyEngine/policyengine-uk

Find a way to easily clone variable trees

Open
#412 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
50
Forks
33
Avg merge
20h 58m
Merged PRs (30d)
14

Description

The current implementation is not optimal, and my intuition is that there's a reasonably neat way of doing it. For context, our use case is that for a given variable, say stamp_duty_land_tax, we want to also have the variable baseline_stamp_duty_land_tax, which returns the same results as the former no matter what variable or parametric reforms are applied. The current implementation guesses if we're using a microsimulation, in which case we can just instantiate a new one and take it from that, but this doesn't work for household simulations. The obvious alternative is to manually clone each variable and parameter in the computation tree, but this would quickly get overcomplicated. I think something like this would be neater:

# In CountryTaxBenefitSystem __init__ function - below already exists
self.parameters.baseline = self.parameters.clone()
self.add_variables_from_directory(
            os.path.join(COUNTRY_DIR, "variables")
        )
# New code
VARIABLES_WITH_BASELINE_CLONES = ["stamp_duty_land_tax"]
for var in VARIABLES_WITH_BASELINE_CLONES:
  baseline_variable = type(inheriting from variable)
  baseline_variable.__name__ = "baseline_" + original name
  baseline_variable.formula = decorator(original_formula)
  for dependency in variable_dependencies:
    recurse_and_repeat_above(dependency)

def decorator(fn) -> fn:
  # In function code string
  # Replace parameters(period).x with parameters(period).baseline.x
  # Replace variable call for variable x with call for variable baseline_x

Obviously this is some overcomplex Python surgery but seems like the only way to avoid code duplication.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked implementation in openfisca_uk/variables/income/poverty.py and the CountryTaxBenefitSystem initialization described in the issue. Trace how variable and parameter dependencies are resolved for microsimulations versus household simulations. Done means a supported variable can produce a baseline counterpart whose results remain unchanged by variable or parametric reforms in both simulation modes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.