Marcos Duarte
Laboratory of Biomechanics and Motor Control
Federal University of ABC, Brazil
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 instance, 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).
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 between 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 former formula for linear approximation of error propagation, 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$.
uncertainties
package 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. With uncertainties installed, let's calculate again the uncertainty of the density:
# 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:
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.
display(Math(f'd = {d:.3L} g/cm^3'))
display(Math(f'd = {d:.2eL} g/cm^3'))
display(Math(f'd = {d:.2uS} g/cm^3'))
The operation above could have been performed in a single line (after you imported the necessary libraries at the first time using them):
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:
d.error_components().items()
dict_items([(2.0+/-0.2, 0.2), (4.0+/-0.5, 0.25)])
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:
d.derivatives
defaultdict(None, {2.0+/-0.2: -1.0, 4.0+/-0.5: 0.5})
from sympy import symbols, Symbol, sqrt, Add, Mul, Pow, init_printing, latex
init_printing()
def stdform(formula, formula_symb, *errors):
"""
Calculate the symbolic formula for error propagation using partial derivatives.
"""
from functools import reduce
formula2 = sqrt(reduce(Add, (Mul(Pow(formula.diff(var), 2, evaluate=False),
Symbol(rf'\sigma_{var.name}', commutative=False)**2, evaluate=False)
for var in formula.atoms(Symbol) if var.name in errors), 0.))
formula3 = sqrt(reduce(Add, (Mul(Pow(formula.diff(var)/formula, 2, evaluate=False),
Symbol(rf'\sigma_{var.name}', commutative=False)**2, evaluate=False)
for var in formula.atoms(Symbol) if var.name in errors), 0.))
print('Uncertainty:')
display(Math(rf'\sigma_{formula_symb} = {latex(formula2)}'))
print('Relative Uncertainty:')
display(Math(rf'\frac{{\sigma_{formula_symb}}}{formula_symb} = {latex(formula3)}'))
return formula2, formula3
print('Example for uncertainty propagation for the density, d=m/v:')
m, V = symbols('m, V')
d = m/V
formula = stdform(d, 'd', 'm', 'V')
Example for uncertainty propagation for the density, d=m/v: Uncertainty:
Relative Uncertainty:
Same as before, but Sympy deduced for us.
Let's estimate the uncertainty in the measurement of the volume of a cylinder 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:
from sympy import pi
d, h = symbols('d, h')
V = h*pi*(d/2)**2
formula = stdform(V, 'V', 'd', 'h')
Uncertainty:
Relative Uncertainty:
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(f'V = {V:.3L} cm^3'))
The uncertainty package employs a linear approximation of error propagation, which is only valid for small errors, and it assumes that the errors follow normal distributions. There are other packages that can be used when these assumptions are violated:
And there are other packages using linear error propagation:
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.