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

Add CirculantNormal distribution. #1988

Merged
merged 17 commits into from
Mar 10, 2025
Merged

Conversation

tillahoffmann
Copy link
Collaborator

@tillahoffmann tillahoffmann commented Feb 26, 2025

This PR adds a CirculantNormal distribution which is a multivariate normal distribution where the covariance matrix has circular boundary conditions. It uses the Fourier transform introduced in #1762 to evaluate the log probability, scaling with n * log(n) instead of the usual n ** 3. The PR

  • Implements the CirculantNormal distribution.
  • Adds a PackRealFastFourierCoefficientsTransform transform. This is required to transform a real vector of size n to a complex vector of size n // 2 + 1 which can be passed to the rfft method. See Add complex constraint and real Fourier transform. #1762 (comment) for a brief initial discussion.
  • Adds a positive_definite_circulant_vector constraint. This is really just required for the test/test_distributions.py::test_distribution_constraints tests to pass.
  • Adds an example notebook, illustrating how the CirculantNormal can be used to accelerate Gaussian process inference.
  • Fix the Jacobian of the Fourier transform (which turns out not to be the identity but depends on the norm parameter).

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@@ -0,0 +1,293 @@
{
Copy link
Contributor

@juanitorduz juanitorduz Feb 26, 2025

Choose a reason for hiding this comment

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

It would be nice to put the legends outside the plots as in https://stackoverflow.com/questions/4700614/how-to-put-the-legend-outside-the-plot :)


Reply via ReviewNB

@tillahoffmann
Copy link
Collaborator Author

tillahoffmann commented Feb 27, 2025

I think the failing tests are due to issues with the data download and not related to the changes in this PR. There are a few further failing tests due to numerics (maybe associated with a new jax release?).

@tillahoffmann tillahoffmann force-pushed the circulant branch 3 times, most recently from 5131aa8 to 3a7a1c8 Compare March 6, 2025 14:34

class CirculantNormal(TransformedDistribution):
"""
Multivariate normal distribution with circulant covariance matrix.
Copy link
Member

Choose a reason for hiding this comment

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

Could you add reference for this distribution or the circulant covariance matrix?

I don't understand why we call it circulant covariance matrix. The covariance matrix in MVN is symmetric. But the circulant matrix is not symmetric.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've added a bit more background and a reference. I unfortunately couldn't find a non-paywalled link.

@tillahoffmann
Copy link
Collaborator Author

There are still a few failing numerical tests. Is it possible the gof tests are a bit strict? E.g., increasing the sample size to 20k for all distributions results in the following tests failing locally.

FAILED test/test_gof.py::test_gof[GaussianRandomWalk-None-params32] - assert 0.0 > 2e-05
FAILED test/test_gof.py::test_gof[GaussianRandomWalk-None-params33] - assert 0.0 > 2e-05
FAILED test/test_gof.py::test_gof[GaussianStateSpace-None-params35] - assert 0.0 > 2e-05
FAILED test/test_gof.py::test_gof[GaussianCopulaBeta-None-params37] - assert 1.623809683759525e-05 > 2e-05
FAILED test/test_gof.py::test_gof[MultivariateStudentT-_multivariate_t_to_scipy-params97] - assert 1.0300558915599112e-05 > 2e-05
FAILED test/test_gof.py::test_gof[VonMises-<lambda>-params161] - assert 0.0 > 2e-05

@fehiepsi fehiepsi added this to the 0.18 milestone Mar 7, 2025
class CirculantNormal(TransformedDistribution):
"""
Multivariate normal distribution with covariance matrix that is positive-definite
and circulant [1], i.e., has periodic boundary conditions. Circulant matrices can be
Copy link
Member

Choose a reason for hiding this comment

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

I think I understand what is a circulant matrix (from the wikipedia page) but I don't understand why this is a MVN distribution (it seems to be a transformed normal distribution based on the implementation) and why this matrix is called covariance matrix, and what "positive-definite" means here. Could you clarify?

It would be helpful to add formula in the docstrings like https://num.pyro.ai/en/stable/distributions.html#numpyro.distributions.continuous.Levy

Copy link
Member

@fehiepsi fehiepsi left a comment

Choose a reason for hiding this comment

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

The implementation looks great to me! Same for the tutorial. Apart from adding more description to the definition of the distribution, could you also add a thumbnail for it https://github.com/pyro-ppl/numpyro/tree/master/docs/source/_static/img/tutorials

@fehiepsi
Copy link
Member

fehiepsi commented Mar 7, 2025

Is it possible the gof tests are a bit strict?

I'm not sure. It seems not robust to multivariate distributions. You can skip it because we have tests for the bijections.

@fehiepsi fehiepsi merged commit 7365f38 into pyro-ppl:master Mar 10, 2025
3 checks passed
@tillahoffmann tillahoffmann deleted the circulant branch March 10, 2025 20:46
@tillahoffmann
Copy link
Collaborator Author

The implementation looks great to me! Same for the tutorial. Apart from adding more description to the definition of the distribution, could you also add a thumbnail for it https://github.com/pyro-ppl/numpyro/tree/master/docs/source/_static/img/tutorials

Ah, I forgot the thumbnail. Will add that in a future PR.

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