Skip to content

Raise TypeError on non-data values of observed #1872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from theano import function
import theano
from ..memoize import memoize
from ..model import Model, get_named_nodes
from ..model import Model, get_named_nodes, FreeRV, ObservedRV
from ..vartypes import string_types
from .dist_math import bound

Expand All @@ -30,11 +30,13 @@ def __new__(cls, name, *args, **kwargs):

if isinstance(name, string_types):
data = kwargs.pop('observed', None)
if isinstance(data, ObservedRV) or isinstance(data, FreeRV):
raise TypeError("observed needs to be data but got: {}".format(type(data)))
total_size = kwargs.pop('total_size', None)
dist = cls.dist(*args, **kwargs)
return model.Var(name, dist, data, total_size)
else:
raise TypeError("Name needs to be a string but got: %s" % name)
raise TypeError("Name needs to be a string but got: {}".format(name))

def __getnewargs__(self):
return _Unpickling,
Expand Down
6 changes: 6 additions & 0 deletions pymc3/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ def test_model_root(self):
with pm.Model() as sub:
self.assertTrue(model is sub.root)

class TestObserved(unittest.TestCase):
def test_observed_rv_fail(self):
with self.assertRaises(TypeError):
with pm.Model() as model:
x = Normal('x')
Normal('n', observed=x)

class TestScaling(unittest.TestCase):
def test_density_scaling(self):
Expand Down