Description Usage Arguments Examples
Hamiltonian Monte Carlo
1 2 |
dtarget |
The target density function |
start |
The starting point. If a vector, all chains start at that point. If is a list, then each element should be a chain starting point. |
Eps |
The stepsize epsilon |
L |
The number of leapfrog steps to take for each MCMC step. |
m |
The particle mass. |
N |
The number of Chain steps to take. |
num.chains |
The number of independent chains to create. |
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 | dtarget <- function(x){
dmvnorm(x, mean=c(3,10), sigma=matrix(c(1,0,0,1), nrow=2))
}
x1 <- seq(0,6,length=101)
x2 <- seq(7,13, length=101)
contour.data <- expand.grid(x1=x1, x2=x2)
contour.data$Z <- apply(contour.data, MARGIN=1, dtarget)
target.map <- ggplot(contour.data, aes(x=x1, y=x2)) +
stat_contour(aes(z=Z))
target.map
# Poor behavior of the chain
chains <- HMC(dtarget, start=c(3,9), Eps=.5, L=20, N=1000, num.chains=4)
trace_plot(chains)
plot_2D_chains(target.map, chains)
# better
chains <- H.MCMC(dtarget, start=c(3,9), Eps=.5, L=10, N=1000, num.chains=4)
trace_plot(chains)
plot_2D_chains(target.map, chains)
# Poor again
chains <- HMC(dtarget, start=c(3,9), Eps=.5, L=7, N=1000, num.chains=4)
trace_plot(chains)
plot_2D_chains(target.map, chains)
# A slightly harder example
dtarget <- function(x){
dmvnorm(x, mean=c(3,10), sigma=matrix(c(3,3,3,7), nrow=2))
}
x1 <- seq(-6,12,length=101)
x2 <- seq(-11,31, length=101)
contour.data <- expand.grid(x1=x1, x2=x2)
contour.data$Z <- apply(contour.data, MARGIN=1, dtarget)
target.map <- ggplot(contour.data, aes(x=x1, y=x2)) +
stat_contour(aes(z=Z))
target.map
chains <- HMC(dtarget, start=c(2,10), Eps=.4, L=6, N=200, num.chains=4)
trace_plot(chains)
plot_2D_chains(target.map, chains)
chains <- HMC(dtarget, start=c(2,10), Eps=.4, L=10, N=200, num.chains=4)
trace_plot(chains)
plot_2D_chains(target.map, chains)
# Now a hard distribution!
dtarget <- function(x){
B <- .05
exp( -x[1]^2 / 200 - (1/2)*(x[2]+B*x[1]^2 -100*B)^2 )
}
x1 <- seq(-20,20, length=201)
x2 <- seq(-15,10, length=201)
contour.data <- expand.grid(x1=x1, x2=x2)
contour.data$Z <- apply(contour.data, MARGIN=1, dtarget)
target.map <- ggplot(contour.data, aes(x=x1, y=x2)) +
stat_contour(aes(z=Z))
target.map
chains <- HMC(dtarget, start=c(2,5), Eps=.5, L=10)
trace_plot(chains)
plot_2D_chains(target.map, chains)
chains <- HMC(dtarget, start=c(2,5), Eps=1, L=10)
trace_plot(chains)
plot_2D_chains(target.map, chains)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.