Skip to main content

Dubious is a Python library for propagating uncertainty through numerical computations

Project description

Dubious

Dubious is a Python library for propagating uncertainty through numerical computations. Instead of collapsing uncertain values into single numbers early, Dubious lets you represent values as probability distributions, combine them with normal arithmetic operations, and only evaluate the resulting uncertainty when you explicitly ask for it.

from dubious import Normal, Beta, Uncertain, Context

normal = Normal(5, 4)
normal2 = Normal(10,2)

x = Uncertain(normal) + Uncertain(normal2)

print(f"variance: {x.var()} mean: {x.mean()} q(0.05): {x.quantile(0.05)}")

Rounded output: variance: 19.9 mean: 15 q(0.05): 7.7


A key idea behind Dubious is lazy uncertainty propagation. We don't calculate aproximations and lose information at each step, instead we build a graph of operations applied to uncertain values. You can construct complex expressions from uncertain inputs in a simple and readable manner, and evaluate the result using Monte Carlo simulations.

Currently all distributions are assumed to be independent, support for dependent distributions is planned for the future. After applying any numerical operations to Uncertain objects, sampling and evaluation only occur when calling a function like mean(), quantile(), etc. is called.

If several instances of the same Uncertain object are involved in an operation these are assumed to represent the same variable so the samples used to calculate these values for each will be identical.

Numpy RNG objects do not need to be provided by default, but they can be optionally provided for any function that generates its result using MC sampling methods, you can alternatively just provide a seed.

Installation

With python v3.9+ pip install dubious

Classes

Distibution(): Currently supporting Normal, LogNormal, Beta and Uniform distributions. Distribution objects also support using other distribution objects for their parameters, although this may lead to unexpected behaviour in cases where parameters can become negative. For each you distribution can get mean(), var(), quantile and sample().

Uncertainty(): Uncertainty objects are the wrapper for distributions that allow them to be used like numeric values. Alongside being able to perform numeric operations on these uncertainty objects, they support the same properties as standard distributions (mean, variance, samping and quantile). You can apply the exact same operations on these objects you might apply to real data, and easily calculate the propagated uncertainty that comes from using several unreliable input values.

Context(): Context objects own the graph through which we manage the uncertainty propagation. You can add uncertainty objects from different contexts, although this is slightly less performant than first creating a context object, and then creating all new uncertainty objects with ctx = Your context object.


Some examples:

from dubious import Normal, Context, Uncertain

#Create a shared context.
ctx = Context()

# Define our Length distribution  (about 10 ± 1)
length_dist = Normal(10, 1)
length = Uncertain(length_dist, ctx=ctx)

# Define Width as 5 ± 0.5
width_dist = Normal(5, 0.5)
width = Uncertain(width_dist, ctx=ctx)

#Compute area using normal arithmetic
area = length * width

#Inspect the uncertainty
print("Mean area:", area.mean())
print("Variance:", area.var())
print("Some samples:", area.sample(5))

We can also use distribution and uncertainty objects as parameters.

from dubious import Normal, Beta, Uncertain, Context

ctx = Context()

#We can define distribution parameters with other distributions.
normal = Normal(10, 1)
beta = Beta(3,normal)

x = Uncertain(normal, ctx=ctx)
y = Uncertain(beta, ctx=ctx)

#Apply some arithmetic.
x = x*y

print(x.sample(5))

#We can also use uncertain distributions to define parameters.
normal3 = Normal(y+2, 3)
print(normal3.mean())

0.2

Added

  • Context objects now handle graph ownership and can be merged, e.g. Uncertain objects from different contexts can be used together.
  • Uncertain objects and Distributions now inherit from Sampleable and can both be used as input parameters
  • Added log, sin, cos, tan, asin, acos and atan operations for Uncertain objects in umath. Umath functions also support normal numbers.

Fixed

  • Some functions had inconsistent requirments regarding numpy generators. Now all do not require one but give the option of either providing one or a seed.

0.1.1

Fixed

  • 0.1 release had a major bug making most uncertain methods unusable on other machines... oops

0.1

  • First release

Added

Distribution objects

  • Normal,
  • LogNormal
  • Uniform
  • Beta
  • Support for using distribution objects as params Uncertainty objects
  • Standard arithmetic through dunder methods

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dubious-0.2.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dubious-0.2.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file dubious-0.2.0.tar.gz.

File metadata

  • Download URL: dubious-0.2.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for dubious-0.2.0.tar.gz
Algorithm Hash digest
SHA256 80dbff8cff8f79014645eb0b4da6279cb9ff518419b196d0e52793d1e0067aa1
MD5 431c588e41521524d5432e78e6efbc36
BLAKE2b-256 8a34989aaebf0bd88ac6690b27618f6dd181fdcb567298cb593a99a78e30fcbf

See more details on using hashes here.

File details

Details for the file dubious-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: dubious-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for dubious-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cecd88449cc3b6153654a4603dfe8fb7701b51a97b1e45c5502eef24305e7856
MD5 6fdfd92113475b48215966e3b063f065
BLAKE2b-256 1162b9085c85910641d8f67b71e3e03c5fa216b9e4f7808d215431fcc8e07d93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page