Skip to content
Draft
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
201 changes: 201 additions & 0 deletions us/healthcare/healthcare_hack.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/pavelmakarchuk/anaconda3/envs/pe/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from policyengine_us import Microsimulation\n",
"from policyengine_core.reforms import Reform\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"baseline = Microsimulation(dataset=\"hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5\")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Set random seed for reproducibility\n",
"np.random.seed(42)\n",
"\n",
"# Get baseline values for each program\n",
"baseline_snap = baseline.calculate(\"snap\", map_to=\"household\", period=2026)\n",
"baseline_medicaid = baseline.calculate(\"medicaid\", map_to=\"household\", period=2026)\n",
"baseline_aca_ptc = baseline.calculate(\"aca_ptc\", map_to=\"household\", period=2026)\n",
"\n",
"# Create copies to modify\n",
"baseline_snap_modified = baseline_snap.copy()\n",
"baseline_medicaid_modified = baseline_medicaid.copy()\n",
"baseline_aca_ptc_modified = baseline_aca_ptc.copy()\n",
"\n",
"# For SNAP: randomly set 5% of positive values to 0\n",
"snap_recipients = baseline_snap_modified > 0\n",
"snap_indices = np.where(snap_recipients)[0]\n",
"snap_to_zero = np.random.choice(snap_indices, size=int(0.05 * len(snap_indices)), replace=False)\n",
"baseline_snap_modified[snap_to_zero] = 0\n",
"\n",
"# For Medicaid: randomly set 5% of positive values to 0\n",
"medicaid_recipients = baseline_medicaid_modified > 0\n",
"medicaid_indices = np.where(medicaid_recipients)[0]\n",
"medicaid_to_zero = np.random.choice(medicaid_indices, size=int(0.05 * len(medicaid_indices)), replace=False)\n",
"baseline_medicaid_modified[medicaid_to_zero] = 0\n",
"\n",
"# For ACA PTC: randomly set 5% of positive values to 0\n",
"aca_recipients = baseline_aca_ptc_modified > 0\n",
"aca_indices = np.where(aca_recipients)[0]\n",
"aca_to_zero = np.random.choice(aca_indices, size=int(0.05 * len(aca_indices)), replace=False)\n",
"baseline_aca_ptc_modified[aca_to_zero] = 0"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"household_data = pd.DataFrame({\n",
" 'Baseline SNAP': baseline_snap,\n",
" 'Reformed SNAP': baseline_snap_modified,\n",
" 'Baseline Medicaid': baseline_medicaid,\n",
" 'Reformed Medicaid': baseline_medicaid_modified,\n",
" 'Baseline ACA PTC': baseline_aca_ptc,\n",
" 'Reformed ACA PTC': baseline_aca_ptc_modified\n",
"})\n",
"# Add summary columns\n",
"household_data['Looses SNAP'] = (household_data['Baseline SNAP'] > 0) & (household_data['Reformed SNAP'] == 0)\n",
"household_data['Looses Medicaid'] = (household_data['Baseline Medicaid'] > 0) & (household_data['Reformed Medicaid'] == 0)\n",
"household_data['Looses ACA PTC'] = (household_data['Baseline ACA PTC'] > 0) & (household_data['Reformed ACA PTC'] == 0)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Save to CSV\n",
"household_data.to_csv('household_benefits_reform.csv', index=False)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data saved to 'household_benefits_reform.csv'\n",
"\n",
"Total households: 41,310\n",
"\n",
"Summary of changes:\n",
"SNAP benefits removed: 571 households\n",
"Medicaid benefits removed: 548 households\n",
"ACA PTC benefits removed: 242 households\n",
"\n",
"First 10 rows of the data:\n",
" household_id Baseline SNAP Reformed SNAP Baseline Medicaid \\\n",
"0 12 0.000000 0.000000 0.000000 \n",
"1 21 0.000000 0.000000 0.000000 \n",
"2 22 3896.510529 3896.510529 11924.721680 \n",
"3 30 288.630402 288.630402 11924.721680 \n",
"4 36 0.000000 0.000000 11924.721680 \n",
"5 41 0.000000 0.000000 0.000000 \n",
"6 44 1524.380127 1524.380127 23849.443359 \n",
"7 68 731.480103 731.480103 11924.721680 \n",
"8 73 0.000000 0.000000 0.000000 \n",
"9 78 0.000000 0.000000 0.000000 \n",
"\n",
" Reformed Medicaid Baseline ACA PTC Reformed ACA PTC Looses SNAP \\\n",
"0 0.000000 0.0 0.0 False \n",
"1 0.000000 0.0 0.0 False \n",
"2 11924.721680 0.0 0.0 False \n",
"3 11924.721680 0.0 0.0 False \n",
"4 11924.721680 0.0 0.0 False \n",
"5 0.000000 0.0 0.0 False \n",
"6 23849.443359 0.0 0.0 False \n",
"7 11924.721680 0.0 0.0 False \n",
"8 0.000000 0.0 0.0 False \n",
"9 0.000000 0.0 0.0 False \n",
"\n",
" Looses Medicaid Looses ACA PTC \n",
"0 False False \n",
"1 False False \n",
"2 False False \n",
"3 False False \n",
"4 False False \n",
"5 False False \n",
"6 False False \n",
"7 False False \n",
"8 False False \n",
"9 False False \n"
]
}
],
"source": [
"# Print summary statistics\n",
"print(\"Data saved to 'household_benefits_reform.csv'\")\n",
"print(f\"\\nTotal households: {len(household_data):,}\")\n",
"print(\"\\nSummary of changes:\")\n",
"print(f\"SNAP benefits removed: {household_data['Looses SNAP'].sum():,} households\")\n",
"print(f\"Medicaid benefits removed: {household_data['Looses Medicaid'].sum():,} households\")\n",
"print(f\"ACA PTC benefits removed: {household_data['Looses ACA PTC'].sum():,} households\")\n",
"\n",
"# Display first few rows\n",
"print(\"\\nFirst 10 rows of the data:\")\n",
"print(household_data.head(10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
Loading