MatrixNormal Example in Documentation Throws an Error

While running the example code for pm.MatrixNormal from the PyMC documentation. link

Pymc version : 5.18.2

Here is the example code:

# Setup data
true_colcov = np.array([[1.0, 0.5, 0.1],
                        [0.5, 1.0, 0.2],
                        [0.1, 0.2, 1.0]])
m = 3
n = true_colcov.shape[0]
true_scale = 3
true_rowcov = np.diag([true_scale**(2*i) for i in range(m)])
mu = np.zeros((m, n))
true_kron = np.kron(true_rowcov, true_colcov)
data = np.random.multivariate_normal(mu.flatten(), true_kron)
data = data.reshape(m, n)

with pm.Model() as model:
    # Setup right cholesky matrix
    sd_dist = pm.HalfCauchy.dist(beta=2.5, shape=3)
    colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,
                                       sd_dist=sd_dist)
    colchol = pm.expand_packed_triangular(3, colchol_packed)

    # Setup left covariance matrix
    scale = pm.LogNormal('scale', mu=np.log(true_scale), sigma=0.5)
    rowcov = pt.diag([scale**(2*i) for i in range(m)])

    vals = pm.MatrixNormal('vals', mu=mu, colchol=colchol, rowcov=rowcov,
                           observed=data)

I have encountered the following error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-6754c6fb0466> in <cell line: 14>()
     17     colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,
     18                                        sd_dist=sd_dist)
---> 19     colchol = pm.expand_packed_triangular(3, colchol_packed)
     20 
     21     # Setup left covariance matrix

/usr/local/lib/python3.10/dist-packages/pymc/math.py in expand_packed_triangular(n, packed, lower, diagonal_only)
    434         If true, return only the diagonal of the matrix.
    435     """
--> 436     if packed.ndim != 1:
    437         raise ValueError("Packed triangular is not one dimensional.")
    438     if not isinstance(n, int):

AttributeError: 'tuple' object has no attribute 'ndim'

I think I’ve identified the issue. The compute_corr parameter in pm.LKJCholeskyCov is set to True by default, which causes the error. Setting compute_corr=False resolves the problem.

# Setup data
true_colcov = np.array([[1.0, 0.5, 0.1],
                        [0.5, 1.0, 0.2],
                        [0.1, 0.2, 1.0]])
m = 3
n = true_colcov.shape[0]
true_scale = 3
true_rowcov = np.diag([true_scale**(2*i) for i in range(m)])
mu = np.zeros((m, n))
true_kron = np.kron(true_rowcov, true_colcov)
data = np.random.multivariate_normal(mu.flatten(), true_kron)
data = data.reshape(m, n)

with pm.Model() as model:
    # Setup right cholesky matrix
    sd_dist = pm.HalfCauchy.dist(beta=2.5, shape=3)
    colchol_packed= pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,
                                       sd_dist=sd_dist,compute_corr=False)
    colchol = pm.expand_packed_triangular(3, colchol_packed)

    # Setup left covariance matrix
    scale = pm.LogNormal('scale', mu=np.log(true_scale), sigma=0.5)
    rowcov = pt.diag([scale**(2*i) for i in range(m)])

    vals = pm.MatrixNormal('vals', mu=mu, colchol=colchol, rowcov=rowcov,
                           observed=data)
    idata=pm.sample()

However, there are some warnings that appear during sampling .

/usr/local/lib/python3.10/dist-packages/pytensor/tensor/nlinalg.py:182: FutureWarning: pytensor.tensor.linalg.trace is deprecated. Use pytensor.tensor.trace instead.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/pytensor/tensor/nlinalg.py:182: FutureWarning: pytensor.tensor.linalg.trace is deprecated. Use pytensor.tensor.trace instead.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/pytensor/tensor/nlinalg.py:182: FutureWarning: pytensor.tensor.linalg.trace is deprecated. Use pytensor.tensor.trace instead.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/pytensor/tensor/nlinalg.py:182: FutureWarning: pytensor.tensor.linalg.trace is deprecated. Use pytensor.tensor.trace instead.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/pytensor/tensor/nlinalg.py:182: FutureWarning: pytensor.tensor.linalg.trace is deprecated. Use pytensor.tensor.trace instead.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/pytensor/tensor/nlinalg.py:182: FutureWarning: pytensor.tensor.linalg.trace is deprecated. Use pytensor.tensor.trace instead.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/pytensor/tensor/nlinalg.py:182: FutureWarning: pytensor.tensor.linalg.trace is deprecated. Use pytensor.tensor.trace instead.
  warnings.warn(
Sampling chain 0, 3 divergences ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 / 0:00:20
Sampling chain 1, 0 divergences ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 / 0:00:21
/usr/local/lib/python3.10/dist-packages/pytensor/compile/function/types.py:960: RuntimeWarning: invalid value 
encountered in accumulate
  self.vm()
ERROR:pymc.stats.convergence:There were 3 divergences after tuning. Increase `target_accept` or reparameterize.
1 Like

Glad you could resolve your problem. PRs to update to the example notebooks are always welcome!

Sure, I would be happy to do that.