# MatrixNormal Example in Documentation Throws an Error

**URL:** <https://discourse.pymc.io/t/matrixnormal-example-in-documentation-throws-an-error/16171>\
**Category:** v5\
**Created:** [November 24, 2024, 11:31am UTC](https://discourse.pymc.io/t/matrixnormal-example-in-documentation-throws-an-error/16171 "2024-11-24T11:31:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![roesta07](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/roesta07/32/6060_2.png) [@roesta07](https://discourse.pymc.io/u/roesta07)\
**Post date:** [November 24, 2024, 11:31am UTC](https://discourse.pymc.io/t/matrixnormal-example-in-documentation-throws-an-error/16171/1 "2024-11-24T11:31:16Z")

</div>

While running the example code for `pm.MatrixNormal` from the PyMC documentation. [link](https://www.pymc.io/projects/docs/en/stable/api/distributions/generated/pymc.MatrixNormal.html)

Pymc version : 5.18.2

Here is the example code:

```py
# 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

```py
---------------------------------------------------------------------------
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'

```

---

<div class="post-metadata">

**Author:** ![roesta07](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/roesta07/32/6060_2.png) [@roesta07](https://discourse.pymc.io/u/roesta07)\
**Post date:** [November 24, 2024, 11:56am UTC](https://discourse.pymc.io/t/matrixnormal-example-in-documentation-throws-an-error/16171/2 "2024-11-24T11:56:28Z")

</div>

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.

```py
# 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 .

```py
/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.

```

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [November 24, 2024, 12:42pm UTC](https://discourse.pymc.io/t/matrixnormal-example-in-documentation-throws-an-error/16171/3 "2024-11-24T12:42:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![roesta07](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/roesta07/32/6060_2.png) [@roesta07](https://discourse.pymc.io/u/roesta07)\
**Post date:** [November 26, 2024, 3:02pm UTC](https://discourse.pymc.io/t/matrixnormal-example-in-documentation-throws-an-error/16171/4 "2024-11-26T15:02:38Z")

</div>

Sure, I would be happy to do that.
