Create a a diagonal matrix from Dirchlet distribution PYMC 5.2.0

Hello,

In numpy I can create an array, z, of random values drawn from a Dirichlet distribution and np.diag(z) will create a diagonal matrix.

In PYMC, I have not had any success in creating the diagonal matrix. I have looked through APIs and a search through the forums hasn’t helped. I believe there used to be an option tt.nlinalg.diag() in older versions of pymc. Any ideas?

The computational backend was changed from theano to pytensor in version 5, but most of the functionality is exactly the same. pytensor.tensor has all of the numpy-like stuff:

import pymc as pm
import pytensor.tensor as pt

d = pm.Dirichlet.dist(a=[1,1,1])
pt.diag(d).eval()
>>>Out:
array([[0.66949914, 0.        , 0.        ],
       [0.        , 0.29879084, 0.        ],
       [0.        , 0.        , 0.03171002]])
2 Likes