Skip to content

Commit

Permalink
Raise SamplingError if model doesn't have free RVs
Browse files Browse the repository at this point in the history
  • Loading branch information
stestoll committed Aug 19, 2021
1 parent 4b57c2f commit 09d5d73
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions pymc3/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def sample(
of completion, the sampling speed in samples per second (SPS), and the estimated remaining
time until completion ("expected time of arrival"; ETA).
model : Model (optional if in ``with`` context)
Model to sample from. The model needs to have free random variables.
random_seed : int or list of ints
Random seed(s) used by the sampling steps. A list is accepted if
``cores`` is greater than one.
Expand Down Expand Up @@ -435,6 +436,11 @@ def sample(
p 0.609 0.047 0.528 0.699
"""
model = modelcontext(model)
if not model.free_RVs:
raise SamplingError(
"Cannot sample from the model, since the model does not contain any free variables."
)

start = deepcopy(start)
model_initial_point = model.initial_point
if start is None:
Expand Down Expand Up @@ -493,9 +499,6 @@ def sample(

draws += tune

if not model.free_RVs:
raise ValueError("The model does not contain any free variables.")

if step is None and init is not None and all_continuous(model.value_vars):
try:
# By default, try to use NUTS
Expand Down
2 changes: 1 addition & 1 deletion pymc3/tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def test_sample_find_MAP_does_not_modify_start():
def test_empty_model():
with pm.Model():
pm.Normal("a", observed=1)
with pytest.raises(ValueError) as error:
with pytest.raises(SamplingError) as error:
pm.sample()
error.match("any free variables")

Expand Down

0 comments on commit 09d5d73

Please sign in to comment.