causallib.evaluation.predictor module

Predictor classes.

Predictors generate sets of predictions for a single fold with no cross-validation or train-test logic.

class causallib.evaluation.predictor.BasePredictor(estimator)[source]

Bases: object

Generate predictions from estimator for evaluation (base class).

abstract fit(X, a, y)[source]

Fit an estimator.

static from_estimator(estimator: Union[causallib.estimation.base_estimator.IndividualOutcomeEstimator, causallib.estimation.base_weight.PropensityEstimator, causallib.estimation.base_weight.WeightEstimator])[source]

Select subclass based on estimator.

Parameters

estimator (Union[IndividualOutcomeEstimator, PropensityEstimator, WeightEstimator]) – Estimator to generate evaluation predictions from.

Returns

the correct predictor for

the supplied estimator

Return type

Union[PropensityPredictor, WeightPredictor, OutcomePredictor]

abstract predict(X, a)[source]

Predict (weights, outcomes, etc. depending on the model). The output can be as flexible as desired, but score_estimation should know to handle it.

class causallib.evaluation.predictor.OutcomePredictor(estimator)[source]

Bases: causallib.evaluation.predictor.BasePredictor

Generate evaluation predictions for IndividualOutcomeEstimator models.

Parameters

estimator (IndividualOutcomeEstimator) –

fit(X, a, y)[source]

Fit estimator.

predict(X, a)[source]

Predict on data.

class causallib.evaluation.predictor.PropensityPredictor(estimator)[source]

Bases: causallib.evaluation.predictor.WeightPredictor

Generate evaluation predictions for PropensityEstimator models.

Parameters

estimator (PropensityEstimator) –

predict(X, a)[source]

Predict on data.

Parameters
  • X (pd.DataFrame) – Covariates.

  • a (pd.Series) – Target variable - treatment assignment

Returns

PropensityEvaluatorPredictions

class causallib.evaluation.predictor.WeightPredictor(estimator)[source]

Bases: causallib.evaluation.predictor.BasePredictor

Generate evaluation predictions for WeightEstimator models.

Parameters

estimator (WeightEstimator) –

fit(X, a, y=None)[source]

Fit estimator. y is ignored.

predict(X, a)[source]

Predict on data.

Parameters
  • X (pd.DataFrame) – Covariates.

  • a (pd.Series) – Target variable - treatment assignment

Returns

WeightEvaluatorPredictions

causallib.evaluation.predictor.predict_cv(estimator, X, a, y, cv, refit=True, phases=('train', 'valid'))[source]

Obtain predictions on the provided data in cross-validation

Parameters
  • X (pd.DataFrame) – Covariates.

  • a (pd.Series) – Treatment assignment.

  • y (pd.Series) – Outcome.

  • cv (list[tuples]) – list the number of folds containing tuples of indices (train_idx, validation_idx)

  • refit (bool) – Whether to refit the model on each fold.

  • phases (list[str]) – {[“train”, “valid”], [“train”], [“valid”]}. Phases names to evaluate on - train (“train”), validation (“valid”) or both. ‘train’ corresponds to cv[i][0] and ‘valid’ to cv[i][1]

Returns

A two-tuple containing:

  • predictions: dictionary with keys being the phases provided and values are

    list the size of the number of folds in cv and containing the output of the estimator on that corresponding fold. For example, predictions[“valid”][3] contains the prediction of the estimator on untrained data of the third fold (i.e. validation set of the third fold)

  • models: list the size of the number of folds in cv containing the fitted estimator

    on the training data of that fold.

Return type

(dict[str, list], list)