Skip to content

DiamondLightSource/numcertain

Repository files navigation

numcertain

Code CI Docs CI Test Coverage Latest PyPI version Apache License

This package provides a python and numpy data type (uncertain) which implements a floating point value with quantified uncertainity, allowing for forward uncertainity propagation of uncorrelated values.

PyPI pip install numcertain
Source code https://github.com/DiamondLightSource/numcertain
Documentation https://DiamondLightSource.github.io/numcertain
Releases https://github.com/DiamondLightSource/numcertain/releases

Aritmatic Examples

A brief example of arithmatic with the provided uncertain data type is presented below:

a = uncertain(42.0, 5.0)
b = uncertain(36, 12)

print(a + b)
print(a - b)
print(a * b)
print(a / b)
>> 78.0±13.0
>> 6.0±13.0
>> 1512.0±535.1784749034662
>> 1.1666666666666667±0.41294635409218067

A brief example of arithmatic with numpy arrays with the uncertain dtype is presented below:

a = array([uncertain(5.0, 3.0), uncertain(7.0, 6.0)])
b = array([uncertain(12.0, 4.0), uncertain(24.0, 8.0)])

print(a + b)
print(a - b)
print(a * b)
print(a / b)
>> [uncertain(17.0, 5.0) uncertain(31.0, 10.0)]
>> [uncertain(-7.0, 5.0) uncertain(-17.0, 10.0)]
>> [uncertain(60.0, 41.182520563948) uncertain(168.0, 154.50566332662373)]
>> [uncertain(0.4166666666666667, 0.2859897261385278) uncertain(0.2916666666666667, 0.268238998830944)]

Alternative Methods

In order to accurately propagate uncertainties of related values the derivative of the computed expectation must be known with respect to expectations it is comprised of. Automatic differentiation (autodiff) provides a mechanism for computing the derivative of arbitrary functions with respect to their components by exploiting the fact that all codes, regardless of complexity, are reduced to a sequence of primative arithmetic operations during execution for which the derivatives are known, by applying the chain rule the overall derivative can be determined automatically.

The python package Uncertainties provides a python data type which performs autodiff to propagate the corresponding uncertainity, unforunately due to Implementation as a python object the library is non-performant when used for array math.

Similarly, the python package soerp extends this technique to perform second order error propagation, but suffers from the same issues.

The python package mcerp approximates uncertainities with use of space sampled Monte Carlo methods, but suffers from the same issues.

The python package jacobi propagates uncertainities with use of but is quadratic in space and is therefore inappropriate when used for array math.

Whilst Propagation of Uncertainty with autodiff, describes the use of autodiff provided by the python package JAX in propagating uncertainities for array math. This approach provides accurate estimates with good performance, however its use limits portability between array backends.

See https://DiamondLightSource.github.io/numcertain for more detailed documentation.

About

A python & numpy extension module providing the uncertain data type, allowing propagation of quantified uncertainties

Resources

License

Stars

Watchers

Forks

Packages

No packages published