CausalModelBase<T>
Base Classes · AiDotNet.CausalInference
Abstract base class for causal inference models.
Implements: ICausalModel<T>, IModelShape, IParameterChunkSource<T>, IParameterManifestProvider
Known derived types: CausalForest<T>, DoublyRobustEstimator<T>, InverseProbabilityWeighting<T>, PropensityScoreMatching<T>, SLearner<T>, TLearner<T>, XLearner<T>
For Beginners
This class contains shared code that all causal inference models need, so each specific model (like PropensityScoreMatching) doesn’t have to reimplement it.
Key shared functionality:
- Estimating propensity scores (probability of treatment)
- Calculating treatment effects
- Checking overlap assumptions
- Managing fitted model state
How It Works
This base class provides common functionality for causal inference models including propensity score estimation, treatment effect calculation, and overlap checking.
Properties
| Property | Summary |
|---|---|
ILossFunction<T> DefaultLossFunction { get; } |
Gets the default loss function. |
ParameterLayoutSnapshot ParameterLayout { get; } |
|
bool IsTrained { get; } |
Gets whether the model is trained. |
bool SupportsParameterInitialization { get; } |
|
long ParameterCount { get; } |
|
string[] FeatureNames { get; set; } |
Gets or sets the feature names. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> GetFeatureImportance() |
Gets the feature importance scores. |
DynamicShapeInfo GetDynamicShapeInfo() |
|
IEnumerable<ParameterChunk<T>> GetParameterStateChunks() |
Enumerates canonical persistent state without flattening tensor-backed components. |
IEnumerable<Tensor<T>> GetParameterChunks() |
Streams state in the same order as the parameter manifest. |
IEnumerable<int> GetActiveFeatureIndices() |
Gets the indices of features that are actively used in the model. |
IFullModel<T, Matrix<T>, Vector<T>> Clone() |
Creates a clone of the model. |
IFullModel<T, Matrix<T>, Vector<T>> DeepCopy() |
Creates a deep copy of the model. |
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) |
Creates a new instance of the model with specified parameters. |
ModelMetadata<T> GetModelMetadata() |
Gets the model type. |
T EstimateAverageTreatmentEffect(Matrix<T> features) |
Estimates the Average Treatment Effect (ATE) across the population. |
ValueTuple<T, T, T, T> CheckOverlap(Matrix<T> x, Vector<int> treatment) |
Checks the overlap/positivity assumption. |
ValueTuple<T, T> EstimateATE(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) |
Estimates the Average Treatment Effect (ATE) from the data with standard error. |
ValueTuple<T, T> EstimateATT(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) |
Estimates the Average Treatment Effect on the Treated (ATT). |
Vector<T> ComputeGradients(Matrix<T> input, Vector<T> target, ILossFunction<T> lossFunction) |
Computes gradients for the given input and target. |
Vector<T> EstimateCATEPerIndividual(Matrix<T> x, Vector<int> treatment, Vector<T> outcome) |
Estimates the Conditional Average Treatment Effect (CATE) for each individual. |
Vector<T> EstimatePropensityScores(Matrix<T> x) |
Estimates propensity scores for each individual. |
Vector<T> EstimateTreatmentEffect(Matrix<T> features) |
Estimates the Conditional Average Treatment Effect (CATE) for subjects. |
Vector<T> GetParameters() |
|
Vector<T> Predict(Matrix<T> input) |
Standard prediction - returns predicted outcomes. |
Vector<T> PredictControl(Matrix<T> features) |
Predicts the outcome under control. |
Vector<T> PredictTreated(Matrix<T> features) |
Predicts the outcome under treatment. |
Vector<T> PredictTreatmentEffect(Matrix<T> x) |
Predicts the treatment effect for new individuals. |
Vector<T> SanitizeParameters(Vector<T> parameters) |
|
bool IsFeatureUsed(int featureIndex) |
Determines whether a specific feature is used in the model. |
byte[] Serialize() |
Serializes the model to a byte array. |
int[] GetInputShape() |
Saves the model to a file. |
int[] GetOutputShape() |
|
void ApplyGradients(Vector<T> gradients, T learningRate) |
Applies gradients to update model parameters. |
void Deserialize(byte[] modelData) |
Deserializes the model from a byte array. |
void Dispose() |
|
void Fit(Matrix<T> features, Vector<T> treatment, Vector<T> outcome) |
Fits the causal model to observational data. |
void LoadModel(string filePath) |
Loads the model from a file. |
void LoadState(Stream stream) |
Loads the model’s state from a stream. |
void SaveModel(string filePath) |
|
void SaveState(Stream stream) |
Saves the model’s state to a stream. |
void SetActiveFeatureIndices(IEnumerable<int> featureIndices) |
Sets the active feature indices for this model. |
void SetParameterChunks(IEnumerable<Tensor<T>> chunks) |
Restores tensor-backed chunks on their device and retains custom-setter semantics. |
void SetParameters(Vector<T> parameters) |
|
void Train(Matrix<T> x, Vector<T> y) |