Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
beta[beta_support] = np.random.normal(size=len(beta_support))
beta = beta / np.linalg.norm(beta)
# DGP. Create samples of data (y, T, X) from known truth
y, T, X, _ = econml.dgp.dgp_data_multiple_treatments(
n_samples, n_cov, n_treatments, alpha, beta, effect)
# DML Estimation.
dml_reg = econml.dml.DML(np.arange(X.shape[1]), [], np.arange(X.shape[1], X.shape[1] + T.shape[1]),
model_y=internal_reg_y,
model_t=internal_reg_t,
model_f=internal_reg_f,
model_c=internal_reg_c)
dml_reg.fit(np.concatenate((X, T), axis=1), y)
y_test, T_test, X_test = econml.dgp.dgp_counterfactual_data_multiple_treatments(
n_samples, n_cov, beta, effect, 5. * np.ones(n_treatments))
dml_r2score.append(dml_reg.score(np.concatenate((X_test, T_test), axis=1), y_test))
dml_te.append(dml_reg.effect(np.zeros((1, 1)), np.ones((1, 1)), np.zeros((1, 0))))
# Estimation with other methods for comparison
direct_reg1.fit(np.concatenate((X, T), axis=1), y)
direct_r2score1.append(direct_reg1.score(np.concatenate((X_test, T_test), axis=1), y_test))
direct_te1.append(direct_reg1.coef_[X.shape[1]])
direct_reg2.fit(np.concatenate((X, T), axis=1), y)
direct_r2score2.append(direct_reg2.score(np.concatenate((X_test, T_test), axis=1), y_test))
direct_te2.append(direct_reg2.coef_[X.shape[1]])
dml_r2scores.append(dml_r2score)
direct_r2scores1.append(direct_r2score1)
direct_r2scores2.append(direct_r2score2)
# Sparse coefficients of treatment as a function of co-variates
alpha_sparsity = sparsity
alpha_support = np.random.choice(n_cov, alpha_sparsity, replace=False)
alpha = np.zeros(n_cov)
alpha[alpha_support] = np.random.normal(size=len(alpha_support))
alpha = alpha.reshape((-1, 1))
alpha = alpha / np.linalg.norm(alpha)
# Coefficients of outcomes as a function of co-variates
beta_sparsity = sparsity
beta_support = np.random.choice(n_cov, beta_sparsity, replace=False)
beta = np.zeros(n_cov)
beta[beta_support] = np.random.normal(size=len(beta_support))
beta = beta / np.linalg.norm(beta)
# DGP. Create samples of data (y, T, X) from known truth
y, T, X, _ = econml.dgp.dgp_data_multiple_treatments(
n_samples, n_cov, n_treatments, alpha, beta, effect)
# DML Estimation.
dml_reg = econml.dml.DML(np.arange(X.shape[1]), [], np.arange(X.shape[1], X.shape[1] + T.shape[1]),
model_y=internal_reg_y,
model_t=internal_reg_t,
model_f=internal_reg_f,
model_c=internal_reg_c)
dml_reg.fit(np.concatenate((X, T), axis=1), y)
y_test, T_test, X_test = econml.dgp.dgp_counterfactual_data_multiple_treatments(
n_samples, n_cov, beta, effect, 5. * np.ones(n_treatments))
dml_r2score.append(dml_reg.score(np.concatenate((X_test, T_test), axis=1), y_test))
dml_te.append(dml_reg.effect(np.zeros((1, 1)), np.ones((1, 1)), np.zeros((1, 0))))
# Estimation with other methods for comparison