# Best Practices for Priors and Flattened 3D Matrices

Hello all.

I have a scenario where I have a 3D matrix comprising measurement data which varies with frequency (frequency being the 3rd dimension). Ultimately, I flatten this matrix into a vector to compare to my likelihood function.

My priors, which could be many, and easily more than 10 in number, influence the matrix.

I have checked the code by finding the maximum a posterior and the expected inference values (correct for my toy model) are returned. The NUTS sampling is very slow though and I wondered if the compact forming of the priors as shown below

``````with pm.Model() as model:
# Priors for unknown model parameters
k = pm.Uniform("k", 1000, 50000, shape=(10,))
k_vals = pm.Deterministic("k_vals", k)

``````

would be slower than declaring them individually, eg

``````with pm.Model() as model:
# Priors for unknown model parameters
k1 = pm.Uniform("k1", 1000, 50000)
k2 = pm.Uniform("k2", 1000, 50000)
k3 = pm.Uniform("k3", 1000, 50000)
# etc.....
``````

Is there a â€ścorrectâ€ť way more appropriate than others?

It shouldnâ€™t make a difference for the sampler. Generally this form is easier to work with, it involves less manually relabeling things whenever you want to make a change to the model:

1 Like

The batched version (shape=10) can be considerably faster as it produces vectorized logp and gradient code so itâ€™s strongly suggested over separating the terms.

Itâ€™s not just a convenience

2 Likes

Ok cool, thanks to both of you for the replies.

Noted on the use of `size`. Should this also be reflected in the number of chains i.e is it good practice to have a sufficient number of chains related to the number of `k`â€™s?

Those are unrelated. Each chain has to contain every k you are trying to estimate. So your modeling decisions and your sampler decisions are (generally) independent.

2 Likes

OK thank you very much for this. Really useful to know and clearly, I have some bottlenecks occurring elsewhere!

1 Like