How to stack same rows of prior distributions inside the with model effectively

Yes - here’s a modified code block that implements the expansion. I don’t have the variable mu_mean so I hardcoded some values in. The basic idea is that we use the row indexing for alpha to pick off the right values. Note that we can repeatedly use the same index and thus repeat a value more than one time.

import pymc3 as pm
import numpy as np
Yogurt_Model = pm.Model()

reshaping_indices = [0]*5 + [1]*2 + [2]*3 + [3]*3

with Yogurt_Model:
    
    mu = pm.MvNormal('mu', mu=np.zeros(3), cov=np.eye(3),shape=3)

    packed_L = pm.LKJCholeskyCov('packed_L', n = 3, eta = 2., sd_dist = pm.HalfCauchy.dist(2.0))
    L = pm.expand_packed_triangular(3,packed_L)

    alpha = pm.MvNormal('alpha', mu = mu, chol = L, shape = (4,3))
    
    alpha_reshaped = pm.Deterministic('alpha_reshaped',alpha[reshaping_indices])
    trace = pm.sample()
   
# Print off the posterior means
print(trace['alpha'].mean(axis=0))
print(trace['alpha_reshaped'].mean(axis=0))
1 Like