Skip to content

Commit db29b5b

Browse files
committedJan 5, 2024
add tests
1 parent a21f659 commit db29b5b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
 

‎causalpy/tests/test_pymc_models.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,38 @@ def test_idata_property():
123123
)
124124
assert hasattr(result, "idata")
125125
assert isinstance(result.idata, az.InferenceData)
126+
127+
128+
def test_result_reproducibility():
129+
"""Test that we can reproduce the results from the model. We could in theory test
130+
this with all the model and experiment types, but what is being targetted is
131+
the ModelBuilder.fit method, so we should be safe testing with just one model. Here
132+
we use the DifferenceInDifferences experiment class."""
133+
# Load the data
134+
df = cp.load_data("did")
135+
# Set a random seed
136+
sample_kwargs["random_seed"] = 42
137+
# Calculate the result twice
138+
result1 = cp.pymc_experiments.DifferenceInDifferences(
139+
df,
140+
formula="y ~ 1 + group + t + group:post_treatment",
141+
time_variable_name="t",
142+
group_variable_name="group",
143+
treated=1,
144+
untreated=0,
145+
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
146+
)
147+
result2 = cp.pymc_experiments.DifferenceInDifferences(
148+
df,
149+
formula="y ~ 1 + group + t + group:post_treatment",
150+
time_variable_name="t",
151+
group_variable_name="group",
152+
treated=1,
153+
untreated=0,
154+
model=cp.pymc_models.LinearRegression(sample_kwargs=sample_kwargs),
155+
)
156+
assert np.all(result1.idata.posterior.mu == result2.idata.posterior.mu)
157+
assert np.all(result1.idata.prior.mu == result2.idata.prior.mu)
158+
assert np.all(
159+
result1.idata.prior_predictive.y_hat == result2.idata.prior_predictive.y_hat
160+
)

0 commit comments

Comments
 (0)
Please sign in to comment.