Hello!
I’d like to generate a random sample of the multivariate normal distribution inside a model. The idea behind is to pass the generated sample further as observed values for some likelihood.
Code is following:
n = 10000 # sample size
m = 5 # number of covariates
model = pm.Model()
with model:
packed_L = pm.LKJCholeskyCov('packed_L', n=m, eta=2., sd_dist=pm.HalfCauchy.dist(2.5))
L = pm.expand_packed_triangular(m, packed_L)
Sigma = pm.Deterministic('Sigma', L.dot(L.T))
mu = pm.Normal('mu', mu=0., sd=1., shape=m)
Y = pm.MvNormal('Y', mu=mu, chol=L, shape=(n,m))
sample = Y.random()
And I got following error:
KeyError Traceback (most recent call last)
<ipython-input-57-b968c007a358> in <module>()
10 mu = pm.Normal('mu', mu=0., sd=1., shape=m)
11 Y = pm.MvNormal('Y', mu=mu, chol=L, shape=(n,m))
---> 12 sample = Y.random()
13
/usr/local/lib/python2.7/site-packages/pymc3-3.5rc1-py2.7.egg/pymc3/model.pyc in __call__(self, *args, **kwargs)
40
41 def __call__(self, *args, **kwargs):
---> 42 return getattr(self.obj, self.method_name)(*args, **kwargs)
43
44
/usr/local/lib/python2.7/site-packages/pymc3-3.5rc1-py2.7.egg/pymc3/distributions/multivariate.pyc in random(self, point, size)
247 return dist.rvs(size)
248 elif self._cov_type == 'chol':
--> 249 mu, chol = draw_values([self.mu, self.chol_cov], point=point, size=size)
250 if mu.shape[-1] != chol[0].shape[-1]:
251 raise ValueError("Shapes for mu and chol don't match")
/usr/local/lib/python2.7/site-packages/pymc3-3.5rc1-py2.7.egg/pymc3/distributions/distribution.pyc in draw_values(params, point, size)
297 # the stack of nodes to try to draw from. We exclude the
298 # nodes in the `params` list.
--> 299 stack.extend([node for node in named_nodes_parents[next_]
300 if node is not None and
301 node.name not in stored and
KeyError: packed_L
Is it conceptually possible to do like this in the framework of PyMC3?
Thanks,
Best Regards
Alex