pangolin.interface.Interface.truncated

pangolin.interface.Interface.truncated#

Interface.truncated(fun, lo=None, hi=None)[source]#

A combinator restricting continuous scalar distributions to a shared interval.

Calling truncated(fun, lo, hi)(*args, **kwargs) constructs an RV with the distribution defined by fun(*args, **kwargs), restricted to the supplied bounds and renormalized. Use None to leave either end unrestricted. Supplied bounds must be finite and strictly inside the base distribution’s support, with lo < hi when both are supplied. Backends enforce these value-dependent requirements during evaluation, not when the graph is constructed.

The function may produce a scalar distribution or independent scalar draws expressed through nested VMap and Composite Ops. Every draw uses the same scalar bounds. The result has the same shape as the original output. The full contract is given in pangolin.functional.truncated. With both bounds omitted, the original output RV is returned, subject to the same requirements.

Numerical support depends on the backend. See pangolin.jax_backend.lower_truncated for JAX’s sampling requirements and numerical limitations.

Return type:

Callable[…, RV]

Parameters:
  • fun (Callable[[P], RV]) – Function creating one continuous random RV, with no contributing newly created random intermediate RVs.

  • lo (TypeAliasForwardRef('RVLike') | None) – Scalar lower bound, or None (the default) to omit it.

  • hi (TypeAliasForwardRef('RVLike') | None) – Scalar upper bound, or None (the default) to omit it.

Returns:

truncated_fun – Function accepting fun’s arguments unchanged and returning a truncated RV with the same shape as its original output.

Raises:

ValueError – If a bound is not scalar, or the supplied function’s output violates the requirements of pangolin.functional.truncated.

Notes

Arguments are supplied to fun unchanged; any coercion or broadcasting performed by fun is preserved. Bounds are scalar and are never broadcast.

Supplied bounds are coerced when the combinator is constructed. Existing RV bounds are preserved; newly coerced bounds use this interface. The result retains the interface chosen by fun, not necessarily this interface. Numerical support and validation of bound values are left to backends.

Examples

Create a lower-truncated Normal with no upper bound:

>>> from pangolin import interface as pi
>>> positive_normal = pi.truncated(pi.normal, 0.0)
>>> x = positive_normal(1.1, 2.2)
>>> x.op
Truncated(Normal(), upper=False)

Normal’s usual broadcasting is preserved, with scalar bounds shared across draws:

>>> y = positive_normal([1.1, 1.2, 1.3], 2.2)
>>> y.op
VMap(Truncated(Normal(), upper=False), [None, 0, None], 3)
>>> y.shape
(3,)

Supply only an upper bound, or both bounds:

>>> x = pi.truncated(pi.normal, hi=0.0)(1.1, 2.2)
>>> x.op
Truncated(Normal(), lower=False)
>>> x = pi.truncated(pi.normal, 0.0, 1.0)(1.1, 2.2)
>>> x.op
Truncated(Normal())

With neither bound, no truncation is added:

>>> x = pi.truncated(pi.normal)(1.1, 2.2)
>>> x.op
Normal()

Even length-one arrays are rejected as bounds:

>>> pi.truncated(pi.normal, [0.0])
Traceback (most recent call last):
    ...
ValueError: truncation bounds must be scalar