Skip to content

Commit 7312736

Browse files
authored
Raise SamplingError if model doesn't have free RVs (#4955)
1 parent 4b57c2f commit 7312736

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

pymc3/sampling.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def sample(
337337
of completion, the sampling speed in samples per second (SPS), and the estimated remaining
338338
time until completion ("expected time of arrival"; ETA).
339339
model : Model (optional if in ``with`` context)
340+
Model to sample from. The model needs to have free random variables.
340341
random_seed : int or list of ints
341342
Random seed(s) used by the sampling steps. A list is accepted if
342343
``cores`` is greater than one.
@@ -435,6 +436,11 @@ def sample(
435436
p 0.609 0.047 0.528 0.699
436437
"""
437438
model = modelcontext(model)
439+
if not model.free_RVs:
440+
raise SamplingError(
441+
"Cannot sample from the model, since the model does not contain any free variables."
442+
)
443+
438444
start = deepcopy(start)
439445
model_initial_point = model.initial_point
440446
if start is None:
@@ -493,9 +499,6 @@ def sample(
493499

494500
draws += tune
495501

496-
if not model.free_RVs:
497-
raise ValueError("The model does not contain any free variables.")
498-
499502
if step is None and init is not None and all_continuous(model.value_vars):
500503
try:
501504
# By default, try to use NUTS

pymc3/tests/test_sampling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def test_sample_find_MAP_does_not_modify_start():
323323
def test_empty_model():
324324
with pm.Model():
325325
pm.Normal("a", observed=1)
326-
with pytest.raises(ValueError) as error:
326+
with pytest.raises(SamplingError) as error:
327327
pm.sample()
328328
error.match("any free variables")
329329

0 commit comments

Comments
 (0)