beanmachine.ppl.inference.proposer.hmc_utils module
- class beanmachine.ppl.inference.proposer.hmc_utils.DictTransform(transforms: Dict[beanmachine.ppl.model.rv_identifier.RVIdentifier, torch.distributions.transforms.Transform])
Bases:
object
A general class for applying a dictionary of Transforms to a dictionary of Tensors
- Parameters
transforms – Dict of torch.distributions.Transform keyed by the RVIdentifier
- inv(node_vals: Dict[beanmachine.ppl.model.rv_identifier.RVIdentifier, torch.Tensor]) Dict[beanmachine.ppl.model.rv_identifier.RVIdentifier, torch.Tensor]
Apply the inverse of each Transform to the corresponding Tensor in node_vals
- log_abs_det_jacobian(untransformed_vals: Dict[beanmachine.ppl.model.rv_identifier.RVIdentifier, torch.Tensor], transformed_vals: Dict[beanmachine.ppl.model.rv_identifier.RVIdentifier, torch.Tensor]) torch.Tensor
Computes the sum of log det jacobian log |dy/dx| on the pairs of Tensors
- class beanmachine.ppl.inference.proposer.hmc_utils.DualAverageAdapter(initial_epsilon: torch.Tensor, delta: float = 0.8)
Bases:
object
The dual averaging mechanism that’s introduced in [1] and was applied to HMC and NUTS for adapting step size in [2]. The implementation and notations follows [2].
- Reference:
- [1] Yurii Nesterov. “Primal-dual subgradient methods for convex problems” (2009).
- [2] Matthew Hoffman and Andrew Gelman. “The No-U-Turn Sampler: Adaptively
Setting Path Lengths in Hamiltonian Monte Carlo” (2014). https://arxiv.org/abs/1111.4246
- finalize() torch.Tensor
- step(alpha: torch.Tensor) torch.Tensor
- class beanmachine.ppl.inference.proposer.hmc_utils.MassMatrixAdapter(initial_positions: torch.Tensor, full_mass_matrix: bool = False)
Bases:
object
Adapts the mass matrix. The (inverse) mass matrix is initialized to identity and will be updated during adaptation windows.
- Parameters
matrix_size – The size of the mass matrix. This value should be the same
tensor. (as the length of the flattened position) –
- Reference:
[1] “HMC algorithm parameters” from Stan Reference Manual https://mc-stan.org/docs/2_26/reference-manual/hmc-algorithm-parameters.html#euclidean-metric
- finalize() None
- initialize_momentums(positions: torch.Tensor) torch.Tensor
Randomly draw momentum from MultivariateNormal(0, M). This momentum variable is denoted as p in [1] and r in [2].
- Parameters
positions – the positions of the energy function.
- step(positions: torch.Tensor)
- class beanmachine.ppl.inference.proposer.hmc_utils.RealSpaceTransform(world: beanmachine.ppl.world.world.World, target_rvs: Set[beanmachine.ppl.model.rv_identifier.RVIdentifier])
Bases:
beanmachine.ppl.inference.proposer.hmc_utils.DictTransform
Transform a dictionary of Tensor values from a constrained space to the unconstrained (real) space.
- Parameters
world – World which contains the random variables of interest.
target_rvs – Set of RVIdentifiers corresponding to the random variables of interest.
- class beanmachine.ppl.inference.proposer.hmc_utils.WelfordCovariance(diagonal: bool = True)
Bases:
object
An implementation of Welford’s online algorithm for estimating the (co)variance of samples.
- Reference:
- [1] “Algorithms for calculating variance” on Wikipedia
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford’s_online_algorithm
- finalize(regularize: bool = True) torch.Tensor
- step(sample: torch.Tensor) None
- class beanmachine.ppl.inference.proposer.hmc_utils.WindowScheme(num_adaptive_samples: int)
Bases:
object
Spliting adaptation iterations into a series of monotonically increasing windows, which can be used to learn the mass matrices in HMC.
- Reference:
- [1] “HMC algorithm parameters” from Stan Reference Manual
- property is_end_window
- property is_in_window
- step()