# In this example, we set the prior of P(Head) to be a uniform distribution
niters <- 1000
curr.theta <- 0.5 # This is the parameter we want to find; initialise it
theta.chain <- numeric(niters) # Initialise the vector to be created
for (i in 0:(niters - 1)) {
proposal.theta <- curr.theta + rnorm(1, mean = 0, sd = 0.05)
if (proposal.theta < 0 || proposal.theta > 1) {
theta.chain[i + 1] <- curr.theta
next
}
curr.likeli <- likelihood(curr.theta, coin.flips)
proposal.likeli <- likelihood(proposal.theta, coin.flips)
alpha <- min(proposal.likeli/curr.likeli, 1)
# Randomly decide whether to accept our new proposal
if (runif(1) < alpha)
curr.theta <- proposal.theta
theta.chain[i + 1] <- curr.theta
}
plot(theta.chain)# In this example, we set the prior of P(Head) to be a uniform distribution
niters <- 1000
curr.theta <- 0.5 # This is the parameter we want to find; initialise it
theta.chain <- numeric(niters)
for (i in 1:niters) {
proposal.theta <- curr.theta + rnorm(1, mean = 0, sd = 0.05)
# If proposed theta leaves the feasible range of values
if (proposal.theta > 1 || proposal.theta < 0) {
theta.chain[i + 1] <- curr.theta
next
}
# It is easier to work with log of probabilities
curr.log.likeli <- log.likelihood(curr.theta, coin.flips)
proposal.log.likeli <- log.likelihood(proposal.theta, coin.flips)
# Because we have taken log(prob), now we need a slightly different formula to
# get the acceptance probability. Follows from exp(log(a/b)) = exp(log(a) -
# log(b))
alpha <- min(exp(proposal.log.likeli - curr.log.likeli), 1)
# Randomly decide whether to accept our new proposal
if (runif(1) < alpha)
curr.theta <- proposal.theta
theta.chain[i + 1] <- curr.theta
}
plot(theta.chain, type = "l")p <- 0.4
mn <- c(-1, 2)
std <- c(0.2, 1.5)
f <- function(x, mu = mn, sd = std) p * dnorm(x, mu[1], sd[1]) + (1 - p) * dnorm(x,
mu[2], sd[2])
curve(f(x), col = "blue", from = -4, to = 8, n = 300, las = 1)x_0 and sigmaset.seed(5003)
x_0.vals <- c(-1, 1, 5)
sigma.vals <- c(0.01, 0.5, 20)
n <- 1000
choices <- expand.grid(x_0.vals, n, sigma.vals)
choices_name <- paste0("x_0=", choices[, 1], " sigma=", choices[, 3])
result <- do.call(mapply, c(mc.algorithm, unname(choices)))
result <- data.frame(result)
colnames(result) <- choices_name
library(reshape)
hist_result <- melt(result)
colnames(hist_result) <- c("Parameter_Choices", "Value")
hist_result$line_x <- rep(seq(-2, 7.99, 0.01), times = 9)
hist_result$line_y <- rep(f(seq(-2, 7.99, 0.01), mn, std), times = 9)Sample the target distribution using the random walk Metropolis-Hastings sampling with different choices of x_0 and sigma(the red line is the true target density):
ggplot(data = hist_result) + geom_histogram(aes(x = Value, color = Parameter_Choices,
y = ..density..), fill = "white") + geom_line(aes(x = line_x, y = line_y, color = "red")) +
facet_wrap(~Parameter_Choices, scales = "free") + theme(legend.position = "none") +
labs(title = "Explore The Impact Of Choices Of `x_0` And `sigma`", x = "x", y = "")According to the result, we will see that x_0=-1, sigma=0.5 can best fit the true target density.