# Positive Distributions as mu Causing Value Error in MvNormal

**URL:** <https://discourse.pymc.io/t/positive-distributions-as-mu-causing-value-error-in-mvnormal/13614>\
**Category:** v5\
**Tags:** bug\
**Created:** [January 10, 2024, 4:14pm UTC](https://discourse.pymc.io/t/positive-distributions-as-mu-causing-value-error-in-mvnormal/13614 "2024-01-10T16:14:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HughKinnear](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@HughKinnear](https://discourse.pymc.io/u/HughKinnear)\
**Post date:** [January 10, 2024, 4:14pm UTC](https://discourse.pymc.io/t/positive-distributions-as-mu-causing-value-error-in-mvnormal/13614/1 "2024-01-10T16:14:17Z")

</div>

Hello, the following code produces an error:

```auto
with pm.Model() as model:

    mu = pm.LogNormal('mu',mu=1,sigma=1,shape=(2,2))
    var = pm.InverseGamma('var',alpha=1,beta=1,shape=2)
    cov = pm.Beta('cov',alpha=1,beta=1)

    cov_matrix = pm.math.stack([var[0],cov,cov, var[1]]).reshape((2,2))

    pm.MvNormal('mvn',
                mu=mu[0],
                cov=cov_matrix,
                observed=np.random.randn(100,2),
                )
    
    pm.sample(random_seed=0)

```

The error:

> \<\<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!\>\> \<class ‘ValueError’\> Shape of data ((4,)) does not match shape of type ((2,)) local\_subtensor\_lift  
> ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local\_subtensor\_lift  
> ERROR (pytensor.graph.rewriting.basic): node: Subtensor{i}(Composite{…}.0, 0)  
> ERROR (pytensor.graph.rewriting.basic): TRACEBACK:  
> ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):  
> File “c:\Users\hkinn\miniconda3\envs\pigs\Lib\site-packages\pytensor\graph\rewriting\basic.py”, line 1968, in process\_node  
> fgraph.replace\_all\_validate\_remove( # type: ignore  
> File “c:\Users\hkinn\miniconda3\envs\pigs\Lib\site-packages\pytensor\graph\features.py”, line 626, in replace\_all\_validate\_remove  
> chk = fgraph.replace\_all\_validate(replacements, reason=reason, \*\*kwargs)  
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
> File “c:\Users\hkinn\miniconda3\envs\pigs\Lib\site-packages\pytensor\graph\features.py”, line 571, in replace\_all\_validate  
> fgraph.replace(r, new\_r, reason=reason, verbose=False, \*\*kwargs)  
> File “c:\Users\hkinn\miniconda3\envs\pigs\Lib\site-packages\pytensor\graph\fg.py”, line 479, in replace  
> new\_var = var.type.filter\_variable(new\_var, allow\_convert=True)  
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
> File “c:\Users\hkinn\miniconda3\envs\pigs\Lib\site-packages\pytensor\tensor\type.py”, line 268, in filter\_variable  
> other = self.constant\_type(type=self, data=other)  
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
> File “c:\Users\hkinn\miniconda3\envs\pigs\Lib\site-packages\pytensor\tensor\variable.py”, line 1052, in **init**  
> raise ValueError(  
> ValueError: Shape of data ((4,)) does not match shape of type ((2,))

The sampler does continue past this error and seemingly samples correctly.

This error can be corrected by firstly changing the shape of mu (I have it as (2,2) in the previous example as that is what is required for my work), like so:

```auto
with pm.Model() as model:

    mu = pm.LogNormal('mu',mu=1,sigma=1,shape=2)
    var = pm.InverseGamma('var',alpha=1,beta=1,shape=2)
    cov = pm.Beta('cov',alpha=1,beta=1)

    cov_matrix = pm.math.stack([var[0],cov,cov, var[1]]).reshape((2,2))

    pm.MvNormal('mvn',
                mu=mu,
                cov=cov_matrix,
                observed=np.random.randn(100,2),
                )
    
    pm.sample(random_seed=0)

```

The error can also be fixed by changing the LogNormal to a Normal (with shape still (2,2)):

```auto
with pm.Model() as model:

    mu = pm.Normal('mu',mu=1,sigma=1,shape=(2,2))
    var = pm.InverseGamma('var',alpha=1,beta=1,shape=2)
    cov = pm.Beta('cov',alpha=1,beta=1)

    cov_matrix = pm.math.stack([var[0],cov,cov, var[1]]).reshape((2,2))

    pm.MvNormal('mvn',
                mu=mu[0],
                cov=cov_matrix,
                observed=np.random.randn(100,2),
                )
    
    pm.sample(random_seed=0)

```

I assume it is something to do with the covariance matrix, if you replace it with a constant identity matrix the error also disappears:

```auto
with pm.Model() as model:

    mu = pm.LogNormal('mu',mu=1,sigma=1,shape=(2,2))
    pm.MvNormal('mvn',
                mu=mu[0],
                cov=np.eye(2),
                observed=np.random.randn(100,2),
                )
    
    pm.sample(random_seed=0)

```

You can reproduce the error with other positive distributions like Gamma and InverseGamma.

Since the error does not stop the sampling it is not a massive issue, but it would be nice to know if I am doing something wrong.

In my actual, more complex model, I am sometimes able to fix the error by using .reshape on some variables, but this seems to be without any logic and I just guess. In this simplified example I could not manage to fix it using .reshape.

Thanks in advance for the help.

Edit: it turns out it is a problem since the sampling with this error is very slow.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [January 11, 2024, 9:24am UTC](https://discourse.pymc.io/t/positive-distributions-as-mu-causing-value-error-in-mvnormal/13614/2 "2024-01-11T09:24:53Z")

</div>

> [@HughKinnear](#):
>
> The error:

I don’t see the error locally. Sampling took ~1 minute for me, although there are divergences (my guess is that the LogNormal mu prior is a bit at odds with negative observed values).

Are you using the last version of PyMC? If you are not, can you try updating?

---

<div class="post-metadata">

**Author:** ![HughKinnear](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@HughKinnear](https://discourse.pymc.io/u/HughKinnear)\
**Post date:** [January 11, 2024, 10:19am UTC](https://discourse.pymc.io/t/positive-distributions-as-mu-causing-value-error-in-mvnormal/13614/3 "2024-01-11T10:19:24Z")

</div>

Hi, that environment was running pymc 5.10.0. I created a fresh environment with 5.10.3, and I no longer have the bugs. No worries about the divergences, I just created the example quickly to recreate the bug.

Unclear if the original environment was somehow corrupted or if it was just the update that fixed the problem.

Should have just tried that in the in the first place, thanks a lot for the help and sorry for the waste of time.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [January 11, 2024, 10:27am UTC](https://discourse.pymc.io/t/positive-distributions-as-mu-causing-value-error-in-mvnormal/13614/4 "2024-01-11T10:27:43Z")

</div>

We have fixed some similar-sounding bugs recently, so I think it was just that.

---

<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:** [January 11, 2024, 2:10pm UTC](https://discourse.pymc.io/t/positive-distributions-as-mu-causing-value-error-in-mvnormal/13614/5 "2024-01-11T14:10:37Z")

</div>

I still get the error on 5.10.2, so it must have been something recent. @ricardoV94 is always fixing things!
