Skip to content

Commit 24e5b0f

Browse files
authored
Add vignette demoing adjusting Barker proposal noise distribution (#67)
* Add vignette demoing changing Barker proposal noise distribution * Decouple descriptions of alt proposals from barker_proposal * Add bimodal Barker proposal convenience function * Fix linter errors
1 parent 7f87e4b commit 24e5b0f

12 files changed

+451
-30
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(barker_proposal)
4+
export(bimodal_barker_proposal)
45
export(chain_state)
56
export(covariance_shape_adapter)
67
export(dual_averaging_scale_adapter)

R/barker.R

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ log_density_ratio_barker <- function(
6464

6565
#' Create a new Barker proposal object.
6666
#'
67-
#' Returns a list with function to sample from the proposal, evaluate the log
68-
#' density ratio for a state pair for the proposal and update the proposal
69-
#' parameters. The proposal has two parameters `scale` and `shape`. At least one
70-
#' of `scale` and `shape` must be set before sampling from the proposal or
71-
#' evaluating the log density ratio.
67+
#' The Barker proposal is a gradient-based proposal inspired by the Barker
68+
#' accept-reject rule and proposed in Livingstone and Zanella (2022). It offers
69+
#' improved robustness compared to alternative gradient-based proposals such as
70+
#' Langevin proposals.
71+
#'
72+
#' For more details see the vignette:
73+
#' \code{vignette("barker-proposal", package = "rmcmc")}
7274
#'
7375
#' @inheritParams sample_barker
7476
#' @param scale Scale parameter of proposal distribution. A non-negative scalar
@@ -89,6 +91,11 @@ log_density_ratio_barker <- function(
8991
#' * `default_initial_scale`: a function which given a dimension gives a default
9092
#' value to use for the initial proposal scale parameter.
9193
#'
94+
#' @references Livingstone, S., & Zanella, G. (2022). The Barker proposal:
95+
#' combining robustness and efficiency in gradient-based MCMC. _Journal of the
96+
#' Royal Statistical Society Series B: Statistical Methodology_, 84(2),
97+
#' 496-523.
98+
#'
9299
#' @export
93100
#'
94101
#' @examples
@@ -123,3 +130,63 @@ barker_proposal <- function(
123130
default_initial_scale = function(dimension) dimension^(-1 / 6)
124131
)
125132
}
133+
134+
135+
#' Create a new Barker proposal object with bimodal noise distribution.
136+
#'
137+
#' Convenience function for creating a Barker proposal with bimodal auxiliary
138+
#' noise variable distribution, corresponding to equally-weighted normal
139+
#' components with shared variance `sigma` and means `±sqrt(1 - sigma^2)`.
140+
#' This choice of noise distribution was suggested in Vogrinc et al. (2023) and
141+
#' found to give improved performance over the default choice of a standard
142+
#' normal auxiliary noise distribution in a range of targets.
143+
#'
144+
#' For more details see the vignette:
145+
#' \code{vignette("adjusting-noise-distribution", package = "rmcmc")}
146+
#'
147+
#' @inherit barker_proposal params return
148+
#'
149+
#' @param sigma Standard deviation of equally-weighted normal components in
150+
#' bimodal auxiliary noise distribution, with corresponding means of
151+
#' `±sqrt(1 - sigma^2)`.
152+
#'
153+
#' @references Vogrinc, J., Livingstone, S., & Zanella, G. (2023). Optimal
154+
#' design of the Barker proposal and other locally balanced
155+
#' Metropolis–Hastings algorithms. _Biometrika_, 110(3), 579-595.
156+
#'
157+
#' @seealso [barker_proposal()]
158+
#'
159+
#' @export
160+
#'
161+
#' @examples
162+
#' target_distribution <- list(
163+
#' log_density = function(x) -sum(x^2) / 2,
164+
#' gradient_log_density = function(x) -x
165+
#' )
166+
#' proposal <- bimodal_barker_proposal(scale = 1.)
167+
#' state <- chain_state(c(0., 0.))
168+
#' withr::with_seed(
169+
#' 876287L, proposed_state <- proposal$sample(state, target_distribution)
170+
#' )
171+
#' log_density_ratio <- proposal$log_density_ratio(
172+
#' state, proposed_state, target_distribution
173+
#' )
174+
#' proposal$update(scale = 0.5)
175+
bimodal_barker_proposal <- function(
176+
sigma = 0.1,
177+
scale = NULL,
178+
shape = NULL,
179+
sample_uniform = stats::runif) {
180+
sample_bimodal <- function(dimension) {
181+
return(
182+
sample(c(-1, 1), dimension, TRUE) * sqrt(1 - sigma^2) +
183+
stats::rnorm(dimension) * sigma
184+
)
185+
}
186+
barker_proposal(
187+
scale = scale,
188+
shape = shape,
189+
sample_auxiliary = sample_bimodal,
190+
sample_uniform = sample_uniform
191+
)
192+
}

R/hamiltonian.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ log_density_ratio_hamiltonian <- function(
5959

6060
#' Create a new Hamiltonian proposal object.
6161
#'
62-
#' @inherit barker_proposal return params description
62+
#' The Hamiltonian proposal augments the target distribution with normally
63+
#' distributed auxiliary momenta variables and simulates the dynamics for a
64+
#' Hamiltonian function corresponding to the negative logarithm of the density
65+
#' of the resulting joint target distribution using a leapfrog integrator, with
66+
#' the proposed new state being the forward integrate state with momenta negated
67+
#' to ensure reversibility.
68+
#'
69+
#' @inherit barker_proposal return params
6370
#'
6471
#' @param n_step Number of leapfrog steps to simulate Hamiltonian dynamics for
6572
#' in each proposed move, or parameter passed to function specified by
@@ -79,6 +86,11 @@ log_density_ratio_hamiltonian <- function(
7986
#' be used to specify parameter(s) of distribution to sample number of steps
8087
#' from.
8188
#'
89+
#' @references Duane, S., Kennedy, A. D., Pendleton, B. J., & Roweth, D. (1987).
90+
#' Hybrid Monte Carlo. _Physics Letters B_, 195(2), 216-222.
91+
#' @references Neal, R. M. (2011). MCMC Using Hamiltonian Dynamics. In _Handbook
92+
#' of Markov Chain Monte Carlo_ (pp. 113-162). Chapman and Hall/CRC.
93+
#'
8294
#' @export
8395
#'
8496
#' @examples

R/langevin.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ log_density_ratio_langevin <- function(
4545

4646
#' Create a new Langevin proposal object.
4747
#'
48-
#' @inherit barker_proposal return params description
48+
#' The Langevin proposal is a gradient-based proposal corresponding to a
49+
#' Euler-Maruyama time discretisation of a Langevin diffusion.
50+
#'
51+
#' @inherit barker_proposal return params
52+
#'
53+
#' @references Besag, J. (1994). "Comments on "Representations of knowledge in
54+
#' complex systems" by U. Grenander and MI Miller". _Journal of the Royal
55+
#' Statistical Society, Series B_. 56: 591–592.
56+
#' @references Roberts, G. O., & Tweedie, R. L. (1996). Exponential convergence
57+
#' of Langevin distributions and their discrete approximations. _Bernoulli_ 2
58+
#' (4), 341 - 363.
4959
#'
5060
#' @export
5161
#'

R/random_walk.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ log_density_ratio_random_walk <- function(
2626
0
2727
}
2828

29-
#' Create a new random walk proposal object.
29+
#' Create a new (Gaussian) random walk proposal object.
3030
#'
31-
#' @inherit barker_proposal return params description
31+
#' The Gaussian random walk proposal samples a new proposed state by perturbing
32+
#' the current state with zero-mean normally distributed noise.
33+
#'
34+
#' @inherit barker_proposal return params
3235
#'
3336
#' @export
3437
#'

man/barker_proposal.Rd

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bimodal_barker_proposal.Rd

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/hamiltonian_proposal.Rd

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/langevin_proposal.Rd

Lines changed: 11 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/random_walk_proposal.Rd

Lines changed: 3 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)