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

Đang mở
#11,106 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
65/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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).
Ngôn ngữ chính
Python
Star
178
Fork
224
Merge trung bình
2 ngày 7 giờ
Pull request đã merge (30 ngày)
296

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của PolicyEngine/policyengine-us

Tất cả issue của PolicyEngine/policyengine-us

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.