Translate Multivariate Normal from WinBUGS to Stan

Hi,

I am working on translating a model from WinBUGS code to Stan. The relevant part of the WinBUGS code is as follows:

	prec[1,1] <- 1; prec[1,2] <- 0; prec[1,3] <- 0
	prec[2,1] <- 0; prec[2,2] <- 1; prec[2,3] <- 0
	prec[3,1] <- 0; prec[3,2] <- 0; prec[3,3] <- 1

	tau.ug[1:3, 1:3] ~ dwish(prec[1:3, 1:3], 3)

	for (i in 1:3) {
		ug0[i] ~ dnorm(0, 0.01)
	}
	
	for (k in 1:K) {
		ug[k,1:3] ~ dmnorm(ug0[1:3], tau.ug[1:3, 1:3])
	}

The R2WinBUGS code also initializes those variables as

tau.ug  = rwish(3, diag(3)),
ug0     = log(c(0.7, 36, 1.3))
ug      = log(cbind(rnorm(K, 0.7, 0.05), rnorm(K, 36, 3), rnorm(K, 1.3, 0.1)))

I tried to translate this to Stan, but it seems to not be working (I am not getting any compilation error messages, but all of the transitions are divergent.)

Here is the relevant portions of the Stan code:

data {
  int<lower=1> K; 
}
transformed data {
  matrix[3, 3] prec = diag_matrix(rep_vector(1, 3));  
}
parameters {
  matrix[3, 3] sigma_ug;
  matrix[K, 3] ug;
  vector[3] ug0; 
}
model {
  sigma_ug ~ inv_wishart(3, prec);
  for (i in 1:3) {
    ug0[i] ~ normal(0, 100);
  }	
  for (k in 1:K) {
    ug[k, 1:3] ~ multi_normal(ug0, sigma_ug);
  }
}

In RStan, I initialized the parameters as follows

sigma_ug  = riwish(3, diag(3)),
ug0       = log(c(0.7, 36, 1.3))
ug        = log(cbind(rnorm(K, 0.7, 0.05), rnorm(K, 36, 3), rnorm(K, 1.3, 0.1)))

(There are also a lot of other parts to the WinBUGS /Stan codes that I did not include, since I assume it would make it too confusing)

Do you notice anything I am doing that would make it not converge correctly?

Thanks so much for your help. Please let me know if there is other information that could be helpful.

1 Like

Hello!

You define sigma_ug as a 3*3 matrix, but that is inconsistent with giving it a inv_wishart prior, as the inv_wishart requires sigma_ug to be positive and symmetric definite. So you’ll need to change the declaration of sigma_ug.

But why use the Wishart in the first place? Maybe you should take a look at section 9.13 in the stan reference manual?

best,
-op

If you don’t just want to translate, you can also switch to the scaled LKJ prior we recommend in the manual. Stan can do this because it doesn’t require conjugate multivariate priors.