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

Conversation

roesta07
Copy link
Contributor

@roesta07 roesta07 commented Dec 1, 2024

…rrent version

Description

The compute_corr parameter in pm.LKJCholeskyCov is set to True by default, which causes the issue in latest pymc versions.
Also discussed here: https://discourse.pymc.io/t/matrixnormal-example-in-documentation-throws-an-error/16171

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • [ x] Documentation
  • Maintenance
  • Other (please specify):

📚 Documentation preview 📚: https://pymc--7599.org.readthedocs.build/en/7599/

Copy link

welcome bot commented Dec 1, 2024

Thank You Banner]
💖 Thanks for opening this pull request! 💖 The PyMC community really appreciates your time and effort to contribute to the project. Please make sure you have read our Contributing Guidelines and filled in our pull request template to the best of your ability.

@roesta07
Copy link
Contributor Author

roesta07 commented Dec 3, 2024

Hi @cluhmann as per our conversation here. Also I am not sure why my pre-commit is failing because of different file which i have not touched.

Comment on lines 1830 to 1831
colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,compute_corr=False,
sd_dist=sd_dist)
Copy link
Member

Choose a reason for hiding this comment

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

A bit more succinct?

Suggested change
colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,compute_corr=False,
sd_dist=sd_dist)
colchol_packed, _, _ = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2, sd_dist=sd_dist)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ricardoV94 Yeah also if we are using this approach then there is No need for expand_packed_triangular(line:1832) since it's already expanded. Also I will rename "cholchol_packed" to "cholchol"

colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,
sd_dist=sd_dist)
colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,compute_corr=False,
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,
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

@ricardoV94 ricardoV94 added the docs label Dec 3, 2024
@ricardoV94 ricardoV94 changed the title updated docstring for pm.MatrixNormal to ensure compatibility with cu… Fix error in docstring example for MatrixNormal Dec 3, 2024
@ricardoV94
Copy link
Member

@roesta07 you can ignore that pre-commit fail, it's indeed not related to your PR

@@ -1827,16 +1827,16 @@ 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_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,compute_corr=False,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,compute_corr=False,
colchol, _, _ = pm.LKJCholeskyCov('colchol', n=3, eta=2,

Comment on lines 1830 to 1831
colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,compute_corr=False,
sd_dist=sd_dist)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ricardoV94 Yeah also if we are using this approach then there is No need for expand_packed_triangular(line:1832) since it's already expanded. Also I will rename "cholchol_packed" to "cholchol"

colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,
sd_dist=sd_dist)
colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,compute_corr=False,
sd_dist=sd_dist)
colchol = pm.expand_packed_triangular(3, colchol_packed)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
colchol = pm.expand_packed_triangular(3, colchol_packed)

@roesta07
Copy link
Contributor Author

roesta07 commented Dec 7, 2024

Hi @ricardoV94, I have fixed the docs string as a fresh commit.
Aside this, there is a different part (below) from the same documentation file is without the model context, should I add with pm.Model() as model: on top of this? of leave it as it is

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)

@ricardoV94
Copy link
Member

@roesta07 Ideally the code snippet would be completely correct that you could copy past and run in the terminal, so it would be better to put it in a model context

@roesta07
Copy link
Contributor Author

@ricardoV94 I have added the model context at the top along with the previous changes.

@@ -1792,7 +1792,7 @@ class MatrixNormal(Continuous):
--------
Define a matrixvariate normal variable for given row and column covariance
matrices::

with pm.Model() as model:
Copy link
Member

@ricardoV94 ricardoV94 Dec 10, 2024

Choose a reason for hiding this comment

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

Should add a .. code:: python with white line above and below to render nicely

Copy link
Member

Choose a reason for hiding this comment

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

Also include imports so example is fully specified

Copy link
Contributor Author

@roesta07 roesta07 Dec 10, 2024

Choose a reason for hiding this comment

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

  • added imports on top.
  • added codeblocks .. code:: python with appropriate whitespaces
    Check these recent changes

@ricardoV94 ricardoV94 merged commit 5d51953 into pymc-devs:main Dec 11, 2024
5 checks passed
Copy link

welcome bot commented Dec 11, 2024

Congratulations Banner]
Congrats on merging your first pull request! 🎉 We here at PyMC are proud of you! 💖 Thank you so much for your contribution 🎁

@ricardoV94
Copy link
Member

Thanks @roesta07

Copy link

codecov bot commented Dec 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.83%. Comparing base (a714b24) to head (c2edfb1).
Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7599      +/-   ##
==========================================
- Coverage   92.84%   92.83%   -0.02%     
==========================================
  Files         106      106              
  Lines       17745    17748       +3     
==========================================
+ Hits        16476    16477       +1     
- Misses       1269     1271       +2     
Files with missing lines Coverage Δ
pymc/distributions/multivariate.py 93.58% <ø> (ø)

... and 1 file with indirect coverage changes

fonnesbeck pushed a commit to fonnesbeck/pymc that referenced this pull request Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: <MatrixNormal Example in Documentation not updated as per latest version ' prefix>
2 participants