pangolin.interface.sfor#
- pangolin.interface.sfor(fun=None, /, *, init, **sizes)#
Build a state sequence with named sequential and parallel coordinates.
A scalar call of the form:
x = sfor(fun, init=init, t=T)
has these semantics:
previous = init states = [] for t in range(T): next_state = fun(previous, t) states.append(next_state) previous = next_state x = stack(states)
For example, if:
fun = lambda previous, t: normal(previous, 1)
then:
x[0] ~ normal(init, 1) x[t] ~ normal(x[t-1], 1), t > 0
A call with parallel coordinates:
x = sfor(fun, init=init, t=T, i=I, j=J)
has these semantics:
previous = init states = [] for t in range(T): next_state = empty((I, J)) for i in range(I): for j in range(J): next_state[i, j] = fun(previous, t, i, j) states.append(next_state) previous = next_state x = stack(states)
The loops are represented by one
Scancontaining nestedVMapoperations; they are not executed in Python. Function-parameter order determines the nesting, while keyword order has no effect.For example, if:
fun = lambda previous, t, i, j: normal(a[i, j] * previous[i, j] + b[t, i, j], c[i, j])
then:
x[0, i, j] ~ normal(a[i, j] * init[i, j] + b[0, i, j], c[i, j]) x[t, i, j] ~ normal(a[i, j] * x[t-1, i, j] + b[t, i, j], c[i, j]), t > 0
Finally, decorator form:
@sfor(init=init, t=T) def x(previous, t): return normal(previous, 1)
is equivalent to:
def fun(previous, t): return normal(previous, 1) x = sfor(fun, init=init, t=T)
In both forms,
initmust be supplied as a keyword argument.Each call to
funmust return exactly oneRVcreated during that call. The result must depend onprevious. Any other RVs created during the call that contribute to the result must have deterministic Ops.The shape of
initmust match one complete state assembled fromfun. Forfun(previous, t, i, j), letSbe the shape returned by one call tofun. Witht=T,i=I, andj=J,initmust have shape(I, J, *S), and the result has shape(T, I, J, *S). Equivalently,result.shape == (T,) + init.shape. Note that the output axes appear in function-parameter order; keyword order is ignored.Each parallel call receives the complete previous state;
sfordoes not slice it along the parallel axes.- Parameters:
fun (Callable[..., RV] | None) – Step function, or
Nonein decorator form. It must have a fixed positional signature with at least two parameters and no defaults, variadic parameters, or keyword-only parameters. The first parameter receives the complete previous state; the second receives the scalar sequential coordinate; any later parameters receive scalar parallel coordinates. No parameter may be namedinitorinterface. The function must satisfy the return, carry-dependence, determinism, and shape requirements above.init (RVLike) – Keyword-only complete state before the first step. It may be any
RVLikeand is coerced to an RV. Its shape must match the state assembled by mappingfunover every parallel coordinate.**sizes (int) – Exactly one positive integer for every coordinate parameter: the sequential parameter and all parallel parameters. Names must match the function parameters. Values determine axis lengths; function-parameter order, not keyword order, determines axis order.
- Returns:
result_or_decorator – When
funis supplied, an RV containing every state produced by the scan, not includinginit. If the sequential size isT,result[t]is the state produced at coordinatetandresult.shape == (T,) + init.shape. Whenfunis omitted, a decorator that accepts it and returns such an RV.- Return type:
- Raises:
TypeError – If
funis not callable or lacks the required signature, a size is not an integer, orfundoes not return exactly oneRV.ValueError – If a parameter is named
initorinterface, the function has no sequential coordinate, sizes are missing, unknown, or nonpositive, the result does not depend onprevious, an intermediate Op is random, or the mapped state does not match the shape or interface of the coercedinit.
Examples
>>> from pangolin import interface as pi >>> walk = pi.sfor(lambda previous, t: pi.normal(previous, 1), init=0, t=3) >>> walk.shape (3,)