xcausal.adapters.CausalMLAdapter¶
- class xcausal.adapters.CausalMLAdapter(model, name=None, propensity_model=None)[source]¶
Bases:
objectWrap a CausalML estimator as a xcausal causal estimator.
Supports all three estimation families from the
causalmlpackage:- Meta-learners (
causalml.inference.meta) BaseSRegressor, BaseTRegressor, BaseXRegressor, BaseDRRegressor, BaseRRegressor, and their Classifier variants.
predict(X)returns(n, 1); squeezed to(n,).estimate_ate()returns(ate, lb, ub)arrays of shape(1,).- Tree-based methods (
causalml.inference.tree) CausalTreeRegressor:
predict(X)returns(n,)directly. UpliftTreeClassifier, UpliftRandomForestClassifier:predict(X)returns(n, 2)— P(Y=1|control) and P(Y=1|treatment). Uplift (CATE) is computed as treatment column minus control column. These models require string treatment labels; the adapter converts binaryTautomatically usingmodel.control_namefor control and"treatment"for the treated group.- TMLE (
TMLELearnerfromcausalml.inference.meta) ATE-only (
supports_cate=False).TMLELearnerhas nofit()orpredict()— it does all work inestimate_ate(X, T, Y, p)wherepis a required propensity-score array. The adapter fits the propensity model duringfit(), seeds TMLE’s internal outcome learner, runsestimate_ateonce, and caches the(ate, lb, ub)triple;ate()is then a constant-time lookup. Caching avoids refitting its outcome learner on every ATE call. Provide apropensity_modelat construction time to customise the propensity estimator.
- Parameters:
model (CausalML estimator) – An initialized (unfitted) CausalML estimator instance.
name (str or None) – Display name. Defaults to the class name of the wrapped model.
propensity_model (sklearn classifier or None) – For
TMLELearner, fitted duringfit()to produce the required propensity scoresp; if omitted, usesLogisticRegression(max_iter=500). For other estimators whosefit()acceptsp(such asBaseXClassifier), an explicitly supplied model is fitted and its probabilities are passed through fitting and prediction. This adapter adds no clipping or cross-fitting to those scores. Default pools supply their own HGB propensity model.
Notes
Seed / reproducibility: CausalML has no uniform
random_stateinfit(). The adapter seeds the wrapped estimator, its nested sklearn estimators, and stored CV splitters where supported. It also passesrandom_stateasseedwhen the upstreamfit()accepts that argument. Opaque upstream randomness may require additional controls.Deep-copy requirement: the wrapped model must be deep-copyable for bootstrap resampling and cross-fitting. Standard sklearn-backed CausalML models are deep-copyable; models with GPU tensors or open file handles may fail.
Examples
>>> from causalml.inference.meta import BaseTRegressor >>> from sklearn.ensemble import HistGradientBoostingRegressor as HGBR >>> from xcausal.adapters import CausalMLAdapter >>> from xcausal.datasets import load_lalonde >>> X, T, Y = load_lalonde() >>> t_learner = CausalMLAdapter(BaseTRegressor(learner=HGBR(max_iter=20))) >>> t_learner.fit(X, T, Y, random_state=42) >>> result = t_learner.ate() >>> result ComponentAteEstimate(ate=..., ci=[..., ...])
Methods
__init__Return the ATE.
Return CATE predictions.
Fit the wrapped CausalML model.
Validate the wrapped CausalML model's nuisance configuration.
Attributes
Details
- ate(X=None)[source]¶
Return the ATE.
TMLELearner: calls
estimate_ate(X_train, T_train, Y_train, p=p_hat)using training data and cached propensity scores. TheXargument is ignored — TMLE’s targeted update is tied to the training sample.All other estimators: if
estimate_ate()is available, calls it on the training data (regardless ofX). CausalML’s DR-based ATE estimators require the original outcome and treatment data. Falls back tomean(cate(X_eval))for estimators withoutestimate_ate()(e.g. uplift tree/forest).- Parameters:
X (ndarray | None)
- Return type:
- cate(X)[source]¶
Return CATE predictions.
Squeezes
(n, 1)meta-learner output to(n,). For uplift tree/forest, computesP(Y=1|T=1,X) - P(Y=1|T=0,X). RaisesNotImplementedErrorforTMLELearner(ATE-only).- Parameters:
X (ndarray)
- Return type:
- fit(X, T, Y, **kwargs)[source]¶
Fit the wrapped CausalML model.
For
TMLELearner: fits the propensity model on(X, T), runs the upstreamestimate_ate(), and caches its ATE and interval. TMLE itself has nofit().For all other classes: deep-copies the model, maps our
(X, T, Y)convention to CausalML’s(X, treatment, y)convention, and callsmodel.fit(). Passesrandom_stateasseedfor estimators whosefit()accepts that parameter (e.g.BaseDRRegressor).
- validate_outcome_type(detected)[source]¶
Validate the wrapped CausalML model’s nuisance configuration.
TMLELearneris the one quirk worth catching: even for binary outcomes, itslearnerslot must be a regressor — TMLE’s internal logit transform expects raw conditional means, not probabilities. For the meta-learner*Classifierfamily, outcome-slot classifier-ness is enforced by CausalML itself; the capability declaration here keeps them out of the continuous path so the misuse can’t arise.- Parameters:
detected (str)
- Return type:
None
- Meta-learners (