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