Description Usage Arguments Value Author(s) References Examples
Portkey two-coin algorithm: returns the result of the accept-reject step of a Portkey Barker's acceptance probability.
1 |
prop |
The proposed value in the MCMC step |
curr |
The current value in the MCMC step |
beta |
The portkey parameter with default being .99. If |
pf |
A function that produces a Bern(p) realization, with argument |
Cprop |
Upper bound for the target density at proposed value |
Ccurr |
Upper bound for the target density current value |
... |
additional arguments that go into |
a variable x
which is either curr
or prop
and integer loops
that returns the number
of loops the Bernoulli factory took
Dootika Vats, dootika@iitk.ac.in
Vats, D., Gonçalves, F. B., Łatuszyński, K., Roberts G. O., Efficient Bernoulli factory MCMC for intractable posteriors (2020+), arxiv.
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 | # The following example implements the portkey-two coin algorithm for a
# Gamma mixture of Weibulls as presented in Vats et. al. (2020)
set.seed(10) # seed is chosen so that example is fast
Cf <- function(value, k)
{
return(k/(exp(1)*value))
}
pf <- function(value, k, lam1, a1, b1)
{
lam1 <- rgamma(1, shape = a1, rate = b1)
dweibull(value, shape = k, scale = lam1)/Cf(value = value, k = k)
}
mcmc <- function(N = 1e3, beta = .95, k = 5, a1 = 2, b1 = 5)
{
out <- numeric(length = N)
loops <- numeric(length = N)
out[1] <- .1
acc <- 0
for(i in 2:N)
{
prop <- rnorm(1, out[i-1], sd = sqrt(.001))
if(prop < 0)
{
out[i] <- out[i-1]
next
}
interim <- portkey(prop = prop, curr = out[i-1], pf = pf, Cprop = Cf(prop, k), Ccurr = Cf(out[i-1], k), beta = beta, k = k, a1 = a1, b1 = b1)
out[i] <- interim[[1]]
if(out[i] != out[i-1]) acc <- acc+1
loops[i] <- interim[[2]]
}
return(list("mcmc" = out, "loops" = loops, "accept" = acc/N))
}
a1 <- 10
b1 <- 100
k <- 10
bark <- mcmc(N = 5e3, beta = 1, k = 10, a1 = 10, b1 = 100)
port <- mcmc(N = 5e3, beta = .99, k = 10, a1 = 10, b1 = 100)
plot(1:5e3, bark$loops, pch = 3)
points(1:5e3, port$loops, pch = 1, col = "red")
par(mfrow = c(1,2))
acf(bark$mcmc)
acf(port$mcmc)
par(mfrow = c(1,1))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.