Skip to content

Add variances dispatch for distributions#8084

Open
jessegrabowski wants to merge 5 commits intopymc-devs:mainfrom
jessegrabowski:variance
Open

Add variances dispatch for distributions#8084
jessegrabowski wants to merge 5 commits intopymc-devs:mainfrom
jessegrabowski:variance

Conversation

@jessegrabowski
Copy link
Member

Description

This PR adds a variance function that returns the (symbolic) variance of a distribution. I added it because I thought it would be nice to have for the AutoGuide stuff; we can use the mean and variance of priors to initialize the parameters.

This is a 90%+ LLM PR. I went through and tried to clean things up. I mostly copied the patterns from mean.py.

Related Issue

Checklist

Type of change

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

@codecov
Copy link

codecov bot commented Feb 2, 2026

Codecov Report

❌ Patch coverage is 0.38610% with 258 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.29%. Comparing base (5ff12c8) to head (1b0ee8f).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
pymc/distributions/moments/variances.py 0.00% 242 Missing ⚠️
pymc/distributions/moments/means.py 0.00% 11 Missing ⚠️
pymc/distributions/shape_utils.py 25.00% 3 Missing ⚠️
pymc/distributions/moments/__init__.py 0.00% 2 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (5ff12c8) and HEAD (1b0ee8f). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (5ff12c8) HEAD (1b0ee8f)
31 30
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8084      +/-   ##
==========================================
- Coverage   90.80%   85.29%   -5.52%     
==========================================
  Files         121      124       +3     
  Lines       19443    19773     +330     
==========================================
- Hits        17656    16865     -791     
- Misses       1787     2908    +1121     
Files with missing lines Coverage Δ
pymc/distributions/moments/__init__.py 0.00% <0.00%> (-100.00%) ⬇️
pymc/distributions/shape_utils.py 89.55% <25.00%> (-2.33%) ⬇️
pymc/distributions/moments/means.py 0.00% <0.00%> (-99.57%) ⬇️
pymc/distributions/moments/variances.py 0.00% <0.00%> (ø)

... and 18 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@lucianopaz lucianopaz left a comment

Choose a reason for hiding this comment

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

LGTM. My only suggestion is that we move the maybe_resize utility from means and variances to utils.py, but I'll approve anyway and you can move it or not.

@jessegrabowski
Copy link
Member Author

I move the maybe_reize helper to distributions/shape_utils.py.

I also added variance for some missing distributions (CAR, Wishart, and KroneckerNormal).

Wishart was missing from means.py so I added it. The signature of KroneckerNormal was wrong in means, so I fixed it. And the formula for the mean of the rice distribution was wrong too, so I fixed that as well.

Copy link
Member

@lucianopaz lucianopaz left a comment

Choose a reason for hiding this comment

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

Thanks for the fixes and improvements. I'm a bit unsure whether the variances should return inf or raise an error for Cauchy, Flat et al. I'm a bit more in favor of raising an error, but someone else should tie break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants