Repository: pymc-devs/pymc

Issue #2848: Shape Parameter Behavior Confusing for Normal and MvNormal
Created: 2018-02-10T02:51:57Z
The way the shape parameter behaves can be confusing and doesn't seem to be present in the docs.
For univariate Normal with scalar valued parameters, providing the shape parameter creates an array of the specified shape, which is I find intuitive.
pm.Normal.dist(mu=0, sd=1, shape=(2,5)).random().shape

For univariate Normal with vector valued parameters, the last dimension is broadcasted to match the rest of the dimensions of the given shape. I couldn’t find anywhere in the documentation that explicitly mentioned this and it took me while of playing around to figure out how the shape parameter behaved in this case.
pm.Normal.dist(mu=np.zeros(5,), sd=np.array(range(1, 6)), shape=(2,3,4,5)).random().shape

For MvNormal, it seems like the shape parameter doesn’t do anything at all. The following doesn’t raise any error and seemingly ignores the shape parameter, returning an array of shape (10,). This last one seems more like a bug to me.
pm.MvNormal.dist(mu=np.zeros(10,), cov=np.eye(10), shape=(33,41)).random().shape

Also, to be consistent with the behavior in the example with the univariate Normal with vector valued parameters, it seems to make sense to be able to write:
pm.MvNormal.dist(mu=np.zeros(10,), cov=np.eye(10), shape=(33,41,10)).random().shape

However, this raises the following error:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-133-8f8da38a9f24> in <module>()
----> 1 print pm.MvNormal.dist(mu=np.zeros(10,), cov=np.eye(10), shape=(33,41,10)).random().shape

/Users/alexhuang/Library/Python/2.7/lib/python/site-packages/pymc3/distributions/distribution.pyc in dist(cls, *args, **kwargs)
     45     def dist(cls, *args, **kwargs):
     46         dist = object.__new__(cls)
---> 47         dist.__init__(*args, **kwargs)
     48         return dist
     49 

/Users/alexhuang/Library/Python/2.7/lib/python/site-packages/pymc3/distributions/multivariate.pyc in __init__(self, mu, cov, tau, chol, lower, *args, **kwargs)
    220                  *args, **kwargs):
    221         super(MvNormal, self).__init__(mu=mu, cov=cov, tau=tau, chol=chol,
--> 222                                        lower=lower, *args, **kwargs)
    223         self.mean = self.median = self.mode = self.mu = self.mu
    224 

/Users/alexhuang/Library/Python/2.7/lib/python/site-packages/pymc3/distributions/multivariate.pyc in __init__(self, mu, cov, chol, tau, lower, *args, **kwargs)
     34         super(_QuadFormBase, self).__init__(*args, **kwargs)
     35         if len(self.shape) > 2:
---> 36             raise ValueError("Only 1 or 2 dimensions are allowed.")
     37 
     38         if chol is not None and not lower:

ValueError: Only 1 or 2 dimensions are allowed.

I think it would make this library much more user friendly if we explicitly documented these “gotchas”.

PyMC3 Version: 3.3
Theano Version: 1.0.1
Python Version: 2.7.12
Operating system: MacOS 10.13.3
How did you install PyMC3: pip

Duplicate issue #3585: Bug in MatrixNormal.sample
Created: 2019-08-13T17:16:43Z
If one of the matrix parameters is a random variable, then MatrixNormal.sample runs into shape errors because it might be passed an array of col or row matrices instead of just one. pm.sample_prior_predictive will fail in a case like this:
import numpy as np
import pymc3 as pm
K = 3
D = 15
mu_0 = np.zeros((D,K))
lambd = 1.0
with pm.Model() as model:
    sd_dist = pm.HalfCauchy.dist(beta=2.5)
    packedL = pm.LKJCholeskyCov(f"packedL",eta=2, n=D, sd_dist=sd_dist)
    L = pm.expand_packed_triangular(D, packedL, lower=True)
    Sigma = pm.Deterministic(f"Sigma", L.dot(L.T)) # D x D covariance
    mu = pm.MatrixNormal(
        f"mu", 
        mu=mu_0, 
        rowcov=(1 / lambd) * Sigma, 
        colcov = np.eye(K), shape=(D,K))
    prior = pm.sample_prior_predictive(2)
In this example rowchol will have shape (2, 15, 15).
https://github.com/pymc-devs/pymc3/blob/master/pymc3/distributions/multivariate.py#L1598
Originally reported by calvinm on discord:
https://discourse.pymc.io/t/reshape-error-inside-sample-prior-predictive/3715

Closing comment (2019-08-19T12:41:45Z): This issue is similar to #2848. Most, if not all, of the multivariate distributions have some shape handling defects when their distribution parameters depend on other RVs.