pyuncertainnumber.pba.pbox_abc

Classes

Pbox

a base class for Pbox

Staircase

distribution free p-box

Leaf

parametric pbox

Cbox

a base class for Pbox

Functions

bound_steps_check(bound)

pbox_from_extredists(rvs[, shape, extre_bound_params])

transform into pbox object from extreme bounds parameterised by sps.dist

pbox_from_ecdf_bundle(→ Pbox)

Construct a p-box from two empirical CDF bundles as the extreme bounds

naive_frechet_pbox(→ Staircase)

A wrapper that returns a Pbox from the naive Frechet operation

vectorised_naive_frechet_pbox(→ Staircase)

A wrapper that returns a Pbox from the naive Frechet operation

classic_frechet_pbox(→ Staircase)

this corresponds to the Frank, Nelson and Sklar Frechet bounds implementation

straddle_frechet_pbox(x, y)

bespoke Frechet for multiplcation when anyone straddles 0

nagative_frechet_pbox(x, y)

frechet_pbox_mul(x, y)

the overall pbox

is_un(un)

if the un is modelled by accepted constructs

convert_pbox(un)

transform the input un into a Pbox object

pbox_number_ops(pbox, n, f)

blueprint for arithmetic between pbox and real numbers

truncate(pbox, min, max)

sin()

cos()

tanh()

exp()

log()

sqrt()

simple_stacking(itvls)

simple version of stacking vector Interval objects into pbox

inspect_pbox(pbox)

quickly inspect a pbox object

Module Contents

pyuncertainnumber.pba.pbox_abc.bound_steps_check(bound)
pyuncertainnumber.pba.pbox_abc.pbox_from_extredists(rvs, shape='beta', extre_bound_params=None)

transform into pbox object from extreme bounds parameterised by sps.dist

Parameters:

rvs (list) – list of scipy.stats.rv_continuous objects

pyuncertainnumber.pba.pbox_abc.pbox_from_ecdf_bundle(lower_bound: pyuncertainnumber.pba.ecdf.eCDF_bundle, upper_bound: pyuncertainnumber.pba.ecdf.eCDF_bundle) Pbox

Construct a p-box from two empirical CDF bundles as the extreme bounds

pyuncertainnumber.pba.pbox_abc.naive_frechet_pbox(x, y, op) Staircase

A wrapper that returns a Pbox from the naive Frechet operation

Note

old implementation from pba.r

pyuncertainnumber.pba.pbox_abc.vectorised_naive_frechet_pbox(x, y, op) Staircase

A wrapper that returns a Pbox from the naive Frechet operation

pyuncertainnumber.pba.pbox_abc.classic_frechet_pbox(x, y, op) Staircase

this corresponds to the Frank, Nelson and Sklar Frechet bounds implementation

pyuncertainnumber.pba.pbox_abc.straddle_frechet_pbox(x, y)

bespoke Frechet for multiplcation when anyone straddles 0

pyuncertainnumber.pba.pbox_abc.nagative_frechet_pbox(x, y)
pyuncertainnumber.pba.pbox_abc.frechet_pbox_mul(x, y)

the overall pbox

class pyuncertainnumber.pba.pbox_abc.Pbox(left: numpy.ndarray | list, right: numpy.ndarray | list, steps=Params.steps, mean=None, var=None, p_values=None)

Bases: pyuncertainnumber.pba.mixins.NominalValueMixin, abc.ABC

a base class for Pbox

Danger

this is an abstract class and should not be instantiated directly.

See also

pbox_abc.Staircase and pbox_abc.Leaf for concrete implementations.

property left
property right
steps = 200
mean = None
var = None
_pvalues
abstractmethod _init_moments()
_init_range()
post_init_check()
steps_check()
_compute_nominal_value()
degenerate_flag() bool

check if the pbox is degenerate (i.e. left == right everywhere)

property degenerate: bool
property p_values
property range
property lo

Returns the left-most value in the interval

property hi

Returns the right-most value in the interval

property support
property median
property enclosed_area

the enclosed area between the two extreme cdfs

__iter__()
__eq__(other)

Equality operator for Pbox objects

Note

  • two pboxes are equal if their left and right bounds are equal

__contains__(item)
to_interval()

discretise pbox into a vec-interval of length of default steps

Note

If desired a custom length of vec-interval as output, use discretise() method.

to_dss(discretisation=Params.steps)

convert pbox to DempsterShafer object

to_numpy()

convert pbox to a 2D numpy array (n, 2) of left and right

class pyuncertainnumber.pba.pbox_abc.Staircase(left, right, steps=200, mean=None, var=None, p_values=None)

Bases: Pbox

distribution free p-box

_init_moments()

Initialize mean/var interval estimates.

strategy:
  1. Try LP-based bounds.

  2. If that fails, try ECDF-based bounds.

  3. If that also fails, set to NaN intervals so the program continues.

This function NEVER raises.

