Module contents


Bases: torch.distributions.transforms.Transform

Volume preserving transformation to the Beta distribution support.

bijective = True
codomain: torch.distributions.constraints.Constraint = IndependentConstraint(Real(), 1)
domain: torch.distributions.constraints.Constraint = Real()

Infers the shape of the forward computation, given the input shape. Defaults to preserving shape.


Infers the shapes of the inverse computation, given the output shape. Defaults to preserving shape.

log_abs_det_jacobian(x, y)

Computes the log det jacobian log |dy/dx| given input and output.

class Optional[Dict[beanmachine.ppl.model.rv_identifier.RVIdentifier, torch.Tensor]] = None, initialize_fn: Callable[[torch.distributions.distribution.Distribution], torch.Tensor] = <function init_from_prior>)

Bases:, Mapping[beanmachine.ppl.model.rv_identifier.RVIdentifier, torch.Tensor]

A World represents an instantiation of the graphical model and can be manipulated or evaluated. In the context of MCMC inference, a world represents a single Monte Carlo posterior sample.

A World can also be used as a context manager to run and sample random variables. Example:

def foo():
  return Normal(0., 1.)

world = World()
with world:
  x = foo()  # returns a sample, ie tensor.

with world:
  y = foo()  # same world = same tensor

assert x == y
  • observations (Optional) – Optional observations, which fixes the random variables to observed values

  • initialize_fn (callable, Optional) – Callable which takes a torch.distribution object as argument and returns a torch.Tensor


Shallow copy of the current world.

enumerate_node(node: beanmachine.ppl.model.rv_identifier.RVIdentifier) torch.Tensor

node (RVIdentifier) – RVIdentifier of node.


A tensor enumerating the support of the node.

get_variable(node: beanmachine.ppl.model.rv_identifier.RVIdentifier)

node (RVIdentifier) – RVIdentifier of node.


Variable object that contains the metadata of the current node in the world.

initialize_value(node: beanmachine.ppl.model.rv_identifier.RVIdentifier) None
classmethod initialize_world(queries: Iterable[RVIdentifier], observations: Optional[RVDict] = None, initialize_fn: InitializeFn = <function init_to_uniform>, max_retries: int = 100, **kwargs) T

Initializes a world with all of the random variables (queries and observations). In case of initializing values outside of support of the distributions, the method will keep resampling until a valid initialization is found up to max_retries times.

  • queries – A list of random variables that need to be inferred.

  • observations – Observations, which fixes the random variables to observed values

  • initialize_fn – Function for initializing the values of random variables

  • max_retries – The number of attempts this method will make before throwing an error (default to 100).

property latent_nodes: Set[beanmachine.ppl.model.rv_identifier.RVIdentifier]

All the latent nodes in the current world.

log_prob(nodes: Optional[Collection[beanmachine.ppl.model.rv_identifier.RVIdentifier]] = None) torch.Tensor

nodes (Optional) – Optional collection of RVIdentifiers to evaluate the log prob of a subset of the graph. If none is specified, then all the variables in the world are used.


The joint log prob of all of the nodes in the current world

replace(values: Dict[beanmachine.ppl.model.rv_identifier.RVIdentifier, torch.Tensor])

values (RVDict) – Dict of RVIdentifiers and their values to replace.


A new world where values specified in the dictionary are replaced. This method will update the internal graph structure.

update_graph(node: beanmachine.ppl.model.rv_identifier.RVIdentifier) torch.Tensor

This function adds a node to the graph and initialize its value if the node is not found in the graph already.


node (RVIdentifier) – RVIdentifier of node to update in the graph.


The value of the node stored in world (in original space). torch.distributions.distribution.Distribution) torch.distributions.transforms.Transform

Get transforms of a distribution to transform it from constrained space into unconstrained space.


distribution – the distribution to check


a Transform that need to be applied to the distribution to transform it from constrained space into unconstrained space Optional[] torch.distributions.distribution.Distribution) torch.Tensor

Samples from the distribution.

Used as an arg for World


distributiontorch.distribution.Distribution corresponding to the distribution to sample from torch.distributions.distribution.Distribution) torch.Tensor

Initializes a uniform distribution to sample from transformed to the support of distribution. A Categorical is used for discrete distributions, a bijective transform is used for constrained continuous distributions, and distribution is used otherwise.

Used as an arg for World


distributiontorch.distribution.Distribution of the RV, usually the prior distribution.