Skip to content

Commit 3e8a000

Browse files
Junpeng Laotwiecki
Junpeng Lao
authored andcommitted
Bug fix #2279 (#2280)
* Bug fix #2279 * add_random_variable for potential and transformed * fix typo * fix test * add test test for duplicate varname
1 parent 6daf11b commit 3e8a000

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

pymc3/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ def Var(self, name, dist, data=None, total_size=None):
539539
name=name,
540540
orig_name=get_transformed_name(name, dist.transform)))
541541
self.deterministics.append(var)
542+
self.add_random_variable(var)
542543
return var
543544
elif isinstance(data, dict):
544545
with self:
@@ -985,7 +986,7 @@ def Deterministic(name, var, model=None):
985986
986987
Returns
987988
-------
988-
n : var but with name name
989+
var : var, with name attribute
989990
"""
990991
model = modelcontext(model)
991992
var.name = model.name_for(name)
@@ -1009,6 +1010,7 @@ def Potential(name, var, model=None):
10091010
model = modelcontext(model)
10101011
var.name = model.name_for(name)
10111012
model.potentials.append(var)
1013+
model.add_random_variable(var)
10121014
return var
10131015

10141016

pymc3/tests/test_model.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import scipy.stats as stats
33
import numpy as np
44
import pymc3 as pm
5-
from pymc3.distributions import HalfCauchy, Normal
5+
from pymc3.distributions import HalfCauchy, Normal, transforms
66
from pymc3 import Potential, Deterministic
77
from pymc3.theanof import generator
88
from .helpers import select_by_precision
@@ -213,3 +213,28 @@ def test_nested(self):
213213
assert theano.config.compute_test_value == 'warn'
214214
assert theano.config.compute_test_value == 'ignore'
215215
assert theano.config.compute_test_value == 'off'
216+
217+
def test_duplicate_vars():
218+
with pytest.raises(ValueError) as err:
219+
with pm.Model():
220+
pm.Normal('a')
221+
pm.Normal('a')
222+
err.match('already exists')
223+
224+
with pytest.raises(ValueError) as err:
225+
with pm.Model():
226+
pm.Normal('a')
227+
pm.Normal('a', transform=transforms.log)
228+
err.match('already exists')
229+
230+
with pytest.raises(ValueError) as err:
231+
with pm.Model():
232+
a = pm.Normal('a')
233+
pm.Potential('a', a**2)
234+
err.match('already exists')
235+
236+
with pytest.raises(ValueError) as err:
237+
with pm.Model():
238+
pm.Binomial('a', 10, .5)
239+
pm.Normal('a', transform=transforms.log)
240+
err.match('already exists')

pymc3/tests/test_models_linear.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def setup_class(cls):
3030

3131
def test_linear_component(self):
3232
vars_to_create = {
33+
'sigma',
3334
'sigma_interval__',
3435
'y_obs',
3536
'lm_x0',
@@ -71,6 +72,7 @@ def test_linear_component_from_formula(self):
7172
def test_glm(self):
7273
with Model() as model:
7374
vars_to_create = {
75+
'glm_sd',
7476
'glm_sd_log__',
7577
'glm_y',
7678
'glm_x0',

0 commit comments

Comments
 (0)