From e8253c5e5df816a5b197e62eb990ba58d940692f Mon Sep 17 00:00:00 2001 From: PavelMakarchuk Date: Fri, 14 Feb 2025 17:39:04 +0100 Subject: [PATCH] Itemized deductions Breakdown --- us/deductions/itemized_deductions.ipynb | 371 ++++++++++++++++++++++++ 1 file changed, 371 insertions(+) create mode 100644 us/deductions/itemized_deductions.ipynb diff --git a/us/deductions/itemized_deductions.ipynb b/us/deductions/itemized_deductions.ipynb new file mode 100644 index 0000000..297462e --- /dev/null +++ b/us/deductions/itemized_deductions.ipynb @@ -0,0 +1,371 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "from policyengine_us import Microsimulation\n", + "from policyengine_core.reforms import Reform\n", + "\n", + "behavioral_responses = Reform.from_dict({\n", + " \"gov.simulation.labor_supply_responses.elasticities.income\": {\n", + " \"2024-01-01.2100-12-31\": -0.05\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.1\": {\n", + " \"2024-01-01.2100-12-31\": 0.31\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.2\": {\n", + " \"2024-01-01.2100-12-31\": 0.28\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.3\": {\n", + " \"2024-01-01.2100-12-31\": 0.27\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.4\": {\n", + " \"2024-01-01.2100-12-31\": 0.27\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.5\": {\n", + " \"2024-01-01.2100-12-31\": 0.25\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.6\": {\n", + " \"2024-01-01.2100-12-31\": 0.25\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.7\": {\n", + " \"2024-01-01.2100-12-31\": 0.22\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.8\": {\n", + " \"2024-01-01.2100-12-31\": 0.22\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.9\": {\n", + " \"2024-01-01.2100-12-31\": 0.22\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.secondary\": {\n", + " \"2024-01-01.2100-12-31\": 0.27\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.10\": {\n", + " \"2024-01-01.2100-12-31\": 0.22\n", + " }\n", + "}, country_id=\"us\")\n", + "\n", + "baseline = Microsimulation(dataset=\"hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5\")\n", + "\n", + "baseline_behavioral = Microsimulation(dataset=\"hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "repeal_it_ded_reform = Reform.from_dict(\n", + " {\n", + " \"gov.irs.deductions.deductions_if_itemizing\": {\n", + " \"2024-01-01.2100-12-31\": [\"wagering_losses_deduction\", \"qualified_business_income_deduction\"]\n", + " },\n", + " \"gov.irs.deductions.itemized_deductions\": {\n", + " \"2024-01-01.2100-12-31\": [\"heat_pump_expenditures\"]\n", + " }\n", + " }\n", + ")\n", + "\n", + "repeal_medical_exp_reform = Reform.from_dict(\n", + " {\n", + " \"gov.irs.deductions.itemized.medical.floor\": {\n", + " \"2024-01-01.2100-12-31\": 1_000_000_000\n", + " }\n", + " }\n", + ")\n", + "\n", + "repeal_chart_ded_reform = Reform.from_dict(\n", + " {\n", + " \"gov.irs.deductions.itemized.charity.ceiling.all\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " }\n", + " }\n", + ")\n", + "\n", + "repeal_interest_ded_reform = Reform.from_dict(\n", + " {\n", + " \"gov.irs.deductions.deductions_if_itemizing\": {\n", + " \"2024-01-01.2100-12-31\": [\"medical_expense_deduction\", \"charitable_deduction\", \"salt_deduction\", \"casualty_loss_deduction\", \"qualified_business_income_deduction\", \"wagering_losses_deduction\"]\n", + " },\n", + " \"gov.irs.deductions.itemized_deductions\": {\n", + " \"2024-01-01.2100-12-31\": [\"medical_expense_deduction\", \"charitable_deduction\", \"salt_deduction\", \"casualty_loss_deduction\"]\n", + " }\n", + " }\n", + ")\n", + "\n", + "repeal_salt_ded_reform = Reform.from_dict(\n", + " {\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.JOINT\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " },\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SINGLE\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " },\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SEPARATE\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " },\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SURVIVING_SPOUSE\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " },\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.HEAD_OF_HOUSEHOLD\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " }\n", + " }\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "# Dictionary of all the reforms\n", + "reforms = {\n", + " \"repeal_it_ded\": repeal_it_ded_reform,\n", + " \"repeal_medical_exp\": repeal_medical_exp_reform,\n", + " \"repeal_chart_ded\": repeal_chart_ded_reform,\n", + " \"repeal_interest_ded\": repeal_interest_ded_reform,\n", + " \"repeal_salt_ded\": repeal_salt_ded_reform,\n", + "}\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "baseline_income = baseline.calculate(\"household_net_income\", period=2024)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reform: repeal_it_ded -> Impact on income tax: -87,298,748,961.58\n", + "Reform: repeal_medical_exp -> Impact on income tax: -4,548,085,250.17\n", + "Reform: repeal_chart_ded -> Impact on income tax: -67,633,722,372.78\n", + "Reform: repeal_interest_ded -> Impact on income tax: -28,691,421,174.73\n", + "Reform: repeal_salt_ded -> Impact on income tax: -10,118,681,939.88\n" + ] + } + ], + "source": [ + "for name, reform in reforms.items():\n", + " sim = Microsimulation(reform=reform,\n", + " dataset=\"hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5\")\n", + " reformed_income = sim.calculate(\"household_net_income\", period=2024)\n", + " impact = (reformed_income - baseline_income).sum()\n", + " print(f\"Reform: {name} -> Impact on income tax: {impact:,.2f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "type object 'reform' has no attribute 'to_dict'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[11], line 8\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# Loop over each reform, combine it with behavioral responses, and compute the impact\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m name, reform \u001b[38;5;129;01min\u001b[39;00m reforms\u001b[38;5;241m.\u001b[39mitems():\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m# Merge the specific reform with the behavioral responses.\u001b[39;00m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Here we assume that each 'Reform' object exposes a to_dict() method to get its underlying dict.\u001b[39;00m\n\u001b[0;32m----> 8\u001b[0m combined_reform_dict \u001b[38;5;241m=\u001b[39m {\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[43mreform\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto_dict\u001b[49m(), \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mbehavioral_responses\u001b[38;5;241m.\u001b[39mto_dict()}\n\u001b[1;32m 9\u001b[0m combined_reform \u001b[38;5;241m=\u001b[39m Reform\u001b[38;5;241m.\u001b[39mfrom_dict(combined_reform_dict, country_id\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mus\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# Initialize a new microsimulation with the combined reform\u001b[39;00m\n", + "\u001b[0;31mAttributeError\u001b[0m: type object 'reform' has no attribute 'to_dict'" + ] + } + ], + "source": [ + "# First, re-calculate the baseline income tax in case you haven't already:\n", + "baseline_income = baseline.calculate(\"income_tax\", period=2024)\n", + "\n", + "# Loop over each reform, combine it with behavioral responses, and compute the impact\n", + "for name, reform in reforms.items():\n", + " # Merge the specific reform with the behavioral responses.\n", + " # Here we assume that each 'Reform' object exposes a to_dict() method to get its underlying dict.\n", + " combined_reform_dict = {**reform.to_dict(), **behavioral_responses.to_dict()}\n", + " combined_reform = Reform.from_dict(combined_reform_dict, country_id=\"us\")\n", + " \n", + " # Initialize a new microsimulation with the combined reform\n", + " sim = Microsimulation(\n", + " reform=combined_reform,\n", + " dataset=\"hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5\"\n", + " )\n", + " \n", + " # Calculate the income tax for the combined reform scenario\n", + " reformed_income = sim.calculate(\"income_tax\", period=2024)\n", + " \n", + " # Compute the impact. Here we use the same arithmetic as before.\n", + " impact = (reformed_income - baseline_income).sum()\n", + " print(f\"Combined Reform: {name} with behavioral responses -> Impact on income tax: {impact:,.2f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Combined Reform: repeal_it_ded with behavioral responses -> Impact on income tax: 80,194,493,082.39\n", + "Combined Reform: repeal_medical_exp with behavioral responses -> Impact on income tax: 3,313,528,155.87\n", + "Combined Reform: repeal_chart_ded with behavioral responses -> Impact on income tax: 62,512,730,450.91\n", + "Combined Reform: repeal_interest_ded with behavioral responses -> Impact on income tax: 25,320,345,309.85\n", + "Combined Reform: repeal_salt_ded with behavioral responses -> Impact on income tax: 8,884,419,415.79\n" + ] + } + ], + "source": [ + "# Calculate the baseline income tax (if not already done)\n", + "baseline_income = baseline.calculate(\"income_tax\", period=2024)\n", + "\n", + "# Define the original individual reform dictionaries\n", + "reforms_data = {\n", + " \"repeal_it_ded\": {\n", + " \"gov.irs.deductions.deductions_if_itemizing\": {\n", + " \"2024-01-01.2100-12-31\": [\"wagering_losses_deduction\", \"qualified_business_income_deduction\"]\n", + " },\n", + " \"gov.irs.deductions.itemized_deductions\": {\n", + " \"2024-01-01.2100-12-31\": [\"heat_pump_expenditures\"]\n", + " }\n", + " },\n", + " \"repeal_medical_exp\": {\n", + " \"gov.irs.deductions.itemized.medical.floor\": {\n", + " \"2024-01-01.2100-12-31\": 1_000_000_000\n", + " }\n", + " },\n", + " \"repeal_chart_ded\": {\n", + " \"gov.irs.deductions.itemized.charity.ceiling.all\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " }\n", + " },\n", + " \"repeal_interest_ded\": {\n", + " \"gov.irs.deductions.deductions_if_itemizing\": {\n", + " \"2024-01-01.2100-12-31\": [\n", + " \"medical_expense_deduction\", \"charitable_deduction\", \"salt_deduction\",\n", + " \"casualty_loss_deduction\", \"qualified_business_income_deduction\", \"wagering_losses_deduction\"\n", + " ]\n", + " },\n", + " \"gov.irs.deductions.itemized_deductions\": {\n", + " \"2024-01-01.2100-12-31\": [\n", + " \"medical_expense_deduction\", \"charitable_deduction\", \"salt_deduction\", \"casualty_loss_deduction\"\n", + " ]\n", + " }\n", + " },\n", + " \"repeal_salt_ded\": {\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.JOINT\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " },\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SINGLE\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " },\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SEPARATE\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " },\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.SURVIVING_SPOUSE\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " },\n", + " \"gov.irs.deductions.itemized.salt_and_real_estate.cap.HEAD_OF_HOUSEHOLD\": {\n", + " \"2024-01-01.2100-12-31\": 0\n", + " }\n", + " }\n", + "}\n", + "\n", + "# Define behavioral responses as a dictionary\n", + "behavioral_responses_data = {\n", + " \"gov.simulation.labor_supply_responses.elasticities.income\": {\n", + " \"2024-01-01.2100-12-31\": -0.05\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.1\": {\n", + " \"2024-01-01.2100-12-31\": 0.31\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.2\": {\n", + " \"2024-01-01.2100-12-31\": 0.28\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.3\": {\n", + " \"2024-01-01.2100-12-31\": 0.27\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.4\": {\n", + " \"2024-01-01.2100-12-31\": 0.27\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.5\": {\n", + " \"2024-01-01.2100-12-31\": 0.25\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.6\": {\n", + " \"2024-01-01.2100-12-31\": 0.25\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.7\": {\n", + " \"2024-01-01.2100-12-31\": 0.22\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.8\": {\n", + " \"2024-01-01.2100-12-31\": 0.22\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.9\": {\n", + " \"2024-01-01.2100-12-31\": 0.22\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.primary.10\": {\n", + " \"2024-01-01.2100-12-31\": 0.22\n", + " },\n", + " \"gov.simulation.labor_supply_responses.elasticities.substitution.by_position_and_decile.secondary\": {\n", + " \"2024-01-01.2100-12-31\": 0.27\n", + " }\n", + "}\n", + "\n", + "# Loop over each individual reform, merge it with behavioral responses, and compare to baseline\n", + "for name, reform_dict in reforms_data.items():\n", + " # Combine the individual reform with behavioral responses.\n", + " combined_dict = {**reform_dict, **behavioral_responses_data}\n", + " \n", + " # Create a combined Reform from the merged dictionary\n", + " combined_reform = Reform.from_dict(combined_dict, country_id=\"us\")\n", + " \n", + " # Initialize the microsimulation using the combined reform\n", + " sim = Microsimulation(\n", + " reform=combined_reform,\n", + " dataset=\"hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5\"\n", + " )\n", + " \n", + " # Calculate the income tax for the combined scenario\n", + " reformed_income_tax = sim.calculate(\"income_tax\", period=2024)\n", + " \n", + " # Compute the impact relative to the baseline (using the same arithmetic as before)\n", + " impact = (reformed_income_tax - baseline_income).sum()\n", + " print(f\"Combined Reform: {name} with behavioral responses -> Impact on income tax: {impact:,.2f}\")" + ] + } + ], + "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.10.14" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}