Concatenate constant to random variable vector

In my model I have a vector that has constant last element and remaining of the elements are RVs. I tried to define it using

a = [pm.Flat("a",shape=n-1),1]

OR

a = at.concatenate([pm.Flat("a",shape=n-1),[1]],axis=0)

But neither works. Any suggestion of how to correctly do the concatenation?
Thank you!

1 Like

The first approach produces a Python list (which is probably not what you want). I’m not sure exact what the second does, but the Python list ([1]) probably doesn’t play well with the tensor. @ricardoV94 would know more, but you might pack the constant value into a pm.ConstantData object? See here and here. Just a guess.

Thank you @cluhmann. Will pack. Still not sure how to concatenate two together. @ricardoV94 any help would be appreciated.

I think the issue isn’t with concatenating; it’s the flat prior. Can you tell me what error you get? Also, in practice, a very wide normal or a uniform prior typically works just as well. Outside of some unusual circumstances, it’s generally a good idea to avoid using flat priors.

I believe your code works well for concatenating with other priors. For reference, here’s an example colab and gist showing the syntax for doing the concatenation in 1 and 2 dimensions.

1 Like

There is nothing wrong with your code. You can convince yourself the operation works as expected like this:

import pymc as pm
n = 5
a = pm.math.concatenate([pm.Flat.dist(shape=n-1),[1]],axis=0)
a.shape.eval()  # array([5])

Just note that you can’t do prior or posterior predictive from Flat (or HalfFlat) variables. That may be why you are getting errors.

4 Likes

Thank you all. Not sure why I was getting an error, it all works now!

1 Like