|
| 1 | +""" |
| 2 | +Oregon Child Tax Credit Analysis by State Senate District |
| 3 | +
|
| 4 | +Calculates the impact of doubling Oregon's Young Child Tax Credit (or_ctc) |
| 5 | +by State Legislative District Upper (SLDU) - i.e., State Senate districts. |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +from pathlib import Path |
| 11 | +from policyengine_us import Microsimulation |
| 12 | +from policyengine_core.reforms import Reform |
| 13 | + |
| 14 | +# Local imports |
| 15 | +from policyengine_us_data.datasets.cps.local_area_calibration.block_assignment import ( |
| 16 | + assign_geography_for_cd, |
| 17 | + load_block_crosswalk, |
| 18 | +) |
| 19 | +from policyengine_us_data.storage import STORAGE_FOLDER |
| 20 | + |
| 21 | +# Oregon congressional districts (119th Congress) |
| 22 | +# Oregon has 6 CDs, geoid format: state_fips * 100 + district |
| 23 | +# Oregon FIPS = 41, so: 4101, 4102, 4103, 4104, 4105, 4106 |
| 24 | +OREGON_CD_GEOIDS = [4101, 4102, 4103, 4104, 4105, 4106] |
| 25 | + |
| 26 | + |
| 27 | +def load_district_data(cd_geoid: int) -> dict: |
| 28 | + """Load household data from a district H5 file.""" |
| 29 | + h5_path = STORAGE_FOLDER / "districts" / f"OR-{cd_geoid % 100:02d}.h5" |
| 30 | + if not h5_path.exists(): |
| 31 | + raise FileNotFoundError(f"District file not found: {h5_path}") |
| 32 | + |
| 33 | + import h5py |
| 34 | + |
| 35 | + data = {} |
| 36 | + with h5py.File(h5_path, "r") as f: |
| 37 | + # Get key variables we need |
| 38 | + for var in [ |
| 39 | + "household_weight", |
| 40 | + "household_id", |
| 41 | + "person_id", |
| 42 | + "age", |
| 43 | + "is_tax_unit_head", |
| 44 | + "tax_unit_id", |
| 45 | + ]: |
| 46 | + if var in f: |
| 47 | + # Handle year dimension if present |
| 48 | + arr = f[var][:] |
| 49 | + if len(arr.shape) > 1: |
| 50 | + arr = arr[:, 0] # Take first year |
| 51 | + data[var] = arr |
| 52 | + return data |
| 53 | + |
| 54 | + |
| 55 | +def run_oregon_ctc_analysis(): |
| 56 | + """Run the Oregon CTC analysis by state senate district.""" |
| 57 | + print("=" * 60) |
| 58 | + print("Oregon Child Tax Credit Analysis by State Senate District") |
| 59 | + print("=" * 60) |
| 60 | + |
| 61 | + # Load block crosswalk for SLDU lookups |
| 62 | + print("\nLoading block crosswalk...") |
| 63 | + crosswalk = load_block_crosswalk() |
| 64 | + oregon_blocks = crosswalk[crosswalk["block_geoid"].str[:2] == "41"] |
| 65 | + print(f" Oregon blocks: {len(oregon_blocks):,}") |
| 66 | + print(f" Unique SLDUs: {oregon_blocks['sldu'].nunique()}") |
| 67 | + |
| 68 | + # Results accumulator |
| 69 | + results_by_sldu = {} |
| 70 | + |
| 71 | + print("\nProcessing Oregon congressional districts...") |
| 72 | + |
| 73 | + for cd_geoid in OREGON_CD_GEOIDS: |
| 74 | + cd_name = f"OR-{cd_geoid % 100:02d}" |
| 75 | + print(f"\n Processing {cd_name}...") |
| 76 | + |
| 77 | + # Load district data |
| 78 | + h5_path = STORAGE_FOLDER / "districts" / f"{cd_name}.h5" |
| 79 | + if not h5_path.exists(): |
| 80 | + print(f" Skipping - file not found") |
| 81 | + continue |
| 82 | + |
| 83 | + # Run microsimulation for this district |
| 84 | + # Baseline |
| 85 | + baseline = Microsimulation(dataset=str(h5_path)) |
| 86 | + baseline_ctc = baseline.calculate("or_ctc", 2024) |
| 87 | + baseline_weights = baseline.calculate("household_weight", 2024) |
| 88 | + |
| 89 | + # Reform: double the OR CTC max amounts |
| 90 | + # or_young_child_tax_credit_max is the parameter |
| 91 | + def double_or_ctc(parameters): |
| 92 | + # Double the max credit amount |
| 93 | + or_ctc = parameters.gov.states.or_.tax.income.credits.ctc |
| 94 | + or_ctc.amount.update( |
| 95 | + start=pd.Timestamp("2024-01-01"), |
| 96 | + stop=pd.Timestamp("2100-12-31"), |
| 97 | + value=or_ctc.amount("2024-01-01") * 2, |
| 98 | + ) |
| 99 | + return parameters |
| 100 | + |
| 101 | + class DoubleORCTC(Reform): |
| 102 | + def apply(self): |
| 103 | + self.modify_parameters(double_or_ctc) |
| 104 | + |
| 105 | + reform = Microsimulation(dataset=str(h5_path), reform=DoubleORCTC) |
| 106 | + reform_ctc = reform.calculate("or_ctc", 2024) |
| 107 | + |
| 108 | + # Get number of households for block assignment |
| 109 | + n_households = len(baseline_weights) |
| 110 | + print(f" Households: {n_households:,}") |
| 111 | + |
| 112 | + # Assign blocks and get SLDU for each household |
| 113 | + geo = assign_geography_for_cd( |
| 114 | + cd_geoid=str(cd_geoid), |
| 115 | + n_households=n_households, |
| 116 | + seed=cd_geoid, # Reproducible |
| 117 | + ) |
| 118 | + |
| 119 | + sldu_assignments = geo["sldu"] |
| 120 | + |
| 121 | + # Calculate impact per household |
| 122 | + impact = reform_ctc - baseline_ctc |
| 123 | + |
| 124 | + # Aggregate by SLDU |
| 125 | + unique_sldus = np.unique(sldu_assignments[sldu_assignments != ""]) |
| 126 | + |
| 127 | + for sldu in unique_sldus: |
| 128 | + mask = sldu_assignments == sldu |
| 129 | + sldu_impact = np.sum(impact[mask] * baseline_weights[mask]) |
| 130 | + sldu_baseline = np.sum(baseline_ctc[mask] * baseline_weights[mask]) |
| 131 | + sldu_reform = np.sum(reform_ctc[mask] * baseline_weights[mask]) |
| 132 | + sldu_hh = np.sum(mask) |
| 133 | + sldu_weighted_hh = np.sum(baseline_weights[mask]) |
| 134 | + |
| 135 | + if sldu not in results_by_sldu: |
| 136 | + results_by_sldu[sldu] = { |
| 137 | + "baseline_ctc": 0, |
| 138 | + "reform_ctc": 0, |
| 139 | + "impact": 0, |
| 140 | + "households": 0, |
| 141 | + "weighted_households": 0, |
| 142 | + } |
| 143 | + |
| 144 | + results_by_sldu[sldu]["baseline_ctc"] += sldu_baseline |
| 145 | + results_by_sldu[sldu]["reform_ctc"] += sldu_reform |
| 146 | + results_by_sldu[sldu]["impact"] += sldu_impact |
| 147 | + results_by_sldu[sldu]["households"] += sldu_hh |
| 148 | + results_by_sldu[sldu]["weighted_households"] += sldu_weighted_hh |
| 149 | + |
| 150 | + # Create results DataFrame |
| 151 | + print("\n" + "=" * 60) |
| 152 | + print("RESULTS: Impact of Doubling Oregon CTC by State Senate District") |
| 153 | + print("=" * 60) |
| 154 | + |
| 155 | + df = pd.DataFrame.from_dict(results_by_sldu, orient="index") |
| 156 | + df.index.name = "sldu" |
| 157 | + df = df.reset_index() |
| 158 | + |
| 159 | + # Convert to millions |
| 160 | + df["baseline_ctc_millions"] = df["baseline_ctc"] / 1e6 |
| 161 | + df["reform_ctc_millions"] = df["reform_ctc"] / 1e6 |
| 162 | + df["impact_millions"] = df["impact"] / 1e6 |
| 163 | + |
| 164 | + # Sort by impact |
| 165 | + df = df.sort_values("impact_millions", ascending=False) |
| 166 | + |
| 167 | + # Display results |
| 168 | + print( |
| 169 | + f"\n{'SLDU':<8} {'Baseline':>12} {'Reform':>12} {'Impact':>12} {'Households':>12}" |
| 170 | + ) |
| 171 | + print(f"{'':8} {'($M)':>12} {'($M)':>12} {'($M)':>12} {'(weighted)':>12}") |
| 172 | + print("-" * 60) |
| 173 | + |
| 174 | + for _, row in df.iterrows(): |
| 175 | + print( |
| 176 | + f"{row['sldu']:<8} " |
| 177 | + f"{row['baseline_ctc_millions']:>12.2f} " |
| 178 | + f"{row['reform_ctc_millions']:>12.2f} " |
| 179 | + f"{row['impact_millions']:>12.2f} " |
| 180 | + f"{row['weighted_households']:>12,.0f}" |
| 181 | + ) |
| 182 | + |
| 183 | + print("-" * 60) |
| 184 | + total_baseline = df["baseline_ctc_millions"].sum() |
| 185 | + total_reform = df["reform_ctc_millions"].sum() |
| 186 | + total_impact = df["impact_millions"].sum() |
| 187 | + total_hh = df["weighted_households"].sum() |
| 188 | + print( |
| 189 | + f"{'TOTAL':<8} {total_baseline:>12.2f} {total_reform:>12.2f} " |
| 190 | + f"{total_impact:>12.2f} {total_hh:>12,.0f}" |
| 191 | + ) |
| 192 | + |
| 193 | + # Save to CSV |
| 194 | + output_path = Path("oregon_ctc_by_sldu.csv") |
| 195 | + df.to_csv(output_path, index=False) |
| 196 | + print(f"\nResults saved to: {output_path}") |
| 197 | + |
| 198 | + return df |
| 199 | + |
| 200 | + |
| 201 | +if __name__ == "__main__": |
| 202 | + run_oregon_ctc_analysis() |
0 commit comments