Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 244 additions & 0 deletions us/deductions/abolish_salt.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "762a9c7e",
"metadata": {},
"outputs": [],
"source": [
"from policyengine_us import Microsimulation\n",
"from policyengine_core.reforms import Reform\n",
"\n",
"reform = Reform.from_dict({\n",
" \"gov.irs.deductions.itemized.salt_and_real_estate.cap.JOINT\": {\n",
" \"2025-01-01.2100-12-31\": 0\n",
" },\n",
" \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SINGLE\": {\n",
" \"2025-01-01.2100-12-31\": 0\n",
" },\n",
" \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SEPARATE\": {\n",
" \"2025-01-01.2100-12-31\": 0\n",
" },\n",
" \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SURVIVING_SPOUSE\": {\n",
" \"2025-01-01.2100-12-31\": 0\n",
" },\n",
" \"gov.irs.deductions.itemized.salt_and_real_estate.cap.HEAD_OF_HOUSEHOLD\": {\n",
" \"2025-01-01.2100-12-31\": 0\n",
" },\n",
" \"gov.irs.deductions.itemized.salt_and_real_estate.phase_out.floor.applies\": {\n",
" \"2025-01-01.2029-12-31\": False\n",
" }\n",
"}, country_id=\"us\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "m36duawjesh",
"metadata": {},
"outputs": [],
"source": [
"# Congressional District Analysis - Setup\n",
"from policyengine_us import Microsimulation\n",
"import pandas as pd\n",
"\n",
"print(\"Setting up congressional district microsimulations...\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bgcpg1k1guu",
"metadata": {},
"outputs": [],
"source": [
"# Create baseline microsimulation with congressional district data\n",
"cd_baseline = Microsimulation(dataset=\"hf://policyengine/test/sparse_cd_stacked_2023.h5\")\n",
"\n",
"# Get congressional district geoids and correct state_fips\n",
"cd_geoids = cd_baseline.calculate(\"congressional_district_geoid\").values\n",
"correct_state_fips = cd_geoids // 100\n",
"cd_baseline.set_input(\"state_fips\", 2023, correct_state_fips)\n",
"\n",
"print(\"Baseline microsimulation ready\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99exvffovks",
"metadata": {},
"outputs": [],
"source": [
"# Create reform microsimulation \n",
"cd_reformed = Microsimulation(dataset=\"hf://policyengine/test/sparse_cd_stacked_2023.h5\", reform=reform)\n",
"cd_reformed.set_input(\"state_fips\", 2023, correct_state_fips)\n",
"\n",
"print(\"Reform microsimulation ready\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "whqowthn9rc",
"metadata": {},
"outputs": [],
"source": [
"# Calculate baseline data - individual variables to save memory\n",
"print(\"Calculating baseline variables individually...\")\n",
"household_ids = cd_baseline.calculate(\"household_id\")\n",
"state_fips = cd_baseline.calculate(\"state_fips\") \n",
"cd_geoids = cd_baseline.calculate(\"congressional_district_geoid\")\n",
"baseline_income = cd_baseline.calculate(\"household_net_income\", period=2025)\n",
"baseline_tax = cd_baseline.calculate(\"income_tax\", period=2025)\n",
"weights = cd_baseline.calculate(\"household_weight\", period=2025)\n",
"\n",
"print(f\"Baseline variables calculated for {len(household_ids)} households\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "wg4ow67ppok",
"metadata": {},
"outputs": [],
"source": [
"# Calculate reform data - individual variables\n",
"print(\"Calculating reform variables...\")\n",
"reform_income = cd_reformed.calculate(\"household_net_income\", period=2025)\n",
"reform_tax = cd_reformed.calculate(\"income_tax\", period=2025)\n",
"\n",
"print(f\"Reform variables calculated\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "tvag721fho",
"metadata": {},
"outputs": [],
"source": [
"# Create DataFrame with calculated impacts\n",
"print(\"Creating impact dataframe...\")\n",
"import pandas as pd\n",
"\n",
"cd_data = pd.DataFrame({\n",
" 'household_id': household_ids,\n",
" 'state_fips': state_fips,\n",
" 'congressional_district_geoid': cd_geoids,\n",
" 'baseline_income': baseline_income,\n",
" 'reform_income': reform_income,\n",
" 'baseline_tax': baseline_tax,\n",
" 'reform_tax': reform_tax,\n",
" 'household_weight': weights\n",
"})\n",
"\n",
"# Calculate impacts\n",
"cd_data['reform_impact'] = cd_data['reform_income'] - cd_data['baseline_income']\n",
"cd_data['tax_change'] = cd_data['reform_tax'] - cd_data['baseline_tax']\n",
"cd_data['district_number'] = cd_data['congressional_district_geoid'] % 100\n",
"\n",
"print(f\"Impact dataframe created with {len(cd_data)} households\")\n",
"\n",
"# Free memory\n",
"del household_ids, state_fips, cd_geoids, baseline_income, reform_income, baseline_tax, reform_tax, weights"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "axc8cqndhyk",
"metadata": {},
"outputs": [],
"source": [
"# Group by congressional district and calculate weighted averages\n",
"print(\"Aggregating by congressional district...\")\n",
"\n",
"cd_summary = cd_data.groupby(['state_fips', 'congressional_district_geoid', 'district_number']).apply(\n",
" lambda x: pd.Series({\n",
" 'avg_income_impact': (x['reform_impact'] * x['household_weight']).sum() / x['household_weight'].sum(),\n",
" 'avg_tax_change': (x['tax_change'] * x['household_weight']).sum() / x['household_weight'].sum(),\n",
" 'total_households': x['household_weight'].sum()\n",
" })\n",
").reset_index()\n",
"\n",
"cd_summary = cd_summary.sort_values('avg_income_impact', ascending=False)\n",
"\n",
"print(f\"Summary created for {len(cd_summary)} congressional districts\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41r7sfheye8",
"metadata": {},
"outputs": [],
"source": [
"# Show top 10 districts\n",
"print(\"Top 10 Congressional Districts by Average Household Income Impact from Abolishing SALT Cap:\")\n",
"print(cd_summary.head(10)[['state_fips', 'district_number', 'avg_income_impact', 'avg_tax_change', 'total_households']])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "saawvdbwkra",
"metadata": {},
"outputs": [],
"source": [
"# Show bottom 10 districts \n",
"print(\"Bottom 10 Congressional Districts (least benefit or most harm):\")\n",
"print(cd_summary.tail(10)[['state_fips', 'district_number', 'avg_income_impact', 'avg_tax_change', 'total_households']])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7uf8bvtd96o",
"metadata": {},
"outputs": [],
"source": [
"# Overall statistics\n",
"print(f\"Overall Statistics:\")\n",
"print(f\"Total Congressional Districts: {len(cd_summary)}\")\n",
"print(f\"Districts with positive income impact: {len(cd_summary[cd_summary['avg_income_impact'] > 0])}\")\n",
"print(f\"Districts with negative income impact: {len(cd_summary[cd_summary['avg_income_impact'] < 0])}\")\n",
"print(f\"Average impact across all districts: ${cd_summary['avg_income_impact'].mean():.2f}\")\n",
"print(f\"Median impact across all districts: ${cd_summary['avg_income_impact'].median():.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4gj9j5j8tp",
"metadata": {},
"outputs": [],
"source": [
"# Display the full summary dataset\n",
"cd_summary"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pe",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading