slice: Wrapper to automate learning in a slice sampler

Description Usage Arguments Details Author(s) References Examples

View source: R/slice.R

Description

Implements Neal (2003) stepping out univariate slice sampler, with learning – best for unimodal distributions (does not break Markov property), but also possible to use in spin-up. Uses enclosure to preserve values from one iteration to the next. Supply the /log density/ to the returned function.

Usage

1
slice(w)

Arguments

w

stepping-out interval. This is just a starting value if returned function is called with learn=T.

Details

slice returns a function which should be called with the current sample (x0), the log density (logf) and a learn toggle (learn). If learn is TRUE the stepping-out interval w is adjusted according to a running sum of samples so far

Author(s)

Jonathan Rougier

References

Neal, Radford M. "Slice sampling." Annals of statistics (2003): 705-741.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## Not run:
# sample from a mixture of normals with spinup

myfun <- function(x)
    log(0.8 * dnorm(x, 0, 1) + 0.2 * dnorm(x, 3, 0.25))
myslice <- slice(w = 0.1) # obviously too small

N <- 1e4
rsam <- rep(NA, N)
x <- 0.0
spinup <- 20

for (i in (-spinup):N) {
 x <- myslice(x, myfun, learn = i <= 0)
 if (i > 0) rsam[i] <- x
 if (abs(i) <= 5)
       cat(sprintf("i = %i, w = %.3f\n", i, get("w", envir = environment(myslice))))
}
hist(rsam, col = "grey", freq = FALSE, breaks = 40)
x <- sort(rsam)
lines(x, exp(myfun(x)), xpd = NA)

## End(Not run)

shazhe/mvst0 documentation built on May 29, 2019, 9:20 p.m.