Skip to contents

Returns a list with function to sample from the proposal, evaluate the log density ratio for a state pair for the proposal and update the proposal parameters. The proposal has two parameters scale and shape. At least one of scale and shape must be set before sampling from the proposal or evaluating the log density ratio.

Usage

hamiltonian_proposal(
  target_distribution,
  n_step,
  scale = NULL,
  shape = NULL,
  sample_auxiliary = stats::rnorm
)

Arguments

target_distribution

Target stationary distribution for chain. A list with named entries log_density and gradient_log_density corresponding to respectively functions for evaluating the logarithm of the (potentially unnormalized) density of the target distribution and its gradient. As an alternative to gradient_log_density an entry value_and_gradient_log_density may instead be provided which is a function returning both the value and gradient of the logarithm of the (unnormalized) density of the target distribution as a list under the names value and gradient respectively.

n_step

Number of leapfrog steps to simulate Hamiltonian dynamics for in each proposed move.

scale

Scale parameter of proposal distribution. A non-negative scalar value determining scale of steps proposed.

shape

Shape parameter of proposal distribution. Either a vector corresponding to a diagonal shape matrix with per-dimension scaling factors, or a matrix allowing arbitrary linear transformations.

sample_auxiliary

Function which generates a random vector from auxiliary variable distribution.

Value

Proposal object. A list with entries

  • sample: a function to generate sample from proposal distribution given current chain state,

  • log_density_ratio: a function to compute log density ratio for proposal for a given pair of current and proposed chain states,

  • update: a function to update parameters of proposal,

  • parameters: a function to return list of current parameter values.

  • default_target_accept_prob: a function returning the default target acceptance rate to use for any scale adaptation.

  • default_initial_scale: a function which given a dimension gives a default value to use for the initial proposal scale parameter.

Examples

target_distribution <- list(
  log_density = function(x) -sum(x^2) / 2,
  gradient_log_density = function(x) -x
)
proposal <- hamiltonian_proposal(target_distribution, scale = 1., n_step = 5)
state <- chain_state(c(0., 0.))
withr::with_seed(876287L, proposed_state <- proposal$sample(state))
log_density_ratio <- proposal$log_density_ratio(state, proposed_state)
proposal$update(scale = 0.5)