Skip to content

Commit

Permalink
SMC stabilize covariance matrix (#3573)
Browse files Browse the repository at this point in the history
* stabilize covariance

* add release note

* fix test
  • Loading branch information
aloctavodia authored Jul 31, 2019
1 parent 187028d commit c1e272c
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 202 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Maintenance
- Moved math operations out of `Rice`, `TruncatedNormal`, `Triangular` and `ZeroInflatedNegativeBinomial` `random` methods. Math operations on values returned by `draw_values` might not broadcast well, and all the `size` aware broadcasting is left to `generate_samples`. Fixes [#3481](https://github.com/pymc-devs/pymc3/issues/3481) and [#3508](https://github.com/pymc-devs/pymc3/issues/3508)
- Parallelization of population steppers (`DEMetropolis`) is now set via the `cores` argument. ([#3559](https://github.com/pymc-devs/pymc3/pull/3559))
- SMC: stabilize covariance matrix [3573](https://github.com/pymc-devs/pymc3/pull/3573)

## PyMC3 3.7 (May 29 2019)

Expand Down
4 changes: 3 additions & 1 deletion pymc3/step_methods/smc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def _calc_covariance(posterior, weights):
Calculate trace covariance matrix based on importance weights.
"""
cov = np.cov(posterior, aweights=weights.ravel(), bias=False, rowvar=0)
cov = np.atleast_2d(cov)
cov += 1e-6 * np.eye(cov.shape[0])
if np.isnan(cov).any() or np.isinf(cov).any():
raise ValueError('Sample covariances not valid! Likely "draws" is too small!')
return np.atleast_2d(cov)
return cov


def _tune(acc_rate, proposed, step):
Expand Down
Loading

0 comments on commit c1e272c

Please sign in to comment.