Skip to content
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

Adjust the harris CTC household notebook #16

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
158 changes: 158 additions & 0 deletions us/irs/income/credits/ctc/harris/harris_ctc_budget.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from policyengine_us import Microsimulation\n",
"from policyengine_core.reforms import Reform\n",
"import plotly.graph_objects as go\n",
"import numpy as np\n",
"from multiprocessing import Pool, cpu_count\n",
"from functools import lru_cache"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"reform = Reform.from_dict({\n",
" \"gov.contrib.congress.delauro.american_family_act.baby_bonus\": {\n",
" \"2024-01-01.2100-12-31\": 2400\n",
" },\n",
" \"gov.irs.credits.ctc.amount.arpa[0].amount\": {\n",
" \"2023-01-01.2028-12-31\": 3600\n",
" },\n",
" \"gov.irs.credits.ctc.amount.arpa[1].amount\": {\n",
" \"2023-01-01.2028-12-31\": 3000\n",
" },\n",
" \"gov.irs.credits.ctc.phase_out.arpa.in_effect\": {\n",
" \"2023-01-01.2028-12-31\": True\n",
" },\n",
" \"gov.irs.credits.ctc.refundable.fully_refundable\": {\n",
" \"2023-01-01.2028-12-31\": True\n",
" }\n",
"}, country_id=\"us\")\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Initialize simulations\n",
"baseline = Microsimulation(dataset=\"enhanced_cps_2022\")\n",
"reformed = Microsimulation(reform=reform, dataset=\"enhanced_cps_2022\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"@lru_cache(maxsize=None)\n",
"def calculate_difference(year):\n",
" try:\n",
" baseline_person = baseline.calculate(\"household_net_income\", period=year)\n",
" reformed_person = reformed.calculate(\"household_net_income\", period=year)\n",
" difference = (reformed_person - baseline_person).sum()\n",
" return year, difference\n",
" except Exception as e:\n",
" print(f\"Error calculating for year {year}: {str(e)}\")\n",
" return year, None"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def main():\n",
" years = list(range(2025, 2035))\n",
" \n",
" # Calculate differences sequentially\n",
" results = [calculate_difference(year) for year in years]\n",
" \n",
" # Filter out any None results from errors\n",
" valid_results = [(year, diff) for year, diff in results if diff is not None]\n",
" valid_years, differences = zip(*valid_results)\n",
"\n",
" # Create Plotly bar chart\n",
" fig = go.Figure(data=[\n",
" go.Bar(name='Impact', x=valid_years, y=differences)\n",
" ])\n",
"\n",
" # Update layout\n",
" fig.update_layout(\n",
" title='Impact of CTC Reform on Household Net Income (2025-2034)',\n",
" xaxis_title='Year',\n",
" yaxis_title='Total Difference in Household Net Income',\n",
" yaxis_tickformat='$,.0f',\n",
" )\n",
"\n",
" # Add hover template to show values in billions\n",
" fig.update_traces(\n",
" hovertemplate='Year: %{x}<br>Impact: $%{y:,.0f}<br>($%{y:,.1f} billion)',\n",
" text=[f'${d/1e9:.1f}B' for d in differences],\n",
" textposition='outside'\n",
" )\n",
"\n",
" # Show the plot\n",
" fig.show()\n",
"\n",
" # Print numerical results\n",
" for year, difference in zip(valid_years, differences):\n",
" print(f\"{year}: ${difference/1e9:.2f} billion\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "policyengine",
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
208 changes: 208 additions & 0 deletions us/irs/income/credits/ctc/harris/harris_ctc_household.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import plotly.graph_objects as go\n",
"from policyengine_us import Simulation\n",
"from policyengine_core.reforms import Reform\n",
"from policyengine_core.charts import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Define the reform\n",
"reform = Reform.from_dict({\n",
" \"gov.contrib.congress.delauro.american_family_act.baby_bonus\": {\n",
" \"2024-01-01.2100-12-31\": 2400\n",
" },\n",
" \"gov.irs.credits.ctc.amount.arpa[0].amount\": {\n",
" \"2023-01-01.2028-12-31\": 3600\n",
" },\n",
" \"gov.irs.credits.ctc.amount.arpa[1].amount\": {\n",
" \"2023-01-01.2028-12-31\": 3000\n",
" },\n",
" \"gov.irs.credits.ctc.phase_out.arpa.in_effect\": {\n",
" \"2023-01-01.2028-12-31\": True\n",
" },\n",
" \"gov.irs.credits.ctc.refundable.fully_refundable\": {\n",
" \"2023-01-01.2028-12-31\": True\n",
" }\n",
"}, country_id=\"us\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"YEAR = \"2025\"\n",
"MAX_INCOME = 250000"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def create_situation(filing_status, child_age):\n",
" situation = {\n",
" \"people\": {\n",
" \"adult\": {\n",
" \"age\": {YEAR: 40},\n",
" },\n",
" \"child\": {\n",
" \"age\": {YEAR: child_age},\n",
" }\n",
" },\n",
" \"families\": {\"family\": {\"members\": [\"adult\", \"child\"]}},\n",
" \"marital_units\": {\"marital_unit\": {\"members\": [\"adult\"]}},\n",
" \"tax_units\": {\"tax_unit\": {\"members\": [\"adult\", \"child\"]}},\n",
" \"households\": {\n",
" \"household\": {\"members\": [\"adult\", \"child\"], \"state_name\": {YEAR: \"TX\"}}\n",
" },\n",
" \"axes\": [[\n",
" {\n",
" \"name\": \"employment_income\",\n",
" \"min\": 0,\n",
" \"max\": MAX_INCOME,\n",
" \"count\": 201,\n",
" \"period\": YEAR,\n",
" }\n",
" ]]\n",
" }\n",
" \n",
" if filing_status == \"married\":\n",
" situation[\"people\"][\"spouse\"] = {\"age\": {YEAR: 40}}\n",
" for unit in [\"families\", \"marital_units\", \"tax_units\", \"households\"]:\n",
" situation[unit][list(situation[unit].keys())[0]][\"members\"].append(\"spouse\")\n",
" \n",
" return situation\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def calculate_income(situation, reform=None):\n",
" simulation = Simulation(situation=situation, reform=reform)\n",
" return simulation.calculate(\"household_net_income\", YEAR)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def create_ctc_reform_comparison_graph():\n",
" colors = {\n",
" \"married_0\": \"#0066cc\", # Dark blue\n",
" \"married_5\": \"#4d94ff\", # Medium blue\n",
" \"married_16\": \"#99c2ff\", # Light blue\n",
" \"hoh_0\": \"#333333\", # Dark grey\n",
" \"hoh_5\": \"#666666\", # Medium grey\n",
" \"hoh_16\": \"#999999\", # Light grey\n",
" }\n",
"\n",
" x = np.linspace(0, MAX_INCOME, 201)\n",
" fig = go.Figure()\n",
"\n",
" for filing_status in [\"married\", \"hoh\"]:\n",
" for child_age in [0, 5, 17]:\n",
" baseline = calculate_income(create_situation(filing_status, child_age))\n",
" reform_result = calculate_income(create_situation(filing_status, child_age), reform)\n",
" \n",
" label = f\"{'Married' if filing_status == 'married' else 'Single'}, Child Age {child_age}\"\n",
" color = colors[f\"{filing_status}_{child_age}\"]\n",
" \n",
" line_style = 'solid' if filing_status == \"married\" else 'dot'\n",
" \n",
" fig.add_trace(go.Scatter(\n",
" x=x, \n",
" y=reform_result - baseline, \n",
" mode='lines', \n",
" name=label, \n",
" line=dict(color=color, dash=line_style)\n",
" ))\n",
"\n",
" fig.update_layout(\n",
" title='Impact of the Harris CTC Reform by Marital Status and Child Age',\n",
" xaxis_title=\"Earnings\",\n",
" yaxis_title=\"Net Impact\",\n",
" xaxis=dict(tickformat='$,.0f', range=[0, MAX_INCOME]),\n",
" yaxis=dict(tickformat='$,.0f'),\n",
" legend=dict(\n",
" yanchor=\"top\",\n",
" y=0.99,\n",
" xanchor=\"left\",\n",
" x=1.01\n",
" ),\n",
" height=600,\n",
" width=800,\n",
" )\n",
"\n",
" return fig"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'married_17'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/t5/1q_n2l7d1ndddzm52gy9n93w0000gn/T/ipykernel_29607/698317625.py\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Create and display the chart\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcreate_ctc_reform_comparison_graph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mformat_fig\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfig\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/var/folders/t5/1q_n2l7d1ndddzm52gy9n93w0000gn/T/ipykernel_29607/1298352449.py\u001b[0m in \u001b[0;36mcreate_ctc_reform_comparison_graph\u001b[0;34m()\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0mlabel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34mf\"{'Married' if filing_status == 'married' else 'Single'}, Child Age {child_age}\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mcolor\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcolors\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34mf\"{filing_status}_{child_age}\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0mline_style\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'solid'\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfiling_status\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"married\"\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0;34m'dot'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: 'married_17'"
]
}
],
"source": [
"# Create and display the chart\n",
"fig = create_ctc_reform_comparison_graph()\n",
"fig = format_fig(fig)\n",
"fig.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "policyengine",
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading