元学习器
元学习器是一种估计模型,旨在当处理手段为离散变量时通过机器学习模型去评估CATE。治疗方案为离散变量的意思也就是当无混淆条件下非1即0。通常来讲,它利用多个可灵活选择的机器学习模型。
YLearn 实现了3个元学习器: S-Learner, T-Learner, and X-Learner.
S-Learner
SLearner 采用一个机器学习模型来评估因果效应。具体来说,我们用机器学习模型 \(f\) 从治疗方案 \(x\) 和调整集 (或者协变量) \(w\) 中拟合一个模型去预测结果 \(y\):
因果效应 \(\tau(w)\) 被计算为:
- class ylearn.estimator_model.meta_learner.SLearner(model, random_state=2022, is_discrete_treatment=True, categories='auto', *args, **kwargs)
- 参数:
model (estimator, optional) – The base machine learning model for training SLearner. Any model should be some valid machine learning model with fit() and predict() functions.
random_state (int, default=2022) –
is_discrete_treatment (bool, default=True) – Treatment must be discrete for SLearner.
categories (str, optional, default='auto') –
- fit(data, outcome, treatment, adjustment=None, covariate=None, treat=None, control=None, combined_treatment=True, **kwargs)
Fit the SLearner in the dataset.
- 参数:
data (pandas.DataFrame) – Training dataset for training the estimator.
outcome (list of str, optional) – Names of the outcome.
treatment (list of str, optional) – Names of the treatment.
adjustment (list of str, optional, default=None) – Names of the adjustment set ensuring the unconfoundness,
covariate (list of str, optional, default=None) – Names of the covariate.
treat (int, optional) – Label of the intended treatment group
control (int, optional) – Label of the intended control group
combined_treatment (bool, optional, default=True) –
Only modify this parameter for multiple treatments, where multiple discrete treatments are combined to give a single new group of discrete treatment if set as True. When combined_treatment is set to True, then if there are multiple treatments, we can use the combined_treatment technique to covert the multiple discrete classification tasks into a single discrete classification task. For an example, if there are two different binary treatments:
treatment_1: \(x_1 | x_1 \in \{'sleep', 'run'\}\),
treatment_2: \(x_2 | x_2 \in \{'study', 'work'\}\),
then we can convert these two binary classification tasks into a single classification task with 4 different classes:
treatment: \(x | x \in \{0, 1, 2, 3\}\),
where, for example, 1 stands for (‘sleep’ and ‘stuy’).
- 返回:
The fitted instance of SLearner.
- 返回类型:
instance of SLearner
- estimate(data=None, quantity=None)
Estimate the causal effect with the type of the quantity.
- 参数:
data (pandas.DataFrame, optional, default=None) – Test data. The model will use the training data if set as None.
quantity (str, optional, default=None) –
Option for returned estimation result. The possible values of quantity include:
’CATE’ : the estimator will evaluate the CATE;
’ATE’ : the estimator will evaluate the ATE;
None : the estimator will evaluate the ITE or CITE.
- 返回:
The estimated causal effects
- 返回类型:
ndarray
- effect_nji(data=None)
Calculate causal effects with different treatment values.
- 返回:
Causal effects with different treatment values.
- 返回类型:
ndarray
- _comp_transormer(x, categories='auto')
Transform the discrete treatment into one-hot vectors properly.
- 参数:
x (numpy.ndarray, shape (n, x_d)) – An array containing the information of the treatment variables.
categories (str or list, optional, default='auto') –
- 返回:
The transformed one-hot vectors.
- 返回类型:
numpy.ndarray
T-Learner
TLearner的问题是当调整集向量为多维时治疗方案向量仅为一维。因此,如果调整集的维度超过1,那么评估结果将总是逼近于0。 TLearner用两个机器学习模型去评估因果效应。具体来讲,令 \(w\) 为调整集(或协变量),我们
分别拟合两个模型 \(f_t(w)\) 对于治疗组 (\(x=\) treat) 和 \(f_0(w)\) 对于控制组 (\(x=\) control):
\[y_t = f_t(w)\]
其中, \(x=\) treat.
\[y_0 = f_0(w)\]其中, \(x=\) control.
计算因果效应 \(\tau(w)\) 作为两个模型预测结果的差异:
\[\tau(w) = f_t(w) - f_0(w).\]
- class ylearn.estimator_model.meta_learner.TLearner(model, random_state=2022, is_discrete_treatment=True, categories='auto', *args, **kwargs)
- 参数:
model (estimator, optional) – The base machine learning model for training TLearner. Any model should be some valid machine learning model with fit() and predict() functions.
random_state (int, default=2022) –
is_discrete_treatment (bool, default=True) – Treatment must be discrete for SLearner.
categories (str, optional, default='auto') –
- fit(data, outcome, treatment, adjustment=None, covariate=None, treat=None, control=None, combined_treatment=True, **kwargs)
Fit the SLearner in the dataset.
- 参数:
data (pandas.DataFrame) – Training dataset for training the estimator.
outcome (list of str, optional) – Names of the outcome.
treatment (list of str, optional) – Names of the treatment.
adjustment (list of str, optional, default=None) – Names of the adjustment set ensuring the unconfoundness,
covariate (list of str, optional, default=None) – Names of the covariate.
treat (int, optional) – Label of the intended treatment group
control (int, optional) – Label of the intended control group
combined_treatment (bool, optional, default=True) –
Only modify this parameter for multiple treatments, where multiple discrete treatments are combined to give a single new group of discrete treatment if set as True. When combined_treatment is set to True, then if there are multiple treatments, we can use the combined_treatment technique to covert the multiple discrete classification tasks into a single discrete classification task. For an example, if there are two different binary treatments:
treatment_1: \(x_1 | x_1 \in \{'sleep', 'run'\}\),
treatment_2: \(x_2 | x_2 \in \{'study', 'work'\}\),
then we can convert these two binary classification tasks into a single classification task with 4 different classes:
treatment: \(x | x \in \{0, 1, 2, 3\}\),
where, for example, 1 stands for (‘sleep’ and ‘stuy’).
- 返回:
The fitted instance of TLearner.
- 返回类型:
instance of TLearner
- estimate(data=None, quantity=None)
Estimate the causal effect with the type of the quantity.
- 参数:
data (pandas.DataFrame, optional, default=None) – Test data. The model will use the training data if set as None.
quantity (str, optional, default=None) –
Option for returned estimation result. The possible values of quantity include:
’CATE’ : the estimator will evaluate the CATE;
’ATE’ : the estimator will evaluate the ATE;
None : the estimator will evaluate the ITE or CITE.
- 返回:
The estimated causal effects
- 返回类型:
ndarray
- effect_nji(data=None)
Calculate causal effects with different treatment values.
- 返回:
Causal effects with different treatment values.
- 返回类型:
ndarray
- _comp_transormer(x, categories='auto')
Transform the discrete treatment into one-hot vectors properly.
- 参数:
x (numpy.ndarray, shape (n, x_d)) – An array containing the information of the treatment variables.
categories (str or list, optional, default='auto') –
- 返回:
The transformed one-hot vectors.
- 返回类型:
numpy.ndarray
X-Learner
TLearner未能完全有效地利用数据,XLearner可以解决这个问题。训练一个XLearner可以分为3步:
与TLearner类似, 我们首先分别训练两个不同的模型对于控制组和治疗组:
\[\begin{split}& f_0(w) \text{for the control group}\\ & f_1(w) \text{for the treat group}.\end{split}\]生成两个新数据集 \(\{(h_0, w)\}\) 用控制组, :math:`{(h_1, w)}`用治疗组。 其中
\[\begin{split}h_0 & = f_1(w) - y_0,\\ h_1 & = y_1 - f_0(w).\end{split}\]然后,训练两个机器学习模型在这些数据集中 \(k_0(w)\) 和 \(k_1(w)\)
\[\begin{split}h_0 & = k_0(w) \\ h_1 & = k_1(w).\end{split}\]结合以上两个模型得到最终的模型:
\[g(w) = k_0(w)a(w) + k_1(w)(1 - a(w))\]其中, \(a(w)\) 是一个调整 \(k_0\) 和 \(k_1\) 的权重调整系数。
最后, 因果效应 \(\tau(w)\) 通过以下方式评估:
- class ylearn.estimator_model.meta_learner.XLearner(model, random_state=2022, is_discrete_treatment=True, categories='auto', *args, **kwargs)
- 参数:
model (estimator, optional) – The base machine learning model for training SLearner. Any model should be some valid machine learning model with fit() and predict() functions.
random_state (int, default=2022) –
is_discrete_treatment (bool, default=True) – Treatment must be discrete for SLearner.
categories (str, optional, default='auto') –
- fit(data, outcome, treatment, adjustment=None, covariate=None, treat=None, control=None, combined_treatment=True, **kwargs)
Fit the SLearner in the dataset.
- 参数:
data (pandas.DataFrame) – Training dataset for training the estimator.
outcome (list of str, optional) – Names of the outcome.
treatment (list of str, optional) – Names of the treatment.
adjustment (list of str, optional, default=None) – Names of the adjustment set ensuring the unconfoundness,
covariate (list of str, optional, default=None) – Names of the covariate.
treat (int, optional) – Label of the intended treatment group
control (int, optional) – Label of the intended control group
combined_treatment (bool, optional, default=True) –
Only modify this parameter for multiple treatments, where multiple discrete treatments are combined to give a single new group of discrete treatment if set as True. When combined_treatment is set to True, then if there are multiple treatments, we can use the combined_treatment technique to covert the multiple discrete classification tasks into a single discrete classification task. For an example, if there are two different binary treatments:
treatment_1: \(x_1 | x_1 \in \{'sleep', 'run'\}\),
treatment_2: \(x_2 | x_2 \in \{'study', 'work'\}\),
then we can convert these two binary classification tasks into a single classification task with 4 different classes:
treatment: \(x | x \in \{0, 1, 2, 3\}\),
where, for example, 1 stands for (‘sleep’ and ‘stuy’).
- 返回:
The fitted instance of XLearner.
- 返回类型:
instance of XLearner
- estimate(data=None, quantity=None)
Estimate the causal effect with the type of the quantity.
- 参数:
data (pandas.DataFrame, optional, default=None) – Test data. The model will use the training data if set as None.
quantity (str, optional, default=None) –
Option for returned estimation result. The possible values of quantity include:
’CATE’ : the estimator will evaluate the CATE;
’ATE’ : the estimator will evaluate the ATE;
None : the estimator will evaluate the ITE or CITE.
- 返回:
The estimated causal effects
- 返回类型:
ndarray
- effect_nji(data=None)
Calculate causal effects with different treatment values.
- 返回:
Causal effects with different treatment values.
- 返回类型:
ndarray
- _comp_transormer(x, categories='auto')
Transform the discrete treatment into one-hot vectors properly.
- 参数:
x (numpy.ndarray, shape (n, x_d)) – An array containing the information of the treatment variables.
categories (str or list, optional, default='auto') –
- 返回:
The transformed one-hot vectors.
- 返回类型:
numpy.ndarray