Frage

When using pymc 3 , is it possible to assemble univariate random variables into a matrix which is then used as a prior for a multivariate distribution? If so, how can I best go about this?

Here is a specific example. I would like to take three R.V.'s and create a triangle matrix, A, with them:

a11_squared=Gamma(alpha=1, beta=2)
a22_squared=Gamma(alpha=1, beta=2)
a12=Normal(mu=0, tau=1)
a21=0

After some manipulation I would then use this matrix as the prior for the precision parameter in the multivariate normal distribution.

I assume this probably has more to do with operations with tensor variables in theano, so I will add the theano tag as well.

Thank you for your time!

Edit 1: Here is a minimal example of what I am trying to do:

from matplotlib.pylab import *
from pymc import *

cov=np.array([[2,1],[1,3]])
mean=([2,7])
tau=np.linalg.inv(cov)
N=1
z_data=np.ndarray.flatten(np.random.multivariate_normal(mean, cov, N))

def ex_tau(a11_squared, a22_squared, a12):
    ex_A=theano.tensor.stacklists([[theano.tensor.sqrt(a11_squared), a12], [theano.tensor.constant(0, dtype='float64'), theano.tensor.sqrt(a22_squared)]])
    ex_cov=ex_A.T.dot(ex_A)
    return theano.sandbox.linalg.ops.matrix_inverse(ex_cov)     

with Model() as model:    
    a11_squared=Gamma('a11_squared', alpha=1, beta=2)
    a22_squared=Gamma('a22_squared', alpha=1, beta=2)
    a12=Normal('a12', mu=0, tau=1)
    z=MvNormal('z', mu=mean, Tau=ex_tau(a11_squared, a22_squared, a12), shape=2, observed=z_data)

Edit 2: Here is a test to show that ex_tau seems to do the job outside of pymc

from theano.tensor import stacklists, scalars, matrices, sqrt, constant, dot
from theano import function, printing
from theano.sandbox.linalg.ops import matrix_inverse

def ex_tau(a11_squared, a22_squared, a12):
    ex_A=stacklists([[sqrt(a11_squared), a12], [constant(0, dtype='float64'), sqrt(a22_squared)]])
    ex_cov=ex_A.T.dot(ex_A)
    return matrix_inverse(ex_cov)

printing.debugprint(ex_tau(2, 4, -3))
War es hilfreich?

Lösung

Looks like stacklist might do what you want.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top