Skip to content

Commit 9f69e18

Browse files
committed
case study v1
1 parent 8f7fb9a commit 9f69e18

File tree

1 file changed

+110
-2
lines changed

1 file changed

+110
-2
lines changed

book/cate_and_policy/policy_learning.ipynb

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,119 @@
500500
"print(\"ANALYSIS COMPLETE\")\n",
501501
"print(\"=\" * 70)"
502502
]
503+
},
504+
{
505+
"cell_type": "code",
506+
"execution_count": 1,
507+
"metadata": {},
508+
"outputs": [],
509+
"source": [
510+
"import numpy as np\n",
511+
"import pandas as pd\n",
512+
"import warnings\n",
513+
"warnings.filterwarnings('ignore')"
514+
]
515+
},
516+
{
517+
"cell_type": "code",
518+
"execution_count": 2,
519+
"metadata": {},
520+
"outputs": [
521+
{
522+
"name": "stdout",
523+
"output_type": "stream",
524+
"text": [
525+
"Loading data...\n",
526+
"Data loaded: 29726 observations\n",
527+
"Treatment variable: w\n",
528+
"Outcome variable: y\n",
529+
"Covariates: age, polviews, income, educ, marital, sex\n",
530+
"\n"
531+
]
532+
}
533+
],
534+
"source": [
535+
"print(\"Loading data...\")\n",
536+
"# Read in data\n",
537+
"url = \"https://docs.google.com/uc?id=1AQva5-vDlgBcM_Tv9yrO8yMYRfQJgqo_&export=download\"\n",
538+
"data = pd.read_csv(url)\n",
539+
"n = len(data)\n",
540+
"\n",
541+
"# NOTE: We'll invert treatment and control, compared to previous chapters\n",
542+
"data['w'] = 1 - data['w']\n",
543+
"\n",
544+
"# Treatment is the wording of the question:\n",
545+
"# 'does the gov't spend too much on 'assistance to the poor' (control: 0)\n",
546+
"# 'does the gov't spend too much on \"welfare\"?' (treatment: 1)\n",
547+
"treatment = 'w'\n",
548+
"\n",
549+
"# Outcome: 1 for 'yes', 0 for 'no'\n",
550+
"outcome = 'y'\n",
551+
"\n",
552+
"# Additional covariates\n",
553+
"covariates = ['age', 'polviews', 'income', 'educ', 'marital', 'sex']\n",
554+
"\n",
555+
"print(f\"Data loaded: {n} observations\")\n",
556+
"print(f\"Treatment variable: {treatment}\")\n",
557+
"print(f\"Outcome variable: {outcome}\")\n",
558+
"print(f\"Covariates: {', '.join(covariates)}\")\n",
559+
"print()"
560+
]
561+
},
562+
{
563+
"cell_type": "code",
564+
"execution_count": 4,
565+
"metadata": {},
566+
"outputs": [
567+
{
568+
"name": "stdout",
569+
"output_type": "stream",
570+
"text": [
571+
"======================================================================\n",
572+
"METHOD 1: Simple Mean-Based Estimation\n",
573+
"(Only valid in randomized setting)\n",
574+
"======================================================================\n",
575+
"\n",
576+
"Policy: Treat if polviews <= 4 OR age > 50\n",
577+
"Proportion treated under policy: 0.773\n",
578+
"Value estimate: 0.3457179812 Std. Error: 0.0038947280\n"
579+
]
580+
}
581+
],
582+
"source": [
583+
"print(\"=\" * 70)\n",
584+
"print(\"METHOD 1: Simple Mean-Based Estimation\")\n",
585+
"print(\"(Only valid in randomized setting)\")\n",
586+
"print(\"=\" * 70)\n",
587+
"\n",
588+
"# Extract variables\n",
589+
"X = data[covariates].values\n",
590+
"Y = data[outcome].values\n",
591+
"W = data[treatment].values\n",
592+
"\n",
593+
"# Define policy: treat if polviews <= 4 (liberal/moderate) OR age > 50\n",
594+
"pi = (data['polviews'].values <= 4) | (data['age'].values > 50)\n",
595+
"A = pi == 1\n",
596+
"\n",
597+
"# Calculate value estimate\n",
598+
"value_estimate = np.mean(Y[A & (W==1)]) * np.mean(A) + \\\n",
599+
" np.mean(Y[~A & (W==0)]) * np.mean(~A)\n",
600+
"\n",
601+
"# Calculate standard error\n",
602+
"value_stderr = np.sqrt(\n",
603+
" np.var(Y[A & (W==1)]) / np.sum(A & (W==1)) * np.mean(A)**2 + \n",
604+
" np.var(Y[~A & (W==0)]) / np.sum(~A & (W==0)) * np.mean(~A)**2\n",
605+
")\n",
606+
"\n",
607+
"print(f\"\\nPolicy: Treat if polviews <= 4 OR age > 50\")\n",
608+
"print(f\"Proportion treated under policy: {np.mean(A):.3f}\")\n",
609+
"print(f\"Value estimate: {value_estimate:.10f} Std. Error: {value_stderr:.10f}\")"
610+
]
503611
}
504612
],
505613
"metadata": {
506614
"kernelspec": {
507-
"display_name": "base",
615+
"display_name": "Python 3",
508616
"language": "python",
509617
"name": "python3"
510618
},
@@ -518,7 +626,7 @@
518626
"name": "python",
519627
"nbconvert_exporter": "python",
520628
"pygments_lexer": "ipython3",
521-
"version": "3.9.18"
629+
"version": "3.11.9"
522630
}
523631
},
524632
"nbformat": 4,

0 commit comments

Comments
 (0)