# Stacking priors problem

**URL:** <https://discourse.pymc.io/t/stacking-priors-problem/8384>\
**Category:** Questions\
**Tags:** theano\
**Created:** [November 30, 2021, 11:55am UTC](https://discourse.pymc.io/t/stacking-priors-problem/8384 "2021-11-30T11:55:05Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Chmielar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/chmielar/32/4460_2.png) [@Chmielar](https://discourse.pymc.io/u/Chmielar)\
**Post date:** [December 1, 2021, 7:43pm UTC](https://discourse.pymc.io/t/stacking-priors-problem/8384/2 "2021-12-01T19:43:52Z")

</div>

I don’t like responding to my own messages but I think, I solved the problem described above. For those who may come across similar issue it is worth taking a look at @junpenglao post [here](https://discourse.pymc.io/t/plate-notation-shape-parameter-for-loop/203). I also struggled a bit with theano and shape parameter. There is fantastic description by @lucianopaz [here](https://lucianopaz.github.io/2019/08/19/pymc3-shape-handling/). Finally the code that works for me is as follows:

```auto
     with pm.Model():
        priors = []
        for i, pri in enumerate(pri_inputs):
            if pri[0] == 1:
                priors.append(pm.Uniform("priors_{}".format(i), lower=pri[1], upper=pri[2]))
            elif pri[0] == 2:
                priors.append(pm.TruncatedNormal("priors_{}".format(i), mu=pri[1], sigma=pri[2], lower=pri[3], upper=pri[4]))

        priors = tt.stack(priors)[:, None]
    
        # Define the surrogate models for the outputs using the priors defined above
        linear = sm_linear * priors
        rsm = tt.sum(sm_rsm * priors * priors.T, axis=2)
        out_mu = sm_intercept + tt.sum(linear, axis=0) + tt.sum(rsm, axis=1)

        # Define loglikelihood as Multivariate Normal with defined covariance matrix and observations
        loglikelihood = pm.MvNormal("loglikelihood", mu=out_mu, cov=true_cov, observed=measured)

        # Inference
        step = pm.NUTS() # using NUTS sampling
        trace = pm.sample(draws=samples, step=step, cores=2, progressbar=True)
```

---

_[View the full topic](https://discourse.pymc.io/t/stacking-priors-problem/8384)._
