Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in MatrixNormal.sample #3585

aseyboldt opened this issue Aug 13, 2019 · 3 comments


Copy link

commented Aug 13, 2019

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", # D x D covariance
    mu = pm.MatrixNormal(
        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).

Originally reported by calvinm on discord:


This comment has been minimized.

Copy link

commented Aug 13, 2019

Should we add this to the tests, as an expected fail for now?


This comment has been minimized.

Copy link
Member Author

commented Aug 13, 2019

Maybe it is not that hard to fix, we could just broadcast both matrices and mu to (sizeprod, M, N), and after making the samples reshape back to size + (M, N)...


This comment has been minimized.

Copy link

commented Aug 19, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.