Interface

Contents

Interface#

Interface creates a separate interface with its own broadcasting rules. It provides the same model-building functions as the default interface, without changing that default interface or any other instance.

The default interface uses simple broadcasting, which rejects these different array shapes:

>>> from pangolin import interface as pi
>>> pi.normal([[0], [1]], [1, 2, 3])
Traceback (most recent call last):
    ...
ValueError: Can't broadcast shapes (2, 1) and (3,) with broadcasting=SIMPLE

Instead of importing the default interface as pi, create one with NumPy broadcasting:

>>> import pangolin
>>> pi = pangolin.interface.Interface(broadcasting="numpy")
>>> x = pi.normal([[0], [1]], [1, 2, 3])
>>> x.shape
(2, 3)

Use this pi in the same way as the default interface. Here NumPy broadcasting combines shapes (2, 1) and (3,) to produce shape (2, 3).

Note

Array operations and distributions with non-scalar parameters use the IR’s shape rules without implicit broadcasting, regardless of this interface’s scalar broadcasting configuration.

Combinators work the same way as in the default interface.

Use pi.overload(...) for a temporary, context-local change. Saved functions observe the active configuration when called:

>>> normal = pi.normal
>>> with pi.overload(broadcasting="off"):
...     y = normal(0, 1)
...     y.shape
()
>>> normal([0, 1], [[1, 1], [2, 2]]).shape
(2, 2)

See overload() for the full behavior of temporary overrides.

Interface#

class pangolin.interface.Interface(*, broadcasting='simple')[source]#

Own one configuration and the graph-building functions that use it.

Parameters:

broadcasting (Literal['off', 'simple', 'rank', 'numpy']) – Broadcasting policy used outside temporary overrides: "off", "simple" (the default), "rank", or "numpy".

Notes

Scalar functions follow the configured broadcasting policy. Non-scalar primitive operations use the IR’s shape rules without implicit broadcasting. Both preserve existing parent RVs and attach this interface to newly created outputs.

Interface.log_prob(fun)

A combinator building log probabilities, accepting an RV-like evaluation value.

Interface.cdf(fun)

A combinator building CDFs, accepting an RV-like evaluation value.

Interface.quantile(fun)

A combinator building quantiles, accepting an RV-like probability.

Interface.truncated(fun[, lo, hi])

A combinator restricting continuous scalar distributions to a shared interval.

Interface.grad(fun[, argnum])

A gradient combinator that also accepts a selected numeric scalar.

Interface.vjp(fun[, argnum])

A combinator that returns a function building a vector-Jacobian product.

Interface.broadcast(...)

A combinator that broadcasts a scalar graph-building function.

Interface.get_attribute(rv, name, /)

Provide vectorized indexing and summation as additional RV conveniences.

Interface.overload(*[, broadcasting])

Temporarily change this interface's configuration in the current context.

Interface.neg(x, /)

Negate a scalar value.

Interface.add(a, b)

Create an RV by applying pangolin.ir.Add to its parents.

Interface.sub(a, b)

Create an RV by applying pangolin.ir.Sub to its parents.

Interface.mul(a, b)

Create an RV by applying pangolin.ir.Mul to its parents.

Interface.div(a, b)

Create an RV by applying pangolin.ir.Div to its parents.

Interface.pow(a, b)

Create an RV by applying pangolin.ir.Pow to its parents.

Interface.arccos(a)

Create an RV by applying pangolin.ir.Arccos to its parents.

Interface.arccosh(a)

Create an RV by applying pangolin.ir.Arccosh to its parents.

Interface.arcsin(a)

Create an RV by applying pangolin.ir.Arcsin to its parents.

Interface.arcsinh(a)

Create an RV by applying pangolin.ir.Arcsinh to its parents.

Interface.arctan(a)

Create an RV by applying pangolin.ir.Arctan to its parents.

Interface.arctanh(a)

Create an RV by applying pangolin.ir.Arctanh to its parents.

Interface.cos(a)

Create an RV by applying pangolin.ir.Cos to its parents.

Interface.cosh(a)

Create an RV by applying pangolin.ir.Cosh to its parents.

Interface.sin(a)

Create an RV by applying pangolin.ir.Sin to its parents.

Interface.sinh(a)

Create an RV by applying pangolin.ir.Sinh to its parents.

Interface.tan(a)

Create an RV by applying pangolin.ir.Tan to its parents.

Interface.tanh(a)

Create an RV by applying pangolin.ir.Tanh to its parents.

