-
Notifications
You must be signed in to change notification settings - Fork 10
Split Enhanced CPS CTC calibration targets across national and unified paths #711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9c73019
Fix legacy refundable CTC calibration
MaxGhenis ff6afc3
Split CTC calibration into refundable and nonrefundable targets
MaxGhenis 5846bd5
Format IRS SOI target mapping
MaxGhenis ee0cf25
Fix database import recursion in CI
MaxGhenis 57e946a
Harden CPS ORG month loading
MaxGhenis f45c408
Format ORG loader changes
MaxGhenis fa8d562
Add DB-backed nonrefundable CTC targets
MaxGhenis f591bcd
Add live CTC diagnostics to national validation
MaxGhenis 44e2272
Fix CTC target periods in database ETL
MaxGhenis ccc2fa3
Format CTC diagnostics files
MaxGhenis 022275a
Use geography-year CTC targets in IRS ETL
MaxGhenis c545a77
Fix HF dataset path unit test in CI
MaxGhenis e53f022
Unify geography-file CTC target specs
MaxGhenis b963086
Merge branch 'main' into codex/fix-legacy-ctc-calibration
MaxGhenis 4a3921f
Emit has_tin in CPS-derived datasets
MaxGhenis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Split legacy national CTC calibration into separate refundable and nonrefundable IRS SOI amount and recipient-count targets, added DB-backed nonrefundable CTC targets for both national and unified district calibration, and fixed recursive package imports so database creation scripts and the national validation tooling can import cleanly in fresh environments. The national validator now also reports CTC totals and grouped diagnostics by AGI band and filing status, its advertised `--hf-path` mode now completes structural checks against published Hugging Face H5 artifacts, and CPS-derived datasets now emit `has_tin` plus a temporary `has_itin` compatibility alias derived from identification status. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,38 @@ | ||
| from .datasets import * | ||
| from importlib import import_module | ||
|
|
||
| from .geography import ZIP_CODE_DATASET | ||
|
|
||
| _LAZY_EXPORTS = { | ||
| "CPS_2024": ( | ||
| "policyengine_us_data.datasets.cps.cps", | ||
| "CPS_2024", | ||
| ), | ||
| "EnhancedCPS_2024": ( | ||
| "policyengine_us_data.datasets.cps.enhanced_cps", | ||
| "EnhancedCPS_2024", | ||
| ), | ||
| "ExtendedCPS_2024": ( | ||
| "policyengine_us_data.datasets.cps.extended_cps", | ||
| "ExtendedCPS_2024", | ||
| ), | ||
| "PUF_2024": ( | ||
| "policyengine_us_data.datasets.puf.puf", | ||
| "PUF_2024", | ||
| ), | ||
| } | ||
|
|
||
| __all__ = ["ZIP_CODE_DATASET", *_LAZY_EXPORTS] | ||
|
|
||
|
|
||
| def __getattr__(name: str): | ||
| if name not in _LAZY_EXPORTS: | ||
| raise AttributeError(f"module {__name__!r} has no attribute {name!r}") | ||
|
|
||
| module_name, attribute_name = _LAZY_EXPORTS[name] | ||
| value = getattr(import_module(module_name), attribute_name) | ||
| globals()[name] = value | ||
| return value | ||
|
|
||
|
|
||
| def __dir__(): | ||
| return sorted(set(globals()) | set(_LAZY_EXPORTS)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
| IRS_AGI_BANDS = [ | ||
| (-np.inf, 1.0, "<$1"), | ||
| (1.0, 10_000.0, "$1-$10k"), | ||
| (10_000.0, 25_000.0, "$10k-$25k"), | ||
| (25_000.0, 50_000.0, "$25k-$50k"), | ||
| (50_000.0, 75_000.0, "$50k-$75k"), | ||
| (75_000.0, 100_000.0, "$75k-$100k"), | ||
| (100_000.0, 200_000.0, "$100k-$200k"), | ||
| (200_000.0, 500_000.0, "$200k-$500k"), | ||
| (500_000.0, np.inf, "$500k+"), | ||
| ] | ||
|
|
||
| FILING_STATUS_LABELS = { | ||
| "SINGLE": "Single", | ||
| "HEAD_OF_HOUSEHOLD": "Head of household", | ||
| "JOINT": "Joint / surviving spouse", | ||
| "SURVIVING_SPOUSE": "Joint / surviving spouse", | ||
| "SEPARATE": "Separate", | ||
| } | ||
|
|
||
| FILING_STATUS_ORDER = [ | ||
| "Single", | ||
| "Head of household", | ||
| "Joint / surviving spouse", | ||
| "Separate", | ||
| "Other", | ||
| ] | ||
|
|
||
| CTC_GROUP_COLUMNS = [ | ||
| "tax_unit_count", | ||
| "ctc_qualifying_children", | ||
| "ctc_recipient_count", | ||
| "refundable_ctc_recipient_count", | ||
| "non_refundable_ctc_recipient_count", | ||
| "ctc", | ||
| "refundable_ctc", | ||
| "non_refundable_ctc", | ||
| ] | ||
|
|
||
|
|
||
| def _assign_agi_bands(adjusted_gross_income: np.ndarray) -> pd.Categorical: | ||
| labels = [label for _, _, label in IRS_AGI_BANDS] | ||
| agi_band = np.full(len(adjusted_gross_income), labels[-1], dtype=object) | ||
| for lower, upper, label in IRS_AGI_BANDS: | ||
| mask = (adjusted_gross_income >= lower) & (adjusted_gross_income < upper) | ||
| agi_band[mask] = label | ||
| return pd.Categorical(agi_band, categories=labels, ordered=True) | ||
|
|
||
|
|
||
| def _normalize_filing_status(filing_status: pd.Series) -> pd.Categorical: | ||
| labels = [ | ||
| FILING_STATUS_LABELS.get(str(value), "Other") | ||
| for value in filing_status.astype(str) | ||
| ] | ||
| return pd.Categorical(labels, categories=FILING_STATUS_ORDER, ordered=True) | ||
|
|
||
|
|
||
| def build_ctc_diagnostic_tables(frame: pd.DataFrame) -> dict[str, pd.DataFrame]: | ||
| """Aggregate weighted CTC diagnostics by AGI band and filing status.""" | ||
| work = frame.copy() | ||
| weights = work["tax_unit_weight"].astype(float).to_numpy() | ||
|
|
||
| work["agi_band"] = _assign_agi_bands( | ||
| work["adjusted_gross_income"].astype(float).to_numpy() | ||
| ) | ||
| work["filing_status_group"] = _normalize_filing_status(work["filing_status"]) | ||
|
|
||
| work["tax_unit_count"] = weights | ||
| work["ctc_qualifying_children"] = ( | ||
| work["ctc_qualifying_children"].astype(float).to_numpy() * weights | ||
| ) | ||
| work["ctc_recipient_count"] = (work["ctc"].astype(float).to_numpy() > 0).astype( | ||
| float | ||
| ) * weights | ||
| work["refundable_ctc_recipient_count"] = ( | ||
| work["refundable_ctc"].astype(float).to_numpy() > 0 | ||
| ).astype(float) * weights | ||
| work["non_refundable_ctc_recipient_count"] = ( | ||
| work["non_refundable_ctc"].astype(float).to_numpy() > 0 | ||
| ).astype(float) * weights | ||
| work["ctc"] = work["ctc"].astype(float).to_numpy() * weights | ||
| work["refundable_ctc"] = work["refundable_ctc"].astype(float).to_numpy() * weights | ||
| work["non_refundable_ctc"] = ( | ||
| work["non_refundable_ctc"].astype(float).to_numpy() * weights | ||
| ) | ||
|
|
||
| by_agi = ( | ||
| work.groupby("agi_band", observed=False)[CTC_GROUP_COLUMNS] | ||
| .sum() | ||
| .reset_index() | ||
| .rename(columns={"agi_band": "group"}) | ||
| ) | ||
| by_filing_status = ( | ||
| work.groupby("filing_status_group", observed=False)[CTC_GROUP_COLUMNS] | ||
| .sum() | ||
| .reset_index() | ||
| .rename(columns={"filing_status_group": "group"}) | ||
| ) | ||
|
|
||
| return { | ||
| "by_agi_band": by_agi, | ||
| "by_filing_status": by_filing_status, | ||
| } | ||
|
|
||
|
|
||
| def create_ctc_diagnostic_tables(sim) -> dict[str, pd.DataFrame]: | ||
| """Calculate weighted CTC diagnostic tables from a microsimulation.""" | ||
| frame = pd.DataFrame( | ||
| { | ||
| "adjusted_gross_income": sim.calculate("adjusted_gross_income").values, | ||
| "filing_status": sim.calculate("filing_status").values, | ||
| "tax_unit_weight": sim.calculate("tax_unit_weight").values, | ||
| "ctc_qualifying_children": sim.calculate("ctc_qualifying_children").values, | ||
| "ctc": sim.calculate("ctc").values, | ||
| "refundable_ctc": sim.calculate("refundable_ctc").values, | ||
| "non_refundable_ctc": sim.calculate("non_refundable_ctc").values, | ||
| } | ||
| ) | ||
| return build_ctc_diagnostic_tables(frame) | ||
|
|
||
|
|
||
| def _format_count(value: float) -> str: | ||
| return f"{value / 1e6:,.2f}M" | ||
|
|
||
|
|
||
| def _format_amount(value: float) -> str: | ||
| return f"${value / 1e9:,.1f}B" | ||
|
|
||
|
|
||
| def format_ctc_diagnostic_table(table: pd.DataFrame) -> str: | ||
| display = table.copy() | ||
| for column in [ | ||
| "tax_unit_count", | ||
| "ctc_qualifying_children", | ||
| "ctc_recipient_count", | ||
| "refundable_ctc_recipient_count", | ||
| "non_refundable_ctc_recipient_count", | ||
| ]: | ||
| display[column] = display[column].map(_format_count) | ||
| for column in ["ctc", "refundable_ctc", "non_refundable_ctc"]: | ||
| display[column] = display[column].map(_format_amount) | ||
| return display.to_string(index=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.