pyuncertainnumber.pba.utils

Exceptions

NotIncreasingError

Common base class for all non-exit exceptions.

Classes

Functions

extend_ecdf(cdf)

add zero and one to the ecdf

sorting(list1, list2)

reweighting(*masses)

reweight the masses to sum to 1

uniform_reparameterisation(a, b)

reparameterise the uniform distribution to a, b

find_nearest(array, value)

Find index/indices of nearest value(s) in array to each value.

plot_intervals(vec_interval[, ax])

plot the intervals in a vectorised form

read_json(file_name)

is_increasing(arr)

check if 'arr' is increasing

condensation(bound, number)

a joint implementation for condensation

condensation_bounds(bounds, number)

condense the bounds of number pbox

condensation_bound(bound, number)

condense the bounds of number pbox

smooth_condensation(bounds[, number])

equi_selection(arr, n)

draw n equidistant points from the array

create_colored_edge_box(x0, y0, width, height[, linewidth])

expose_functions_as_public(mapping, wrapper)

expose private functions as public APIs

left_right_switch(left, right)

build_constraints_from_pbox_robust(q_a, p_a, q_b, p_b, ...)

Returns x, L, U, n and also the envelopes F_L, F_U on x_grid.

variance_bounds_via_lp(q_a, p_a, q_b, p_b, x_grid[, ...])

get_mean_var_from_ecdf(q, p)

Numerically estimate the mean and var from ECDF data

sample_ecdf_in_pbox(q_a, p_a, q_b, p_b[, x_grid, n, ...])

Sample a random ECDF (quantile & probability vectors) lying inside the p-box

area_between_ecdfs(x_upper, p_upper, x_lower, p_lower)

Compute the area between two ECDFs defined by (x_upper, p_upper) and (x_lower, p_lower).

_ecdf_value_on_left_of_intervals(x, p, grid_left)

Right-continuous ECDF value used on [grid_left[k], grid_right[k]).

Module Contents

pyuncertainnumber.pba.utils.extend_ecdf(cdf)

add zero and one to the ecdf

Parameters:

CDF_bundle

pyuncertainnumber.pba.utils.sorting(list1, list2)
pyuncertainnumber.pba.utils.reweighting(*masses)

reweight the masses to sum to 1

pyuncertainnumber.pba.utils.uniform_reparameterisation(a, b)

reparameterise the uniform distribution to a, b

pyuncertainnumber.pba.utils.find_nearest(array, value)

Find index/indices of nearest value(s) in array to each value.

Efficient for both scalar and array inputs.

pyuncertainnumber.pba.utils.plot_intervals(vec_interval: list[pyuncertainnumber.pba.intervals.number.Interval], ax=None, **kwargs)

plot the intervals in a vectorised form :param vec_interval: vectorised interval objects

pyuncertainnumber.pba.utils.read_json(file_name)
pyuncertainnumber.pba.utils.is_increasing(arr)

check if ‘arr’ is increasing

exception pyuncertainnumber.pba.utils.NotIncreasingError

Bases: Exception

Common base class for all non-exit exceptions.

pyuncertainnumber.pba.utils.condensation(bound, number: int)

a joint implementation for condensation

Parameters:
  • number (int) – the number to be reduced

  • bound (array-like) – either the left or right bound to be reduced

Note

It will keep the first and last from the bound

pyuncertainnumber.pba.utils.condensation_bounds(bounds, number)

condense the bounds of number pbox

Parameters:
  • number (int) – the number to be reduced

  • bounds (list or tuple) – the left and right bound to be reduced

pyuncertainnumber.pba.utils.condensation_bound(bound, number)

condense the bounds of number pbox

Parameters:
  • number (int) – the number to be reduced

  • bound (array-like) – either the left or right bound to be reduced

pyuncertainnumber.pba.utils.smooth_condensation(bounds, number=200)
pyuncertainnumber.pba.utils.equi_selection(arr, n)

draw n equidistant points from the array

