I am attempting to learn the mechanics of Gibbs sampling. I have 2 variables for which I am trying to conduct inference from. This example assumes only Gaussian distributions. My code in R looks like the following.
library(condMVNorm)
means <- c(0, 25)
cov <- matrix(c(1.09, 1.95, 1.95, 4.52), 2, 2)
k <- 10000
sample <- c(0, 0)
trace <- matrix(, k, 2)
for (i in 1:k) {
c1 <- rcmvnorm(n=1, mean = means, sigma = cov, dep=2, given=1, X=c(sample(1)))
c2 <- rcmvnorm(n=1, mean = means, sigma = cov, dep=1, given=2, X=c(c1))
trace[i, ] <- c(c1, c2)
sample <- trace[i, ]
}
colMeans(trace)
What I get as the output is the following.
[1] 26.8024135 0.7780053
I would have expected that the first variable would be pretty close to 25 and the second one to 0.
I do not know if my understanding is wrong with Gibbs sampling because the literature invariably says you sample from the conditional distribution p(X1=x1|X2=x2). To me, p(X1=x1|X2=x2) is the density estimation of X1=x1 given X2=x2, and one would map that to dcmvnorm and not rcmvnorm.
Any ideas on what I am doing wrong?
Aucun commentaire:
Enregistrer un commentaire