Skip to content

Let callers request extra variables from calculate_household_impact#287

Open
MaxGhenis wants to merge 1 commit intomainfrom
extend-calculate-household-impact
Open

Let callers request extra variables from calculate_household_impact#287
MaxGhenis wants to merge 1 commit intomainfrom
extend-calculate-household-impact

Conversation

@MaxGhenis
Copy link
Copy Markdown
Contributor

Problem

`policyengine.tax_benefit_models.{us,uk}.calculate_household_impact` (and the underlying `Simulation.run`) only computes variables that appear in the hardcoded `entity_variables` dict on `PolicyEngineUSLatest` / `PolicyEngineUKLatest`. That list is a small curated default for UI/API views; it excludes most variables a benchmark suite needs:

`adjusted_gross_income`, `state_agi`, `free_school_meals`, `is_medicaid_eligible`, `income_tax_refundable_credits`, `state_refundable_credits`, `state_income_tax_before_refundable_credits`, …

This blocks the `policybench` migration from direct `policyengine_us` imports to the `policyengine.py` certified-bundle path.

Fix

Thread a simulation-scoped `extra_variables` mapping through `Simulation` → `PolicyEngineUSLatest.run` / `PolicyEngineUKLatest.run` → `calculate_household_impact`. The merged dict is deduped; bundle defaults keep their order. Existing callers see identical output.

```python
from policyengine.tax_benefit_models.us import (
calculate_household_impact, USHouseholdInput,
)

result = calculate_household_impact(
USHouseholdInput(people=[{"age": 35, "employment_income": 60000}], year=2026),
extra_variables={"tax_unit": ["adjusted_gross_income"]},
)
assert result.tax_unit[0]["adjusted_gross_income"] > 0
```

Test plan

  • 3 new tests in `tests/test_household_impact.py` covering the tax_unit, household, and no-op paths
  • Full local run: `pytest tests/test_household_impact.py tests/test_release_manifests.py tests/test_trace_tro.py tests/test_results.py tests/test_models.py tests/test_us_reform_application.py` — 126 passed
  • `ruff check .` + `ruff format --check .` — clean

🤖 Generated with Claude Code

Adds an `extra_variables` mapping (`{entity_name: [variable_name,...]}`)
to:
  - policyengine.core.Simulation
  - policyengine.tax_benefit_models.us.calculate_household_impact
  - policyengine.tax_benefit_models.uk.calculate_household_impact
  - PolicyEngineUSLatest.run / PolicyEngineUKLatest.run

Previously both simulations computed only the hardcoded
`self.entity_variables` list (a small default curated for UI/API
views) and returned outputs missing anything else — including
adjusted_gross_income, state_agi, free_school_meals,
is_medicaid_eligible, income_tax_refundable_credits,
state_refundable_credits, state_income_tax_before_refundable_credits,
and every other variable a benchmark suite typically needs.

The fix threads a simulation-scoped override through the run path.
Extra variables are merged with the defaults (dedup; order preserved
for the defaults) so existing callers see no change, and the
returned USHouseholdOutput / UKHouseholdOutput dicts gain the
requested keys.

Unblocks the policybench migration to policyengine.py: callers can
now do

    calculate_household_impact(
        input,
        extra_variables={"tax_unit": ["adjusted_gross_income", ...]},
    )

without monkey-patching us_latest.entity_variables.

Tests: three new tests in test_household_impact.py cover the tax_unit
and household paths plus the no-op behaviour when extra_variables is
empty/omitted.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant