Skip to content

Commit

Permalink
Draft: Improve handling of covariance calculation (#813)
Browse files Browse the repository at this point in the history
* improve handling of covariance calculation

* update whatsnew.rst

* move diagonal elements check inside try
  • Loading branch information
eendebakpt authored Oct 26, 2022
1 parent 8b5a295 commit 4173760
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Bug fixes/enhancements:
- fixed function definition for ``StepModel(form='linear')``, was not consistent with the other ones. (@matpompili; PR #794)
- fixed height factor for ``Gaussian2dModel``, was not correct. (@matpompili; PR #795)
- added ``Pearson4`` fitting model
- for covariances with negative diagonal elements, we set the covariance to ``None`` (PR #813)

Deprecations:

Expand Down
6 changes: 5 additions & 1 deletion lmfit/minimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,12 @@ def _calculate_covariance_matrix(self, fvars):
Hfun = ndt.Hessian(self.penalty, step=1.e-4)
hessian_ndt = Hfun(fvars)
cov_x = inv(hessian_ndt) * 2.0

if cov_x.diagonal().min() < 0:
# we know the calculated covariance is incorrect, so we set the covariance to None
cov_x = None
except (LinAlgError, ValueError):
return None
cov_x = None
finally:
self.result.nfev = nfev

Expand Down

0 comments on commit 4173760

Please sign in to comment.