PolicyEngine / policyengine-us

The PolicyEngine US Python package contains a rules engine of the US tax-benefit system, and microdata generation for microsimulation analysis.
https://policyengine.org/us
GNU Affero General Public License v3.0
99 stars 174 forks source link

Fix MT income tax model crash #3971

Closed martinholmer closed 6 months ago

martinholmer commented 6 months ago

In the 0.672.1 version, calculating the mt_income_tax variable causes the model to crash with the following fatal error message:

vars/gov/states/mt/tax/income/deductions/mt_head_deductions_exemptions_indiv.py"          
       line 21, in formula
         spouse_agi_attributed_to_head = tax_unit.sum(spouse_agi) * head
ValueError: operands could not be broadcast together with shapes (100000,) (279905,) 

Please add an integration test that contains a tax unit with more than one person!

PavelMakarchuk commented 6 months ago

I see the issue but I am unsure how the existing mt_head_deductions_exemptions_indiv does not catch that.

- name: The AGI difference is attributed to the person with the higher AGI and the rest is halved
  period: 2021
  input:
    people: 
      person1:
        is_tax_unit_head: true
        mt_agi: 20_000
      person2:
        is_tax_unit_spouse: true
        mt_agi: 18_000
      person3:
        mt_agi: 0
    tax_units:
      tax_unit:
        members: [person1, person2, person3]
        mt_tax_unit_deductions_exemptions_indiv: 10_000
    households:
      household:
        members: [person1, person2, person3]
        state_code: MT
  output:
    mt_head_deductions_exemptions_indiv: 6_000
MaxGhenis commented 6 months ago

Unit tests won't catch this because we can broadcast k-element arrays with 1-element arrays (or other k-element arrays). To catch errors broadcasting j- and k-element arrays (e.g., person and tax unit), we need microsimulation tests with multiple tax units, like Martin is doing. I filed https://github.com/PolicyEngine/policyengine-us/issues/3975 to address this.