Skip to content

[WIP] Add givens argument to DensityDist #4433

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
55 changes: 32 additions & 23 deletions pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,30 @@ def __new__(cls, name, *args, **kwargs):
if not isinstance(name, string_types):
raise TypeError(f"Name needs to be a string but got: {name}")

data = kwargs.pop("observed", None)
cls.data = data
if isinstance(data, ObservedRV) or isinstance(data, FreeRV):
raise TypeError("observed needs to be data but got: {}".format(type(data)))
observed_data = kwargs.pop("observed", None)
if isinstance(observed_data, ObservedRV) or isinstance(observed_data, FreeRV):
raise TypeError("observed needs to be data but got: {}".format(type(observed_data)))
given_data = kwargs.pop("givens", None)
if given_data is None:
cls.data = observed_data
elif not isinstance(given_data, dict):
raise TypeError(f"givens needs to be of type dict but got: {type(givens)}")
elif observed_data is None:
cls.data = given_data
elif isinstance(observed_data, dict):
intersection = given_data.keys() & observed_data.keys()
if intersection:
raise ValueError(
f"{intersection} keys found in both givens and observed dicts but "
"they can not have repeated keys"
)
cls.data = {**observed_data, **given_data}
else:
raise ValueError(
"If both observed and givens argument are present, observed needs to "
f"be a dict but got: {type(observed_data)}"
)
data = cls.data
total_size = kwargs.pop("total_size", None)

dims = kwargs.pop("dims", None)
Expand All @@ -116,10 +136,14 @@ def __new__(cls, name, *args, **kwargs):

# Some distributions do not accept shape=None
if has_shape or shape is not None:
if "givens" in kwargs:
raise ValueError("givens found")
dist = cls.dist(*args, **kwargs, shape=shape)
else:
if "givens" in kwargs:
raise ValueError("givens found")
dist = cls.dist(*args, **kwargs)
return model.Var(name, dist, data, total_size, dims=dims)
return model.Var(name, dist, data, total_size, dims=dims, givens=given_data)

def __getnewargs__(self):
return (_Unpickling,)
Expand Down Expand Up @@ -358,6 +382,7 @@ def __init__(
logp,
shape=(),
dtype=None,
givens=None,
testval=0,
random=None,
wrap_random_with_dist_shape=True,
Expand All @@ -379,6 +404,8 @@ def __init__(
a value here.
dtype: None, str (Optional)
The dtype of the distribution.
givens : dict, optional
Model variables on which the DensityDist is conditioned.
testval: number or array (Optional)
The ``testval`` of the RV's tensor that follow the ``DensityDist``
distribution.
Expand Down Expand Up @@ -506,24 +533,6 @@ def __init__(
the returned array of samples. It is the user's responsibility to
wrap the callable to make it comply with PyMC3's interpretation
of ``size``.


.. code-block:: python

with pm.Model():
mu = pm.Normal('mu', 0 , 1)
normal_dist = pm.Normal.dist(mu, 1, shape=3)
dens = pm.DensityDist(
'density_dist',
normal_dist.logp,
observed=np.random.randn(100, 3),
shape=3,
random=stats.norm.rvs,
pymc3_size_interpretation=False, # Is True by default
)
prior = pm.sample_prior_predictive(10)['density_dist']
assert prior.shape == (10, 100, 3)

"""
if dtype is None:
dtype = theano.config.floatX
Expand Down
6 changes: 4 additions & 2 deletions pymc3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ def add_coords(self, coords):
else:
self.coords[name] = coords[name]

def Var(self, name, dist, data=None, total_size=None, dims=None):
def Var(self, name, dist, data=None, total_size=None, dims=None, givens=None):
"""Create and add (un)observed random variable to the model with an
appropriate prior distribution.

Expand Down Expand Up @@ -1161,6 +1161,7 @@ def Var(self, name, dist, data=None, total_size=None, dims=None):
var = MultiObservedRV(
name=name,
data=data,
givens=givens,
distribution=dist,
total_size=total_size,
model=self,
Expand Down Expand Up @@ -1834,7 +1835,7 @@ class MultiObservedRV(Factor):
Potentially partially observed.
"""

def __init__(self, name, data, distribution, total_size=None, model=None):
def __init__(self, name, data, distribution, total_size=None, model=None, givens=None):
"""
Parameters
----------
Expand All @@ -1850,6 +1851,7 @@ def __init__(self, name, data, distribution, total_size=None, model=None):
self.data = {
name: as_tensor(data, name, model, distribution) for name, data in data.items()
}
self.givens = givens

self.missing_values = [
datum.missing_values for datum in self.data.values() if datum.missing_values is not None
Expand Down