TLearner<T>

Models & Types · AiDotNet.CausalInference

Implements the T-Learner (Two-model learner) for treatment effect estimation.

Inheritance: CausalModelBase<T> → TLearner<T>

For Beginners

T-Learner trains two separate models: one for the treated group and one for the control group. Treatment effects are estimated by subtracting the control model prediction from the treatment model prediction.

How It Works

How it works:

  • Train μ₁(X) on treated samples: {(Xᵢ, Yᵢ) : Tᵢ = 1}
  • Train μ₀(X) on control samples: {(Xᵢ, Yᵢ) : Tᵢ = 0}
  • Estimate CATE: τ(X) = μ₁(X) - μ₀(X)

Pros and Cons:

  • Pro: Can capture complex heterogeneous treatment effects
  • Pro: Each model is trained only on relevant data
  • Con: Requires sufficient data in both groups
  • Con: May have high variance when groups have different covariate distributions

When to use:

  • When you have enough data in both treatment groups
  • When treatment effects are expected to be heterogeneous
  • When covariate distributions are similar across groups

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 TLearner<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
TLearner(double lambda) Creates a new T-Learner.
TLearner(int maxIterations) Creates a new T-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 T-Learner model.