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

CO CTC return "nan" if WIC is calculated first. #4835

Open CalebPena opened 3 months ago

CalebPena commented 3 months ago

I was trying to get the results for the co_ctc variable in PE, but I kept getting nan in the results. I slowly removed programs, and found that wic might be causing the problem.

Minimal reproducible example:

from policyengine_us import Simulation
import requests

request = {
    "people": {
        "418": {
            "wic": {"2024": None},
        },
        "419": {
            "wic": {"2024": None},
        },
    },
    "tax_units": {
        "tax_unit": {
            "members": ["418", "419"],
            "co_ctc": {"2023": None},
        }
    },
    "families": {"family": {"members": ["418", "419"]}},
    "households": {"household": {"members": ["418", "419"], "state_code": {"2024": "CO", "2023": "CO"}}},
    "spm_units": {
        "spm_unit": {
            "members": ["418", "419"],
        }
    },
    "marital_units": {},
}

# using the Python package
sim = Simulation(situation=request)
sim.calculate("wic", "2024") # does not return `nan` if you do not call calculate on `wic`
data = sim.calculate("co_ctc", "2023")
print(data)

# using the API
response = requests.post("https://api.policyengine.org/us/calculate", json={"household": request})
data = response.json()["result"]["tax_units"]["tax_unit"]["co_ctc"]["2023"]
print(data)
MaxGhenis commented 3 months ago

Sorry Caleb - couple questions:

  1. Does this occur for all households?
  2. What if you calculate co_ctc for 2024?
  3. Re: (2), We previously had an issue with misaligned periods, in which you opted to run two separate calls. Could you remind me of the history there, e.g. were you still doing that for some other variables?
CalebPena commented 3 months ago
  1. I ran into the problem for all households that I tested.
  2. It does work for 2024, but this is not ideal because we calculate all of our other tax credits for the prior year.
  3. I believe that the issue was with snap when we used 2023-10 as the period we would also get nan. We resolved that one by switching the period to 2024-01. I just tried to switch the snap period back and can confirm that it is still an issue.
MaxGhenis commented 3 months ago

As a workaround could you do a separate call? We don't officially support API requests that cover multiple periods, and may disable that for now given these issues.

CalebPena commented 3 months ago

I think that I fixed this in https://github.com/PolicyEngine/policyengine-core/pull/235.