# Propagation of uncertainty (error)¶

Marcos Duarte

The result of a measurement is only an approximation or estimate of the value of the measurand and thus is complete only when accompanied by a statement of the uncertainty of that estimate.

Uncertainty (of measurement) is a parameter, associated with the result of a measurement, that characterizes the dispersion of the values that could reasonably be attributed to the measurand.

Evaluation of measurement data - Guide to the expression of uncertainty in measurement (2008)

For istance, to calculate the density of a material someone made the following measurements of mass and volume (m=d/V):

$$m = 4.0 \pm 0.5 \; g \\ V = 2.0 \pm 0.2 \; cm^3$$

Where 0.5 g and 0.2 cm$^3$ each represent a value of one standard deviation and express the errors in the measured weight and volume.
Based on these numbers, the material density is 2 g/cm$^3$; but how much is the error (uncertainty) of the density?

One way to estimate the error in the density is by propagation of uncertainty:

Propagation of uncertainty (or propagation of error) is the effect of variables' uncertainties (or errors) on the uncertainty of a function based on them. When the variables are the values of experimental measurements they have uncertainties due to measurement limitations (e.g., instrument precision and noise) which propagate to the combination of variables in the function (Wikipedia).

## Propagation of uncertainty by linear approximation¶

The classic way for propagation of uncertainty is through linear approximation of error propagation theory. We start with the mathematical function that relates the physical quantities and estimate the contribution of their variations by means of partial derivatives (the rate of variation) of each of these primary quantities. The linearity of the mathematical function in the vicinity of the obtained value and the statistical correlations between these quantities are also taken into consideration in the propagation of uncertainty.
If the measurand $f$ is defined in terms of the variables $x, y, z, ...$ by a general expression $f(x, y, z, ...)$, a first order approximation of the propagation of uncertainty ignoring correlations bettwen these variables is given by the well known formula:

$$\sigma _{f}\;=\;\sqrt{\left ( \frac{\partial f}{\partial x} \right)^2\sigma _{x}^{2} + \left ( \frac{\partial f}{\partial y} \right)^2 \sigma _{y}^{2} + \left ( \frac{\partial f}{\partial z} \right)^2 \sigma _{z}^{2}\: + \ldots}$$

Where $\partial f/\partial i$ is the first partial derivative of $f$ with respect to variable $i$ and $\sigma_i$ is the uncertainty of the measurement of the variable $i$ (one standard deviation).

Using the formula above, the estimation of the uncertainty of the density is given by:

$$d=\frac{m}{V} \\ \sigma _{d}\;=\;\sqrt{ \left ( \frac{\partial d}{\partial m} \right)^2 \sigma _{m}^{2}+\left ( \frac{\partial d}{\partial V} \right)^2 \sigma _{V}^{2} } \\ \sigma _{d}\;=\;\sqrt{ \left ( \frac{1}{V} \right)^2 \sigma _{m}^{2}+\left ( -\frac{m}{V^2} \right)^2 \sigma _{V}^{2} }$$

And considering the values of mass and volume given earlier:

$$\sigma_{d}\;=\;\sqrt{\left(\frac{1}{2}\right)^2\times 0.5\:^2+\left(-\frac{4}{2^2}\right)^2\times 0.2\:^2}\;\;\approx\; 0.32$$

Then, the estimated value of the density can now be expressed in a complete form: d = 2.00±0.32 g/cm$^3$.

## Using a software to calculate uncertainty¶

It is essential that any result of a measurement is accompanied by the estimate of its uncertainty as above, this way we can better understand the value of the measure and how reliable it is. However, it is unlikely that anyone will repeat these calculations in the day-to-day work (although there are tables with partial derivatives for the most common expressions). Therefore, it is useful a software that can perform these calculations (at least for checking the results manually obtained). Fortunately, there are software for the propagation of uncertainty, see this list on Wikipedia.

One such software, free, open source, and cross-platform, is the library for Python called uncertainties. If you don't know or don't have Python, the easiest way to get it is to install the Anaconda distribution, which is also free and cross-platform. Read the notebook How to install Python if you need help with that.

With uncertainties installed, let's calculate again the uncertainty of the density:

