-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
72 lines (60 loc) · 2.95 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# rmcmc: Robust Markov chain Monte Carlo methods
<!-- badges: start -->
[![R-CMD-check](https://github.com/UCL/rmcmc/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/UCL/rmcmc/actions/workflows/R-CMD-check.yaml)
[![pkgdown](https://github.com/UCL/rmcmc/actions/workflows/pkgdown.yaml/badge.svg)](https://github.com/UCL/rmcmc/actions/workflows/pkgdown.yaml)
[![lint](https://github.com/UCL/rmcmc/actions/workflows/lint.yaml/badge.svg)](https://github.com/UCL/rmcmc/actions/workflows/lint.yaml)
[![pre-commit](https://github.com/UCL/rmcmc/actions/workflows/pre-commit.yaml/badge.svg)](https://github.com/UCL/rmcmc/actions/workflows/pre-commit.yaml)
[![codecov](https://codecov.io/github/UCL/rmcmc/graph/badge.svg?token=PL8557fpgT)](https://codecov.io/github/UCL/rmcmc)
<!-- badges: end -->
`rmcmc` is an R package for simulating Markov chains using the Barker proposal to compute _Markov chain Monte Carlo_ (MCMC) estimates of expectations with respect to a target distribution on a real-valued vector space.
The Barker proposal, described in Livingstone and Zanella (2022) <https://doi.org/10.1111/rssb.12482>, is a gradient-based MCMC algorithm inspired by the Barker accept-reject rule.
It combines the robustness of simpler MCMC schemes such as random-walk Metropolis with the efficiency of gradient-based algorithms such as Metropolis adjusted Langevin algorithm.
## Installation
You can install the development version of `rmcmc` like so:
``` r
# install.packages("devtools")
devtools::install_github("UCL/rmcmc")
```
## Example
This is a basic example which shows you how to generate samples from a normal target distribution with random scales.
Adapters are used to tune the proposal scale to achieve a target average acceptance probability;
to tune the proposal shape with per-dimension scale factors based on online estimates of the target distribution variances.
```{r, collapse = TRUE}
library(rmcmc)
set.seed(876287L)
dimension <- 3
scales <- exp(rnorm(dimension))
target_distribution <- list(
log_density = function(x) -sum((x / scales)^2) / 2,
gradient_log_density = function(x) -x / scales^2
)
proposal <- barker_proposal(target_distribution)
results <- sample_chain(
target_distribution = target_distribution,
proposal = proposal,
initial_state = rnorm(dimension),
n_warm_up_iteration = 1000,
n_main_iteration = 1000,
adapters = list(simple_scale_adapter(), variance_shape_adapter())
)
mean_accept_prob <- mean(results$statistics[, "accept_prob"])
adapted_shape <- proposal$parameters()$shape
cat(
sprintf("Average acceptance probability is %.2f", mean_accept_prob),
sprintf("True target scales: %s", toString(scales)),
sprintf("Adapter scale est.: %s", toString(adapted_shape)),
sep = "\n"
)
```