econml/ortho_forest.py
def second_stage_parameter_estimator_gen(lambda_reg):
        """
        For the second stage parameter estimation we add a local linear correction. So
        we fit a local linear function as opposed to a local constant function. We also penalize
        the linear part to reduce variance.
        """
        def parameter_estimator_func(Y, T, X,
                                     nuisance_estimates,
                                     sample_weight=None):
            """Calculate the parameter of interest for points given by (Y, T) and corresponding nuisance estimates."""
            # Compute residuals
            Y_hat, T_hat = nuisance_estimates
            Y_res, T_res = reshape_Y_T(Y - Y_hat, T - T_hat)
            X_aug = PolynomialFeatures(degree=1, include_bias=True).fit_transform(X)
            XT_res = cross_product(T_res, X_aug)
            # Compute coefficient by OLS on residuals
            if sample_weight is not None:
                weighted_XT_res = sample_weight.reshape(-1, 1) * XT_res
            else:
                weighted_XT_res = XT_res / XT_res.shape[0]
            # ell_2 regularization
            diagonal = np.ones(XT_res.shape[1])
            diagonal[:T_res.shape[1]] = 0
            reg = lambda_reg * np.diag(diagonal)
            # Ridge regression estimate
            param_estimate = np.linalg.lstsq(np.matmul(weighted_XT_res.T, XT_res) + reg,
                                             np.matmul(weighted_XT_res.T, Y_res.reshape(-1, 1)),
                                             rcond=None)[0].flatten()
            # Parameter returned by LinearRegression is (d_T, )
            return param_estimate

        return parameter_estimator_func

        For the second stage parameter estimation we add a local linear correction. So
        we fit a local linear function as opposed to a local constant function. We also penalize
        the linear part to reduce variance.
        
For the second stage parameter estimation we add a local linear correction. So
we fit a local linear function as opposed to a local constant function. We also penalize
the linear part to reduce variance.
For the second stage parameter estimation we add a local linear correction . So we fit a local linear function as opposed to a local constant function . We also penalize the linear part to reduce variance .
def second_stage_parameter_estimator_gen(lambda_reg):
        def parameter_estimator_func(Y, T, X,
                                     nuisance_estimates,
                                     sample_weight=None):
            Y_hat, T_hat = nuisance_estimates
            Y_res, T_res = reshape_Y_T(Y - Y_hat, T - T_hat)
            X_aug = PolynomialFeatures(degree=1, include_bias=True).fit_transform(X)
            XT_res = cross_product(T_res, X_aug)
            if sample_weight is not None:
                weighted_XT_res = sample_weight.reshape(-1, 1) * XT_res
            else:
                weighted_XT_res = XT_res / XT_res.shape[0]
            diagonal = np.ones(XT_res.shape[1])
            diagonal[:T_res.shape[1]] = 0
            reg = lambda_reg * np.diag(diagonal)
            param_estimate = np.linalg.lstsq(np.matmul(weighted_XT_res.T, XT_res) + reg,
                                             np.matmul(weighted_XT_res.T, Y_res.reshape(-1, 1)),
                                             rcond=None)[0].flatten()
            return param_estimate
        return parameter_estimator_func
def second_stage_parameter_estimator_gen ( lambda_reg ) : def parameter_estimator_func ( Y , T , X , nuisance_estimates , sample_weight = None ) : """Calculate the parameter of interest for points given by (Y, T) and corresponding nuisance estimates.""" Y_hat , T_hat = nuisance_estimates Y_res , T_res = reshape_Y_T ( Y - Y_hat , T - T_hat ) X_aug = PolynomialFeatures ( degree = 1 , include_bias = True ) . fit_transform ( X ) XT_res = cross_product ( T_res , X_aug ) if sample_weight is not None : weighted_XT_res = sample_weight . reshape ( - 1 , 1 ) * XT_res else : weighted_XT_res = XT_res / XT_res . shape [ 0 ] diagonal = np . ones ( XT_res . shape [ 1 ] ) diagonal [ : T_res . shape [ 1 ] ] = 0 reg = lambda_reg * np . diag ( diagonal ) param_estimate = np . linalg . lstsq ( np . matmul ( weighted_XT_res . T , XT_res ) + reg , np . matmul ( weighted_XT_res . T , Y_res . reshape ( - 1 , 1 ) ) , rcond = None ) [ 0 ] . flatten ( ) return param_estimate return parameter_estimator_func
For the second stage parameter estimation we add a local linear correction.
For the second stage parameter estimation we add a local linear correction .
"""
        For the second stage parameter estimation we add a local linear correction. So
        we fit a local linear function as opposed to a local constant function. We also penalize
        the linear part to reduce variance.
        """ """Calculate the parameter of interest for points given by (Y, T) and corresponding nuisance estimates.""" # Compute residuals # Compute coefficient by OLS on residuals # ell_2 regularization # Ridge regression estimate # Parameter returned by LinearRegression is (d_T, )
lambda_reg