5
$\begingroup$

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.

$\endgroup$
  • 3
    $\begingroup$ pythonhosted.org/uncertainties $\endgroup$ – Ronaldo Carpio Aug 14 '14 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$ – Jaime Aug 14 '14 at 13:33
1
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
4
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ If you can characterize your uncertainty in terms of probability distributions, polynomial chaos methods are much more preferable to interval methods. $\endgroup$ – Paul Aug 14 '14 at 18:03
2
$\begingroup$

It sounds like what you may need is a library that supports interval arithmetic. A cursory google search reveals the following library:

Pyinterval

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.

| cite | improve this answer | |
$\endgroup$
  • $\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$ – Eric O Lebigot Nov 26 '14 at 10:39
0
$\begingroup$

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
| cite | improve this answer | |
$\endgroup$
  • $\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$ – Geoff Oxberry Aug 15 '14 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$ – Eric O Lebigot Nov 26 '14 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.