Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
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.
Individual Numbers with Uncertainties
The following import statements make the parts of the package for inidividual numbers available.
There are multiple ways to create a variable like . With the ufloat function, the two arguments are the number and its uncertainty.
Using the ufloat_fromstr function, the the number and its uncertainty are entered as strings (between double quotes). The second example is equivalent to .
Calculations can be performed just as for ordinary numbers, but the uncertainties are propagated automatically.
You can also access the number (nominal value) and the uncertainty (standard deviation) separately.
It is possible to perform calculations involving many mathematical functions (see a list a the end of this tutorial).
Arrays of Numbers with Uncertainties
The following import statement makes it possible to use arrays of numbers with uncertainties.
The unumpy.uarray function takes lists of the nominal values and their uncertainties.
The numbers (nominal values) and the uncertainties (standard deviations) can be accessed as follows.
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.
It is often useful to find an average (mean) of the elements of an array.
As an example, suppose that you have data for variables and , where there are uncertainties for the values of . It is easy to make a plot of vs. with appropriate error bars.
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 in the description. If there is a second argument, it is called .
Individual Number | Array of Numbers | Function Description |
---|---|---|
acos | unumpy.arccos | Return the arc cosine (in radians) |
acosh | unumpy.arccosh | Return the inverse hyperbolic cosine |
asin | unumpy.arcsin | Return the arc sine (in radians) |
asinh | unumpy.asinh | Return the inverse hyperbolic sine |
atan | unumpy.arctan | Return the arc tangent (in radians) |
atan2 | unumpy.arctan2 | Return the arc tangent (in radians) of y/x (signs of both x and y are considered) |
atanh | unumpy.arctanh | Return the inverse hyperbolic tangent |
ceil | unumpy.ceil | Return the smallest integer >= x |
copysign | unumpy.copysign | Return a float with the magnitude (absolute value) of x but the sign of y |
cos | unumpy.cos | Return the cosine of x (measured in radians) |
cosh | unumpy.cosh | Return the hyperbolic cosine |
degrees | unumpy.degrees | Convert angle from radians to degrees |
erf | unumpy.erf | Returns the error function |
erfc | unumpy.erfc | Returns the complementary error function |
exp | unumpy.exp | Return e raised to the power of x |
expm1 | unumpy.expm1 | Return exp(x)-1 avoiding the loss of precision for small x |
fabs | unumpy.fabs | Return the absolute value of the float x |
factorial | Return x! (error if x is negative or non-integer) | |
floor | unumpy.floor | Returns the largest integer <= x |
fmod | unumpy.fmod | Returns x modulo y |
frexp | Return the mantissa (m) and exponent (e) of x such that x = m * 2**e | |
gamma | unumpy.gamma | Returns the Gamma function |
hypot | unumpy.hypot | Return the Euclidean distance, sqrt(x*x + y*y) |
isinf | unumpy.isinf | Return True if x is a positive or negative infinity, and False otherwise |
isnan | unumpy.isnan | Return True if x is a NaN (not a number), and False otherwise |
ldexp | unumpy.ldexp | Return x * (2 **i) |
lgamma | unumpy.lgamma | Return the atural logarithm of absolute value of Gamma function |
log | unumpy.log | Return the logarithm of x to the base y; if the base not specified, returns the natural logarithm (base e) |
log10 | unumpy.log10 | Return the base 10 logarithm |
log1p | unumpy.log1p | Return the natural logarithm of 1+x (base e) in a way which is accurate for x near zero |
modf | unumpy.modf | Return the fractional and integer parts of x |
pow | unumpy.pow | Return x to the power of y |
radians | unumpy.radians | Convert angle from degrees to radians |
sin | unumpy.sin | Return the sine of x (measured in radians) |
sinh | unumpy.sinh | Return the hyperbolic sine |
sqrt | unumpy.sqrt | Return the square root |
tan | unumpy.tan | Return the tangent of x (measured in radians) |
tanh | unumpy.tanh | Return the hyperbolic tangent |
trunc | unumpy.trunc | Truncates x to the nearest integer toward 0 |