Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 10
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2204
Kernel: Python 3 (system-wide)

Uncertainty Calculations

The uncertainties package allows you to perform calculations on numbers with uncertainties. It handles the propagation of uncertainties for you. The following line can be used to install (or upgrade) the package in Anaconda or Google Colabaoratory.

pip install --upgrade uncertainties

Individual Numbers with Uncertainties

The following import statements make the parts of the package for inidividual numbers available.

from uncertainties import ufloat, ufloat_fromstr from uncertainties.umath import *

There are multiple ways to create a variable like 2.0±0.22.0 \pm 0.2. With the ufloat function, the two arguments are the number and its uncertainty.

r = ufloat(2.0, 0.2) print(r)
2.00+/-0.20

Using the ufloat_fromstr function, the the number and its uncertainty are entered as strings (between double quotes). The second example is equivalent to (4.1±0.3)×104(4.1 \pm 0.3)\times 10^{-4}.

s = ufloat_fromstr("3.2 +/- 0.4") t = ufloat_fromstr("(4.1 +/- 0.3)e-4") # factored exponential print(s) print(t)
3.2+/-0.4 0.000410+/-0.000030

Calculations can be performed just as for ordinary numbers, but the uncertainties are propagated automatically.

square = r**2 print(square) product = s*t print(product)
4.0+/-0.8 0.00131+/-0.00019

You can also access the number (nominal value) and the uncertainty (standard deviation) separately.

print(square.nominal_value) print(square.std_dev)
4.0 0.8

It is possible to perform calculations involving many mathematical functions (see a list a the end of this tutorial).

print(sin(1+r**2))
-0.96+/-0.23

Arrays of Numbers with Uncertainties

The following import statement makes it possible to use arrays of numbers with uncertainties.

from uncertainties import unumpy

The unumpy.uarray function takes lists of the nominal values and their uncertainties.

y = unumpy.uarray([1, 4, 9, 16], [0.1, 0.2, 0.3, 0.4]) print(y)
[1.0+/-0.1 4.0+/-0.2 9.0+/-0.3 16.0+/-0.4]

The numbers (nominal values) and the uncertainties (standard deviations) can be accessed as follows.

print(unumpy.nominal_values(y)) print(unumpy.std_devs(y))
[ 1. 4. 9. 16.] [0.1 0.2 0.3 0.4]

You can perform calculations on the elements of the array. The list of functions that can follow "unumpy" are listed at the end of this tutorial.

print(unumpy.sqrt(y))
[1.0+/-0.05 2.0+/-0.05 3.0+/-0.049999999999999996 4.0+/-0.05]
print(unumpy.cos(y))
[0.5403023058681398+/-0.08414709848078966 -0.6536436208636119+/-0.15136049906158566 -0.9111302618846769+/-0.12363554557252697 -0.9576594803233847+/-0.11516132666602613]

It is often useful to find an average (mean) of the elements of an array.

print(y.mean())
7.50+/-0.14

As an example, suppose that you have data for variables xx and yy, where there are uncertainties for the values of yy. It is easy to make a plot of log(y)\log(y) vs. log(x)\log(x) with appropriate error bars.

import pylab as pl x = pl.array([1,2,3,4]) logx = pl.log10(x) logy = unumpy.log10(y) pl.figure() pl.errorbar(logx, unumpy.nominal_values(logy), unumpy.std_devs(logy), ls='None', marker='o', ms=4) pl.show()
Image in a Jupyter notebook

Additional Documentation

More information is available at: http://pythonhosted.org/uncertainties/

A list of the mathematical functions that work on numbers with uncertainties is below. If there is one argument it is called xx in the description. If there is a second argument, it is called yy.

Individual NumberArray of NumbersFunction Description
acosunumpy.arccosReturn the arc cosine (in radians)
acoshunumpy.arccoshReturn the inverse hyperbolic cosine
asinunumpy.arcsinReturn the arc sine (in radians)
asinhunumpy.asinhReturn the inverse hyperbolic sine
atanunumpy.arctanReturn the arc tangent (in radians)
atan2unumpy.arctan2Return the arc tangent (in radians) of y/x (signs of both x and y are considered)
atanhunumpy.arctanhReturn the inverse hyperbolic tangent
ceilunumpy.ceilReturn the smallest integer >= x
copysignunumpy.copysignReturn a float with the magnitude (absolute value) of x but the sign of y
cosunumpy.cosReturn the cosine of x (measured in radians)
coshunumpy.coshReturn the hyperbolic cosine
degreesunumpy.degreesConvert angle from radians to degrees
erfunumpy.erfReturns the error function
erfcunumpy.erfcReturns the complementary error function
expunumpy.expReturn e raised to the power of x
expm1unumpy.expm1Return exp(x)-1 avoiding the loss of precision for small x
fabsunumpy.fabsReturn the absolute value of the float x
factorialReturn x! (error if x is negative or non-integer)
floorunumpy.floorReturns the largest integer <= x
fmodunumpy.fmodReturns x modulo y
frexpReturn the mantissa (m) and exponent (e) of x such that x = m * 2**e
gammaunumpy.gammaReturns the Gamma function
hypotunumpy.hypotReturn the Euclidean distance, sqrt(x*x + y*y)
isinfunumpy.isinfReturn True if x is a positive or negative infinity, and False otherwise
isnanunumpy.isnanReturn True if x is a NaN (not a number), and False otherwise
ldexpunumpy.ldexpReturn x * (2 **i)
lgammaunumpy.lgammaReturn the atural logarithm of absolute value of Gamma function
logunumpy.logReturn the logarithm of x to the base y; if the base not specified, returns the natural logarithm (base e)
log10unumpy.log10Return the base 10 logarithm
log1punumpy.log1pReturn the natural logarithm of 1+x (base e) in a way which is accurate for x near zero
modfunumpy.modfReturn the fractional and integer parts of x
powunumpy.powReturn x to the power of y
radiansunumpy.radiansConvert angle from degrees to radians
sinunumpy.sinReturn the sine of x (measured in radians)
sinhunumpy.sinhReturn the hyperbolic sine
sqrtunumpy.sqrtReturn the square root
tanunumpy.tanReturn the tangent of x (measured in radians)
tanhunumpy.tanhReturn the hyperbolic tangent
truncunumpy.truncTruncates x to the nearest integer toward 0