pangolin.interface.print_upstream

Contents

pangolin.interface.print_upstream#

pangolin.interface.print_upstream(vars, namespace=None)#

Print all upstream RVs as a readable table.

Parameters:
  • vars (PyTree[RV]) – An RV or a PyTree of RVs whose upstream graph is printed. Each distinct RV appears once.

  • namespace (Mapping[str, object] | None) – Optional mapping supplying names, such as locals(). Lists, tuples, and dictionary values are searched recursively; the first name found for each upstream RV is used. The namespace supplies labels only and does not add roots to the printed graph. Unnamed RVs receive names a, b, … in topological order, skipping all discovered names, including unused aliases. See pangolin.util.node_names for path naming rules.

Raises:
  • TypeError – If an input leaf is not an RV.

  • ValueError – If namespace traversal encounters a cyclic container or a name shared by distinct upstream RVs.

Return type:

None

Examples

>>> r = RV(Constant(0.5))
>>> s = RV(Bernoulli(), r)
>>> t = RV(Constant(2))
>>> u = RV(Normal(), s, t)
>>> v = RV(Constant([75, 50, 99]))
>>> print_upstream([u, v]) # use autogenerated names
shape | statement
----- | ---------
()    | a = 0.5
()    | b ~ bernoulli(a)
()    | c = 2
()    | d ~ normal(b, c)
(3,)  | e = [75 50 99]
>>> print_upstream({'dog':[u], 'kitty':(v,)}) # any pytree is OK
shape | statement
----- | ---------
()    | a = 0.5
()    | b ~ bernoulli(a)
()    | c = 2
()    | d ~ normal(b, c)
(3,)  | e = [75 50 99]
>>> print_upstream([u, v], locals())
shape | statement
----- | ---------
()    | r = 0.5
()    | s ~ bernoulli(r)
()    | t = 2
()    | u ~ normal(s, t)
(3,)  | v = [75 50 99]
>>> print_upstream([u, v], {"cool_r": r, "dog": u, "kitty": v})
shape | statement
----- | ---------
()    | cool_r = 0.5
()    | a ~ bernoulli(cool_r)
()    | b = 2
()    | dog ~ normal(a, b)
(3,)  | kitty = [75 50 99]

RVs stored in nested containers receive names using dictionary keys and sequence indices. Shared parents appear only once:

>>> from pangolin import interface as pi
>>> params = {
...     "location": pi.normal(0, 1),
...     "scale": pi.exponential(1),
... }
>>> observations = [
...     pi.normal(params["location"], params["scale"])
...     for _ in range(2)
... ]
>>> print_upstream(observations, locals())
shape | statement
----- | ---------
()    | a = 0
()    | b = 1
()    | params['location'] ~ normal(a, b)
()    | c = 1
()    | params['scale'] ~ exponential(c)
()    | observations[0] ~ normal(params['location'], params['scale'])
()    | observations[1] ~ normal(params['location'], params['scale'])

See also

graph_upstream

Print upstream RVs as a Unicode DAG.