Skip to content

Commit f96447d

Browse files
committed
Merge branch 'main' into pr/483
2 parents 9aeddb5 + e14d1ac commit f96447d

24 files changed

+855
-1156
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ repos:
2525
exclude: &exclude_pattern 'iv_weak_instruments.ipynb'
2626
args: ["--maxkb=1500"]
2727
- repo: https://github.com/astral-sh/ruff-pre-commit
28-
rev: v0.11.11
28+
rev: v0.11.12
2929
hooks:
3030
# Run the linter
3131
- id: ruff

causalpy/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
# limitations under the License.
1414
import arviz as az
1515

16-
import causalpy.pymc_experiments as pymc_experiments # to be deprecated
1716
import causalpy.pymc_models as pymc_models
18-
import causalpy.skl_experiments as skl_experiments # to be deprecated
1917
import causalpy.skl_models as skl_models
2018
from causalpy.skl_models import create_causalpy_compatible_class
2119
from causalpy.version import __version__
@@ -41,11 +39,9 @@
4139
"InversePropensityWeighting",
4240
"load_data",
4341
"PrePostNEGD",
44-
"pymc_experiments", # to be deprecated
4542
"pymc_models",
4643
"RegressionDiscontinuity",
4744
"RegressionKink",
48-
"skl_experiments", # to be deprecated
4945
"skl_models",
5046
"SyntheticControl",
5147
]

causalpy/experiments/diff_in_diff.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import numpy as np
2020
import pandas as pd
2121
import seaborn as sns
22+
import xarray as xr
2223
from matplotlib import pyplot as plt
2324
from patsy import build_design_matrices, dmatrices
2425
from sklearn.base import RegressorMixin
@@ -87,7 +88,8 @@ def __init__(
8788
**kwargs,
8889
) -> None:
8990
super().__init__(model=model)
90-
91+
# rename the index to "obs_ind"
92+
data.index.name = "obs_ind"
9193
self.data = data
9294
self.expt_type = "Difference in Differences"
9395
self.formula = formula
@@ -102,6 +104,21 @@ def __init__(
102104
self.y, self.X = np.asarray(y), np.asarray(X)
103105
self.outcome_variable_name = y.design_info.column_names[0]
104106

107+
# turn into xarray.DataArray's
108+
self.X = xr.DataArray(
109+
self.X,
110+
dims=["obs_ind", "coeffs"],
111+
coords={
112+
"obs_ind": np.arange(self.X.shape[0]),
113+
"coeffs": self.labels,
114+
},
115+
)
116+
self.y = xr.DataArray(
117+
self.y[:, 0],
118+
dims=["obs_ind"],
119+
coords={"obs_ind": np.arange(self.y.shape[0])},
120+
)
121+
105122
# fit model
106123
if isinstance(self.model, PyMCModel):
107124
COORDS = {"coeffs": self.labels, "obs_ind": np.arange(self.X.shape[0])}
@@ -183,13 +200,15 @@ def __init__(
183200
)
184201
elif isinstance(self.model, RegressorMixin):
185202
# This is the coefficient on the interaction term
186-
# TODO: THIS IS NOT YET CORRECT ?????
203+
# TODO: CHECK FOR CORRECTNESS
187204
self.causal_impact = (
188205
self.y_pred_treatment[1] - self.y_pred_counterfactual[0]
189-
)[0]
206+
)
190207
else:
191208
raise ValueError("Model type not recognized")
192209

210+
return
211+
193212
def input_validation(self):
194213
"""Validate the input data and model formula for correctness"""
195214
if "post_treatment" not in self.formula:

causalpy/experiments/interrupted_time_series.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import arviz as az
2121
import numpy as np
2222
import pandas as pd
23+
import xarray as xr
2324
from matplotlib import pyplot as plt
2425
from patsy import build_design_matrices, dmatrices
2526
from sklearn.base import RegressorMixin
@@ -84,6 +85,8 @@ def __init__(
8485
**kwargs,
8586
) -> None:
8687
super().__init__(model=model)
88+
# rename the index to "obs_ind"
89+
data.index.name = "obs_ind"
8790
self.input_validation(data, treatment_time)
8891
self.treatment_time = treatment_time
8992
# set experiment type - usually done in subclasses
@@ -107,6 +110,33 @@ def __init__(
107110
)
108111
self.post_X = np.asarray(new_x)
109112
self.post_y = np.asarray(new_y)
113+
# turn into xarray.DataArray's
114+
self.pre_X = xr.DataArray(
115+
self.pre_X,
116+
dims=["obs_ind", "coeffs"],
117+
coords={
118+
"obs_ind": self.datapre.index,
119+
"coeffs": self.labels,
120+
},
121+
)
122+
self.pre_y = xr.DataArray(
123+
self.pre_y[:, 0],
124+
dims=["obs_ind"],
125+
coords={"obs_ind": self.datapre.index},
126+
)
127+
self.post_X = xr.DataArray(
128+
self.post_X,
129+
dims=["obs_ind", "coeffs"],
130+
coords={
131+
"obs_ind": self.datapost.index,
132+
"coeffs": self.labels,
133+
},
134+
)
135+
self.post_y = xr.DataArray(
136+
self.post_y[:, 0],
137+
dims=["obs_ind"],
138+
coords={"obs_ind": self.datapost.index},
139+
)
110140

