How to use the econml.dml function in econml

To help you get started, we’ve selected a few econml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / EconML / econml / test_integration.py View on Github external
# for now, require one feature per store/product combination
            # TODO: would be nice to relax this somehow
            assert X.shape[0] == Y.shape[0]
            n_weeks = Y.shape[0]
            Y = np.reshape(Y, (-1, 1))  # flatten prices into 1-d list
            self._model.fit(self._reshape(X), Y)

        def predict(self, X):
            predicted = self._model.predict(self._reshape(X))
            return np.reshape(predicted, (-1, block_size))

        @property
        def coef_(self):
            return self._model.coef_

    return econml.dml.LinearDMLCateEstimator(
        model_t=GroupRegression(t_model, constant_features=[], constant_controls=constant_controls,
                                compute_gp_avgs=False, is_first_stage=True),
        model_y=GroupRegression(y_model, constant_features=features, constant_controls=constant_controls,
                                compute_gp_avgs=True, is_first_stage=True),
        model_final=GroupRegression(f_model, effect_features=features, compute_gp_avgs=True, is_first_stage=False)
    ).fit(X, Y).coef_
github microsoft / EconML / econml / test_integration.py View on Github external
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
            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]])