Is there a python library that would keep track of uncertainty in measured data? i.e. if I put in a figure of a±b is there an easy way to track the propagation of error through calculations.
The usual way used by scientist for tracking uncertainties is to keep track of the standard deviation of quantities, so I will assume that this is what you need.
I just had another look at the current offer, and I would recommend the uncertainties package too. You can perform arbitrary calculations with numbers with uncertainties as if they were simple floats, and the results automatically contains an uncertainty, propagated correctly.
Disclaimer: I'm the author of this package.
The two basic approaches I can think of are:
- interval arithmetic
- something like polynomial chaos expansions
I'm sure there are other approaches out there.
In brief, interval arithmetic redefines common arithmetic operations (addition, subtraction, multiplication, etc.) over intervals, so that given an interval $I$, a function $f$, and the interval extension $F$ of $f$, $f(I) \subset F(I)$. In other words, the image of the function is always contained in the interval extension, and the interval extension may drastically overestimate the bounds of your function.
Paul is right that naive implementations of interval arithmetic may yield overly conservative bounds; the "dependency problem" will also yield overly conservative bounds, even if the implementation of interval arithmetic is state-of-the-art.
Polynomial chaos expansions instead treat uncertain quantities as random variables, and decompose these random variables (and thus, their probability density functions) into the sum of a collection of standard random variables (for instance, with a uniform probability density function, a Gaussian probability density function, etc.) scaled by coefficients. The random variables are selected so that their probability density functions are orthogonal to each other with respect to a weighted inner product, so these expansions look a lot like Fourier series, Chebyshev series, and so on. Propagating this expansion through your calculations will give you a probability density function for your output, and is another way to propagate uncertainty.
It sounds like what you may need is a library that supports interval arithmetic. A cursory google search reveals the following library:
As others have suggested, it seems that the pythonhosted.org package "uncertainties" also accomplishes this task as well.
Keep in mind that interval computations may yield overly conservative bounds on the resulting computations, since dependencies between intervals are not taken into consideration.
If the errors are quite small, and you don't need exact conservative estimates, you can estimate such uncertainties with an algorithmic differentiation library, such as
ad (which I never used, but found by searching). Here is an example:
from ad import adnumber import ad.admath as adm import math as m x = adnumber(1.0) x_err = 0.01 # Error in starting value for Newton's method z = adnumber(1) # Solve the equation y + sin(y) - x = 0 for y y = x - adm.sin(x) + z while abs(y + m.sin(y) - x) > 1e-8: y = y - (y + adm.sin(y) - x) / (1 + adm.cos(y)) print((y, y.d(x))) print("Value: %s\nEstimated error: %s\nSensitivity to starting value: %s" % \ (y.x, abs(y.d(x)) * x_err, abs(y.d(z)/y.x)))
(ad(0.3912318441940903), 0.48321150365251125) (ad(0.5094172293334197), 0.5309477254386529) (ad(0.5109731138269538), 0.5341096893266438) (ad(0.510973429388556), 0.5341113161301007) Value: 0.510973429388556 Estimated error: 0.005341113161301007 Sensitivity to starting value: 7.680410775256825e-13