Skip to content

Commit 9fd2b51

Browse files
committed
Seed models in test_examples.py
1 parent b14d303 commit 9fd2b51

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

pymc3/tests/test_examples.py

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def build_model(self):
6767
P -= P.mean()
6868
P["1"] = 1
6969

70-
with pm.Model() as model:
70+
with pm.Model(rng_seeder=self.get_random_state()) as model:
7171
effects = pm.Normal("effects", mu=0, sigma=100, size=len(P.columns))
7272
logit_p = at.dot(floatX(np.array(P)), effects)
7373
pm.Bernoulli("s", logit_p=logit_p, observed=floatX(data.switch.values))
@@ -89,7 +89,7 @@ def build_model(self):
8989
floor = data.floor.to_numpy()
9090
group = data.group.to_numpy()
9191

92-
with pm.Model() as model:
92+
with pm.Model(rng_seeder=self.get_random_state()) as model:
9393
groupmean = pm.Normal("groupmean", 0, 10.0 ** -2.0)
9494
groupsd = pm.Uniform("groupsd", 0, 10.0)
9595
sd = pm.Uniform("sd", 0, 10.0)
@@ -126,7 +126,7 @@ def build_model(self):
126126
group = data.group.to_numpy()
127127
ufull = data.Uppm.to_numpy()
128128

129-
with pm.Model() as model:
129+
with pm.Model(rng_seeder=self.get_random_state()) as model:
130130
groupmean = pm.Normal("groupmean", 0, 10.0 ** -2.0)
131131
groupsd = pm.Uniform("groupsd", 0, 10.0)
132132
sd = pm.Uniform("sd", 0, 10.0)
@@ -163,40 +163,39 @@ def too_slow(self):
163163
pm.sample(50, step=step, start=start)
164164

165165

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-
196166
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+
197196
# Time series of recorded coal mining disasters in the UK from 1851 to 1962
198197
def test_disaster_model(self):
199-
model = build_disaster_model(masked=False)
198+
model = self.build_model(masked=False)
200199
with model:
201200
# Initial values for stochastic nodes
202201
start = {"early_mean": 2, "late_mean": 3.0}
@@ -206,7 +205,7 @@ def test_disaster_model(self):
206205
az.summary(idata)
207206

208207
def test_disaster_model_missing(self):
209-
model = build_disaster_model(masked=True)
208+
model = self.build_model(masked=True)
210209
with model:
211210
# Initial values for stochastic nodes
212211
start = {"early_mean": 2.0, "late_mean": 3.0}
@@ -251,7 +250,7 @@ def setup_method(self):
251250
self.y = ((np.random.random(n) < pi) * np.random.poisson(lam=theta, size=n)).astype("int16")
252251

253252
def build_model(self):
254-
with pm.Model() as model:
253+
with pm.Model(rng_seeder=self.get_random_state()) as model:
255254
# Estimated occupancy
256255
psi = pm.Beta("psi", 1, 1)
257256
# Latent variable for occupancy
@@ -299,7 +298,7 @@ def build_model(self):
299298
amman_prop = 0.35
300299
# infant RSV cases in Al Bashir hostpital
301300
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:
303302
# Al Bashir hospital market share
304303
market_share = pm.Uniform("market_share", 0.5, 0.6)
305304
# Number of 1 y.o. in Amman
@@ -333,19 +332,19 @@ def build_models(self):
333332
true_mean = 11.0
334333
y = np.array([true_mean])
335334

336-
with pm.Model() as model_coarse_0:
335+
with pm.Model(rng_seeder=self.get_random_state()) as model_coarse_0:
337336
sigma = 1.0
338337
x_coeff = pm.Normal("x", true_mean, sigma=10.0)
339338
pm.Normal("y", mu=x_coeff, sigma=sigma, observed=y + 1.0)
340339

341-
with pm.Model() as model_coarse_1:
340+
with pm.Model(rng_seeder=self.get_random_state()) as model_coarse_1:
342341
sigma = 1.0
343342
x_coeff = pm.Normal("x", true_mean, sigma=10.0)
344343
pm.Normal("y", mu=x_coeff, sigma=sigma, observed=y + 0.5)
345344

346345
coarse_models = [model_coarse_0, model_coarse_1]
347346

348-
with pm.Model() as model:
347+
with pm.Model(rng_seeder=self.get_random_state()) as model:
349348
sigma = 1.0
350349
x_coeff = pm.Normal("x", true_mean, sigma=10.0)
351350
pm.Normal("y", mu=x_coeff, sigma=sigma, observed=y)

0 commit comments

Comments
 (0)