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.
-
3$\begingroup$ pythonhosted.org/uncertainties $\endgroup$– Ronaldo CarpioAug 14, 2014 at 13:33
-
1$\begingroup$ Have never tried it myself, but after I introduced him to Python, one of the mechanical engineers at work is using this. $\endgroup$– JaimeAug 14, 2014 at 13:33
4 Answers
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.
There are a few programs that can do this automatically so that you don't have to implement formulas that are inconvenient to handle.
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.
-
$\begingroup$ If you can characterize your uncertainty in terms of probability distributions, polynomial chaos methods are much more preferable to interval methods. $\endgroup$– PaulAug 14, 2014 at 18:03
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.
-
$\begingroup$ The
uncertainties
package does take dependencies ("correlations") between errors into account. It does not implements interval arithmetic but instead (linear) error propagation theory. $\endgroup$ Nov 26, 2014 at 10:39
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:
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)))
Output
(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
-
$\begingroup$ Taylor series approaches are used by interval arithmetic libraries, and these typically involve some automatic differentiation tool. I've seen more work combining AD and interval arithmetic for uncertainties than AD alone. $\endgroup$ Aug 15, 2014 at 21:13
-
$\begingroup$ @Kirill: What you propose here is exactly what the
uncertainties
package does transparently for the user, since it applies linear error propagation theory (pythonhosted.org//uncertainties). $\endgroup$ Nov 26, 2014 at 10:44