1010from causal_testing .specification .variable import Variable
1111from causal_testing .estimation .genetic_programming_regression_fitter import GP
1212from causal_testing .estimation .abstract_regression_estimator import RegressionEstimator
13+ from causal_testing .estimation .effect_estimate import EffectEstimate
1314from causal_testing .testing .base_test_case import BaseTestCase
1415
1516logger = logging .getLogger (__name__ )
@@ -92,7 +93,7 @@ def gp_formula(
9293 formula = gp .simplify (formula )
9394 self .formula = f"{ self .base_test_case .outcome_variable .name } ~ I({ formula } ) - 1"
9495
95- def estimate_coefficient (self ) -> tuple [ pd . Series , list [ pd . Series , pd . Series ]] :
96+ def estimate_coefficient (self ) -> EffectEstimate :
9697 """Estimate the unit average treatment effect of the treatment on the outcome. That is, the change in outcome
9798 caused by a unit change in treatment.
9899
@@ -121,9 +122,9 @@ def estimate_coefficient(self) -> tuple[pd.Series, list[pd.Series, pd.Series]]:
121122 ), f"{ treatment } not in\n { ' ' + str (model .params .index ).replace (newline , newline + ' ' )} "
122123 unit_effect = model .params [treatment ] # Unit effect is the coefficient of the treatment
123124 [ci_low , ci_high ] = self ._get_confidence_intervals (model , treatment )
124- return unit_effect , [ ci_low , ci_high ]
125+ return EffectEstimate ( "coefficient" , unit_effect , ci_low , ci_high )
125126
126- def estimate_ate (self ) -> tuple [ pd . Series , list [ pd . Series , pd . Series ]] :
127+ def estimate_ate (self ) -> EffectEstimate :
127128 """Estimate the average treatment effect of the treatment on the outcome. That is, the change in outcome caused
128129 by changing the treatment variable from the control value to the treatment value.
129130
@@ -146,10 +147,10 @@ def estimate_ate(self) -> tuple[pd.Series, list[pd.Series, pd.Series]]:
146147 t_test_results = model .t_test (individuals .loc ["treated" ] - individuals .loc ["control" ])
147148 ate = pd .Series (t_test_results .effect [0 ])
148149 confidence_intervals = list (t_test_results .conf_int (alpha = self .alpha ).flatten ())
149- confidence_intervals = [pd .Series (interval ) for interval in confidence_intervals ]
150- return ate , confidence_intervals
150+ ci_low , ci_high = [pd .Series (interval ) for interval in confidence_intervals ]
151+ return EffectEstimate ( " ate" , ate , ci_low , ci_high )
151152
152- def estimate_risk_ratio (self , adjustment_config : dict = None ) -> tuple [ pd . Series , list [ pd . Series , pd . Series ]] :
153+ def estimate_risk_ratio (self , adjustment_config : dict = None ) -> EffectEstimate :
153154 """Estimate the risk_ratio effect of the treatment on the outcome. That is, the change in outcome caused
154155 by changing the treatment variable from the control value to the treatment value.
155156
@@ -159,9 +160,11 @@ def estimate_risk_ratio(self, adjustment_config: dict = None) -> tuple[pd.Series
159160 control_outcome , treatment_outcome = prediction .iloc [1 ], prediction .iloc [0 ]
160161 ci_low = pd .Series (treatment_outcome ["mean_ci_lower" ] / control_outcome ["mean_ci_upper" ])
161162 ci_high = pd .Series (treatment_outcome ["mean_ci_upper" ] / control_outcome ["mean_ci_lower" ])
162- return pd .Series (treatment_outcome ["mean" ] / control_outcome ["mean" ]), [ci_low , ci_high ]
163+ return EffectEstimate (
164+ "risk_ratio" , pd .Series (treatment_outcome ["mean" ] / control_outcome ["mean" ]), ci_low , ci_high
165+ )
163166
164- def estimate_ate_calculated (self , adjustment_config : dict = None ) -> tuple [ pd . Series , list [ pd . Series , pd . Series ]] :
167+ def estimate_ate_calculated (self , adjustment_config : dict = None ) -> EffectEstimate :
165168 """Estimate the ate effect of the treatment on the outcome. That is, the change in outcome caused
166169 by changing the treatment variable from the control value to the treatment value. Here, we actually
167170 calculate the expected outcomes under control and treatment and divide one by the other. This
@@ -177,7 +180,7 @@ def estimate_ate_calculated(self, adjustment_config: dict = None) -> tuple[pd.Se
177180 control_outcome , treatment_outcome = prediction .iloc [1 ], prediction .iloc [0 ]
178181 ci_low = pd .Series (treatment_outcome ["mean_ci_lower" ] - control_outcome ["mean_ci_upper" ])
179182 ci_high = pd .Series (treatment_outcome ["mean_ci_upper" ] - control_outcome ["mean_ci_lower" ])
180- return pd .Series (treatment_outcome ["mean" ] - control_outcome ["mean" ]), [ ci_low , ci_high ]
183+ return EffectEstimate ( "ate" , pd .Series (treatment_outcome ["mean" ] - control_outcome ["mean" ]), ci_low , ci_high )
181184
182185 def _get_confidence_intervals (self , model , treatment ):
183186 confidence_intervals = model .conf_int (alpha = self .alpha , cols = None )
0 commit comments