Skip to content

Commit 4ea3151

Browse files
authored
Added seeding to draws in tests (#195)
Added seeding to tests that draw from random variables and check the values drawn.
1 parent af900ba commit 4ea3151

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

pymc_experimental/tests/distributions/test_discrete_markov_chain.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def transition_probability_tests(steps, n_states, n_lags, n_draws, atol):
1818
P=pt.as_tensor_variable(P), init_dist=x0, steps=steps, n_lags=n_lags
1919
)
2020

21-
draws = pm.draw(chain, n_draws)
21+
draws = pm.draw(chain, n_draws, random_seed=172)
2222

2323
# Test x0 is uniform over n_states
2424
for i in range(n_lags):

pymc_experimental/tests/model_transform/test_conditioning.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ def test_observe_dims():
9292

9393

9494
def test_do():
95+
rng = np.random.default_rng(seed=435)
9596
with pm.Model() as m_old:
9697
x = pm.Normal("x", 0, 1e-3)
9798
y = pm.Normal("y", x, 1e-3)
9899
z = pm.Normal("z", y + x, 1e-3)
99100

100-
assert -5 < pm.draw(z) < 5
101+
assert -5 < pm.draw(z, random_seed=rng) < 5
101102

102103
m_new = do(m_old, {y: x + 100})
103104

@@ -106,7 +107,7 @@ def test_do():
106107
assert m_new["y"] in m_new.deterministics
107108
assert m_new["z"] in m_new.free_RVs
108109

109-
assert 95 < pm.draw(m_new["z"]) < 105
110+
assert 95 < pm.draw(m_new["z"], random_seed=rng) < 105
110111

111112
# Test two substitutions
112113
with m_old:
@@ -118,10 +119,10 @@ def test_do():
118119
assert m_new["x"] not in m_new.deterministics
119120
assert m_new["z"] in m_new.free_RVs
120121

121-
assert 195 < pm.draw(m_new["z"]) < 205
122+
assert 195 < pm.draw(m_new["z"], random_seed=rng) < 205
122123
with m_new:
123124
pm.set_data({"switch": 0})
124-
assert -5 < pm.draw(m_new["z"]) < 5
125+
assert -5 < pm.draw(m_new["z"], random_seed=rng) < 5
125126

126127

127128
def test_do_posterior_predictive():
@@ -149,22 +150,24 @@ def test_do_posterior_predictive():
149150

150151
@pytest.mark.parametrize("mutable", (False, True))
151152
def test_do_constant(mutable):
153+
rng = np.random.default_rng(seed=122)
152154
with pm.Model() as m:
153155
x = pm.Data("x", 0, mutable=mutable)
154156
y = pm.Normal("y", x, 1e-3)
155157

156158
do_m = do(m, {x: 105})
157-
assert pm.draw(do_m["y"]) > 100
159+
assert pm.draw(do_m["y"], random_seed=rng) > 100
158160

159161

160162
def test_do_deterministic():
163+
rng = np.random.default_rng(seed=435)
161164
with pm.Model() as m:
162165
x = pm.Normal("x", 0, 1e-3)
163166
y = pm.Deterministic("y", x + 105)
164167
z = pm.Normal("z", y, 1e-3)
165168

166169
do_m = do(m, {"z": x - 105})
167-
assert pm.draw(do_m["z"]) < 100
170+
assert pm.draw(do_m["z"], random_seed=rng) < 100
168171

169172

170173
def test_do_dims():

pymc_experimental/tests/utils/test_model_fgraph.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_data(inline_views):
124124
pm.set_data({"x": [100.0, 200.0]}, coords={"test_dim": range(2)})
125125

126126
assert m_new.dim_lengths["test_dim"].eval() == 2
127-
np.testing.assert_array_almost_equal(pm.draw(m_new["x"]), [100.0, 200.0])
127+
np.testing.assert_array_almost_equal(pm.draw(m_new["x"], random_seed=63), [100.0, 200.0])
128128

129129

130130
@pytest.mark.parametrize("inline_views", (False, True))

0 commit comments

Comments
 (0)