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

Abierto
#11,106 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
65/100
Tipo de issue
Refactorización
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
performance

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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).
Lenguaje dominante
Python
Estrellas
178
Forks
224
Merge medio
2 d 7 h
PR fusionados (30 d)
309

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de PolicyEngine/policyengine-us

Todos los issues de PolicyEngine/policyengine-us

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.