111141
# fit the model to the observed (pre-intervention) data
112142
if isinstance(self.model, PyMCModel):
@@ -125,10 +155,8 @@ def __init__(
125155

126156
# calculate the counterfactual
127157
self.post_pred = self.model.predict(X=self.post_X)
128-
self.pre_impact = self.model.calculate_impact(self.pre_y[:, 0], self.pre_pred)
129-
self.post_impact = self.model.calculate_impact(
130-
self.post_y[:, 0], self.post_pred
131-
)
158+
self.pre_impact = self.model.calculate_impact(self.pre_y, self.pre_pred)
159+
self.post_impact = self.model.calculate_impact(self.post_y, self.post_pred)
132160
self.post_impact_cumulative = self.model.calculate_cumulative_impact(
133161
self.post_impact
134162
)

causalpy/experiments/prepostnegd.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import numpy as np
2222
import pandas as pd
2323
import seaborn as sns
24+
import xarray as xr
2425
from matplotlib import pyplot as plt
2526
from patsy import build_design_matrices, dmatrices
2627
from sklearn.base import RegressorMixin
@@ -111,6 +112,21 @@ def __init__(
111112
self.y, self.X = np.asarray(y), np.asarray(X)
112113
self.outcome_variable_name = y.design_info.column_names[0]
113114

115+
# turn into xarray.DataArray's
116+
self.X = xr.DataArray(
117+
self.X,
118+
dims=["obs_ind", "coeffs"],
119+
coords={
120+
"obs_ind": np.arange(self.X.shape[0]),
121+
"coeffs": self.labels,
122+
},
123+
)
124+
self.y = xr.DataArray(
125+
self.y[:, 0],
126+
dims=["obs_ind"],
127+
coords={"obs_ind": self.data.index},
128+
)
129+
114130
# fit the model to the observed (pre-intervention) data
115131
if isinstance(self.model, PyMCModel):
116132
COORDS = {"coeffs": self.labels, "obs_ind": np.arange(self.X.shape[0])}

causalpy/experiments/regression_discontinuity.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from matplotlib import pyplot as plt
2424
from patsy import build_design_matrices, dmatrices
2525
from sklearn.base import RegressorMixin
26-
26+
import xarray as xr
2727
from causalpy.custom_exceptions import (
2828
DataException,
2929
FormulaException,
@@ -121,6 +121,21 @@ def __init__(
121121
self.y, self.X = np.asarray(y), np.asarray(X)
122122
self.outcome_variable_name = y.design_info.column_names[0]
123123

124+
# turn into xarray.DataArray's
125+
self.X = xr.DataArray(
126+
self.X,
127+
dims=["obs_ind", "coeffs"],
128+
coords={
129+
"obs_ind": np.arange(self.X.shape[0]),
130+
"coeffs": self.labels,
131+
},
132+
)
133+
self.y = xr.DataArray(
134+
self.y[:, 0],
135+
dims=["obs_ind"],
136+
coords={"obs_ind": np.arange(self.y.shape[0])},
137+
)
138+
124139
# fit model
125140
if isinstance(self.model, PyMCModel):
126141
# fit the model to the observed (pre-intervention) data

causalpy/experiments/regression_kink.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import pandas as pd
2424
import seaborn as sns
2525
from patsy import build_design_matrices, dmatrices
26-
26+
import xarray as xr
2727
from causalpy.plot_utils import plot_xY
2828

2929
from .base import BaseExperiment
@@ -84,6 +84,21 @@ def __init__(
8484
self.y, self.X = np.asarray(y), np.asarray(X)
8585
self.outcome_variable_name = y.design_info.column_names[0]
8686

87+
# turn into xarray.DataArray's
88+
self.X = xr.DataArray(
89+
self.X,
90+
dims=["obs_ind", "coeffs"],
91+
coords={
92+
"obs_ind": np.arange(self.X.shape[0]),
93+
"coeffs": self.labels,
94+
},
95+
)
96+
self.y = xr.DataArray(
97+
self.y[:, 0],
98+
dims=["obs_ind"],
99+
coords={"obs_ind": np.arange(self.y.shape[0])},
100+
)
101+
87102
COORDS = {"coeffs": self.labels, "obs_ind": np.arange(self.X.shape[0])}
88103
self.model.fit(X=self.X, y=self.y, coords=COORDS)
89104

0 commit comments

Comments
 (0)