In [1]:
# Import the necessary libraries
from IPython.display import display, Math  # IPython formatting
from uncertainties import ufloat           # to define variables with uncertainty


Let's input the values:

In [2]:
m = ufloat(nominal_value=4.0, std_dev=.5)  # value with uncertainty
V = ufloat(2.0, 0.2)                       # we can pass values directly
d = m/V                                    # calculate density

print(d)

2.00+/-0.32


The result we obtained before, but without having to deduct the partial derivatives because uncertainties does this with an algorithm for automatic differentiation.

And we can display this output more elegantly:

In [3]:
display(Math('d = ' + r'{:.3L}'.format(d) + r'\; g/cm^3'))
display(Math('d = ' + r'{:.2eL}'.format(d) + r'\; g/cm^3'))
display(Math('d = ' + r'{:.2uS}'.format(d) + r'\; g/cm^3'))

$$d = 2.00 \pm 0.32\; g/cm^3$$
$$d = \left(2.00 \pm 0.32\right) \times 10^{0}\; g/cm^3$$
$$d = 2.00(32)\; g/cm^3$$

To evince how simple is to perform propagation of uncertainty in this way, the operation above could have been performed in a single line (after you imported the necessary libraries at the first time using them):

In [4]:
print(ufloat(4.0, 0.5) / ufloat(2.0, 0.2))

2.00+/-0.32


And how much each variable mass and volume contributed to the final uncertainty (i.e., the partial derivative of density in relation to the variable mass or volume times its standard deviation (each term on the right side of the equation above without squaring)) is given by:

In [5]:
d.error_components().items()

Out[5]:
dict_items([(4.0+/-0.5, 0.25), (2.0+/-0.2, 0.2)])

By definition, according to the formula above, the square root of the quadratic sum of these values is the total uncertainty for the density.

The value of each partial derivative of the density function with respect to the mass and volume variables is given by:

In [6]:
d.derivatives

Out[6]:
{2.0+/-0.2: -1.0, 4.0+/-0.5: 0.5}

Stepping back, we can even deduce the formula for the error propagation using the partial derivatives in case we want the actual formula using Sympy:

In [7]:
from sympy import symbols, Symbol, sqrt, Add, init_printing, latex
from IPython.display import display, Math, Latex
init_printing()

def stdform(formula, *errors):
"""
Calculate the symbolic formula for error propagation using partial derivatives.
Based on http://code.activestate.com/recipes/535164/
"""
from functools import reduce

formula = sqrt(reduce(Add, (formula.diff(var)**2 * Symbol('\sigma_' + var.name)**2
for var in formula.atoms(Symbol) if var.name in errors), 0.))

return formula

In [8]:
m, V = symbols('m, V')
d = m/V
formula = stdform(d, 'm', 'V')
display(Math('\sigma_d = ' + latex(formula)))

$$\sigma_d = \sqrt{\frac{\sigma_m^{2}}{V^{2}} + \frac{\sigma_V^{2} m^{2}}{V^{4}}}$$

Same as before, but Sympy deduced for us.

### Another example¶

Let's estimate the uncertainty in the measurement of the volume of a cilinder given its diameter ($d=2.0\pm0.1$) cm and height $h=10.0\pm0.5$ cm. But now, let's go straight to the solution:

In [9]:
from sympy import pi
d, h = symbols('d, h')
V = h*pi*(d/2)**2

formula = stdform(V, 'd', 'h')
display(Math('\sigma_V = ' + latex(formula)))

$$\sigma_V = \sqrt{\frac{\pi^{2} \sigma_d^{2}}{4} d^{2} h^{2} + \frac{\pi^{2} \sigma_h^{2}}{16} d^{4}}$$
In [10]:
from math import pi
h = ufloat(nominal_value=10.0, std_dev=0.5)
d = ufloat(nominal_value=2.0, std_dev=0.1)
V = h*pi*(d/2)**2
display(Math('V = ' + r'{:.3L}'.format(V) + r'\; cm^3'))

$$V = 31.4 \pm 3.5\; cm^3$$

## In conclusion¶

There is no excuse to not perform propagation of uncertainty of the result of a measurement.

Remember, your work is complete only when you report the uncertainty.