66
77
88def fit_did (y , x , d ,
9- learner_g , learner_m , all_smpls , dml_procedure , score , in_sample_normalization ,
9+ learner_g , learner_m , all_smpls , score , in_sample_normalization ,
1010 n_rep = 1 , g0_params = None , g1_params = None , m_params = None ,
1111 trimming_threshold = 1e-2 ):
1212 n_obs = len (y )
@@ -43,11 +43,7 @@ def fit_did(y, x, d,
4343 all_psi_a .append (psi_a )
4444 all_psi_b .append (psi_b )
4545
46- if dml_procedure == 'dml1' :
47- thetas [i_rep ], ses [i_rep ] = did_dml1 (psi_a , psi_b , smpls )
48- else :
49- assert dml_procedure == 'dml2'
50- thetas [i_rep ], ses [i_rep ] = did_dml2 (psi_a , psi_b )
46+ thetas [i_rep ], ses [i_rep ] = did_dml2 (psi_a , psi_b )
5147
5248 theta = np .median (thetas )
5349 se = np .sqrt (np .median (np .power (ses , 2 ) * n_obs + np .power (thetas - theta , 2 )) / n_obs )
@@ -107,25 +103,6 @@ def compute_did_residuals(y, g_hat0_list, g_hat1_list, m_hat_list, p_hat_list, s
107103 return resid_d0 , g_hat0 , g_hat1 , m_hat , p_hat
108104
109105
110- def did_dml1 (psi_a , psi_b , smpls ):
111- thetas = np .zeros (len (smpls ))
112- n_obs = len (psi_a )
113-
114- for idx , (_ , test_index ) in enumerate (smpls ):
115- thetas [idx ] = - np .mean (psi_b [test_index ]) / np .mean (psi_a [test_index ])
116- theta_hat = np .mean (thetas )
117-
118- if len (smpls ) > 1 :
119- se = np .sqrt (var_did (theta_hat , psi_a , psi_b , n_obs ))
120- else :
121- assert len (smpls ) == 1
122- test_index = smpls [0 ][1 ]
123- n_obs = len (test_index )
124- se = np .sqrt (var_did (theta_hat , psi_a [test_index ], psi_b [test_index ], n_obs ))
125-
126- return theta_hat , se
127-
128-
129106def did_dml2 (psi_a , psi_b ):
130107 n_obs = len (psi_a )
131108 theta_hat = - np .mean (psi_b ) / np .mean (psi_a )
@@ -176,7 +153,6 @@ def var_did(theta, psi_a, psi_b, n_obs):
176153
177154def boot_did (y , thetas , ses , all_psi_a , all_psi_b ,
178155 all_smpls , bootstrap , n_rep_boot , n_rep = 1 , apply_cross_fitting = True ):
179- all_boot_theta = list ()
180156 all_boot_t_stat = list ()
181157 for i_rep in range (n_rep ):
182158 smpls = all_smpls [i_rep ]
@@ -186,16 +162,14 @@ def boot_did(y, thetas, ses, all_psi_a, all_psi_b,
186162 test_index = smpls [0 ][1 ]
187163 n_obs = len (test_index )
188164 weights = draw_weights (bootstrap , n_rep_boot , n_obs )
189- boot_theta , boot_t_stat = boot_did_single_split (
165+ boot_t_stat = boot_did_single_split (
190166 thetas [i_rep ], all_psi_a [i_rep ], all_psi_b [i_rep ], smpls ,
191167 ses [i_rep ], weights , n_rep_boot , apply_cross_fitting )
192- all_boot_theta .append (boot_theta )
193168 all_boot_t_stat .append (boot_t_stat )
194169
195- boot_theta = np .hstack (all_boot_theta )
196170 boot_t_stat = np .hstack (all_boot_t_stat )
197171
198- return boot_theta , boot_t_stat
172+ return boot_t_stat
199173
200174
201175def boot_did_single_split (theta , psi_a , psi_b ,
@@ -208,9 +182,9 @@ def boot_did_single_split(theta, psi_a, psi_b,
208182 J = np .mean (psi_a [test_index ])
209183
210184 psi = np .multiply (psi_a , theta ) + psi_b
211- boot_theta , boot_t_stat = boot_manual (psi , J , smpls , se , weights , n_rep_boot , apply_cross_fitting )
185+ boot_t_stat = boot_manual (psi , J , smpls , se , weights , n_rep_boot , apply_cross_fitting )
212186
213- return boot_theta , boot_t_stat
187+ return boot_t_stat
214188
215189
216190def tune_nuisance_did (y , x , d , ml_g , ml_m , smpls , score , n_folds_tune ,
0 commit comments