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
103 stars 176 forks source link

IndividualSim deriv bug #693

Closed yvvijay121 closed 1 year ago

yvvijay121 commented 2 years ago
from openfisca_us import IndividualSim

sim_emp = IndividualSim(year=2022)
sim_emp.add_person(name="adult", age=30, employment_income=250 * 12)
sim_emp.add_person(name="child", age=10)
sim_emp.add_spm_unit(name="spm_unit", members=["adult", "child"])
sim_emp.add_household(name="household", members=["adult", "child"], state_code="CA")

sim_emp.vary("employment_income")
sim_emp.deriv("snap", "employment_income")

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17988/3750074487.py in <module>
     10 
     11 sim_emp.vary("employment_income")
---> 12 sim_emp.deriv("snap", "employment_income")

~\anaconda3\envs\poliengvenv\lib\site-packages\openfisca_tools\hypothetical.py in deriv(self, var, wrt, period, var_target, wrt_target)
    247             len(y) > 1 and len(x) > 1
    248         ), "Simulation must vary on an axis to calculate derivatives."
--> 249         deriv = (y[1:] - y[:-1]) / (x[1:] - x[:-1])
    250         deriv = np.append(deriv, deriv[-1])
    251         return deriv

ValueError: operands could not be broadcast together with shapes (2000,) (1,2001) 
MaxGhenis commented 2 years ago

This occurs even minimally (notebook):

sim = IndividualSim(year=2022)
sim.add_person()

sim.vary("employment_income")
sim.deriv("snap", "employment_income")

I think it's because snap is at the SPM unit level, but sim doesn't have an SPM unit. Adding one fixes it:

sim = IndividualSim(year=2022)
sim.add_person(name="person")
sim.add_spm_unit(name="spm_unit", members=["person"])
sim.vary("employment_income")
sim.deriv("snap", "employment_income")

Though adding only a household throws the same error as person-only:

sim = IndividualSim(year=2022)
sim.add_person(name="person")
sim.add_household(name="household", members=["person"])
sim.vary("employment_income")
sim.deriv("snap", "employment_income")

Seems like an openfisca-tools issue.

nikhilwoodruff commented 2 years ago
nikhilwoodruff commented 1 year ago

I think we should use the Simulation interface for this now (docs)