XLearner<T>
Models & Types · AiDotNet.CausalInference
Implements the X-Learner (Cross-learner) for treatment effect estimation.
Inheritance: CausalModelBase<T> → XLearner<T>
Implements: IGeneratedParameterRegistrar<T>
For Beginners
X-Learner is a sophisticated meta-learner that adapts to the data by using cross-fitting. It’s especially effective when treatment and control groups have different sizes, as it leverages information from both groups more efficiently.
How It Works
How it works (5 stages):
- Train μ₀(X) and μ₁(X) using T-Learner approach
- Impute treatment effects: D₁ᵢ = Y₁ᵢ - μ₀(X₁ᵢ) for treated, D₀ᵢ = μ₁(X₀ᵢ) - Y₀ᵢ for control
- Train τ₁(X) on D₁ (treated imputed effects) and τ₀(X) on D₀ (control imputed effects)
- Estimate propensity score e(X) = P(T=1|X)
- Combine: τ(X) = e(X)·τ₀(X) + (1-e(X))·τ₁(X)
Key Insight: The weighted combination uses propensity scores to give more weight to the model trained on the larger group, making X-Learner robust to imbalanced data.
Pros and Cons:
- Pro: Excellent for imbalanced treatment groups
- Pro: Can outperform T-Learner when one group is much smaller
- Pro: Adapts to the data structure through propensity weighting
- Con: More complex, requires fitting 5 models
- Con: Propensity estimation can be sensitive
When to use:
- When treatment/control groups are imbalanced
- When you want state-of-the-art CATE estimation
- When you have sufficient data for multiple model fitting
Reference: Künzel et al., “Metalearners for Estimating Heterogeneous Treatment Effects” (2019)
Example
var covariates = new Matrix<double>(new double[,]
{
{ 45, 1 }, { 52, 0 }, { 38, 1 }, { 61, 0 }, { 47, 1 }, { 55, 0 }
});
var treatment = new Vector<int>(new int[] { 0, 1, 0, 1, 0, 1 }); // who was treated
var outcome = new Vector<double>(new double[] { 3.1, 7.2, 2.8, 8.0, 3.4, 7.6 });
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new XLearner<double>(lambda: 0.01))
.Build(covariates, treatment, outcome);
// One effect per person, over the covariates alone.
Vector<double> cate = result.EstimateTreatmentEffect(covariates);
Constructors
| Constructor | Summary |
|---|---|
XLearner(double lambda) |
Creates a new X-Learner. |
XLearner(int maxIterations) |
Creates a new X-Learner. |
Properties
| Property | Summary |
|---|---|
double Lambda { get; } |
Gets the L2 regularization strength. |
Methods
| Method | Summary |
|---|---|
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
|
ValueTuple<T, T> EstimateATE(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) |
|
ValueTuple<T, T> EstimateATT(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) |
|
Vector<T> EstimateCATEPerIndividual(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) |
|
Vector<T> EstimateTreatmentEffect(Matrix<T> features) |
Estimates the Conditional Average Treatment Effect (CATE). |
Vector<T> Predict(Matrix<T> input) |
Standard prediction - returns treatment effects. |
Vector<T> PredictControl(Matrix<T> features) |
Predicts outcome under control. |
Vector<T> PredictTreated(Matrix<T> features) |
Predicts outcome under treatment. |
Vector<T> PredictTreatmentEffect(Matrix<T> x) |
|
void Fit(Matrix<T> features, Vector<T> treatment, Vector<T> outcome) |
Fits the X-Learner model. |