Skip to content

Commit 28f3b07

Browse files
committed
improve manual type checking sections in the experiment modules
1 parent 0af9bfb commit 28f3b07

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

causalpy/expt_diff_in_diff.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from causalpy.data_validation import DiDDataValidator
2020
from causalpy.experiments import ExperimentalDesign
2121
from causalpy.pymc_models import PyMCModel
22+
from causalpy.skl_models import ScikitLearnModel
2223
from causalpy.utils import convert_to_string
2324

2425

@@ -85,13 +86,14 @@ def __init__(
8586
self.y, self.X = np.asarray(y), np.asarray(X)
8687
self.outcome_variable_name = y.design_info.column_names[0]
8788

88-
# ******** THIS IS SUBOPTIMAL AT THE MOMENT ************************************
89+
# fit model
8990
if isinstance(self.model, PyMCModel):
9091
COORDS = {"coeffs": self.labels, "obs_indx": np.arange(self.X.shape[0])}
9192
self.model.fit(X=self.X, y=self.y, coords=COORDS)
92-
else:
93+
elif isinstance(self.model, ScikitLearnModel):
9394
self.model.fit(X=self.X, y=self.y)
94-
# ******************************************************************************
95+
else:
96+
raise ValueError("Model type not recognized")
9597

9698
# predicted outcome for control group
9799
self.x_pred_control = (
@@ -151,25 +153,23 @@ def __init__(
151153
new_x.iloc[:, i] = 0
152154
self.y_pred_counterfactual = self.model.predict(np.asarray(new_x))
153155

154-
# ******** THIS IS SUBOPTIMAL AT THE MOMENT ************************************
156+
# calculate causal impact
155157
if isinstance(self.model, PyMCModel):
156-
# calculate causal impact &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
157158
# This is the coefficient on the interaction term
158159
coeff_names = self.model.idata.posterior.coords["coeffs"].data
159160
for i, label in enumerate(coeff_names):
160161
if "post_treatment" in label and self.group_variable_name in label:
161162
self.causal_impact = self.model.idata.posterior["beta"].isel(
162163
{"coeffs": i}
163164
)
164-
# &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
165-
else:
166-
# calculate causal impact
165+
elif isinstance(self.model, ScikitLearnModel):
167166
# This is the coefficient on the interaction term
168-
# TODO: THIS IS NOT YET CORRECT
167+
# TODO: THIS IS NOT YET CORRECT ?????
169168
self.causal_impact = (
170169
self.y_pred_treatment[1] - self.y_pred_counterfactual[0]
171170
)[0]
172-
# ******************************************************************************
171+
else:
172+
raise ValueError("Model type not recognized")
173173

174174
def plot(self, round_to=None):
175175
"""

causalpy/expt_prepostfit.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from causalpy.data_validation import PrePostFitDataValidator
2121
from causalpy.experiments import ExperimentalDesign
2222
from causalpy.pymc_models import PyMCModel
23+
from causalpy.skl_models import ScikitLearnModel
2324

2425

2526
class PrePostFit(ExperimentalDesign, PrePostFitDataValidator):
@@ -62,13 +63,13 @@ def __init__(
6263
self.post_y = np.asarray(new_y)
6364

6465
# fit the model to the observed (pre-intervention) data
65-
# ******** THIS IS SUBOPTIMAL AT THE MOMENT ************************************
6666
if isinstance(self.model, PyMCModel):
6767
COORDS = {"coeffs": self.labels, "obs_indx": np.arange(self.pre_X.shape[0])}
6868
self.model.fit(X=self.pre_X, y=self.pre_y, coords=COORDS)
69-
else:
69+
elif isinstance(self.model, ScikitLearnModel):
7070
self.model.fit(X=self.pre_X, y=self.pre_y)
71-
# ******************************************************************************
71+
else:
72+
raise ValueError("Model type not recognized")
7273

7374
# score the goodness of fit to the pre-intervention data
7475
self.score = self.model.score(X=self.pre_X, y=self.pre_y)

causalpy/expt_prepostnegd.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from causalpy.data_validation import PrePostNEGDDataValidator
2020
from causalpy.experiments import ExperimentalDesign
2121
from causalpy.pymc_models import PyMCModel
22+
from causalpy.skl_models import ScikitLearnModel
2223
from causalpy.utils import round_num
2324

2425

@@ -94,13 +95,13 @@ def __init__(
9495
self.outcome_variable_name = y.design_info.column_names[0]
9596

9697
# fit the model to the observed (pre-intervention) data
97-
# ******** THIS IS SUBOPTIMAL AT THE MOMENT ************************************
9898
if isinstance(self.model, PyMCModel):
9999
COORDS = {"coeffs": self.labels, "obs_indx": np.arange(self.X.shape[0])}
100100
self.model.fit(X=self.X, y=self.y, coords=COORDS)
101-
else:
101+
elif isinstance(self.model, ScikitLearnModel):
102102
raise NotImplementedError("Not implemented for OLS model")
103-
# ******************************************************************************
103+
else:
104+
raise ValueError("Model type not recognized")
104105

105106
# Calculate the posterior predictive for the treatment and control for an
106107
# interpolated set of pretest values

causalpy/expt_regression_discontinuity.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from causalpy.data_validation import RDDataValidator
2222
from causalpy.experiments import ExperimentalDesign
2323
from causalpy.pymc_models import PyMCModel
24+
from causalpy.skl_models import ScikitLearnModel
2425
from causalpy.utils import convert_to_string
2526

2627

@@ -105,14 +106,15 @@ def __init__(
105106
self.y, self.X = np.asarray(y), np.asarray(X)
106107
self.outcome_variable_name = y.design_info.column_names[0]
107108

108-
# ******** THIS IS SUBOPTIMAL AT THE MOMENT ************************************
109+
# fit model
109110
if isinstance(self.model, PyMCModel):
110111
# fit the model to the observed (pre-intervention) data
111112
COORDS = {"coeffs": self.labels, "obs_indx": np.arange(self.X.shape[0])}
112113
self.model.fit(X=self.X, y=self.y, coords=COORDS)
113-
else:
114+
elif isinstance(self.model, ScikitLearnModel):
114115
self.model.fit(X=self.X, y=self.y)
115-
# ******************************************************************************
116+
else:
117+
raise ValueError("Model type not recognized")
116118

117119
# score the goodness of fit to all data
118120
self.score = self.model.score(X=self.X, y=self.y)

0 commit comments

Comments
 (0)