# Issue about initializing parameters in Matlab

#1

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: https://github.com/brian-lau/MatlabStan/wiki/Initializing-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.

Hamed

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`

``````data{
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 {
//prior
x ~ multi_normal(mu, Sigma);
//likelihood
target += normal_lpdf(g | 0,0.15);
}``````

#2

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 : https://github.com/stan-dev/cmdstan/issues/547), so you are seeing the results of the first iteration.

#3

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].

Regards,
Hamed

#4

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.

#5

Dear Dr. Carpenter,