Skip to content

Commit 2067d5b

Browse files
committed
all iv chapters
1 parent 6358186 commit 2067d5b

File tree

4 files changed

+7669
-0
lines changed

4 files changed

+7669
-0
lines changed

Chapter22.ipynb

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Chapter 22: Disentangling Mixture Distributions and Instrumental Variable Inequalities"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"# n_z,d,d1 under monotonicity\n",
17+
"def IVbinary(n111, n110, n101, n100, n011, n010, n001, n000):\n",
18+
" n_tr = n111 + n110 + n101 + n100\n",
19+
" n_co = n011 + n010 + n001 + n000\n",
20+
" n = n_tr + n_co\n",
21+
"\n",
22+
" # proportions of latent strata\n",
23+
" pi_n = (n101 + n100)/ n_tr # p(d = 0 | z = 1) never taker\n",
24+
" pi_a = (n011 + n010)/ n_co # p(d = 1 | z = 0) always taker\n",
25+
" pi_c = 1 - pi_n - pi_a # by monotonicity, the only remaining group are compliers\n",
26+
"\n",
27+
" # four observed means of the outcomes (Z = z, D = d)\n",
28+
" mean_y_11 = n111 / (n111 + n110)\n",
29+
" mean_y_10 = n101 / (n101 + n100)\n",
30+
" mean_y_01 = n011 / (n011 + n010)\n",
31+
" mean_y_00 = n001 / (n001 + n000)\n",
32+
" # all four means are positive for binary outcomes\n",
33+
"\n",
34+
" # means of the outcome of two strata\n",
35+
" mu_n1, mu_a0 = mean_y_10, mean_y_01\n",
36+
" # exclusion implies 0 and 1 are same for always takers and never takers\n",
37+
" mu_n0, mu_a1 = mu_n1, mu_a0\n",
38+
" # stratum (Z=1, D = 1) is a mixture of c, a\n",
39+
" mu_c1 = ((pi_c + pi_a) * mean_y_11 - pi_a * mu_a1) / pi_c\n",
40+
" # stratum (Z=0, D = 0) is a mixture of c, n\n",
41+
" mu_c0 = ((pi_c + pi_n) * mean_y_00 - pi_n * mu_n0) / pi_c\n",
42+
" # identifiable quantities\n",
43+
" return {\n",
44+
" 'pi_c' : pi_c,\n",
45+
" 'pi_n' : pi_n,\n",
46+
" 'pi_a' : pi_a,\n",
47+
" 'mu_n1': mu_n1,\n",
48+
" 'mu_n0': mu_n0,\n",
49+
" 'mu_a1': mu_a1,\n",
50+
" 'mu_a0': mu_a0,\n",
51+
" 'mu_c1': mu_c1,\n",
52+
" 'mu_c0': mu_c0,\n",
53+
" 'tau_c': mu_c1 - mu_c0,\n",
54+
" }\n",
55+
"\n"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 2,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"data": {
65+
"text/plain": [
66+
"{'pi_c': 0.44305817033089756,\n",
67+
" 'pi_n': 0.4247104247104247,\n",
68+
" 'pi_a': 0.1322314049586777,\n",
69+
" 'mu_n1': 0.6181818181818182,\n",
70+
" 'mu_n0': 0.6181818181818182,\n",
71+
" 'mu_a1': 0.75,\n",
72+
" 'mu_a0': 0.75,\n",
73+
" 'mu_c1': 0.7086064097947424,\n",
74+
" 'mu_c0': 0.6292041771696075,\n",
75+
" 'tau_c': 0.0794022326251349}"
76+
]
77+
},
78+
"execution_count": 2,
79+
"metadata": {},
80+
"output_type": "execute_result"
81+
}
82+
],
83+
"source": [
84+
"## Investigators et al.(2014) data\n",
85+
"(investigators_analysis := IVbinary(\n",
86+
" n111 = 107,\n",
87+
" n110 = 42,\n",
88+
" n101 = 68,\n",
89+
" n100 = 42,\n",
90+
" n011 = 24,\n",
91+
" n010 = 8,\n",
92+
" n001 = 131,\n",
93+
" n000 = 79\n",
94+
"))\n",
95+
"\n"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 3,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"data": {
105+
"text/plain": [
106+
"{'pi_c': 0.11839971280558428,\n",
107+
" 'pi_n': 0.6922554347826086,\n",
108+
" 'pi_a': 0.18934485241180707,\n",
109+
" 'mu_n1': 0.08243375858684986,\n",
110+
" 'mu_n0': 0.08243375858684986,\n",
111+
" 'mu_a1': 0.11406844106463879,\n",
112+
" 'mu_a0': 0.11406844106463879,\n",
113+
" 'mu_c1': -0.004548064490810916,\n",
114+
" 'mu_c0': 0.12000941833518534,\n",
115+
" 'tau_c': -0.12455748282599625}"
116+
]
117+
},
118+
"execution_count": 3,
119+
"metadata": {},
120+
"output_type": "execute_result"
121+
}
122+
],
123+
"source": [
124+
"(flu_analysis := IVbinary(\n",
125+
" n111 = 31,\n",
126+
" n110 = 422,\n",
127+
" n101 = 84,\n",
128+
" n100 = 935,\n",
129+
" n011 = 30,\n",
130+
" n010 = 233,\n",
131+
" n001 = 99,\n",
132+
" n000 = 1027\n",
133+
"))\n"
134+
]
135+
}
136+
],
137+
"metadata": {
138+
"kernelspec": {
139+
"display_name": "metrics",
140+
"language": "python",
141+
"name": "python3"
142+
},
143+
"language_info": {
144+
"codemirror_mode": {
145+
"name": "ipython",
146+
"version": 3
147+
},
148+
"file_extension": ".py",
149+
"mimetype": "text/x-python",
150+
"name": "python",
151+
"nbconvert_exporter": "python",
152+
"pygments_lexer": "ipython3",
153+
"version": "3.11.5"
154+
}
155+
},
156+
"nbformat": 4,
157+
"nbformat_minor": 2
158+
}

Chapter23.ipynb

Lines changed: 1380 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)