Hi. Is there a way to group various subsets of random variables into a single vector to use in vector operations? For example, say my model looks something like:
a = pm.Normal(…, shape=4)
b = pm.Normal(…, shape=7)
and I would like to compute something like the dot product of [ a[2], b[1], b[2], b[3], b[4] ] with a 5-element vector x = [x1, x2, x3, x4, x5]. How would I go about doing that?
I have tried doing approximately these two solutions:
So when I try printing the various shapes, I get that the shape of a[2] is [] and the shape of b[1:5] is [4]. For reference, the shape of a is [4] and the shape of b is [7]. It looks like this is just returning the value of the shape parameter in the respective pm.Normal call.
Your suggestion to specify the axis resulted in the error “TypeError: Join() can only join tensors with the same number of dimensions.” for axis = 0, and “IndexError: axis 1 is out of bounds [-1, 1)” for axis = 1.
I tried one more thing. I guessed that this might be happening because a[2] is a scalar and b[1:5] is a vector, so I attempted pm.math.stack( [ a[2:4], b[1:5] ] , axis = 0). This yielded the error “ValueError: all the input array dimensions except for the concatenation axis must match exactly”.
When you index one element from a the result is a scalar, thus the shape error when you are trying to concatenate it with a 1d tensor. You can expand the dimension, or create 2d RVs like
a = pm.Normal(…, shape=(4, 1))
b = pm.Normal(…, shape=(7, 1))
This answer works great, but is there a way to assign vec to its own pymc3 variable in the model, and ignore a and b? I do not need a and b as standalone parameters in the trace, but would like to use vec__0, …, vec__n, instead. That way, I can work with a variable number of terms in an N-d linear model where I want different priors for different terms.