InverseProbabilityWeighting<T>

Models & Types · AiDotNet.CausalInference

Implements Inverse Probability Weighting (IPW) for causal effect estimation.

Inheritance: CausalModelBase<T> → InverseProbabilityWeighting<T>

Implements: IGeneratedParameterRegistrar<T>

For Beginners

IPW works by giving more weight to “surprising” observations:

  • A treated person who was unlikely to be treated gets high weight
  • A control person who was likely to be treated gets high weight

Why? In observational data, treatment isn’t random - some people are more likely to be treated. IPW corrects for this by “up-weighting” under-represented cases.

Example - Job Training Study:

  • Highly motivated person who didn’t get training: weight = 1/(1-0.9) = 10
  • Less motivated person who did get training: weight = 1/0.2 = 5
  • This gives more influence to the “surprising” cases

Mathematical formula: ATE = E[T×Y/e(X)] - E[(1-T)×Y/(1-e(X))]

Where:

  • T = treatment (0 or 1)
  • Y = outcome
  • e(X) = propensity score = P(T=1|X)

Advantages:

  • Uses all data (unlike matching which discards unmatched)
  • Computationally simple
  • Easy to combine with regression (augmented IPW)

Limitations:

  • Can be unstable if propensity scores are extreme (near 0 or 1)
  • Sensitive to propensity score model misspecification

References:

  • Horvitz & Thompson (1952). “A Generalization of Sampling Without Replacement”
  • Robins, Rotnitzky & Zhao (1994). “Estimation of Regression Coefficients”

How It Works

IPW estimates treatment effects by weighting observations inversely by their probability of receiving their actual treatment status, creating a pseudo-population where treatment is independent of confounders.

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 InverseProbabilityWeighting<double>(trimMin: 0.01, trimMax: 0.99))
    .Build(covariates, treatment, outcome);
            
var (ate, se) = result.EstimateATE(covariates, treatment, outcome);

Constructors

Constructor Summary
InverseProbabilityWeighting(double trimMin, double trimMax, bool stabilizedWeights, int? randomSeed) Gets the model type.

Methods

Method Summary
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 using IPW.
ValueTuple<T, T> EstimateATT(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) Estimates the Average Treatment Effect on the Treated using IPW.
ValueTuple<T, T> GetEffectiveSampleSize(Matrix<T> x, Vector<int> treatment) Gets the effective sample size after weighting.
Vector<T> ComputeWeights(Matrix<T> x, Vector<int> treatment) Computes IPW weights for each observation.
Vector<T> EstimateCATEPerIndividual(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) Estimates individual treatment effects.
Vector<T> EstimateTreatmentEffect(Matrix<T> features) Estimates treatment effects for individuals using IPW.
Vector<T> Predict(Matrix<T> input) Standard prediction - returns propensity scores.
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) Fits the propensity score model to the data.