Instrumental Variables Example
This example demonstrates using CausalFM for causal inference with instrumental variables.
Overview
In this example, we will:
Generate IV training data (binary instrument)
Train an IV model
Evaluate on test data with unobserved confounding
Compare with standard CATE (which fails with confounding)
Coming Soon
This tutorial is under development. In the meantime, check out:
Standard CATE Estimation Example - Complete Standard CATE example
Data Generation - IV data generation guide
Models - IV model usage
Quick Example
from causalfm.data import IVDataGenerator
from causalfm.models import IVModel
from causalfm.training import IVTrainer, TrainingConfig
# Generate IV data
generator = IVDataGenerator(
num_samples=1024,
num_features=10,
instrument_type='binary'
)
generator.generate_multiple(500, "data/iv_train/")
# Train
if __name__ == '__main__':
config = TrainingConfig(
data_path="data/iv_train/*.csv",
epochs=100,
save_dir="checkpoints/iv/"
)
trainer = IVTrainer(config)
trainer.train()
# Evaluate
model = IVModel.from_pretrained("checkpoints/iv/best_model.pth")
# Use instrument z along with x, a, y
result = model.estimate_cate(
x_train, z_train, a_train, y_train, x_test
)
cate = result['cate']
For a complete working example, see the notebook at:
evaluation/notebook/test_iv_binary.ipynb