|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | +IRS_AGI_BANDS = [ |
| 5 | + (-np.inf, 1.0, "<$1"), |
| 6 | + (1.0, 10_000.0, "$1-$10k"), |
| 7 | + (10_000.0, 25_000.0, "$10k-$25k"), |
| 8 | + (25_000.0, 50_000.0, "$25k-$50k"), |
| 9 | + (50_000.0, 75_000.0, "$50k-$75k"), |
| 10 | + (75_000.0, 100_000.0, "$75k-$100k"), |
| 11 | + (100_000.0, 200_000.0, "$100k-$200k"), |
| 12 | + (200_000.0, 500_000.0, "$200k-$500k"), |
| 13 | + (500_000.0, np.inf, "$500k+"), |
| 14 | +] |
| 15 | + |
| 16 | +FILING_STATUS_LABELS = { |
| 17 | + "SINGLE": "Single", |
| 18 | + "HEAD_OF_HOUSEHOLD": "Head of household", |
| 19 | + "JOINT": "Joint / surviving spouse", |
| 20 | + "SURVIVING_SPOUSE": "Joint / surviving spouse", |
| 21 | + "SEPARATE": "Separate", |
| 22 | +} |
| 23 | + |
| 24 | +FILING_STATUS_ORDER = [ |
| 25 | + "Single", |
| 26 | + "Head of household", |
| 27 | + "Joint / surviving spouse", |
| 28 | + "Separate", |
| 29 | + "Other", |
| 30 | +] |
| 31 | + |
| 32 | +CTC_GROUP_COLUMNS = [ |
| 33 | + "tax_unit_count", |
| 34 | + "ctc_qualifying_children", |
| 35 | + "ctc_recipient_count", |
| 36 | + "refundable_ctc_recipient_count", |
| 37 | + "non_refundable_ctc_recipient_count", |
| 38 | + "ctc", |
| 39 | + "refundable_ctc", |
| 40 | + "non_refundable_ctc", |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +def _assign_agi_bands(adjusted_gross_income: np.ndarray) -> pd.Categorical: |
| 45 | + labels = [label for _, _, label in IRS_AGI_BANDS] |
| 46 | + agi_band = np.full(len(adjusted_gross_income), labels[-1], dtype=object) |
| 47 | + for lower, upper, label in IRS_AGI_BANDS: |
| 48 | + mask = (adjusted_gross_income >= lower) & (adjusted_gross_income < upper) |
| 49 | + agi_band[mask] = label |
| 50 | + return pd.Categorical(agi_band, categories=labels, ordered=True) |
| 51 | + |
| 52 | + |
| 53 | +def _normalize_filing_status(filing_status: pd.Series) -> pd.Categorical: |
| 54 | + labels = [ |
| 55 | + FILING_STATUS_LABELS.get(str(value), "Other") |
| 56 | + for value in filing_status.astype(str) |
| 57 | + ] |
| 58 | + return pd.Categorical(labels, categories=FILING_STATUS_ORDER, ordered=True) |
| 59 | + |
| 60 | + |
| 61 | +def build_ctc_diagnostic_tables(frame: pd.DataFrame) -> dict[str, pd.DataFrame]: |
| 62 | + """Aggregate weighted CTC diagnostics by AGI band and filing status.""" |
| 63 | + work = frame.copy() |
| 64 | + weights = work["tax_unit_weight"].astype(float).to_numpy() |
| 65 | + |
| 66 | + work["agi_band"] = _assign_agi_bands( |
| 67 | + work["adjusted_gross_income"].astype(float).to_numpy() |
| 68 | + ) |
| 69 | + work["filing_status_group"] = _normalize_filing_status(work["filing_status"]) |
| 70 | + |
| 71 | + work["tax_unit_count"] = weights |
| 72 | + work["ctc_qualifying_children"] = ( |
| 73 | + work["ctc_qualifying_children"].astype(float).to_numpy() * weights |
| 74 | + ) |
| 75 | + work["ctc_recipient_count"] = (work["ctc"].astype(float).to_numpy() > 0).astype( |
| 76 | + float |
| 77 | + ) * weights |
| 78 | + work["refundable_ctc_recipient_count"] = ( |
| 79 | + work["refundable_ctc"].astype(float).to_numpy() > 0 |
| 80 | + ).astype(float) * weights |
| 81 | + work["non_refundable_ctc_recipient_count"] = ( |
| 82 | + work["non_refundable_ctc"].astype(float).to_numpy() > 0 |
| 83 | + ).astype(float) * weights |
| 84 | + work["ctc"] = work["ctc"].astype(float).to_numpy() * weights |
| 85 | + work["refundable_ctc"] = work["refundable_ctc"].astype(float).to_numpy() * weights |
| 86 | + work["non_refundable_ctc"] = ( |
| 87 | + work["non_refundable_ctc"].astype(float).to_numpy() * weights |
| 88 | + ) |
| 89 | + |
| 90 | + by_agi = ( |
| 91 | + work.groupby("agi_band", observed=False)[CTC_GROUP_COLUMNS] |
| 92 | + .sum() |
| 93 | + .reset_index() |
| 94 | + .rename(columns={"agi_band": "group"}) |
| 95 | + ) |
| 96 | + by_filing_status = ( |
| 97 | + work.groupby("filing_status_group", observed=False)[CTC_GROUP_COLUMNS] |
| 98 | + .sum() |
| 99 | + .reset_index() |
| 100 | + .rename(columns={"filing_status_group": "group"}) |
| 101 | + ) |
| 102 | + |
| 103 | + return { |
| 104 | + "by_agi_band": by_agi, |
| 105 | + "by_filing_status": by_filing_status, |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +def create_ctc_diagnostic_tables(sim) -> dict[str, pd.DataFrame]: |
| 110 | + """Calculate weighted CTC diagnostic tables from a microsimulation.""" |
| 111 | + frame = pd.DataFrame( |
| 112 | + { |
| 113 | + "adjusted_gross_income": sim.calculate("adjusted_gross_income").values, |
| 114 | + "filing_status": sim.calculate("filing_status").values, |
| 115 | + "tax_unit_weight": sim.calculate("tax_unit_weight").values, |
| 116 | + "ctc_qualifying_children": sim.calculate("ctc_qualifying_children").values, |
| 117 | + "ctc": sim.calculate("ctc").values, |
| 118 | + "refundable_ctc": sim.calculate("refundable_ctc").values, |
| 119 | + "non_refundable_ctc": sim.calculate("non_refundable_ctc").values, |
| 120 | + } |
| 121 | + ) |
| 122 | + return build_ctc_diagnostic_tables(frame) |
| 123 | + |
| 124 | + |
| 125 | +def _format_count(value: float) -> str: |
| 126 | + return f"{value / 1e6:,.2f}M" |
| 127 | + |
| 128 | + |
| 129 | +def _format_amount(value: float) -> str: |
| 130 | + return f"${value / 1e9:,.1f}B" |
| 131 | + |
| 132 | + |
| 133 | +def format_ctc_diagnostic_table(table: pd.DataFrame) -> str: |
| 134 | + display = table.copy() |
| 135 | + for column in [ |
| 136 | + "tax_unit_count", |
| 137 | + "ctc_qualifying_children", |
| 138 | + "ctc_recipient_count", |
| 139 | + "refundable_ctc_recipient_count", |
| 140 | + "non_refundable_ctc_recipient_count", |
| 141 | + ]: |
| 142 | + display[column] = display[column].map(_format_count) |
| 143 | + for column in ["ctc", "refundable_ctc", "non_refundable_ctc"]: |
| 144 | + display[column] = display[column].map(_format_amount) |
| 145 | + return display.to_string(index=False) |
0 commit comments