Cache covariance matrix decomposition in frozen multivariate_normal
See original GitHub issueCurrently, it seems that a frozen multivariate_normal distribution unnecessarily re-computes the root decomposition (and other properties such as evals for the logpdf) of the covariance matrix for each operation. For instance, when sampling scipy just calls the sampling of the underlying distribution with the full covariance matrix: https://github.com/scipy/scipy/blob/adc4f4f7bab120ccfab9383aba272954a0a12fb0/scipy/stats/_multivariate.py#L762
This is super wasteful, as most of the computation is in fact in computing this decomposition.
What should happen instead is that the frozen object should also have a factor
or L
attribute s.t. L @ L.T = cov
, and then it would compute that only once (upon the first iteratio), and in later steps pass that into the sampling method instead of cov
, avoiding a bunch of unnecessary compute.
Torch’s mvn does this (though it doesn’t allow singular matrices at this point): https://github.com/pytorch/pytorch/blob/master/torch/distributions/multivariate_normal.py#L146-L15
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Might be nice to have A benchmark given that (AFAICS) this is a (very useful) speed up. Might be a good place to start.
Here is a good read if you’re not familiar with benchmarks in scipy
http://scipy.github.io/devdocs/dev/contributor/benchmarking.html
Sincerely,
-Lucas Roberts
Thanks! Let me use the opportunity to make a shameless plug for a related (PyTorch) project for structured linear algebra that can also encode various structured covariance matrices: https://github.com/cornellius-gp/linear_operator