Skip to content

Commit

Permalink
improve manual type checking sections in the experiment modules
Browse files Browse the repository at this point in the history
  • Loading branch information
drbenvincent committed Jul 2, 2024
1 parent 0af9bfb commit 28f3b07
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
20 changes: 10 additions & 10 deletions causalpy/expt_diff_in_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from causalpy.data_validation import DiDDataValidator
from causalpy.experiments import ExperimentalDesign
from causalpy.pymc_models import PyMCModel
from causalpy.skl_models import ScikitLearnModel
from causalpy.utils import convert_to_string


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

# ******** THIS IS SUBOPTIMAL AT THE MOMENT ************************************
# fit model
if isinstance(self.model, PyMCModel):
COORDS = {"coeffs": self.labels, "obs_indx": np.arange(self.X.shape[0])}
self.model.fit(X=self.X, y=self.y, coords=COORDS)
else:
elif isinstance(self.model, ScikitLearnModel):
self.model.fit(X=self.X, y=self.y)
# ******************************************************************************
else:
raise ValueError("Model type not recognized")

Check warning on line 96 in causalpy/expt_diff_in_diff.py

View check run for this annotation

Codecov / codecov/patch

causalpy/expt_diff_in_diff.py#L96

Added line #L96 was not covered by tests

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

# ******** THIS IS SUBOPTIMAL AT THE MOMENT ************************************
# calculate causal impact
if isinstance(self.model, PyMCModel):
# calculate causal impact &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
# This is the coefficient on the interaction term
coeff_names = self.model.idata.posterior.coords["coeffs"].data
for i, label in enumerate(coeff_names):
if "post_treatment" in label and self.group_variable_name in label:
self.causal_impact = self.model.idata.posterior["beta"].isel(
{"coeffs": i}
)
# &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
else:
# calculate causal impact
elif isinstance(self.model, ScikitLearnModel):
# This is the coefficient on the interaction term
# TODO: THIS IS NOT YET CORRECT
# TODO: THIS IS NOT YET CORRECT ?????
self.causal_impact = (
self.y_pred_treatment[1] - self.y_pred_counterfactual[0]
)[0]
# ******************************************************************************
else:
raise ValueError("Model type not recognized")

Check warning on line 172 in causalpy/expt_diff_in_diff.py

View check run for this annotation

Codecov / codecov/patch

causalpy/expt_diff_in_diff.py#L172

Added line #L172 was not covered by tests

def plot(self, round_to=None):
"""
Expand Down
7 changes: 4 additions & 3 deletions causalpy/expt_prepostfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from causalpy.data_validation import PrePostFitDataValidator
from causalpy.experiments import ExperimentalDesign
from causalpy.pymc_models import PyMCModel
from causalpy.skl_models import ScikitLearnModel


class PrePostFit(ExperimentalDesign, PrePostFitDataValidator):
Expand Down Expand Up @@ -62,13 +63,13 @@ def __init__(
self.post_y = np.asarray(new_y)

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

Check warning on line 72 in causalpy/expt_prepostfit.py

View check run for this annotation

Codecov / codecov/patch

causalpy/expt_prepostfit.py#L72

Added line #L72 was not covered by tests

# score the goodness of fit to the pre-intervention data
self.score = self.model.score(X=self.pre_X, y=self.pre_y)
Expand Down
7 changes: 4 additions & 3 deletions causalpy/expt_prepostnegd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from causalpy.data_validation import PrePostNEGDDataValidator
from causalpy.experiments import ExperimentalDesign
from causalpy.pymc_models import PyMCModel
from causalpy.skl_models import ScikitLearnModel
from causalpy.utils import round_num


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

# fit the model to the observed (pre-intervention) data
# ******** THIS IS SUBOPTIMAL AT THE MOMENT ************************************
if isinstance(self.model, PyMCModel):
COORDS = {"coeffs": self.labels, "obs_indx": np.arange(self.X.shape[0])}
self.model.fit(X=self.X, y=self.y, coords=COORDS)
else:
elif isinstance(self.model, ScikitLearnModel):
raise NotImplementedError("Not implemented for OLS model")

Check warning on line 102 in causalpy/expt_prepostnegd.py

View check run for this annotation

Codecov / codecov/patch

causalpy/expt_prepostnegd.py#L101-L102

Added lines #L101 - L102 were not covered by tests
# ******************************************************************************
else:
raise ValueError("Model type not recognized")

Check warning on line 104 in causalpy/expt_prepostnegd.py

View check run for this annotation

Codecov / codecov/patch

causalpy/expt_prepostnegd.py#L104

Added line #L104 was not covered by tests

# Calculate the posterior predictive for the treatment and control for an
# interpolated set of pretest values
Expand Down
8 changes: 5 additions & 3 deletions causalpy/expt_regression_discontinuity.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from causalpy.data_validation import RDDataValidator
from causalpy.experiments import ExperimentalDesign
from causalpy.pymc_models import PyMCModel
from causalpy.skl_models import ScikitLearnModel
from causalpy.utils import convert_to_string


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

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

Check warning on line 117 in causalpy/expt_regression_discontinuity.py

View check run for this annotation

Codecov / codecov/patch

causalpy/expt_regression_discontinuity.py#L117

Added line #L117 was not covered by tests

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

0 comments on commit 28f3b07

Please sign in to comment.