Skip to content

Commit d347168

Browse files
aseyboldttwiecki
authored andcommitted
Improve error message for empty models (#2293)
* Improve error message for empty models * Fix model.ndim
1 parent 7aa253e commit d347168

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

pymc3/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def dict_to_array(self):
425425

426426
@property
427427
def ndim(self):
428-
return self.dict_to_array(self.test_point).shape[0]
428+
return sum(var.dsize for var in self.free_RVs)
429429

430430
@property
431431
@memoize

pymc3/sampling.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ def sample(draws=500, step=None, init='auto', n_init=200000, start=None,
228228
raise ValueError("Specify only one of step_kwargs and nuts_kwargs")
229229
step_kwargs = {'nuts': nuts_kwargs}
230230

231+
if model.ndim == 0:
232+
raise ValueError('The model does not contain any free variables.')
233+
231234
if step is None and init is not None and pm.model.all_continuous(model.vars):
232235
# By default, use NUTS sampler
233236
pm._log.info('Auto-assigning NUTS sampler...')

pymc3/tests/test_sampling.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,18 @@ def test_sample_tune_len(self):
117117
assert len(trace) == 100
118118

119119

120+
def test_empty_model():
121+
with pm.Model():
122+
pm.Normal('a', observed=1)
123+
with pytest.raises(ValueError) as error:
124+
pm.sample()
125+
error.match('any free variables')
126+
127+
120128
class TestSoftUpdate(SeededTest):
121129
def setup_method(self):
122130
super(TestSoftUpdate, self).setup_method()
123-
131+
124132
def test_soft_update_all_present(self):
125133
start = {'a': 1, 'b': 2}
126134
test_point = {'a': 3, 'b': 4}

0 commit comments

Comments
 (0)