lmfit / uncertainties

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

Support for groups of errors (e.g. statistical and systematic) #70

Open natfarleydev opened 7 years ago

natfarleydev commented 7 years ago

I propose something like the following:

import uncertainties as uc
x = uc.ufloat(1, 0.5, "x_error_name", error_group="stats")
y = uc.ufloat(-1, 0.7, "y_error_name2", error_group="syst")
z = x + y

print("z: {:L}".format(z))
print("z.std_dev: {}".format(z.std_dev)) # Acts as the independent sum in quadrature of all error groups

would print something like

z: 0 \pm 0.5 \pm 0.7
z.std_dev: 0.86023252670426

This would provide a few advantages over manually calculating separate errors including having proper error propagation for both groups independently.

Changes/improvements to syntax/methods names/etc. are welcome.

lebigot commented 7 years ago

Thanks, that's an interesting use case.

The interaction of a form like 0±0.5±0.7 with formatting is not fully trivial I guess (because the nominal value and the uncertainty use the same format, so here we would have to find a meaningful common format for three or more numbers), but it is worth investigating.

Now, what you propose should definitely be complemented by a way of specifying the order in which each group prints their uncertainty (or we don't know which error comes from where, which makes the feature less useful).

I would suggest to proceed as follows:

  1. Maybe a first attempt at providing this kind of feature would be to not go through a new format, but to provide some convenient access to each group error and let the user print them. That should be quite easy to implement.

  2. A next step could be to offer some formatting facility so that the nominal value and errors are printed in some particularly legible way (maybe like 1.234 ± 0.021 ± 0.001?).

  3. Then later a formatting option could be considered.

I am not promising anything in terms of timeline (I want to implement some speed up idea that I worked on more recently (see issue #53), and I feel that providing some deeper support for arrays is more urgent). However, adding the concept of error groups and providing some interface to them should be quite doable for external contributors: pull requests are welcome!