Ryan
Ryan

Reputation: 53

Pymc3 multi-dimensional variable equation

I am trying to use multi-dimensional variables in pymc3 to describe a system of 4 variables that share a common factor. Perhaps I am thinking about this wrong, but when I setup the model I continually get errors from pymc3 about input dimension mismatch. I have tried several variations to no avail. How can I use pymc3's shape argument to describe a system of 4 observed variables with a structure that shares a common observed variable?

import pymc3
import numpy

# Simulate some data
shared_obs = numpy.random.normal(0., 0.25, 1000)
indiv_obs = numpy.random.normal(0.1, 0.5, 1000)

# Build a model
with pymc3.Model():
    shared_mu = pymc3.Uniform('shared_mu', -0.5, 0.5)
    shared_sigma = pymc3.Lognormal('shared_sigma', 0.1, 1.)
    shared = pymc3.Normal('shared', mu=shared_mu, sd=shared_sigma, observed=shared_obs)
    a = pymc3.Uniform('a', -1., 1., shape=4)
    B = pymc3.Uniform('B', -1., 1., shape=4)
    sigma = pymc3.Lognormal('sigma', 0.1, 1., shape=4)
    indiv = pymc3.Normal('indiv', mu=a + B*shared, sd = sigma, observed=indiv_obs, shape=4)

ValueError Traceback (most recent call last) in () 6 B = pymc3.Uniform('B', -1., 1., shape=4) 7 sigma = pymc3.Lognormal('sigma', 0.1, 1., shape=4) ----> 8 indiv = pymc3.Normal('indiv', mu=a + B*shared, sd = sigma, observed=indiv_obs, shape=4)

C:\Program Files\Anaconda3\lib\site-packages\theano\tensor\var.py in mul(self, other) 160 # and the return value in that case 161 try: --> 162 return theano.tensor.mul(self, other) 163 except (NotImplementedError, AsTensorError): 164 return NotImplemented

C:\Program Files\Anaconda3\lib\site-packages\theano\gof\op.py in call(self, *inputs, **kwargs) 666 thunk.outputs = [storage_map[v] for v in node.outputs] 667 --> 668 required = thunk() 669 assert not required # We provided all inputs 670

C:\Program Files\Anaconda3\lib\site-packages\theano\gof\op.py in rval() 881 882 def rval(): --> 883 fill_storage() 884 for o in node.outputs: 885 compute_map[o][0] = True

C:\Program Files\Anaconda3\lib\site-packages\theano\gof\cc.py in call(self) 1705 print(self.error_storage, file=sys.stderr) 1706 raise -> 1707 reraise(exc_type, exc_value, exc_trace) 1708 1709

C:\Program Files\Anaconda3\lib\site-packages\six.py in reraise(tp, value, tb) 684 if value.traceback is not tb: 685 raise value.with_traceback(tb) --> 686 raise value 687 688 else:

ValueError: Input dimension mis-match. (input[0].shape[0] = 4, input[1].shape[0] = 1000)

Upvotes: 0

Views: 1340

Answers (2)

Benjamin Doughty
Benjamin Doughty

Reputation: 475

I got something similar to work by using theano.shared.stack(). Basically, I stacked a bunch of copies of my data (making sure to set the axis right) and then multiplied by the parameters I needed. My command was

mu_alphas = pm.Normal('mu_alphas', 0, 5, shape=3)
mu_betas = pm.Normal('mu_betas', 0, 5, shape=3)
mu = pm.Deterministic('mu', mu_alphas + mu_betas * tt.stack([df.x, df.x, df.x], axis=1))

where df.x is (1000,). Hope something similar can work for you.

Upvotes: 0

aseyboldt
aseyboldt

Reputation: 1090

* is element-wise multiplication. If you want the outer product you can use

B[None, :] * shared[:, None]

Upvotes: 0

Related Questions