Avoid running apply_reform on every baseline Simulation.__init__ (unconditional mi_surtax & combined_reform)

Open
#11,106 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
performance

Research direction

Start in policyengine_us/system.py at the Simulation.__init__ call to create_structural_reforms_from_parameters, then read policyengine_us/reforms/reforms.py around lines 754–761 and policyengine_us/reforms/states/mi/surtax.py. Compare the proposed baseline mi_surtax variable location under policyengine_us/variables/gov/states/mi/tax/income/ with mi_income_tax.adds. Done means a baseline Simulation with no gov.contrib structural reform returns no reform and skips apply_reform, while the Michigan surtax still respects gov.contrib.states.mi.surtax.in_effect.

Written by the indexing model from the issue text.

Description

Summary

Every Simulation(...) construction in policyengine_us currently runs self.apply_reform(reform) even when using the unmodified baseline system and no user-supplied reform. In a cProfile trace of a single-household simulation, apply_reform inside Simulation.__init__ accounts for ~29 ms per call (~18% of Simulation.__init__).

Root Cause

There are two pieces causing apply_reform to run unconditionally on every baseline Simulation:

  1. mi_surtax is unconditionally returned by create_structural_reforms_from_parameters (policyengine_us/reforms/states/mi/surtax.py):

    • Of the ~140 parameter-driven reforms checked in create_structural_reforms_from_parameters, mi_surtax is the only reform returned on the default baseline system.parameters.
    • Following #7377 (which moved the in_effect check into mi_surtax.formula) and #10039 (which added mi_surtax to the baseline gov.states.household.state_income_tax_before_refundable_credits list), create_mi_surtax_reform unconditionally returns create_mi_surtax() so mi_surtax exists in every system.
    • Because mi_surtax is already part of every baseline system and gates on parameters(period).gov.contrib.states.mi.surtax.in_effect at calculation time, wrapping it in a Reform is no longer needed.
  2. create_structural_reforms_from_parameters never returns None (policyengine_us/reforms/reforms.py, lines 754–761):

    • Simulation.__init__ (policyengine_us/system.py, lines 356–360) checks:
      reform = create_structural_reforms_from_parameters(
          self.tax_benefit_system.parameters, start_instant
      )
      if reform is not None:
          self.apply_reform(reform)
      
    • However, create_structural_reforms_from_parameters currently returns combined_reform even if reforms is empty (()), so if reform is not None: would still evaluate to True.
Proposed Fix
  1. Integrate mi_surtax as a standard baseline variable under policyengine_us/variables/gov/states/mi/tax/income/ (and include "mi_surtax" in mi_income_tax.adds), keeping the calculation-time gov.contrib.states.mi.surtax.in_effect check inside mi_surtax.formula (short-circuiting to 0 before evaluating mi_taxable_income / tax_unit_is_joint when in_effect is false). Remove mi_surtax from create_structural_reforms_from_parameters (keeping any backwards-compatible import alias if needed).
  2. Return None when no structural reform is active in policyengine_us/reforms/reforms.py:
    reforms = tuple(filter(lambda x: x is not None, reforms))
    if not reforms:
        return None
    
Expected Impact
  • Baseline Simulation(...) calls skip self.apply_reform(reform) completely when no gov.contrib structural reform is active.
  • Exact calculation behavior is preserved for both in_effect = False (baseline) and in_effect = True (parameter-toggled Michigan surtax).
Dominant language
Python
Stars
178
Forks
224
Avg merge
2d 7h
Merged PRs (30d)
309

Getting set up

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.

More from PolicyEngine/policyengine-us

All issues in PolicyEngine/policyengine-us

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.