# NOT RUN {
# The normal distribution, which is log concave, so metropolis can be FALSE
result <- arms(
1000, dnorm, -1000, 1000, metropolis = FALSE,
arguments = list(log = TRUE), include_n_evaluations = TRUE
)
print(result$n_evaluations)
hist(result$samples, freq = FALSE, br = 20)
curve(dnorm(x), min(result$samples), max(result$samples), col = 'red', add = TRUE)
# Mixture of normals: 0.4 N(-1, 1) + 0.6 N(4, 1). Not log concave.
dnormmixture <- function(x) {
parts <- log(c(0.4, 0.6)) + dnorm(x, mean = c(-1, 4), log = TRUE)
log(sum(exp(parts - max(parts)))) + max(parts)
}
samples <- arms(1000, dnormmixture, -1000, 1000)
hist(samples, freq = FALSE)
# List of log pdfs, demonstrating recycling of log_pdf argument
samples <- arms(
10,
list(
function(x) -x ^ 2 / 2,
function(x) -(x - 10) ^ 2 / 2
),
-1000,
1000
)
print(samples)
# Another way to achieve the above, this time with recycling in arguments
samples <- arms(
10, dnorm, -1000, 1000,
arguments = list(
mean = c(0, 10), sd = 1, log = TRUE
)
)
print(samples)
# }
Run the code above in your browser using DataLab