CausalForest<T>

Models & Types · AiDotNet.CausalInference

Causal Forest for heterogeneous treatment effect estimation using random forests.

Inheritance: CausalModelBase<T> → CausalForest<T>

For Beginners

Causal Forests are like regular random forests, but instead of predicting outcomes, they predict how much the treatment CHANGES the outcome for each individual.

Key concepts:

  1. CATE (Conditional Average Treatment Effect): The expected treatment effect

for individuals with specific characteristics.

  1. Honest estimation: Using separate data for building trees vs estimating effects.
  2. Heterogeneity: Treatment effects can vary across the population.

How it works:

  1. Build many decision trees on bootstrap samples
  2. At each node, split to maximize treatment effect heterogeneity
  3. For prediction, average treatment effect estimates across trees

Example interpretation:

  • CATE = +5 for young patients: Treatment increases outcome by 5 for young patients
  • CATE = -2 for elderly: Treatment decreases outcome by 2 for elderly patients
  • This helps target treatments to those who benefit most

References:

  • Athey & Imbens (2016). “Recursive Partitioning for Heterogeneous Causal Effects”
  • Wager & Athey (2018). “Estimation and Inference of Heterogeneous Treatment Effects using Random Forests”

How It Works

Causal Forests extend the random forest framework to estimate Conditional Average Treatment Effects (CATE) - how treatment effects vary across individuals.

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 CausalForest<double>(numTrees: 100, maxDepth: 10))
    .Build(covariates, treatment, outcome);
            
// One effect per person, over the covariates alone.
Vector<double> cate = result.EstimateTreatmentEffect(covariates);

Constructors

Constructor Summary
CausalForest(int numTrees, int maxDepth, int minSamplesLeaf, int? maxFeatures, bool honest, double honestFraction, int? seed, double sampleFraction) Gets the model type.

Properties

Property Summary
int NumTrees { get; } Gets the number of trees in the forest.

Methods

Method Summary
Dictionary<string, T> GetFeatureImportance() Gets feature importance based on split frequency.
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) Creates a new instance with specified parameters.
ValueTuple<T, T> EstimateATE(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) Estimates the Average Treatment Effect (ATE).
ValueTuple<T, T> EstimateATT(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) Estimates the Average Treatment Effect on the Treated (ATT).
Vector<T> EstimateCATEPerIndividual(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) Estimates CATE for each individual.
Vector<T> EstimateTreatmentEffect(Matrix<T> features) Estimates treatment effects for individuals using the causal forest.
Vector<T> Predict(Matrix<T> input) Standard prediction - returns treatment effect predictions.
Vector<T> PredictControl(Matrix<T> features) Predicts outcomes under control for the given features.
Vector<T> PredictTreated(Matrix<T> features) Predicts outcomes under treatment for the given features.
Vector<T> PredictTreatmentEffect(Matrix<T> x) Predicts treatment effects for new individuals.
void Fit(Matrix<T> features, Vector<T> treatment, Vector<T> outcome) Fits the causal model using the ICausalModel interface signature.
void Fit(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) Fits the causal model using the ICausalModel interface signature.