lmfit / uncertainties

Transparent calculations with uncertainties on the quantities involved (aka "error propagation"); calculation of derivatives.
http://uncertainties.readthedocs.io/
Other
584 stars 75 forks source link

Consider Supporting Asymmetric Uncertainty Propagation #266

Open TutuchanXD opened 4 weeks ago

TutuchanXD commented 4 weeks ago

Description

I have been using the uncertainties package for uncertainty propagation calculations and noticed that it currently only supports symmetric uncertainties. While values expressed as x = 5 ± 0.2 can be easily handled, the package seems to lack the capability to accommodate asymmetric uncertainties, such as x = 5^{+0.2}_{-0.1}.

In the field of astrophysics, especially when using sampling methods like MCMC (Markov Chain Monte Carlo) to obtain uncertainties in observational values, we often encounter asymmetric uncertainties. This arises because the uncertainty of a value is related to the shape of its posterior distribution, leading to different upper and lower bounds.

Suggested Solution

I would like to humbly propose a potential solution for this issue. One approach could be to create a new class that would accept three values during initialization: value, std_dev_upper, and std_dev_lower. Within this class, we could instantiate:

Subsequently, when performing calculations with this value, the actual computations would be based on upper_var and lower_var, returning their combined results.

Inquiry

Is there any plan to support this feature in future versions? If needed, I would be more than happy to assist with related development.

newville commented 3 weeks ago

@TutuchanXD The basic goal here in this project is to propagate uncertainties, including correlations. Extending the definition of an uncertain value from "x +/- s" to "x + s1 - s2" might not difficult if the goal is simply to represent such numbers.

The code here is focused on handling user code like

v1 = ufloat(1, 0.25)
v2 = ufloat(3, 0.4)

print(v1 +v2, v1*v2, sqrt(v2 * sin(v1/100) ))

The math for this is well understood (even with correlations). Implementing this is not trivial, but there is good agreement about what to implement and what the right answers should be.

I could be out-of-date on the latest statistical understanding, but as far as I know, handling

v1 = asym_ufloat(1, 0.25, 0.2)
v2 = aysm_ufloat(3, 0.4, 0.47)

print(v1 +v2, v1*v2, sqrt(v2 * sin(v1/100) ))

is not trivial. Do you have an approach to that error propagation in mind? I think that might need citations and a review.

Help with development would be appreciated.

jagerber48 commented 3 weeks ago

I agree with newville. Perhaps if you provided a reference about how linear error propagation works with asymmetric error bars we could consider implementing it in uncertainties. However, I'm suspicious that you probably don't want to do error propagation on distributions that are known to be significantly skewed. Rather, you should do some MCMC or something at the end and just report the asymmetric error bars in your result.

TutuchanXD commented 3 weeks ago

@newville @jagerber48 Thank you for your thoughtful replies. I understand that incorporating asymmetric uncertainties into the uncertainties package is not a trivial task.

I’ve come across literature that suggests using appropriate asymmetric probability distributions to model these uncertainties and employing suitable Monte Carlo methods for sampling, rather than relying solely on linear error propagation. One relevant reference is:

Possolo, A., Merkatas, C., & Bodnar, O. (2019). Asymmetrical uncertainties. Metrologia, 56(4), 045009.

I will continue to think about the implementation details and potential approaches. I look forward to discussing this further with you. Thank you again for your attention to this topic!

newville commented 3 weeks ago

@TutuchanXD I sort of doubt this library would want to support using Monte Carlo methods.

Propagating asymmetric uncertainties might be a fine thing for a library that already supports MCMC methods, such as EMCEE and PyMC.

jagerber48 commented 3 weeks ago

Possolo, A., Merkatas, C., & Bodnar, O. (2019). Asymmetrical uncertainties. Metrologia, 56(4), 045009.

This works looks interesting, but yes, it looks like it's using a Monte Carlo method for asymmetric error propagation. I again agree with Newville that Monte Carlo is out of scope for uncertainties. uncertainties is a package specifically dedicated to linear error propagation and it refers users out if they have more sophisticated statistical requirements.

That said, still let us know if you find a reference that applies linear error propagation to asymmetric error bars. I wouldn't be totally surprised if, e.g. you could just apply linear error propagation to the upper and lower error bars separately. But I also wouldn't be surprised if that naive approach is totally wrong. Hence the need for a reference that has thought through it carefully to clarify.