__repr__()
plot(title=None, ax=None, style='box', fill_color='lightgray', bound_colors=None, bound_styles=None, left_line_kwargs=None, right_line_kwargs=None, nuance='step', alpha=0.3, **kwargs)

default plotting function

Parameters:
  • style (str) – ‘box’ or ‘simple’

  • fill_color (str) – color to fill the box (only for ‘box’ style)

  • bound_colors (list) – list of two colors for left and right bound lines

  • bound_styles (list) – list of two linestyles for left and right bound lines

  • left_line_kwargs (dict) – additional kwargs for left bound line

  • right_line_kwargs (dict) – additional kwargs for right bound line

  • nuance (str) – ‘step’ or ‘curve’ for bound line styles

  • alpha (float) – transparency level for the box fill (only for ‘box’ style)

  • **kwargs – additional keyword arguments for the plot

Note

Two styles are supported: a ‘box’ with fill-in color and a ‘simple’ one without fill-in color. Color and linestyle of the bound lines can be customized via the bound_styles, left_line_kwargs, and right_line_kwargs parameters. The argument nuance controls whether the bound lines are plotted as step functions (‘step’) or smooth curves (‘curve’).

Example

>>> a = pba.normal([2, 6], [0.5, 1])
>>> fig, ax = plt.subplots()
>>> a.plot(ax=ax, style='simple')  # simple style without fill-in color
>>> # box style with fill-in color and also customized bound colors
>>> a.plot(ax=ax, style='box',
...     fill_color='lightblue',
...     bound_colors = ['lightblue', 'lightblue'],
...     bound_styles=("--", ":"),
...     alpha=0.5
... )
>>> # customized left and right bound line styles
>>> ax = pbox.plot(
...     left_line_kwargs={"linestyle": "--", "linewidth": 2},
...     right_line_kwargs={"linestyle": ":", "linewidth": 2, "alpha": 0.8},
)
plot_reverse_axis(title=None, ax=None, style='box', fill_color='lightgray', bound_colors=None, nuance='step', alpha=0.3, orientation='xy', invert_xaxis=True, **kwargs)

A testing plotting function that can swap quantile and probability axes.

Parameters:
  • style (str) – ‘box’ or ‘simple’

  • orientation (str) – ‘xy’ keeps x on horizontal and Pr(X<=x) on vertical; ‘yx’ swaps them.

plot_outside_legend(title=None, ax=None, style='box', fill_color='lightgray', bound_colors=None, nuance='step', alpha=0.3, **kwargs)

a specific variant of plot() which is used for scipy proceeding only.

Parameters:

style (str) – ‘box’ or ‘simple’

display(*args, **kwargs)
plot_probability_bound(x: float, ax=None, linecolor='r', markercolor='r', **kwargs)

plot the probability bound at a certain quantile x

Note

  • a vertical line

plot_quantile_bound(p: float, ax=None, **kwargs)

plot the quantile bound at a certain probability level p

Note

  • a horizontal line

classmethod from_CDFbundle(a, b)

pbox from two emipirical CDF bundle

Parameters:
  • a (-) – CDF bundle of lower extreme F;

  • b (-) – CDF bundle of upper extreme F;

__neg__()
__add__(other)
__radd__(other)
__sub__(other)
__rsub__(other)
__mul__(other)
__rmul__(other)
__truediv__(other)
__rtruediv__(other)
__pow__(other)
__rpow__(other: numbers.Number)

Power operation with the base as other and self as the exponent

__array_ufunc__(ufunc, method, *inputs, **kwargs)
cdf(x: numpy.ndarray)

get the bounds on the cdf w.r.t x value

Parameters:

x (array-like) – x values

alpha_cut(alpha=0.5)

test the lightweight alpha_cut method

Parameters:

alpha (array-like) – probability levels

sample(n_sam)

LHS sampling by default

precise_sample(n_a: int, theta: float = None, n_e: int = None)

Generate precise samples from a p-box

discretise(n=None) pyuncertainnumber.Interval

alpha-cut discretisation of the p-box without outward rounding

Parameters:

n (int) – number of steps to be used in the discretisation.

Returns:

vector Interval

outer_discretisation(n=None)

discretisation of a p-box to get intervals based on the scheme of outer approximation

Parameters:

n (int) – number of steps to be used in the discretisation

Note

the_interval_list will have length one less than that of default p_values (i.e. 100 and 99)

Returns:

the outer intervals in vec-Interval form

condensation(n) Self

ourter condensation of the pbox to reduce the number of steps and get a sparser staircase pbox

Parameters:

n (int) – number of steps to be used in the discretisation

Note

Have not thought about a better name so we call it condensation for now. Candidate names include ‘approximation’. It will ouput a p-box and keep steps as 200 for computational consistency.

Example

>>> p.condensation(n=5)
Returns:

a staircase p-box that looks sparser but has the same number of steps

condense(n) pyuncertainnumber.pba.dss.DempsterShafer

