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

Fix error in docstring example for MatrixNormal #7599

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions pymc/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,14 +1791,21 @@ class MatrixNormal(Continuous):
Examples
--------
Define a matrixvariate normal variable for given row and column covariance
matrices::
matrices.

colcov = np.array([[1.0, 0.5], [0.5, 2]])
rowcov = np.array([[1, 0, 0], [0, 4, 0], [0, 0, 16]])
m = rowcov.shape[0]
n = colcov.shape[0]
mu = np.zeros((m, n))
vals = pm.MatrixNormal("vals", mu=mu, colcov=colcov, rowcov=rowcov)
.. code:: python

import pymc as pm
import numpy as np
import pytensor.tensor as pt

with pm.Model() as model:
colcov = np.array([[1.0, 0.5], [0.5, 2]])
rowcov = np.array([[1, 0, 0], [0, 4, 0], [0, 0, 16]])
m = rowcov.shape[0]
n = colcov.shape[0]
mu = np.zeros((m, n))
vals = pm.MatrixNormal("vals", mu=mu, colcov=colcov, rowcov=rowcov)

Above, the ith row in vals has a variance that is scaled by 4^i.
Alternatively, row or column cholesky matrices could be substituted for
Expand Down Expand Up @@ -1827,16 +1834,13 @@ class MatrixNormal(Continuous):
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)

colchol,_,_ = pm.LKJCholeskyCov('colchol', n=3, eta=2,sd_dist=sd_dist)
# 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,
Copy link
Member

@ricardoV94 ricardoV94 Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion can't be applied directyl, but this seems more clean

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missid this. I would put in same line, looks odd split

observed=data)
observed=data)
"""

rv_op = matrixnormal
Expand Down
Loading