Warnings generated in calculation of covariance matrix #812
Replies: 3 comments 2 replies
-
@eendebakpt I guess we could make that step size an option. Another option would be to check whether the diagonal elements are positive and return It turns out that |
Beta Was this translation helpful? Give feedback.
-
@newville The fit is indeed terrible (that is ok, I know how to deal with that), so the actual value of the covariance is not important for me. It is more the warnings that I would like to get rid of. I did check the effect of the Since the values are varying wildly, the I do not expect to find a "good" value of the I like your suggestion of returning Code to reproduce the figure
|
Beta Was this translation helpful? Give feedback.
-
Well, you are kind of raising this issue because you are not dealing with it, right? We really cannot prevent someone from getting a terrible fit if that is their goal, and it is sort of pointless for us to worry about the results of bad fits.
If getting rid of the warning is the goal, then perhaps import warnings
warnings.simplefilter("ignore", RuntimeWarning) is what you are looking for. I cannot recommend that. Anyway, I would be ok with leaving try:
Hfun = ndt.Hessian(self.penalty, step=1.e-4)
hessian_ndt = Hfun(fvars)
cov_x = inv(hessian_ndt) * 2.0
except (LinAlgError, ValueError):
nvar = len(fvars)
cov_x = -np.ones(nvar*nvar, dtype=np.float64).reshape(nvar, nvar)
finally:
self.result.nfev = nfev
if cov_x.diagonal().min() < 0:
cov_x = None
# restore original values
for name in self.result.var_names:
self.result.params[name].value = best_vals[name]
return cov_x That would make |
Beta Was this translation helpful? Give feedback.
-
When fitting a method with lmfit there are some warnings generated about an invalid sqrt. This happens in
minimizer.py
at (around line 852)The reason is that the covariance matrix (which should have a positive diagonal) has some negative entries.
I traced this down to the calculation in
_calculate_covariance_matrix
which is:By changing the value of
step
to1e-6
the covariance matrix calculations gives no warnings.How can I prevent the warnings and have a correct calculation of the covariance matrix? The model I am using seems reasonable (e.g. no extreme values in the input data, smooth function).
Could the default value of
1e-4
be changed into a lower value, a value determined automatically or perhaps a value that can be configured?Minimal example reproducing the warning:
Output:
Beta Was this translation helpful? Give feedback.
All reactions