interface#
This module defines a friendly interface for creating models in the Pangolin IR.
Working with RVs#
An RV represents a scalar or array-shaped value in a probabilistic model. Calling a distribution function creates an RV; it does not draw a sample. Arithmetic expressions create additional RVs:
>>> from pangolin import interface as pi
>>> slope = pi.normal(0, 1)
>>> mean = 2 * slope + 1
>>> observation = pi.normal(mean, 0.5)
>>> pi.print_upstream(observation, locals())
shape | statement
----- | ---------
() | a = 2
() | b = 0
() | c = 1
() | slope ~ normal(b, c)
() | d = mul(a, slope)
() | e = 1
() | mean = add(d, e)
() | f = 0.5
() | observation ~ normal(mean, f)
Most graph-building functions accept RVLike inputs: RVs, numbers, or numeric arrays.
Numbers and arrays become constant RVs automatically.
Every RV has a fixed shape, available as x.shape. Scalars have shape ():
>>> x = pi.normal(0, 1)
>>> x.shape
()
>>> y = pi.constant([1, 2, 3])
>>> y.shape
(3,)
You can use familiar Python notation instead of calling the corresponding functions:
Notation |
Equivalent function call |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For RVs created using the default interface, these are shorthand for the corresponding function calls and follow the same rules.
Broadcasting is configurable. The default interface uses simple broadcasting: scalar arguments can be combined with arrays, while all non-scalar arguments must have the same shape. This applies to scalar distribution functions and scalar arithmetic:
>>> x = pi.normal([0, 1, 2], 1)
>>> x.shape
(3,)
>>> y = 2 * x + 1
>>> y.shape
(3,)
You can disable broadcasting, allow rank promotion, or use NumPy-style broadcasting.
Use pi.overload(...) for a temporary change or create a separately configured
Interface. See Broadcasting for the rules and examples.
Combinators take ordinary Python graph-building functions and return new graph-building
functions. For example, vmap turns a function into one that operates across an array:
>>> def shifted_normal(mean):
... return pi.normal(mean, 1) + 2
>>> mapped_normal = pi.vmap(shifted_normal)
>>> means = pi.constant([0, 1, 2])
>>> x = mapped_normal(means)
>>> x.shape
(3,)
Combinators can be combined: for example, vmap can map a function built using scan
to create multiple time series. See Combinators for the available operations.
See Examples for examples using arrays, time series, and nested combinators.
See Broadcasting for broadcasting rules and configuration options.
See Indexing for indexing rules and examples.
Reference card#
The entire interface is based on methods that create pangolin.ir.RV objects (or that create functions that create pangolin.ir.RV objects).
Here are the main public tools:
To do… |
Use… |
|---|---|
Constants |
|
Arithmetic |
|
Trigonometry |
|
Other scalar functions |
|
Linear algebra |
|
Other multivariate functions |
|
Indexing |
|
Scalar distributions |
|
Multivariate distributions |
|
Bijected densities |
|
Functional combinators |
|
Graph inspection |
|
Configuration |
Constants#
|
Create an interface RV representing a fixed numeric value. |
Scalar arithmetic and mathematics#
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Take a square root. |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
An alias for |
|
An alias for |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
|
Create an RV by applying |
Array operations and linear algebra#
|
Multiply two vectors or matrices. |
|
Transpose a matrix. |
|
Invert a square matrix. |
|
Compute the lower-triangular Cholesky factor of a square matrix. |
|
Extract the diagonal of a square matrix as a vector. |
|
Construct a square diagonal matrix from a vector. |
|
Apply the softmax function to a vector. |
|
Concatenate arrays along an existing axis without broadcasting. |
|
Sum an array along one axis, removing that axis from the result. |
|
Fill a lower-triangular matrix row by row from a vector. |
|
Extract the lower-triangular entries of a square matrix row by row. |
Indexing#
See Indexing for indexing rules and examples.
Scalar distributions#
|
|
|
Create a |
|
|
|
|
|
|
|
Create a |
|
|
|
|
|
|
|
Create a |
|
Create a |
|
|
|
|
|
Multivariate distributions#
|
Create a multivariate normal RV. |
|
Create a scalar categorical RV (Wikipedia). |
|
Create a multinomial RV (Wikipedia). |
|
Create a simplex-valued Dirichlet RV (Wikipedia). |
|
Create a matrix-valued Wishart RV (Wikipedia). |
Bijected distributions#
Construct transformed distributions using an invertible transformation. |
|
Compose Transforms in left-to-right application order. |
|
Standard transform library. |
Combinators#
The combinators and Transform machinery are also available in pangolin.functional.
For their IR-level contracts, see functional.
Broadcast a scalar function. |
|
Map a function over argument axes. |
|
Build a loop with vectorized and sequential axes. |
|
Specify a sequential loop axis. |
|
Build a vectorized indexed loop. |
|
Build a sequential indexed loop. |
|
Construct a log-probability graph. |
|
Construct a cumulative-probability graph. |
|
Construct a scalar quantile graph. |
|
Restrict scalar distributions to a shared interval. |
|
Differentiate a scalar-valued function. |
|
Construct a vector-Jacobian product. |
|
Build a Composite operation. |
|
Build a recurrence. |
Graph inspection#
Find upstream graph nodes. |
|
Print upstream RVs as a table. |
|
Print upstream RVs as a Unicode DAG. |
|
Inspect a graph-building function as IR code. |
|
Describe the inputs and output of a function call. |
Configuration#
The default interface uses simple broadcasting. Use with pi.overload(broadcasting="rank")
for a temporary change. To create a separate configured interface, see Interface.
Create a separately configured interface. |
|
Temporarily change the default interface’s configuration. |
See Broadcasting for the rules and examples for each mode.