|
| 1 | +"""This module contains the Estimator abstract class""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from abc import ABC, abstractmethod |
| 5 | +from typing import Any |
| 6 | +from math import ceil |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +import statsmodels.api as sm |
| 11 | +import statsmodels.formula.api as smf |
| 12 | +from patsy import dmatrix # pylint: disable = no-name-in-module |
| 13 | +from patsy import ModelDesc |
| 14 | +from statsmodels.regression.linear_model import RegressionResultsWrapper |
| 15 | +from statsmodels.tools.sm_exceptions import PerfectSeparationError |
| 16 | +from lifelines import CoxPHFitter |
| 17 | + |
| 18 | +from causal_testing.specification.variable import Variable |
| 19 | +from causal_testing.specification.capabilities import TreatmentSequence, Capability |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class Estimator(ABC): |
| 25 | + # pylint: disable=too-many-instance-attributes |
| 26 | + """An estimator contains all of the information necessary to compute a causal estimate for the effect of changing |
| 27 | + a set of treatment variables to a set of values. |
| 28 | +
|
| 29 | + All estimators must implement the following two methods: |
| 30 | +
|
| 31 | + 1) add_modelling_assumptions: The validity of a model-assisted causal inference result depends on whether |
| 32 | + the modelling assumptions imposed by a model actually hold. Therefore, for each model, is important to state |
| 33 | + the modelling assumption upon which the validity of the results depend. To achieve this, the estimator object |
| 34 | + maintains a list of modelling assumptions (as strings). If a user wishes to implement their own estimator, they |
| 35 | + must implement this method and add all assumptions to the list of modelling assumptions. |
| 36 | +
|
| 37 | + 2) estimate_ate: All estimators must be capable of returning the average treatment effect as a minimum. That is, the |
| 38 | + average effect of the intervention (changing treatment from control to treated value) on the outcome of interest |
| 39 | + adjusted for all confounders. |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__( |
| 43 | + # pylint: disable=too-many-arguments |
| 44 | + self, |
| 45 | + treatment: str, |
| 46 | + treatment_value: float, |
| 47 | + control_value: float, |
| 48 | + adjustment_set: set, |
| 49 | + outcome: str, |
| 50 | + df: pd.DataFrame = None, |
| 51 | + effect_modifiers: dict[str:Any] = None, |
| 52 | + alpha: float = 0.05, |
| 53 | + query: str = "", |
| 54 | + ): |
| 55 | + self.treatment = treatment |
| 56 | + self.treatment_value = treatment_value |
| 57 | + self.control_value = control_value |
| 58 | + self.adjustment_set = adjustment_set |
| 59 | + self.outcome = outcome |
| 60 | + self.alpha = alpha |
| 61 | + self.df = df.query(query) if query else df |
| 62 | + |
| 63 | + if effect_modifiers is None: |
| 64 | + self.effect_modifiers = {} |
| 65 | + elif isinstance(effect_modifiers, dict): |
| 66 | + self.effect_modifiers = effect_modifiers |
| 67 | + else: |
| 68 | + raise ValueError(f"Unsupported type for effect_modifiers {effect_modifiers}. Expected iterable") |
| 69 | + self.modelling_assumptions = [] |
| 70 | + if query: |
| 71 | + self.modelling_assumptions.append(query) |
| 72 | + self.add_modelling_assumptions() |
| 73 | + logger.debug("Effect Modifiers: %s", self.effect_modifiers) |
| 74 | + |
| 75 | + @abstractmethod |
| 76 | + def add_modelling_assumptions(self): |
| 77 | + """ |
| 78 | + Add modelling assumptions to the estimator. This is a list of strings which list the modelling assumptions that |
| 79 | + must hold if the resulting causal inference is to be considered valid. |
| 80 | + """ |
| 81 | + |
| 82 | + def compute_confidence_intervals(self) -> list[float, float]: |
| 83 | + """ |
| 84 | + Estimate the 95% Wald confidence intervals for the effect of changing the treatment from control values to |
| 85 | + treatment values on the outcome. |
| 86 | + :return: 95% Wald confidence intervals. |
| 87 | + """ |
0 commit comments