Skip to content

Commit

Permalink
Add banana distribution example to README (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-graham authored Dec 10, 2024
1 parent 7828831 commit 27e0c2b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
37 changes: 35 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ You can install the development version of `rmcmc` like so:
devtools::install_github("UCL/rmcmc")
```

## Example
## Examples

This is a basic example which shows you how to generate samples from a normal target distribution with random scales.
The snippet belows shows a basic example of using the package 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,
and to tune the proposal shape with per-dimension scale factors based on online estimates of the target distribution variances.

Expand Down Expand Up @@ -70,3 +70,36 @@ cat(
sep = "\n"
)
```

As a second example, the snippet below demonstrates sampling from a two-dimensional 'banana' shaped distribution based on the [Rosenbrock function](https://en.wikipedia.org/wiki/Rosenbrock_function) and plotting the generated chain samples.

```{r banana-samples, fig.width=7, fig.height=4}
library(rmcmc)
set.seed(651239L)
target_distribution <- list(
log_density = function(x) -sum(x^2) / 8 - (x[1]^2 - x[2])^2 - (x[1] - 1)^2 / 10,
gradient_log_density = function(x) {
c(
-x[1] / 4 + 4 * x[1] * (x[2] - x[1]^2) - 0.2 * x[1] + 0.2,
-x[2] / 4 + 2 * x[1]^2 - 2 * x[2]
)
}
)
proposal <- barker_proposal(target_distribution)
results <- sample_chain(
target_distribution = target_distribution,
proposal = proposal,
initial_state = rnorm(2),
n_warm_up_iteration = 10000,
n_main_iteration = 10000,
)
plot(
results$traces[, "position1"],
results$traces[, "position2"],
xlab = expression(x[1]),
ylab = expression(x[2]),
col = "#1f77b4",
pch = 20
)
```
51 changes: 45 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ You can install the development version of `rmcmc` like so:
devtools::install_github("UCL/rmcmc")
```

## Example
## Examples

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,
and to tune the proposal shape with per-dimension scale factors based on
online estimates of the target distribution variances.
The snippet belows shows a basic example of using the package 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, and to tune the proposal shape with
per-dimension scale factors based on online estimates of the target
distribution variances.

``` r
library(rmcmc)
Expand Down Expand Up @@ -70,3 +71,41 @@ cat(
#> True target scales: 1.50538046096953, 1.37774732725824, 0.277038897322645
#> Adapter scale est.: 1.5328097767097, 1.42342707172926, 0.280359693392091
```

As a second example, the snippet below demonstrates sampling from a
two-dimensional ‘banana’ shaped distribution based on the [Rosenbrock
function](https://en.wikipedia.org/wiki/Rosenbrock_function) and
plotting the generated chain samples.

``` r
library(rmcmc)

set.seed(651239L)
target_distribution <- list(
log_density = function(x) -sum(x^2) / 8 - (x[1]^2 - x[2])^2 - (x[1] - 1)^2 / 10,
gradient_log_density = function(x) {
c(
-x[1] / 4 + 4 * x[1] * (x[2] - x[1]^2) - 0.2 * x[1] + 0.2,
-x[2] / 4 + 2 * x[1]^2 - 2 * x[2]
)
}
)
proposal <- barker_proposal(target_distribution)
results <- sample_chain(
target_distribution = target_distribution,
proposal = proposal,
initial_state = rnorm(2),
n_warm_up_iteration = 10000,
n_main_iteration = 10000,
)
plot(
results$traces[, "position1"],
results$traces[, "position2"],
xlab = expression(x[1]),
ylab = expression(x[2]),
col = "#1f77b4",
pch = 20
)
```

<img src="man/figures/README-banana-samples-1.png" width="100%" />
Binary file added man/figures/README-banana-samples-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 27e0c2b

Please sign in to comment.