@@ -67,7 +67,7 @@ def build_model(self):
67
67
P -= P .mean ()
68
68
P ["1" ] = 1
69
69
70
- with pm .Model () as model :
70
+ with pm .Model (rng_seeder = self . get_random_state () ) as model :
71
71
effects = pm .Normal ("effects" , mu = 0 , sigma = 100 , size = len (P .columns ))
72
72
logit_p = at .dot (floatX (np .array (P )), effects )
73
73
pm .Bernoulli ("s" , logit_p = logit_p , observed = floatX (data .switch .values ))
@@ -89,7 +89,7 @@ def build_model(self):
89
89
floor = data .floor .to_numpy ()
90
90
group = data .group .to_numpy ()
91
91
92
- with pm .Model () as model :
92
+ with pm .Model (rng_seeder = self . get_random_state () ) as model :
93
93
groupmean = pm .Normal ("groupmean" , 0 , 10.0 ** - 2.0 )
94
94
groupsd = pm .Uniform ("groupsd" , 0 , 10.0 )
95
95
sd = pm .Uniform ("sd" , 0 , 10.0 )
@@ -126,7 +126,7 @@ def build_model(self):
126
126
group = data .group .to_numpy ()
127
127
ufull = data .Uppm .to_numpy ()
128
128
129
- with pm .Model () as model :
129
+ with pm .Model (rng_seeder = self . get_random_state () ) as model :
130
130
groupmean = pm .Normal ("groupmean" , 0 , 10.0 ** - 2.0 )
131
131
groupsd = pm .Uniform ("groupsd" , 0 , 10.0 )
132
132
sd = pm .Uniform ("sd" , 0 , 10.0 )
@@ -163,40 +163,39 @@ def too_slow(self):
163
163
pm .sample (50 , step = step , start = start )
164
164
165
165
166
- def build_disaster_model (masked = False ):
167
- # fmt: off
168
- disasters_data = np .array ([4 , 5 , 4 , 0 , 1 , 4 , 3 , 4 , 0 , 6 , 3 , 3 , 4 , 0 , 2 , 6 ,
169
- 3 , 3 , 5 , 4 , 5 , 3 , 1 , 4 , 4 , 1 , 5 , 5 , 3 , 4 , 2 , 5 ,
170
- 2 , 2 , 3 , 4 , 2 , 1 , 3 , 2 , 2 , 1 , 1 , 1 , 1 , 3 , 0 , 0 ,
171
- 1 , 0 , 1 , 1 , 0 , 0 , 3 , 1 , 0 , 3 , 2 , 2 , 0 , 1 , 1 , 1 ,
172
- 0 , 1 , 0 , 1 , 0 , 0 , 0 , 2 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 2 ,
173
- 3 , 3 , 1 , 1 , 2 , 1 , 1 , 1 , 1 , 2 , 4 , 2 , 0 , 0 , 1 , 4 ,
174
- 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 ])
175
- # fmt: on
176
- if masked :
177
- disasters_data [[23 , 68 ]] = - 1
178
- disasters_data = np .ma .masked_values (disasters_data , value = - 1 )
179
- years = len (disasters_data )
180
-
181
- with pm .Model () as model :
182
- # Prior for distribution of switchpoint location
183
- switchpoint = pm .DiscreteUniform ("switchpoint" , lower = 0 , upper = years )
184
- # Priors for pre- and post-switch mean number of disasters
185
- early_mean = pm .Exponential ("early_mean" , lam = 1.0 )
186
- late_mean = pm .Exponential ("late_mean" , lam = 1.0 )
187
- # Allocate appropriate Poisson rates to years before and after current
188
- # switchpoint location
189
- idx = np .arange (years )
190
- rate = at .switch (switchpoint >= idx , early_mean , late_mean )
191
- # Data likelihood
192
- pm .Poisson ("disasters" , rate , observed = disasters_data )
193
- return model
194
-
195
-
196
166
class TestDisasterModel (SeededTest ):
167
+ def build_model (self , masked = False ):
168
+ # fmt: off
169
+ disasters_data = np .array ([4 , 5 , 4 , 0 , 1 , 4 , 3 , 4 , 0 , 6 , 3 , 3 , 4 , 0 , 2 , 6 ,
170
+ 3 , 3 , 5 , 4 , 5 , 3 , 1 , 4 , 4 , 1 , 5 , 5 , 3 , 4 , 2 , 5 ,
171
+ 2 , 2 , 3 , 4 , 2 , 1 , 3 , 2 , 2 , 1 , 1 , 1 , 1 , 3 , 0 , 0 ,
172
+ 1 , 0 , 1 , 1 , 0 , 0 , 3 , 1 , 0 , 3 , 2 , 2 , 0 , 1 , 1 , 1 ,
173
+ 0 , 1 , 0 , 1 , 0 , 0 , 0 , 2 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 2 ,
174
+ 3 , 3 , 1 , 1 , 2 , 1 , 1 , 1 , 1 , 2 , 4 , 2 , 0 , 0 , 1 , 4 ,
175
+ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 ])
176
+ # fmt: on
177
+ if masked :
178
+ disasters_data [[23 , 68 ]] = - 1
179
+ disasters_data = np .ma .masked_values (disasters_data , value = - 1 )
180
+ years = len (disasters_data )
181
+
182
+ with pm .Model (rng_seeder = self .get_random_state ()) as model :
183
+ # Prior for distribution of switchpoint location
184
+ switchpoint = pm .DiscreteUniform ("switchpoint" , lower = 0 , upper = years )
185
+ # Priors for pre- and post-switch mean number of disasters
186
+ early_mean = pm .Exponential ("early_mean" , lam = 1.0 )
187
+ late_mean = pm .Exponential ("late_mean" , lam = 1.0 )
188
+ # Allocate appropriate Poisson rates to years before and after current
189
+ # switchpoint location
190
+ idx = np .arange (years )
191
+ rate = at .switch (switchpoint >= idx , early_mean , late_mean )
192
+ # Data likelihood
193
+ pm .Poisson ("disasters" , rate , observed = disasters_data )
194
+ return model
195
+
197
196
# Time series of recorded coal mining disasters in the UK from 1851 to 1962
198
197
def test_disaster_model (self ):
199
- model = build_disaster_model (masked = False )
198
+ model = self . build_model (masked = False )
200
199
with model :
201
200
# Initial values for stochastic nodes
202
201
start = {"early_mean" : 2 , "late_mean" : 3.0 }
@@ -206,7 +205,7 @@ def test_disaster_model(self):
206
205
az .summary (idata )
207
206
208
207
def test_disaster_model_missing (self ):
209
- model = build_disaster_model (masked = True )
208
+ model = self . build_model (masked = True )
210
209
with model :
211
210
# Initial values for stochastic nodes
212
211
start = {"early_mean" : 2.0 , "late_mean" : 3.0 }
@@ -251,7 +250,7 @@ def setup_method(self):
251
250
self .y = ((np .random .random (n ) < pi ) * np .random .poisson (lam = theta , size = n )).astype ("int16" )
252
251
253
252
def build_model (self ):
254
- with pm .Model () as model :
253
+ with pm .Model (rng_seeder = self . get_random_state () ) as model :
255
254
# Estimated occupancy
256
255
psi = pm .Beta ("psi" , 1 , 1 )
257
256
# Latent variable for occupancy
@@ -299,7 +298,7 @@ def build_model(self):
299
298
amman_prop = 0.35
300
299
# infant RSV cases in Al Bashir hostpital
301
300
rsv_cases = np .array ([40 , 59 , 65 ])
302
- with pm .Model () as model :
301
+ with pm .Model (rng_seeder = self . get_random_state () ) as model :
303
302
# Al Bashir hospital market share
304
303
market_share = pm .Uniform ("market_share" , 0.5 , 0.6 )
305
304
# Number of 1 y.o. in Amman
@@ -333,19 +332,19 @@ def build_models(self):
333
332
true_mean = 11.0
334
333
y = np .array ([true_mean ])
335
334
336
- with pm .Model () as model_coarse_0 :
335
+ with pm .Model (rng_seeder = self . get_random_state () ) as model_coarse_0 :
337
336
sigma = 1.0
338
337
x_coeff = pm .Normal ("x" , true_mean , sigma = 10.0 )
339
338
pm .Normal ("y" , mu = x_coeff , sigma = sigma , observed = y + 1.0 )
340
339
341
- with pm .Model () as model_coarse_1 :
340
+ with pm .Model (rng_seeder = self . get_random_state () ) as model_coarse_1 :
342
341
sigma = 1.0
343
342
x_coeff = pm .Normal ("x" , true_mean , sigma = 10.0 )
344
343
pm .Normal ("y" , mu = x_coeff , sigma = sigma , observed = y + 0.5 )
345
344
346
345
coarse_models = [model_coarse_0 , model_coarse_1 ]
347
346
348
- with pm .Model () as model :
347
+ with pm .Model (rng_seeder = self . get_random_state () ) as model :
349
348
sigma = 1.0
350
349
x_coeff = pm .Normal ("x" , true_mean , sigma = 10.0 )
351
350
pm .Normal ("y" , mu = x_coeff , sigma = sigma , observed = y )
0 commit comments