Another condensation function which has steps of n

Compared to the above condensation method that ouputs a p-box and keeps steps as 200 for computational consistency. This one condenses in a more literal manner, as in having n steps in the resulting Dempster-Shafer structure.

truncate(a, b)

Truncate the Pbox to the range [a, b].

example: >>> from pyuncertainnumber import pba >>> p = pba.normal([4, 9], 1) >>> tr = p.truncate(3, 8) >>> fig, ax = plt.subplots() >>> p.plot(ax=ax) >>> tr.plot(ax=ax, fill_color=’r’) >>> plt.show()

min(other, method='f')

Returns a new Pbox object that represents the element-wise minimum of two Pboxes.

Parameters:
  • other (-) – Another Pbox object or a numeric value.

  • method (-) – Calculation method to determine the minimum. Can be one of ‘f’, ‘p’, ‘o’, ‘i’.

Returns:

Pbox

max(other, method='f')
get_PI(alpha: numbers.Number = 0.95, style='narrowest') pyuncertainnumber.Interval

Compute the predictive interval at the coverage level of alpha

Parameters:
  • alpha (Number) – coverage level for the predictive interval, default is 0.95

  • style (str) – ‘narrowest’ or ‘widest’, default is ‘narrowest’

Note

by default, narrowest predictive interval is returned; when the narrowest does not exist, a warning will the generated and then the widest is returned instead.

Example

>>> from pyuncertainnumber import pba
>>> p = pba.normal([10, 15, 1])
>>> p.get_PI(alpha=0.95, style='narrowest')
straddles(N, endpoints=True) bool

Check whether the p-box straddles a number N

Parameters:
  • N (float) – the Number to check

  • endpoints (Boolean) – Whether to include the endpoints within the check

Returns:

True

If \(\mathrm{left} \leq N \leq \mathrm{right}\) (Assuming endpoints=True)

False

Otherwise

Note

This could affect the results of Frechet bounds

straddles_zero() bool

Checks specifically whether \(0\) is within the p-box

is_zero()
is_nagative()
env(other)

computes the envelope of two Pboxes.

Parameters:

other (Pbox)

Returns:

  • Pbox

imp(other)

Returns the imposition of self with other pbox

Note

  • binary imposition between two pboxes only

_unary_template(f)
exp()

exponential function: e^x

sqrt()

square root function: √x

reciprocal()

Calculate the reciprocal of the pbox

Note

the pbox should not straddle zero, otherwise a warning is raised

log()

natural logarithm of the pbox

Note

  • the pbox must be positive

sin()
cos()
tanh()
add(other, dependency='f')
sub(other, dependency='f')
mul(other, dependency='f')

Multiplication of uncertain numbers with the defined dependency dependency

div(other, dependency='f')
pow(other, dependency='f')

Exponentiation of uncertain numbers with the defined dependency dependency

This suggests that the exponent (i.e. other) can also be an uncertain number.

balchprod(other)

Frechet convolution of two pboxes when any of them straddles zero

class pyuncertainnumber.pba.pbox_abc.Leaf(left=None, right=None, steps=200, mean=None, var=None, dist_params=None, shape=None)

Bases: Staircase

parametric pbox

shape = None
dist_params = None
_init_moments_range()
__repr__()
sample(n_sam)

sample from a parametric pbox or distribution

class pyuncertainnumber.pba.pbox_abc.Cbox(left, right, steps=200)

Bases: Pbox

a base class for Pbox

Danger

this is an abstract class and should not be instantiated directly.

See also

pbox_abc.Staircase and pbox_abc.Leaf for concrete implementations.

pyuncertainnumber.pba.pbox_abc.is_un(un)

if the un is modelled by accepted constructs

pyuncertainnumber.pba.pbox_abc.convert_pbox(un)

transform the input un into a Pbox object

Note

  • theorically ‘un’ can be {Interval, DempsterShafer, Distribution, float, int}

pyuncertainnumber.pba.pbox_abc.pbox_number_ops(pbox: Pbox, n: numbers.Number, f: callable)

blueprint for arithmetic between pbox and real numbers

pyuncertainnumber.pba.pbox_abc.truncate(pbox, min, max)
pyuncertainnumber.pba.pbox_abc.sin()
pyuncertainnumber.pba.pbox_abc.cos()
pyuncertainnumber.pba.pbox_abc.tanh()
pyuncertainnumber.pba.pbox_abc.exp()
pyuncertainnumber.pba.pbox_abc.log()
pyuncertainnumber.pba.pbox_abc.sqrt()
pyuncertainnumber.pba.pbox_abc.simple_stacking(itvls)

simple version of stacking vector Interval objects into pbox

Parameters:

itvls (Interval) – a vector Interval object to be stacked

Note

  • only meant for quick use during development

  • see stacking function for production use

pyuncertainnumber.pba.pbox_abc.inspect_pbox(pbox)

quickly inspect a pbox object