beanmachine.ppl.inference.sampler module

class beanmachine.ppl.inference.sampler.Sampler(kernel: BaseInference, initial_world: World, num_samples: Optional[int] = None, num_adaptive_samples: int = 0)

Bases: Generator[beanmachine.ppl.world.world.World, Optional[beanmachine.ppl.world.world.World], None]

Samplers are generators of Worlds that generate samples from the joint. It is used to generate Monte Carlo samples during MCMC inference. At each iteration, the proposer(s) proposer a values for the random variables, which are then accepted according to the MH ratio. The next world is then returned.

Parameters
  • kernel (BaseInference) – Inference class to get proposers from.

  • initial_world (World) – Optional initial world to initialize from.

  • num_samples (int, Optional) – Number of samples. If none is specified, num_samples = inf.

  • num_adaptive_samples (int, Optional) – Number of adaptive samples, defaults to 0.

send(world: Optional[beanmachine.ppl.world.world.World] = None) beanmachine.ppl.world.world.World

At each iteration, the following is executed: 1. Shuffle all the proposers in the world. 2. For each proposer, propose a world and accept/reject it based on MH ratio. 3. Run adaptation method if applicable. 4. Update the new current world as self.world.

Parameters

world – Optional World to use to propose. If none is provided, self.world is used.

throw(typ: Type[BaseException], val: Optional[BaseException] = None, tb: Optional[types.TracebackType] = None) NoReturn

Use the default error handling behavior (thorw Exception as-is)