Interface.abs(a)

Create an RV by applying pangolin.ir.Abs to its parents.

Interface.minimum(a, b)

Create an RV by applying pangolin.ir.Minimum to its parents.

Interface.maximum(a, b)

Create an RV by applying pangolin.ir.Maximum to its parents.

Interface.exp(a)

Create an RV by applying pangolin.ir.Exp to its parents.

Interface.inv_logit(a)

Create an RV by applying pangolin.ir.InvLogit to its parents.

Interface.log(a)

Create an RV by applying pangolin.ir.Log to its parents.

Interface.logit(a)

Create an RV by applying pangolin.ir.Logit to its parents.

Interface.step(a)

Create an RV by applying pangolin.ir.Step to its parents.

Interface.loggamma(a)

Create an RV by applying pangolin.ir.Loggamma to its parents.

Interface.normal(mu, sigma)

Create a Normal distributed RV (Wikipedia).

Interface.normal_prec(mu, tau)

Create a NormalPrec distributed RV (Wikipedia).

Interface.lognormal(mu, sigma)

Create a Lognormal distributed RV (Wikipedia).

Interface.bernoulli(theta)

Create a Bernoulli distributed RV (Wikipedia).

Interface.bernoulli_logit(theta)

Create a BernoulliLogit distributed RV.

Interface.binomial(N, theta)

Create a Binomial distributed RV (Wikipedia).

Interface.cauchy(mu, sigma)

Create a Cauchy distributed RV (Wikipedia).

Interface.uniform(alpha, beta)

Create a Uniform distributed RV (Wikipedia).

Interface.beta(alpha, beta)

Create a Beta distributed RV (Wikipedia).

Interface.exponential(beta)

Create a Exponential distributed RV (Wikipedia).

Interface.gamma(alpha, beta)

Create a Gamma distributed RV (Wikipedia).

Interface.poisson(lambd)

Create a Poisson distributed RV (Wikipedia).

Interface.beta_binomial(N, alpha, beta)

Create a BetaBinomial distributed RV (Wikipedia).

Interface.student_t(nu, mu, sigma)

Create a StudentT distributed RV (Wikipedia).

Interface.expit(a)

An alias for pangolin.interface.inv_logit().

Interface.sigmoid(a)

An alias for pangolin.interface.inv_logit().

Interface.sqrt(x)

Take a square root.

Interface.matmul(a, b)

Multiply two vectors or matrices.

Interface.transpose(a)

Transpose a matrix.

Interface.diag(a)

Extract the diagonal of a square matrix as a vector.

Interface.diag_matrix(a)

Construct a square diagonal matrix from a vector.

Interface.inv(a)

Invert a square matrix.

Interface.cholesky(a)

Compute the lower-triangular Cholesky factor of a square matrix.

Interface.softmax(a)

Apply the softmax function to a vector.

Interface.sum(x, axis)

Sum an array along one axis, removing that axis from the result.

Interface.concatenate(*arrays[, axis])

Concatenate arrays along an existing axis without broadcasting.

Interface.fill_tril(params)

Fill a lower-triangular matrix row by row from a vector.

Interface.extract_tril(L)

Extract the lower-triangular entries of a square matrix row by row.

Interface.multi_normal(mean, cov)

Create a multivariate normal RV.

Interface.categorical(theta)

Create a scalar categorical RV (Wikipedia).

Interface.multinomial(n, p)

Create a multinomial RV (Wikipedia).

Interface.dirichlet(alpha)

Create a simplex-valued Dirichlet RV (Wikipedia).

Interface.wishart(nu, scale)

Create a matrix-valued Wishart RV (Wikipedia).

Interface.getitem(x, /, *indices)

Implement bracket indexing using this interface's orthogonal indexing rules.

Interface.oindex(x, *indices)

Index an RV using fully orthogonal indexing.

Interface.vindex(var, *indices)

Index an RV using fully vectorized indexing.

Interface.loop(fun, /, **sizes)

Build an RV over named vectorized and sequential axes, in function-parameter order.

Interface.seq(size[, initializer])

Specify a sequential axis for loop, returning (size, initializer).

Interface.vfor(...)

Vectorize a function over named integer ranges.

Interface.sfor(...)

Build a state sequence with named sequential and parallel coordinates.

Interface.constant(value, /)

Create an interface RV representing a fixed numeric value.

Interface.coerce(value, /)

Preserve an RV or create a constant owned by this interface.