zigzagHMC: Sample from a truncated Gaussian distribution

View source: R/zigzagHMC.R

zigzagHMCR Documentation

Sample from a truncated Gaussian distribution

Description

Generate MCMC samples from a d-dimensional truncated Gaussian distribution with element-wise truncations using the Zigzag Hamiltonian Monte Carlo sampler (Zigzag-HMC).

Usage

zigzagHMC(
  nSample,
  burnin = 0,
  mean,
  prec,
  lowerBounds,
  upperBounds,
  init = NULL,
  stepSize = NULL,
  nutsFlg = FALSE,
  precondition = FALSE,
  seed = 1,
  numThreads = 1,
  diagnosticMode = FALSE
)

Arguments

nSample

number of samples after burn-in.

burnin

number of burn-in samples (default = 0).

mean

a d-dimensional mean vector.

prec

a d-by-d precision matrix of the Gaussian distribution.

lowerBounds

a d-dimensional vector specifying the lower bounds. -Inf is accepted.

upperBounds

a d-dimensional vector specifying the upper bounds. Inf is accepted.

init

a d-dimensional vector of the initial value. init must satisfy all constraints. If init = NULL, a random initial value will be used.

stepSize

step size for Zigzag-HMC or Zigzag-NUTS (if nutsFlg = TRUE). Default value is the empirically optimal choice: sqrt(2)(lambda)^(-1/2) for Zigzag-HMC and 0.1(lambda)^(-1/2) for Zigzag-NUTS, where lambda is the minimal eigenvalue of the precision matrix.

nutsFlg

logical. If TRUE the No-U-Turn sampler will be used (Zigzag-NUTS).

precondition

logical. If TRUE, the precision matrix will be preconditioned so that its diagonals (i.e. conditional variances) are all 1.

seed

random seed (default = 1).

numThreads

number of threads for parallel execution (default = 1). Set to 0 for automatic detection of available cores.

diagnosticMode

logical. TRUE for also returning diagnostic information such as the stepsize used.

Value

When diagnosticMode = FALSE (default), returns an nSample-by-d matrix of samples.

When diagnosticMode = TRUE, returns a list with elements:

samples

nSample-by-d matrix of samples

stepsize

The step size used for sampling

References

Nishimura, A., Zhang, Z., and Suchard, M. A. (2024). Zigzag path connects two Monte Carlo samplers: Hamiltonian counterpart to a piecewise deterministic Markov process. Journal of the American Statistical Association, 1-13.

Nishimura, A., Dunson, D. B., and Lu, J. (2020). Discontinuous Hamiltonian Monte Carlo for discrete parameters and discontinuous likelihoods. Biometrika, 107(2): 365-380.

See Also

getZigzagSample(), createEngine(), createNutsEngine(), setMean(), setPrecision()

Examples

set.seed(1)
d <- 10
A <- matrix(runif(d^2)*2-1, ncol=d)
precMat <- t(A) %*% A
initial <- rep(1, d)
results <- zigzagHMC(nSample = 1000, burnin = 1000, mean = rep(0, d), prec = precMat,
lowerBounds = rep(0, d), upperBounds = rep(Inf, d))


hdtg documentation built on Jan. 26, 2026, 9:06 a.m.

Related to zigzagHMC in hdtg...