def test5(): """The tests checks if the simulation process works even if the covariance between U1 and V and U0 and V is equal. Further the test ensures that the mte_information function returns the same value for each quantile. """ for _ in range(10): generate_random_dict() init_dict = read("test.grmpy.yml") # We impose that the covariance between the random components of the potential # outcomes and the random component determining choice is identical. init_dict["DIST"]["params"][2] = init_dict["DIST"]["params"][4] # Distribute information coeffs_untreated = init_dict["UNTREATED"]["params"] coeffs_treated = init_dict["TREATED"]["params"] # Construct auxiliary information cov = construct_covariance_matrix(init_dict) df = simulate("test.grmpy.yml") x = df[list( set(init_dict["TREATED"]["order"] + init_dict["UNTREATED"]["order"]))] q = [0.01] + list(np.arange(0.05, 1, 0.05)) + [0.99] mte = mte_information(coeffs_treated, coeffs_untreated, cov, q, x, init_dict) # We simply test that there is a single unique value for the marginal treatment # effect. np.testing.assert_equal(len(set(mte)), 1)
def test5(): """The tests checks if the simulation process works even if the covariance between U1 and V and U0 and V is equal. Further the test ensures that the mte_information function returns the same value for each quantile. """ for _ in range(10): generate_random_dict() init_dict = read('test.grmpy.ini') # We impose that the covariance between the random components of the potential # outcomes and the random component determining choice is identical. init_dict['DIST']['all'][2] = init_dict['DIST']['all'][4] # Distribute information coeffs_untreated = init_dict['UNTREATED']['all'] coeffs_treated = init_dict['TREATED']['all'] # Construct auxiliary information cov = construct_covariance_matrix(init_dict) df = simulate('test.grmpy.ini') x = df.filter(regex=r'^X\_', axis=1) q = [0.01] + list(np.arange(0.05, 1, 0.05)) + [0.99] mte = mte_information(coeffs_treated, coeffs_untreated, cov, q, x) # We simply test that there is a single unique value for the marginal treatment effect. np.testing.assert_equal(len(set(mte)), 1)
def weights_treatment_parameters(init_dict, GRID): """This function calculates the weights for the special case in Heckman & Vytlacil (2005) Figure 1B. """ GRID = np.linspace(0.01, 0.99, num=99, endpoint=True) coeffs_untreated = init_dict['UNTREATED']['all'] coeffs_treated = init_dict['TREATED']['all'] cov = construct_covariance_matrix(init_dict) x = simulate_covariates(init_dict) x = x[:, :2] # We take the specified distribution for the cost shifters from the paper. cost_mean, cost_sd = -0.0026, np.sqrt(0.270) v_mean, v_sd = 0.00, np.sqrt(cov[2, 2]) eval_points = norm.ppf(GRID, loc=v_mean, scale=v_sd) ate_weights = np.tile(1.0, 99) tut_weights = norm.cdf(eval_points, loc=cost_mean, scale=cost_sd) tt_weights = 1 - tut_weights def tut_integrand(point): eval_point = norm.ppf(point, loc=v_mean, scale=v_sd) return norm.cdf(eval_point, loc=cost_mean, scale=cost_sd) def tt_integrand(point): eval_point = norm.ppf(point, loc=v_mean, scale=v_sd) return norm.cdf(eval_point, loc=cost_mean, scale=cost_sd) # Scaling so that the weights integrate to one. tut_scaling = quad(tut_integrand, 0.01, 0.99)[0] tut_weights /= tut_scaling tt_scaling = quad(tt_integrand, 0.01, 0.99)[0] tt_weights /= tt_scaling mte = mte_information(coeffs_treated, coeffs_untreated, cov, GRID, x, init_dict) return ate_weights, tt_weights, tut_weights, mte
def calculate_mte(rslt, init_dict, data_frame, quant=None): coeffs_treated = rslt['TREATED']['all'] coeffs_untreated = rslt['UNTREATED']['all'] if quant is None: quantiles = [1] + np.arange(2.5, 100, 2.5).tolist() + [99] args = [str(i) + '%' for i in quantiles] quantiles = [i * 0.01 for i in quantiles] else: quantiles = quant cov = np.zeros((3, 3)) cov[2, 0] = rslt['AUX']['x_internal'][-3] * rslt['AUX']['x_internal'][-4] cov[2, 1] = rslt['AUX']['x_internal'][-1] * rslt['AUX']['x_internal'][-2] cov[2, 2] = 1.0 help_ = list(set(init_dict['TREATED']['order'] + init_dict['UNTREATED']['order'])) x = data_frame[[init_dict['varnames'][i - 1] for i in help_]] value = mte_information(coeffs_treated, coeffs_untreated, cov, quantiles, x, rslt) if quant is None: return value, args else: return value
def calculate_mte(rslt, data_frame, quant=None): coeffs_treated = rslt["TREATED"]["params"] coeffs_untreated = rslt["UNTREATED"]["params"] if quant is None: quantiles = [1] + np.arange(5, 100, 5).tolist() + [99] args = [str(i) + "%" for i in quantiles] quantiles = [i * 0.01 for i in quantiles] else: quantiles = quant cov = np.zeros((3, 3)) cov[2, 0] = rslt["AUX"]["x_internal"][-3] * rslt["AUX"]["x_internal"][-4] cov[2, 1] = rslt["AUX"]["x_internal"][-1] * rslt["AUX"]["x_internal"][-2] cov[2, 2] = 1.0 value = mte_information(coeffs_treated, coeffs_untreated, cov, quantiles, data_frame, rslt) if quant is None: return value, args else: return value
index = GRID.index(round(xtick, 2)) height = mte[index] ax.text(x=xtick - 0.002, y=height - 0.1, s='[', fontsize=30) for xtick in [0.39, 0.79]: index = GRID.index(round(xtick, 2)) height = mte[index] ax.text(x=xtick - 0.01, y=height - 0.1, s=']', fontsize=30) ax.set_xlabel('$u_S$') ax.set_ylabel(r'$B^{MTE}$') ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1]) ax.set_xticklabels([0, '$p_1$', '$p_2$', '$p_3$', '$p_4$', 1]) ax.tick_params(axis='x', which='major') ax.set_ylim([1.5, 4.5]) plt.tight_layout() plt.savefig(ppj("OUT_FIGURES", 'fig-local-average-treatment.png')) if __name__ == '__main__': coeffs_untreated = init_dict['UNTREATED']['all'] coeffs_treated = init_dict['TREATED']['all'] cov = construct_covariance_matrix(init_dict) x = np.loadtxt(ppj("OUT_DATA", "X.csv"), delimiter=",") x = x.reshape(165, 2) mte = mte_information(coeffs_treated, coeffs_untreated, cov, GRID, x) plot_local_average_treatment(mte)
ax.plot(GRID, abs_, label="Absence", linestyle="--") ax.set_ylim([1.5, 4.5]) plt.legend() plt.tight_layout() plt.savefig(OUTPUT_DIR + "/fig-eh-marginal-effect.png", dpi=300) if __name__ == "__main__": coeffs_untreated = init_dict["UNTREATED"]["params"] coeffs_treated = init_dict["TREATED"]["params"] cov = construct_covariance_matrix(init_dict) df = simulate(RESOURCE_DIR + filename) x = df[init_dict["TREATED"]["order"]] MTE_pres = mte_information(coeffs_treated, coeffs_untreated, cov, GRID, x, init_dict) para_diff = coeffs_treated - coeffs_untreated MTE_abs = [] for i in GRID: if cov[2, 2] == 0.00: MTE_abs += ["---"] else: MTE_abs += [np.mean(np.dot(para_diff, x.T))] plot_marginal_treatment_effect(MTE_pres, MTE_abs) cleanup()