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 is_pos_def(dict_): """The function tests if the specified covariance matrix is positive semi definite.""" cov = construct_covariance_matrix(dict_) try: np.linalg.cholesky(cov) return True except LinAlgError: return False
def test6(): """The test ensures that the cholesky decomposition and recomposition works appropriately. For this purpose the test creates a positive smi definite matrix fom a wishart distribution, decomposes this matrix with, reconstruct it and compares the matrix with the one that was specified as the input for the decomposition process. """ pseudo_dict = {'DIST': {'all': []}, 'AUX': {'init_values': []}} for _ in range(20): b = wishart.rvs(df=10, scale=np.identity(3), size=1) parameter = b[np.triu_indices(3)] for i in [0, 3, 5]: parameter[i] **= 0.5 pseudo_dict['DIST']['all'] = parameter pseudo_dict['AUX']['init_values'] = parameter cov_1 = construct_covariance_matrix(pseudo_dict) x0, _ = provide_cholesky_decom(pseudo_dict, [], 'init') output = backward_cholesky_transformation(x0, test=True) output = adjust_output_cholesky(output) pseudo_dict['DIST']['all'] = output cov_2 = construct_covariance_matrix(pseudo_dict) np.testing.assert_array_almost_equal(cov_1, cov_2) cleanup()
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 provide_cholesky_decom(init_dict, x0, option, sd_=None): """The function transforms the start covariance matrix into its cholesky decomposition.""" if option == 'init': cov = construct_covariance_matrix(init_dict) L = np.linalg.cholesky(cov) L = L[np.tril_indices(3)] distribution_characteristics = init_dict['AUX']['init_values'][-6:] x0 = np.concatenate((x0, L)) elif option == 'auto': distribution_characteristics = [sd_[0], init_dict['DIST']['all'][1], 0, sd_[1], 0, init_dict['DIST']['all'][5]] cov = np.zeros((3, 3)) cov[np.triu_indices(3)] = [distribution_characteristics] cov[np.tril_indices(3, k=-1)] = cov[np.triu_indices(3, k=1)] cov[np.diag_indices(3)] **= 2 L = np.linalg.cholesky(cov) L = L[np.tril_indices(3)] x0 = np.concatenate((x0, L)) init_dict['AUX']['cholesky_decomposition'] = L.tolist() start = [i for i in x0] + distribution_characteristics return x0, start
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)
def is_pos_def(dict_): """The function tests if the specified covariance matrix is positive semi definite. """ return np.all(np.linalg.eigvals(construct_covariance_matrix(dict_)) >= 0)