Measurement error library

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.

• pythonhosted.org/uncertainties Aug 14 '14 at 13:33
• Have never tried it myself, but after I introduced him to Python, one of the mechanical engineers at work is using this. Aug 14 '14 at 13:33

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.

• If you can characterize your uncertainty in terms of probability distributions, polynomial chaos methods are much more preferable to interval methods.
– Paul
Aug 14 '14 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.

• The uncertainties package does take dependencies ("correlations") between errors into account. It does not implements interval arithmetic but instead (linear) error propagation theory. Nov 26 '14 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 math as m

x_err = 0.01

# Error in starting value for Newton's method

# 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)

• @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). Nov 26 '14 at 10:44