Mathematical Utilities

Math functions that preserve PAL custom types.

This module provides wrappers around numpy math functions that preserve PAL’s custom types (StochasticScalar, etc.). Import as ‘pnp’ to mimic numpy usage patterns.

Type signatures are in maths.pyi.

pal.maths.exp(x)[source]

Exponential function that preserves custom PAL types.

Return type:

Any

pal.maths.sum(x)[source]

Sum function that works with PAL types.

Return type:

Any

pal.maths.mean(x)[source]

Mean function that works with PAL types.

All PAL types implement the numpy array protocol, so this just delegates to numpy’s mean function which will dispatch to the appropriate __array_function__ or __array__ method.

Return type:

Any

pal.maths.std(x)[source]

Standard deviation function that works with PAL types.

Return type:

Any

pal.maths.var(x)[source]

Variance function that works with PAL types.

Return type:

Any

pal.maths.percentile(x, q)[source]

Percentile function that works with PAL types.

Return type:

Any

pal.maths.min(x)[source]

Min function that works with PAL types.

Return type:

Any

pal.maths.max(x)[source]

Max function that works with PAL types.

Return type:

Any

pal.maths.where(condition, x, y)[source]

Conditional selection that preserves PAL types.

Return type:

Any

pal.maths.safe_divide(numerator, denominator, default)[source]

Divide numerator by denominator, returning default where denominator is 0.

Works with PAL types (StochasticScalar, FreqSevSims) and plain numpy arrays/scalars.

Parameters:
  • numerator (Any) – The numerator value(s).

  • denominator (Any) – The denominator value(s).

  • default (Any) – Value to use where denominator equals zero.

Return type:

Any

Returns:

The result of the division, with default substituted where division by zero would occur.

Examples

>>> from pal.stochastic_scalar import StochasticScalar
>>> import pal.maths as pnp
>>> x = StochasticScalar([10., 20., 30.])
>>> y = StochasticScalar([2., 0., 5.])
>>> result = pnp.safe_divide(x, y, 0.0)
>>> result.values
array([5., 0., 6.])
pal.maths.minimum(x, y)[source]

Element-wise minimum that preserves PAL types.

Return type:

Any

pal.maths.maximum(x, y)[source]

Element-wise maximum that preserves PAL types.

Return type:

Any

pal.maths.cumsum(x)[source]

Cumulative sum that preserves PAL types.

When given a list of StochasticScalar objects, stacks their values into a 2D array and computes cumsum along axis 0.

Return type:

Any

pal.maths.floor(x)[source]

Floor function that preserves PAL types.

Return type:

Any

pal.maths.all(x)[source]

Check if all elements are True.

Return type:

bool