|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Chapter 9: Bridging Finite and Super-population Causal Inference" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 36, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "from joblib import Parallel, delayed\n", |
| 17 | + "\n", |
| 18 | + "import numpy as np\n", |
| 19 | + "import statsmodels.api as sm\n", |
| 20 | + "\n", |
| 21 | + "np.random.seed(42)\n" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 37, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "def linestimator(Z, Y, X):\n", |
| 31 | + " X = (X - X.mean(axis=0))/X.std(axis=0)\n", |
| 32 | + " n, p = X.shape\n", |
| 33 | + " # fully interacted OLS\n", |
| 34 | + " Xmat = np.c_[sm.add_constant(Z),\n", |
| 35 | + " X,\n", |
| 36 | + " Z.reshape(-1, 1) * X]\n", |
| 37 | + " m = sm.OLS(Y, Xmat).fit(cov_type=\"HC2\")\n", |
| 38 | + " est, vehw = m.params[1], m.bse[1]**2\n", |
| 39 | + " # super-population correction\n", |
| 40 | + " inter = m.params[-p:] # (β_1 - β_0) term - last p elements of coef\n", |
| 41 | + " # (β_1 - β_0)' Σ (β_1 - β_0) / n\n", |
| 42 | + " superCorr = np.sum(inter * (np.cov(X.T) @ inter))/n\n", |
| 43 | + " vsuper = vehw + superCorr\n", |
| 44 | + " return est, np.sqrt(vehw), np.sqrt(vsuper)\n" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 38, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "def onerepl(*args):\n", |
| 54 | + " n = 500\n", |
| 55 | + " X = np.random.normal(0, 1, n*2).reshape(n, 2)\n", |
| 56 | + " Y0 = X[:, 0] + X[:, 0]**2 + np.random.uniform(-.5, .5, n)\n", |
| 57 | + " Y1 = X[:, 1] + X[:, 1]**2 + np.random.uniform(-1, 1, n)\n", |
| 58 | + " Z = np.random.binomial(1, .6, n)\n", |
| 59 | + " Y = Y0 * (1 - Z) + Y1 * Z\n", |
| 60 | + " return linestimator(Z, Y, X)\n" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 39, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [ |
| 68 | + { |
| 69 | + "data": { |
| 70 | + "text/plain": [ |
| 71 | + "(0.052230404017171474, 0.02176516995732536, 0.026679530224550992)" |
| 72 | + ] |
| 73 | + }, |
| 74 | + "execution_count": 39, |
| 75 | + "metadata": {}, |
| 76 | + "output_type": "execute_result" |
| 77 | + } |
| 78 | + ], |
| 79 | + "source": [ |
| 80 | + "onerepl()\n" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 40, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [], |
| 88 | + "source": [ |
| 89 | + "nrep, k = 2000, 8\n", |
| 90 | + "results = Parallel(n_jobs = k)(delayed(onerepl)(i) for i in range(nrep))\n", |
| 91 | + "simres = np.vstack(results)\n" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": 41, |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [ |
| 99 | + { |
| 100 | + "data": { |
| 101 | + "text/plain": [ |
| 102 | + "(0.007213145049286639, 0.01843410232910921, 0.022661715589192874)" |
| 103 | + ] |
| 104 | + }, |
| 105 | + "execution_count": 41, |
| 106 | + "metadata": {}, |
| 107 | + "output_type": "execute_result" |
| 108 | + } |
| 109 | + ], |
| 110 | + "source": [ |
| 111 | + "# bias, estimated EHW SE, estimated super-population SE\n", |
| 112 | + "simres[:, 0].mean(), simres[:, 1].mean(), simres[:, 2].mean()\n" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": 42, |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [ |
| 120 | + { |
| 121 | + "data": { |
| 122 | + "text/plain": [ |
| 123 | + "0.15129734780104556" |
| 124 | + ] |
| 125 | + }, |
| 126 | + "execution_count": 42, |
| 127 | + "metadata": {}, |
| 128 | + "output_type": "execute_result" |
| 129 | + } |
| 130 | + ], |
| 131 | + "source": [ |
| 132 | + "# empirical SD\n", |
| 133 | + "simres[:, 0].std()\n" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": 43, |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [ |
| 141 | + { |
| 142 | + "data": { |
| 143 | + "text/plain": [ |
| 144 | + "0.1795" |
| 145 | + ] |
| 146 | + }, |
| 147 | + "execution_count": 43, |
| 148 | + "metadata": {}, |
| 149 | + "output_type": "execute_result" |
| 150 | + } |
| 151 | + ], |
| 152 | + "source": [ |
| 153 | + "# EHW coverage\n", |
| 154 | + "np.mean((simres[:, 0] - 1.96 * simres[:, 1]) * (simres[:, 0] + 1.96 * simres[:, 1] ) <= 0)\n" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": 44, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [ |
| 162 | + { |
| 163 | + "data": { |
| 164 | + "text/plain": [ |
| 165 | + "0.218" |
| 166 | + ] |
| 167 | + }, |
| 168 | + "execution_count": 44, |
| 169 | + "metadata": {}, |
| 170 | + "output_type": "execute_result" |
| 171 | + } |
| 172 | + ], |
| 173 | + "source": [ |
| 174 | + "# superpop coverage\n", |
| 175 | + "np.mean((simres[:, 0] - 1.96 * simres[:, 2]) * (simres[:, 0] + 1.96 * simres[:, 2] ) <= 0)\n" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "code", |
| 180 | + "execution_count": null, |
| 181 | + "metadata": {}, |
| 182 | + "outputs": [], |
| 183 | + "source": [] |
| 184 | + } |
| 185 | + ], |
| 186 | + "metadata": { |
| 187 | + "kernelspec": { |
| 188 | + "display_name": "econometrics", |
| 189 | + "language": "python", |
| 190 | + "name": "econometrics" |
| 191 | + }, |
| 192 | + "language_info": { |
| 193 | + "codemirror_mode": { |
| 194 | + "name": "ipython", |
| 195 | + "version": 3 |
| 196 | + }, |
| 197 | + "file_extension": ".py", |
| 198 | + "mimetype": "text/x-python", |
| 199 | + "name": "python", |
| 200 | + "nbconvert_exporter": "python", |
| 201 | + "pygments_lexer": "ipython3", |
| 202 | + "version": "3.9.13" |
| 203 | + } |
| 204 | + }, |
| 205 | + "nbformat": 4, |
| 206 | + "nbformat_minor": 2 |
| 207 | +} |
0 commit comments