mh {hacks} | R Documentation |
Computes random variables from an unnormalized distribution using the Metropolis Hastings algorithm.
mh(FUN, n = 10000, mean = 0, sd = 1)
FUN |
a function desribing the unnormalized distribution. |
n |
the number of iterations to perform. |
mean |
The initial value for computing the random variables. This is the mean of the proposal distribution. |
sd |
The standard deviation of the proposal distribution. |
This implementation of the Metropolis Hastings algorithm uses the normal distribution for the prosal distribution.
Nathan Stephens
set.seed(2037) f <- function(x) 0.1*((1+(x-10)^2)/3)^-2 x <- mh(f) par(mfrow=c(2,1)) plot(density(x), main = 'Normalized Distribution') plot(x,type='l', main = 'Sampled Values')