pyuncertainnumber.pba.utils.create_colored_edge_box(x0, y0, width, height, linewidth=1)
class pyuncertainnumber.pba.utils.CustomEdgeRectHandler

Bases: matplotlib.legend_handler.HandlerBase

create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)
pyuncertainnumber.pba.utils.expose_functions_as_public(mapping, wrapper)

expose private functions as public APIs

Parameters:
  • mapping (dict) – a dictionary containing private function names mapped to public APIs

  • wrapper (callable) – a function that wraps the original functions (e.g., the decorator UNtoUN)

Note

the decorator which wraps the original function returning Pbox into returning UN, hence making the public UN API

pyuncertainnumber.pba.utils.left_right_switch(left, right)

Note

right quantile should be greater and equal than left quantile

pyuncertainnumber.pba.utils.build_constraints_from_pbox_robust(q_a, p_a, q_b, p_b, x_grid, n=None, eps=1e-12)

Returns x, L, U, n and also the envelopes F_L, F_U on x_grid. If F_L==F_U (degenerate p-box), we set L=U=round(n*F) exactly to avoid infeasibility.

pyuncertainnumber.pba.utils.variance_bounds_via_lp(q_a, p_a, q_b, p_b, x_grid, n=None, mu_grid=101)
pyuncertainnumber.pba.utils.get_mean_var_from_ecdf(q, p)

Numerically estimate the mean and var from ECDF data

Parameters:
  • q (array-like) – quantiles

  • p (array-like) – probabilities

Example

>>> # Given ECDF data an example
>>> q = [1, 2, 3, 4]
>>> p = [0.25, 0.5, 0.75, 1.0]
>>> mean, var = get_mean_var_from_ecdf(q, p)
pyuncertainnumber.pba.utils.sample_ecdf_in_pbox(q_a, p_a, q_b, p_b, x_grid=None, n=None, rng=None, eps=1e-12)

Sample a random ECDF (quantile & probability vectors) lying inside the p-box defined by lower envelope (q_a, p_a) and upper envelope (q_b, p_b).

Parameters:
  • q_a – arrays Lower (left) bounding quantile function sampled at probabilities p_a.

  • p_a – arrays Lower (left) bounding quantile function sampled at probabilities p_a.

  • q_b – arrays Upper (right) bounding quantile function sampled at probabilities p_b.

  • p_b – arrays Upper (right) bounding quantile function sampled at probabilities p_b.

  • x_grid – array or None Discrete support where ECDF masses are allowed to sit. If None, uses the union of q_a and q_b sorted and uniqued. You can also pass a custom grid (e.g., np.linspace(min(q_a), max(q_b), 200)).

  • n – int or None ECDF size (number of jumps). If None, defaults to len(p_a).

  • rng – numpy.random.Generator or None Random generator to control reproducibility.

  • eps – float Small tolerance to avoid rounding issues in DP bounds.

Returns
qndarray (length n)

Quantile vector (nondecreasing), values taken from x_grid.

pndarray (length n)

Probability vector for the ECDF: p[r] = (r+1)/n.

Note

Choose an x-grid (or let the function use the union of q_a and q_b). # x_grid = np.linspace(min(q_a), max(q_b), 200) # e.g., a uniform 200-point support

Example

>>> p = pba.normal([4, 6], 1)
>>> ecdf_q, ecdf_p = sample_ecdf_in_pbox(p.left, p.p_values, p.right, p.p_values)
pyuncertainnumber.pba.utils.area_between_ecdfs(x_upper, p_upper, x_lower, p_lower)

Compute the area between two ECDFs defined by (x_upper, p_upper) and (x_lower, p_lower).

Parameters:
  • x_upper – arrays defining the upper ECDF

  • p_upper – arrays defining the upper ECDF

  • x_lower – arrays defining the lower ECDF

  • p_lower – arrays defining the lower ECDF

pyuncertainnumber.pba.utils._ecdf_value_on_left_of_intervals(x, p, grid_left)

Right-continuous ECDF value used on [grid_left[k], grid_right[k]). For values below the first x, value is 0. For above the last x, value stays at p[-1].