UncertainArray

UncertainArray(name, μ, σ2[, stdzr])

Structured array containing mean and variance of a normal distribution at each point.

Methods

UncertainArray.BC(other)

Bhattacharyya Coefficient

UncertainArray.BD(other)

Bhattacharyya Distance

UncertainArray.EI(target, best_yet[, k])

Expected improvement

UncertainArray.HD(other)

Hellinger Distance

UncertainArray.KLD(other)

Kullback–Leibler Divergence

UncertainArray.mean([axis, dtype, out, keepdims])

Mean with uncertainty propagation

UncertainArray.nlpd(target)

Negative log posterior density

UncertainArray.stack(uarray_list[, axis])

UncertainArray.sum([axis, dtype, out, keepdims])

Summation with uncertainty propagation

Attributes

UncertainArray.dist

Array of scipy.stats.norm() objects

UncertainArray.μ

Nominal value (mean)

UncertainArray.σ

Standard deviation

UncertainArray.σ2

Variance

class gumbi.arrays.UncertainArray(name: str, μ: ndarray, σ2: ndarray, stdzr=None, **kwargs)

Bases: ndarray

Structured array containing mean and variance of a normal distribution at each point.

The main purpose of this object is to correctly propagate uncertainty under transformations. Arithmetic operations between distributions or between distributions and scalars are handled appropriately via the uncertainties package.

Additionally, a scipy Normal distribution object can be created at each point through the dist property, allowing access to that objects such as rvs(), ppf(), pdf(), etc.

This class can also be accessed through the alias uarray.

Notes

The name argument is intended to be the general name of the value held, not unique to this instance. Combining two UncertainArray objects with the same name results in a new object with that name; combining two objects with different names results in a new name that reflects this combination (so 'A'+'B' becomes '(A+B)').

Parameters:
  • name (str) – Name of variable.

  • μ (array) – Mean at each point

  • σ2 (array) – Variance at each point

  • **kwargs – Names and values of additional arrays to store

Examples

>>> ua1 = UncertainArray('A', μ=1, σ2=0.1)
>>> ua2 = uarray('A', μ=2, σ2=0.2)  #equivalent
>>> ua2
A['μ', 'σ2']: (2, 0.2)

Addition of a scalar

>>> ua1+1
A['μ', 'σ2']: (2, 0.1)

Addition and subtraction of two UncertainArrays:

>>> ua2+ua1
A['μ', 'σ2']: (3., 0.3)
>>> ua2-ua1
A['μ', 'σ2']: (1., 0.3)

Note, however, that correlations are not properly accounted for (yet). Subtracting one UncertainArray from itself should give exactly zero with no uncertainty, but it doesn’t:

>>> ua2+ua2
A['μ', 'σ2']: (4., 0.4)
>>> ua2-ua2
A['μ', 'σ2']: (0., 0.4)

Mean of two uarray objects:

>>> uarray.stack([ua1, ua2]).mean(axis=0)
A['μ', 'σ2']: (1.5, 0.075)

Mean within a single uarray object:

>>> ua3 = uarray('B', np.arange(1,5)/10, np.arange(1,5)/100)
>>> ua3
B['μ', 'σ2']: [(0.1, 0.01) (0.2, 0.02) (0.3, 0.03) (0.4, 0.04)]
>>> ua3.μ
array([0.1, 0.2, 0.3, 0.4])
>>> ua3.mean()
B['μ', 'σ2']: (0.25, 0.00625)

Adding two uarrays with differnt name creates an object with a new name

>>> ua1+ua3.mean()
(A+B)['μ', 'σ2']: (1.25, 0.10625)

Accessing dist methods

>>> ua3.dist.ppf(0.95)
array([0.26448536, 0.43261743, 0.58489701, 0.72897073])
>>> ua3.dist.rvs([3,*ua3.shape])
array([[0.05361942, 0.14164882, 0.14924506, 0.03808633],
       [0.05804824, 0.09946732, 0.08727794, 0.28091272],
       [0.06291355, 0.47451576, 0.20756356, 0.2108717 ]])  # random
name

Name of variable.

Type:

str

fields

Names of each level held in the array

Type:

list of str

BC(other)

Bhattacharyya Coefficient

Parameters:

other (UncertainArray) –

Returns:

coefficient

Return type:

float

BD(other)

Bhattacharyya Distance

Parameters:

other (UncertainArray) –

Returns:

distance

Return type:

float

EI(target, best_yet, k=1) float

Expected improvement

Taken from https://github.com/akuhren/target_vector_estimation

Parameters:
  • target (float) –

  • best_yet (float) –

  • k (int) –

Returns:

EI

Return type:

float

HD(other)

Hellinger Distance

Parameters:

other (UncertainArray) –

Returns:

distance

Return type:

float

KLD(other)

Kullback–Leibler Divergence

Parameters:

other (UncertainArray) –

Returns:

divergence

Return type:

float

property dist: rv_continuous

Array of scipy.stats.norm() objects

mean(axis=None, dtype=None, out=None, keepdims=False, **kwargs) UncertainArray

Mean with uncertainty propagation

nlpd(target) float

Negative log posterior density

sum(axis=None, dtype=None, out=None, keepdims=False, **kwargs) UncertainArray

Summation with uncertainty propagation

property μ: ndarray

Nominal value (mean)

property σ: ndarray

Standard deviation

property σ2: ndarray

Variance