Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Returns
-------
self: an instance of self.
"""
# Check that T is shape (n, )
# Check T is numeric
T = self._check_treatment(T)
# Train label encoder
T = self._label_encoder.fit_transform(T)
self._one_hot_encoder = OneHotEncoder(sparse=False, categories='auto').fit(T.reshape(-1, 1))
# Define number of classes
self.n_T = self._label_encoder.classes_.shape[0]
self.nuisance_estimator = DiscreteTreatmentOrthoForest.nuisance_estimator_generator(
self.propensity_model, self.model_Y, self.n_T, self.random_state, second_stage=False)
self.second_stage_nuisance_estimator = DiscreteTreatmentOrthoForest.nuisance_estimator_generator(
self.propensity_model_final, self.model_Y_final, self.n_T, self.random_state, second_stage=True)
self.transformer = FunctionTransformer(
func=(lambda T:
self._one_hot_encoder.transform(
reshape(self._label_encoder.transform(T.ravel()), (-1, 1)))[:, 1:]),
validate=False)
# Call `fit` from parent class
return super().fit(Y, T, X, W=W, inference=inference)
self.propensity_model = clone(propensity_model, safe=False)
self.model_Y = clone(model_Y, safe=False)
self.propensity_model_final = clone(propensity_model_final, safe=False)
self.model_Y_final = clone(model_Y_final, safe=False)
if self.propensity_model_final is None:
self.propensity_model_final = clone(self.propensity_model, safe=False)
if self.model_Y_final is None:
self.model_Y_final = clone(self.model_Y, safe=False)
# Nuisance estimators shall be defined during fitting because they need to know the number of distinct
# treatments
nuisance_estimator = None
second_stage_nuisance_estimator = None
# Define parameter estimators
parameter_estimator = DiscreteTreatmentOrthoForest.parameter_estimator_func
second_stage_parameter_estimator =\
DiscreteTreatmentOrthoForest.second_stage_parameter_estimator_gen(lambda_reg)
# Define moment and mean gradient estimator
moment_and_mean_gradient_estimator =\
DiscreteTreatmentOrthoForest.moment_and_mean_gradient_estimator_func
# Define autoencoder
self._label_encoder = LabelEncoder()
super(DiscreteTreatmentOrthoForest, self).__init__(
nuisance_estimator,
second_stage_nuisance_estimator,
parameter_estimator,
second_stage_parameter_estimator,
moment_and_mean_gradient_estimator,
n_trees=n_trees,
min_leaf_size=min_leaf_size,
max_depth=max_depth,
subsample_ratio=subsample_ratio,
bootstrap=bootstrap,
random_state=None):
# Copy and/or define models
self.propensity_model = clone(propensity_model, safe=False)
self.model_Y = clone(model_Y, safe=False)
self.propensity_model_final = clone(propensity_model_final, safe=False)
self.model_Y_final = clone(model_Y_final, safe=False)
if self.propensity_model_final is None:
self.propensity_model_final = clone(self.propensity_model, safe=False)
if self.model_Y_final is None:
self.model_Y_final = clone(self.model_Y, safe=False)
# Nuisance estimators shall be defined during fitting because they need to know the number of distinct
# treatments
nuisance_estimator = None
second_stage_nuisance_estimator = None
# Define parameter estimators
parameter_estimator = DiscreteTreatmentOrthoForest.parameter_estimator_func
second_stage_parameter_estimator =\
DiscreteTreatmentOrthoForest.second_stage_parameter_estimator_gen(lambda_reg)
# Define moment and mean gradient estimator
moment_and_mean_gradient_estimator =\
DiscreteTreatmentOrthoForest.moment_and_mean_gradient_estimator_func
# Define autoencoder
self._label_encoder = LabelEncoder()
super(DiscreteTreatmentOrthoForest, self).__init__(
nuisance_estimator,
second_stage_nuisance_estimator,
parameter_estimator,
second_stage_parameter_estimator,
moment_and_mean_gradient_estimator,
n_trees=n_trees,
min_leaf_size=min_leaf_size,
max_depth=max_depth,
if self.model_Y_final is None:
self.model_Y_final = clone(self.model_Y, safe=False)
# Nuisance estimators shall be defined during fitting because they need to know the number of distinct
# treatments
nuisance_estimator = None
second_stage_nuisance_estimator = None
# Define parameter estimators
parameter_estimator = DiscreteTreatmentOrthoForest.parameter_estimator_func
second_stage_parameter_estimator =\
DiscreteTreatmentOrthoForest.second_stage_parameter_estimator_gen(lambda_reg)
# Define moment and mean gradient estimator
moment_and_mean_gradient_estimator =\
DiscreteTreatmentOrthoForest.moment_and_mean_gradient_estimator_func
# Define autoencoder
self._label_encoder = LabelEncoder()
super(DiscreteTreatmentOrthoForest, self).__init__(
nuisance_estimator,
second_stage_nuisance_estimator,
parameter_estimator,
second_stage_parameter_estimator,
moment_and_mean_gradient_estimator,
n_trees=n_trees,
min_leaf_size=min_leaf_size,
max_depth=max_depth,
subsample_ratio=subsample_ratio,
bootstrap=bootstrap,
n_jobs=n_jobs,
random_state=random_state)
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 partial moments
pointwise_params = DiscreteTreatmentOrthoForest._partial_moments(Y, T, nuisance_estimates)
param_estimate = np.average(pointwise_params, weights=sample_weight, axis=0)
# If any of the values in the parameter estimate is nan, return None
return param_estimate
self.model_Y_final = clone(model_Y_final, safe=False)
if self.propensity_model_final is None:
self.propensity_model_final = clone(self.propensity_model, safe=False)
if self.model_Y_final is None:
self.model_Y_final = clone(self.model_Y, safe=False)
# Nuisance estimators shall be defined during fitting because they need to know the number of distinct
# treatments
nuisance_estimator = None
second_stage_nuisance_estimator = None
# Define parameter estimators
parameter_estimator = DiscreteTreatmentOrthoForest.parameter_estimator_func
second_stage_parameter_estimator =\
DiscreteTreatmentOrthoForest.second_stage_parameter_estimator_gen(lambda_reg)
# Define moment and mean gradient estimator
moment_and_mean_gradient_estimator =\
DiscreteTreatmentOrthoForest.moment_and_mean_gradient_estimator_func
# Define autoencoder
self._label_encoder = LabelEncoder()
super(DiscreteTreatmentOrthoForest, self).__init__(
nuisance_estimator,
second_stage_nuisance_estimator,
parameter_estimator,
second_stage_parameter_estimator,
moment_and_mean_gradient_estimator,
n_trees=n_trees,
min_leaf_size=min_leaf_size,
max_depth=max_depth,
subsample_ratio=subsample_ratio,
bootstrap=bootstrap,
n_jobs=n_jobs,
random_state=random_state)