[S] Metropolis-Hastings algoritm - Programming problem

Josť Ailton Alencar andrade (andrade@inep.gov.br)
Wed, 02 Sep 1998 18:47:27 -0300


Hello All,

I am using a simples aplication (a example) of M-H algoritm.
The problem is to simulate X=(x1,x2) from the bivariate normal
distribution with parameters(S notation):
m.mu <- c(1,2) (mean)
m.sig <- structure(.Data = c(1, 0.9, 0.9, 1), .Dim = c(2, 2))
(covariance matrix)
The problem is that the following code is producing the samples with
identical values in the two components of X. I thing it is a
programation problem. Why is it happenning?

Thank you very much!!

My program is:

function(t)
{
sig <- diag(0.6, 0.4)
x <- matrix(NA, nrow = t, ncol = 2)
x[1, ] <- rnorm(n = 2, 0, c(0.6, 0.4)) # initial values
alpha <- function(x, yy)
{
min(exp(-0.5 * t(yy - m.mu) %*% solve(m.sig) %*% (yy -
m.mu))/(exp(-0.5 * t(x - m.mu) %*%
solve(m.sig) %*% (x - m.mu))), 1) # Acceptance probability
}
for(i in 2:t) {
y <- rnorm(n = 2, mean = 0, sd = c(0.6, 0.4)) # Proposal
u <- runif(1, 0, 1)
x[i, ] <- ifelse(u <= alpha(x = x[i - 1, ], yy = y), y, x[i - 1, ])

}
list(x = x)
}

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news