-
Notifications
You must be signed in to change notification settings - Fork 8
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
Errors propagation #150
Comments
Hey @rugfri , it certainly is possible and @MarJMue and I discussed it several times already and we wanted to bring this feature in when we refactor the internal data model. Generally, it is not straightforward to obtain uncertainties from a fit. For lmfit the approach would be to scale the residuals by σ, but I'm not sure if lmfit takes this into account for its covariance matrix / stderr values or just to adapt the actual fit. Lmfit does also support F-testing: https://lmfit.github.io/lmfit-py/confidence.html which may be a bit more robust here. If it's important/urgent for you we could try to build an example together to do a manual fit and try to calculate confidence intervals from it. |
Thanks @domna. |
yes
Sounds good. I see you already added a notebook there. I'll have a more detailed look at it soon |
Hey @rugfri I added a scaling with the residuals in the notebook. However, if I do use this as weights the fit does not properly converge to the data. If you want you can have a look, maybe it's just a small mistake I did. I also removed the 680nm point as the error there was around 1000 (probably some device malfunction?) and tried to use normalized weights. |
Thanks @domna, Thanks. Otherwise looks everything correct. I tried derive an analytical expression for the error where real and imaginary parts are separated but I couldn't get anything usable so far. Yes, I did also notice the large error on some points. The instrument seems to have some problems, since it is systematic. |
You are completely right. The problem now is that we are using numpys "normal" sqrt which does not generate imaginary numbers for negative inputs. I changed it to However, the fit is still not converging to the measured data 🤔
Edit: Ok I found the problem. We need to scale the weights by the variance and not the stderr. Hence, we need to square the errors before we scale the residuals. I added this in the notebook and the fit converges now. I think we also need to use leastsq's exclusively for this as L-BFGS-B does use a gradient based approach which cannot really deal with weights (it also does not converge when I use the weights). |
Thanks!! anyway the fit message and other attributes confirm. The plot too :-). Now, my original question was also about the best way to error propagation to the dielectric function and the refractive index. |
Exactly
Yeah I don't know how reliable they are, i.e., whether they scale with the absolute magnitude of the errors. If they do they would be strongly influenced by the normalization we currently do (and I think it did not work w/o it). I think for really reliable results one has to calculate the confidence intervals according to https://lmfit.github.io/lmfit-py/confidence.html
I'm not sure how to propagate these errors for multiple parameters. For one parameter I would just do |
I found a paper which deals with this issue: https://doi.org/10.1016/j.optcom.2018.07.025 |
thanks! |
Hey, I think I extracted the main parts and changes for us (from Is and Ic to rho). I think from this I could do an implementation. However, I contacted the author of the paper and asked if he is willing to share his implementation and reference data. I think this will be extremely helpful. So I'd wait until next week whether I hear something back (I think france has also a strong holiday season so it might take a while....). |
Hey, I did not get any answer, so we might just have to do an implementation ourselves. I'll compile my notes here or in our testing notebook soonish and then we can start implementing something (I'll be, however, going in holidays from mid next week and I'll probably not be able to an implementation before then. But I'll at least try to write everything down before then). |
Hey @rugfri, this is what I extracted from the paper: where , where The jacobians we should just be able to calcualte with I think this should be pretty straightforward to implement like this. Let me know what you think and if you can spot any mistakes here. Edit: One thing which we also need to do is to reduce the covariance matrix from lmfit to the appropriate dimension, i.e., only using the parameter used by the dispersion model. Or we just add zeros to the Jacobian |
Thanks @domna! I am just back from vacations and will double check again from tomorrow. |
Hey @rugfri, I'm back. I would try to give an implementation a shot. Did you implement something already I can build on? I also heard back from the author and he sent me the data from the paper. So we can use it to check our results (but somehow have to convert it to Is and Ic - or we do the fitting directly in Is and Ic, which might be the better choice for the start). |
Hi @domna, |
Indeed it was very nice
I already started to add Is/Ic fitting and could fit the data from the paper. I shared a new upload in nomad with you, which contains the data and my fitting, so we can work together there to deduce the errors.
This should be straightforward, we just need to calculate the jacobian of Is and Ic in C. As soon as we are happy we simply can replace Is and Ic with rho if we want to do rho based fitting (actually we should get the same errors in n, so its a good check). |
Hi @domna, thanks. |
Yes, I already do this in the notebook under |
Oh, yes, sorry. |
No worries :)
From the paper it should be
It is the error of Is/Ic in the experimental data. It's constant for every data point. I actually think my formula is not completely right. I think all uncertainties have to be taken into account and not only the ones which are parameters of |
Sorry @rugfri I did not have much time to work on this in the last month(s) and it will probably not have it in the next weeks, too (I still try to sit down and find my error in the formula, though). Is this still of interest for you? |
Hi @domna no problem! Yes it is still of interest for me. |
Somehow related to #94, I was wondering if you think it could be possible to implement the error propagation (on fitting parameters, experimental uncertainties if present/enabled) to the calculated quantities like dielectric function, refractive index, and such.
I recently started using https://uncertainties-python-package.readthedocs.io/en/latest/, https://github.com/lebigot/uncertainties/
which may be useful, though I am not sure how far complex numbers/arrays are supported.
Thanks
The text was updated successfully, but these errors were encountered: