Skip to main content

AutoUncertainties

Static Badge GitHub Release python JOSS Paper

AutoUncertainties is a package that makes handling linear uncertainty propagation for scientific applications straightforward and automatic using auto-differentiation.

@article{gopalaswamy2025autouncertainties,
  title={AutoUncertainties: A Python Package for Uncertainty Propagation},
  author={Gopalaswamy, Varchas and Mentzer, Ethan},
  journal={Journal of Open Source Software},
  volume={10},
  number={111},
  pages={8037},
  year={2025}
}

Statement of Need

AutoUncertainties is a Python package for uncertainty propagation of independent random variables. It provides a drop-in mechanism to add uncertainty information to Python scalar and NumPy array objects. It implements manual propagation rules for the Python dunder math methods, and uses automatic differentiation via JAX to propagate uncertainties for most NumPy methods applied to both scalar and NumPy array variables. In doing so, it eliminates the need for carrying around additional uncertainty variables or for implementing custom propagation rules for any NumPy operator with a gradient rule implemented by JAX. In most cases, it requires minimal modification to existing code—typically only when uncertainties are attached to central values.

One of the most important aspects of AutoUncertainties is its seamless support for NumPy:

import numpy as np
from auto_uncertainties import Uncertainty

vals = np.array([0.5, 0.75])
errs = np.array([0.05, 0.3])
u = Uncertainty(vals, errs)
print(np.cos(u))  # [0.878 +/- 0.024, 0.73 +/- 0.20]

This is in contrast to the uncertainties package, which would have required the use of unumpy, a module containing several hand-implemented analogs of the true NumPy functions.

Supported Features

  • Scalars
  • Arrays, with support for most NumPy ufuncs and functions

Prerequisites

Python 3.15 or newer is required. Core numerical dependencies are:

  • jax >= 0.9
  • jaxlib >= 0.9
  • numpy >= 2.0

The installer selects versions compatible with your Python version.

Installation

To install, simply run:

pip install auto-uncertainties

Build Documentation

To build the documentation locally, clone the repository, create a virtual Python environment (if desired), and run the following commands within the repository directory:

pip install -e .[docs]
sphinx-build docs/source docs/build

Once built, the docs can be found under the docs/build subdirectory.

CI and Unit Testing

Development of AutoUncertainties relies on a series of unit tests located in the tests directory. These are automatically run using Woodpecker when commits are pushed to Codeberg. To run the tests manually, sync the development dependency group with uv:

uv sync --locked --group ci
uv run --locked --group ci pytest --cov --cov-report=term

Run the same lint, formatting, type, and generated-export checks used by CI:

uv run --locked --group ci ruff check .
uv run --locked --group ci ruff format --check .
uv run --locked --group ci pyrefly check
uv run --locked --group ci geninit --check

Package initializers are generated by geninit from module __all__ declarations, using Python 3.15 native lazy imports. Run uv run --locked --group ci geninit after changing exports; generated initializers should not be edited manually. The pre-commit hooks use these same tools and locked dependency versions. Static checking is configured in pyrefly.toml.

Basic Usage

  • Creating a scalar Uncertainty variable is relatively simple:

    from auto_uncertainties import Uncertainty
    
    value = 1.0
    error = 0.1
    u = Uncertainty(value, error)
    print(u)  # 1.00 +/- 0.10
    

    As is creating a NumPy array of Uncertainties:

    from auto_uncertainties import Uncertainty
    import numpy as np
    
    value = np.linspace(start=0, stop=10, num=5)
    error = np.ones_like(value) * 0.1
    u = Uncertainty(value, error)
    print(u)  # [0.00 +/- 0.10, 2.50 +/- 0.10, 5.00 +/- 0.10, 7.50 +/- 0.10, 10.00 +/- 0.10]
    

    The Uncertainty class automatically determines which methods should be implemented based on whether it represents a vector uncertainty, or a scalar uncertainty. When instantiated with sequences or NumPy arrays, vector-based operations are enabled; when instantiated with scalars, only scalar operations are permitted.

  • Scalar uncertainties implement all mathematical and logical dunder methods explicitly using linear uncertainty propagation.

    from auto_uncertainties import Uncertainty
    
    u = Uncertainty(10.0, 3.0)
    v = Uncertainty(20.0, 4.0)
    print(u + v)  # 30 +/- 5
    
  • Array uncertainties implement a large subset of the NumPy ufuncs and methods using JAX automatic differentiation.

    from auto_uncertainties import Uncertainty
    import numpy as np
    
    value = np.linspace(start=0, stop=10, num=5)
    error = np.ones_like(value) * 0.1
    u = Uncertainty(value, error)
    print(np.exp(u))
    # [1.00 +/- 0.10, 12.2 +/- 1.2, 148 +/- 15, 1.81e+3 +/- 1.8e+2, 2.20e+4 +/- 2.2e+3]
    
    print(np.sum(u))  # 25.00 +/- 0.22
    print(u.sum())  # 25.00 +/- 0.22
    print(np.sqrt(np.sum(error**2)))  # 0.223606797749979
    
  • The central value, uncertainty, and relative error are available as attributes:

    from auto_uncertainties import Uncertainty
    
    u = Uncertainty(10.0, 3.0)
    print(u.value)  # 10.0
    print(u.error)  # 3.0
    print(u.relative)  # 0.3
    
  • To strip central values and uncertainty from arbitrary variables, accessor functions nominal_values and std_devs are provided:

    from auto_uncertainties import nominal_values, std_devs
    
    u = Uncertainty(10.0, 3.0)
    v = 5.0
    print(nominal_values(u))  # 10.0
    print(std_devs(u))  # 3.0
    
    print(nominal_values(v))  # 5.0
    print(std_devs(v))  # 0.0
    
  • Displayed values follow the Particle Data Group (PDG) rounding rules by default. Use configure(round_on_display=False) to disable PDG rounding and display values with the g format specifier:

    from auto_uncertainties import Uncertainty, configure
    import numpy as np
    
    value = np.linspace(start=0, stop=10, num=5)
    error = np.ones_like(value) * 0.1
    u = Uncertainty(value, error)
    print(np.sum(u))  # 25.00 +/- 0.22
    configure(round_on_display=False)  # disable PDG rounding
    print(np.sum(u))  # 25 +/- 0.223607
    configure(round_on_display=True)  # restore the default
    

    PDG rounding displays Uncertainty objects with:

    • Error to one or two significant digits, following the PDG thresholds.
    • Central value to the same decimal place as the rounded error.

    Rounding affects display only; .value and .error retain their stored precision.

  • If numpy.array is called on an Uncertainty object, it will automatically get cast down to a numpy array (losing all uncertainty information!), and emit a warning. To force an exception to be raised instead, use configure:

    from auto_uncertainties import Uncertainty, configure
    import numpy as np
    
    configure(error_on_downcast=True)
    value = np.linspace(start=0, stop=10, num=5)
    error = np.ones_like(value) * 0.1
    u = Uncertainty(value, error)
    print(np.array(u))
    # Traceback (most recent call last):
    #     ...
    # auto_uncertainties.exceptions.DowncastError: The uncertainty is stripped when downcasting to ndarray.
    

Propagating Through Custom Functions

Use @propagate_uncertainties (or @propagate_uncertainties()) to propagate errors through a JAX-compatible function. The function receives nominal values and should use jax.numpy for array operations. Uncertain inputs can be positional arguments, keyword arguments, or leaves of nested lists, tuples, and dictionaries. Outputs can have different shapes from the inputs and can use the same nested structures.

import jax.numpy as jnp
from auto_uncertainties import Uncertainty, propagate_uncertainties


@propagate_uncertainties
def scale_and_sum(values, *, gain):
    scaled = values * gain
    return {"scaled": scaled, "total": jnp.sum(scaled)}


result = scale_and_sum(Uncertainty([1.0, 2.0], [0.1, 0.2]), gain=Uncertainty(2.0, 0.05))
print(result["scaled"])  # [2.00 +/- 0.21, 4.0 +/- 0.4]
print(result["total"])  # 6.0 +/- 0.5

The decorator uses the full Jacobian to combine independent input errors for each output element. Reusing an input within the function is accounted for by differentiation; correlations between separate returned values are not stored. Keeping a complete calculation inside the decorated function preserves the shared effect of inputs such as gain when computing its total.

Functions that mix array elements work too, and tuple outputs preserve their structure:

@propagate_uncertainties()
def center_and_mean(values):
    mean = jnp.mean(values)
    return values - mean, mean


centered, mean = center_and_mean(Uncertainty([1.0, 2.0], [0.1, 0.2]))
print(centered)  # [-0.50 +/- 0.11, 0.50 +/- 0.11]
print(mean)  # 1.50 +/- 0.11

Each centered value depends on both inputs. Its error is sqrt(0.1**2 + 0.2**2) / 2, approximately 0.1118, as is the mean's error. Correlations are not retained between separate calls or subsequent operations on returned objects.

Dtypes follow JAX promotion. Float64 inputs enable JAX's 64-bit mode locally for the call, restoring the caller's setting afterward. Results use NumPy-backed containers, so the decorated wrapper itself cannot be used inside jax.jit.

Current Limitations and Future Work

Dependent Random Variables

To simplify operations on Uncertainty objects, AutoUncertainties assumes all variables are independent. This means that, in the case where the programmer assumes dependence between two or more Uncertainty objects, unexpected and counter-intuitive behavior may arise during uncertainty propagation. This is a common pitfall when working with Uncertainty objects, especially since the package will not prevent you from manipulating variables in a manner that implies dependence.

  • Subtracting Equivalent Uncertainties

    Subtracting an Uncertainty from itself will not result in a standard deviation of zero:

    x = Uncertainty(5.0, 0.5)
    print(x - x)  # 0.0 +/- 0.7
    
  • Mean Error Propagation

    When multiplying a vector by a scalar Uncertainty object, each component of the resulting vector is assumed to be a multivariate normal distribution with no covariance, which may not be the desired behavior. For instance, taking the mean of such a vector will return an Uncertainty object with an unexpectedly small standard deviation.

    u = Uncertainty(5.0, 0.5)
    arr = np.ones(10) * 10
    print(np.mean(u * arr))  # 50.0 +/- 1.6, rather than 50 +/- 5 as expected
    

    To obtain the uncertainty corresponding to the case where each element of the array is fully correlated, two workaround techniques can be used:

    1. Separate the central value from the relative error, multiply the vector by the central value, take the mean of the resulting vector, and then multiply by the previously stored relative error.

      u = Uncertainty(5.0, 0.5)
      scale_error = Uncertainty(1, u.relative)  # collect relative error
      scale_value = u.value  # collect central value
      
      arr = np.ones(10) * 10
      print(np.mean(scale_value * arr) * scale_error)  # 50 +/- 5
      
    2. Take the mean of the vector, and then multiply by the Uncertainty:

      u = Uncertainty(5.0, 0.5)
      arr = np.ones(10) * 10
      print(u * np.mean(arr))  # 50 +/- 5
      

These workarounds are nevertheless cumbersome, and cause AutoUncertainties to fall somewhat short of the original goals of automated error propagation. In principle, this could be addressed by storing a full computational graph of the result of chained operations, similar to what is done in uncertainties. However, the complexity of such a system places it out of scope for AutoUncertainties at this time.

It should be noted that, in cases where random variables have covariance that lies somewhere between fully correlated and fully independent, calculations like those described above would be more complex. To accurately propagate uncertainty, one would need to specify individual correlations between each variable, and adjust the computation as necessary. This is also currently out of scope for AutoUncertainties.

Inspirations

The class structure of Uncertainty and the NumPy ufunc implementation is heavily inspired by the excellent package Pint.

Configuration

Settings are initialized from environment variables with the AUTO_UNCERTAINTIES_ prefix when the configuration module is first imported. Environment variable names are case-insensitive.

Setting Default Environment variable
round_on_display True AUTO_UNCERTAINTIES_ROUND_ON_DISPLAY
error_on_downcast False AUTO_UNCERTAINTIES_ERROR_ON_DOWNCAST
error_on_equality False AUTO_UNCERTAINTIES_ERROR_ON_EQUALITY
compare_rtol 1e-9 AUTO_UNCERTAINTIES_COMPARE_RTOL
error_on_numpy_kwargs True AUTO_UNCERTAINTIES_ERROR_ON_NUMPY_KWARGS
warn_on_numpy_kwargs True AUTO_UNCERTAINTIES_WARN_ON_NUMPY_KWARGS
nonfinite_error_policy "zero" AUTO_UNCERTAINTIES_NONFINITE_ERROR_POLICY

error_on_numpy_kwargs and warn_on_numpy_kwargs are retained for configuration compatibility and no longer affect propagation. Uncertain keyword arguments are supported.

For example, set these variables before starting Python:

export AUTO_UNCERTAINTIES_NONFINITE_ERROR_POLICY=propagate
export AUTO_UNCERTAINTIES_ROUND_ON_DISPLAY=true

Inspect or update configuration at runtime:

from auto_uncertainties import Uncertainty, configure, get_settings

configure(nonfinite_error_policy="propagate", round_on_display=True)
print(get_settings().nonfinite_error_policy)  # propagate
u = Uncertainty(1.0, float("nan"))
print(u)  # nan +/- nan

The default "zero" policy replaces NaN and infinite errors with zero. Under "propagate", NaN in either value or error sets both to NaN, element by element for arrays. Positive infinite errors are preserved; negative errors, including negative infinity, raise NegativeStdDevError.

Releases

To publish a release, push an annotated version tag such as v1.1.0 to Codeberg. Woodpecker runs tests on Python 3.15, then builds and validates the wheel and source distribution before uploading them to PyPI using the CI secret pypi_token. Branch pushes and pull requests run tests without publishing. The publishing workflow can also be triggered manually.

Metadata

Release files for auto-uncertainties 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for auto-uncertainties 1.2.0
File Size Uploaded Provenance Actions
auto_uncertainties-1.2.0.tar.gz 130.4 kB Without provenance Details for auto_uncertainties-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for auto-uncertainties 1.2.0
File Interpreter ABI Platform Actions
auto_uncertainties-1.2.0-py3-none-any.whl Python 3 none any Details for auto_uncertainties-1.2.0-py3-none-any.whl

Total release size: 233.0 kB

Release files / auto_uncertainties-1.2.0.tar.gz

Download URL auto_uncertainties-1.2.0.tar.gz
Size 130.4 kB
Tags Source
SHA-256 checksum
How to use checksums
af6965ad344362282b93bf209030895b80699952739c777a354f4288c9eac6c1
BLAKE2b-256 checksum
How to use checksums
da2d5f4b185a82a2a8968ac35f4a13cfff1eaa8ae9b31d81fe16f9c9a5b4e6f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Release files / auto_uncertainties-1.2.0-py3-none-any.whl

Download URL auto_uncertainties-1.2.0-py3-none-any.whl
Size 102.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
25fdc8e0ee2f497da2eef9d91ed0d8015222e240179c9536af0e871718de97a5
BLAKE2b-256 checksum
How to use checksums
3e986c5748fa975df343f491f92ff49295f4a758413523cf2889f890ff66ce6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Security for auto-uncertainties 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page