Skip to content

Commit 3d368ef

Browse files
committed
Fix testval of ObservedRV
1 parent af2e68f commit 3d368ef

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pymc3/model.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ def as_tensor(data, name, model, distribution):
915915

916916
if hasattr(data, 'mask'):
917917
from .distributions import NoDistribution
918-
testval = distribution.testval or data.mean().astype(dtype)
918+
testval = distribution.default()
919919
fakedist = NoDistribution.dist(shape=data.mask.sum(), dtype=dtype,
920920
testval=testval, parent_dist=distribution)
921921
missing_values = FreeRV(name=name + '_missing', distribution=fakedist,
@@ -959,13 +959,14 @@ def __init__(self, type=None, owner=None, index=None, name=None, data=None,
959959
data = pandas_to_array(data)
960960
type = TensorType(distribution.dtype, data.shape)
961961

962+
self.observations = data
963+
962964
super(ObservedRV, self).__init__(type, owner, index, name)
963965

964966
if distribution is not None:
965967
data = as_tensor(data, name, model, distribution)
966968

967969
self.missing_values = data.missing_values
968-
969970
self.logp_elemwiset = distribution.logp(data)
970971
self.total_size = total_size
971972
self.model = model

pymc3/tests/test_model.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import pytest
22
from theano import theano, tensor as tt
3+
import numpy as np
4+
import pandas as pd
5+
import numpy.testing as npt
36

47
import pymc3 as pm
58
from pymc3.distributions import HalfCauchy, Normal, transforms
@@ -165,4 +168,14 @@ def test_duplicate_vars():
165168
with pm.Model():
166169
pm.Binomial('a', 10, .5)
167170
pm.Normal('a', transform=transforms.log)
168-
err.match('already exists')
171+
err.match('already exists')
172+
173+
174+
def test_empty_observed():
175+
data = pd.DataFrame(np.ones((2, 3)) / 3)
176+
data.values[:] = np.nan
177+
with pm.Model():
178+
a = pm.Normal('a', observed=data)
179+
npt.assert_allclose(a.tag.test_value, np.zeros((2, 3)))
180+
b = pm.Beta('b', alpha=1, beta=1, observed=data)
181+
npt.assert_allclose(b.tag.test_value, np.ones((2, 3)) / 2)

0 commit comments

Comments
 (0)