Free Access Week-  Data Engineering + BI
Data engineering and BI courses are free!
Free AI Access Week from June 2-8

samplr (version 1.0.1)

sampler_rec: Recycled-Momentum HMC Sampler (REC)

Description

Recycled-Momentum HMC is a sampling algorithm that uses Hamiltonian Dynamics to approximate a posterior distribution. Unlike in standard HMC, proposals are autocorrelated, as the momentum of the current trajectory is not independent of the last trajectory, but is instead updated by a parameter alpha (see Details).

Usage

sampler_rec(
  start,
  distr_name = NULL,
  distr_params = NULL,
  epsilon = 0.5,
  L = 10,
  alpha = 0.1,
  iterations = 1024L,
  weights = NULL,
  custom_density = NULL
)

Value

A named list containing

  1. Samples: the history of visited places (an n x d x c array, n = iterations; d = dimensions; c = chain index, with c==1 being the 'cold chain')

  2. Momentums: the history of momentum values (an n x d matrix, n = iterations; d = dimensions). Nothing is proposed in the first iteration (the first iteration is the start value) and so the first row is NA

  3. Acceptance Ratio: The proportion of proposals that were accepted (for each chain).

Arguments

start

Vector. Starting position of the sampler.

distr_name

Name of the distribution from which to sample from.

distr_params

Distribution parameters.

epsilon

Size of the leapfrog step

L

Number of leapfrog steps per iteration

alpha

Recycling factor, from -1 to 1 (see Details).

iterations

Number of iterations of the sampler.

weights

If using a mixture distribution, the weights given to each constituent distribution. If none given, it defaults to equal weights for all distributions.

custom_density

Instead of providing names, params and weights, the user may prefer to provide a custom density function.

Details

While in HMC the momentum in each iteration is an independent draw,, here the momentum of the last utterance pn1 is also involved. In each iteration, the momentum p is obtained as follows pα×pn1+(1α2)12×v; where vN(0,I).

Recycled-Momentum HMC does not support discrete distributions.

This algorithm has been used to model human data in castillo2024ExplainingFlawsHuman;textualsamplr

References

Examples

Run this code

result <- sampler_rec(
    distr_name = "norm", distr_params = c(0,1), 
    start = 1, epsilon = .01, L = 100
)
cold_chain <- result$Samples

Run the code above in your browser using DataLab