1

I am trying to create an observed variable in PyMC3 which has a multivariate normal distribution. I want the covariance matrix whose elements are other random variables. As an example, consider the following code:

import pymc3 as pm

with pm.Model() as model:
  a = pm.Normal('a', mu=0, sigma=10)
  b = pm.Normal('a', mu=0, sigma=10)
  c = pm.Normal('a', mu=0, sigma=10)

  # I recognize that the matrix is not positive definite
  # with this parameterization. This is just a toy example.
  # The main point is that I want the elements of the matrix
  # to be random variables.
  cov = [[a, b]
         [b, c]]

  data = pm.MvNormal('data', mu=[0, 0],
                       cov=cov, observed=obs)

This does not work. Neither does using np.array(cov). I imagine the solution is to use Theano tensors somehow. I am unable to figure out how to use them.

I'd appreciate any help with this. Thanks.

awareeye
  • 3,051
  • 3
  • 22
  • 22
  • 1
    The values of the covariance matrix can come from a distribution, see how to define it here: https://docs.pymc.io/api/distributions/multivariate.html. Also this SO question: https://stackoverflow.com/questions/53222664/pymc3-passing-stochastic-covariance-matrix-to-pm-mvnormal has a worked out example. Something that's not clear from the question is whether you want the covariance matrix to be constructed from multiple independent random variables? – LeoC Apr 09 '20 at 16:45
  • I second @balleveryday's comment, specifically, the LKJ is the standard prior on the Cholesky (a numerically efficient decomposition of PSD matrices) and PyMC3 provides for this in its API, as demonstrated in the other answer. [The Stan manual covers some details](https://mc-stan.org/docs/2_23/stan-users-guide/multivariate-hierarchical-priors-section.html), plus references. – merv Apr 27 '20 at 05:35

0 Answers0