Issue about initializing parameters in Matlab


Hi all,

I have an issue regarding the initializing parameters in Matlab.

My parameter is x which is bivariate normal distribution and i want the x[1] and x[2] both start from zero.
Regarding the Stan guideline about initialing parameters:, I defined my parameters with the same format as the guideline’s example but the x[1] and x[2] never start from a value even close to zero (both start from a value close to 4).

Honestly, I don’t know what my problem is.
I would gratefully appreciate it if you could help me out in this regard.

I attach my Matab and Stan code via this post.

Thank you very much in advance for your consideration.


Matlab code:

muf = zeros(1,2);
SIGMAf = eye(2);
dat = struct('mu',muf,'Sigma',SIGMAf);
params = struct('file','ConvLimSt.stan','data',dat,'chains',5,'iter',...
    1000,'warmup', 20,'algorithm','NUTS','inc_warmup',true,'verbose',true);
fit = stan(params,'init',0);

Stan code: ConvLimSt.stan

vector[2] mu;
matrix[2, 2] Sigma;
parameters {
  vector[2] x;

transformed parameters {
real g = 0.1*(x[1] - x[2])^2 - ((1/sqrt(2))*(x[1] + x[2])) + 4.5;
model {
    x ~ multi_normal(mu, Sigma);
  target += normal_lpdf(g | 0,0.15);


The correct initialization is being passed to CmdStan (you can see this in the header of the output files). Note that the initialization itself is not saved (see issue :, so you are seeing the results of the first iteration.


Dear Brian,

Thank you very much for your comment. I got the point.
But, Shouldn’t be the second value in warm-up sample at least close to zero? (since the initial conditions themselves are not saved)

In my case, it starts with a value close to 4 in each chain for both x[1] and x[2].



HMC is very fast moving, so not necessarily. If you were using Metropolis with a small step size that’d be the case. Depends on the model.

For efficiency, you want to cholesky factor Sigma and assign to L_Sigma in transformed data and then call multi_normal_cholesky. Also, I don’t see why you wrote the likelihood with target += and the prior with a sampling statement (~). The sampling statements are more efficient as they drop constant terms that we don’t need for sampling from the posterior.


Dear Dr. Carpenter,

Thanks a lot for your complete answer.