-
Notifications
You must be signed in to change notification settings - Fork 47
Initializing parameters
Brian Lau edited this page May 25, 2017
·
5 revisions
Parameters defined in the parameters block are initialized randomly in the range -2 to +2 by default. This can be changed using the init
property as illustrated below:
y = [151, 145, 147, 155, 135, 159, 141, 159, 177, 134, ...
160, 143, 154, 171, 163, 160, 142, 156, 157, 152, 154, 139, 146, ...
157, 132, 160, 169, 157, 137, 153, 199, 199, 214, 200, 188, 210, ...
189, 201, 236, 182, 208, 188, 200, 221, 216, 207, 187, 203, 212, ...
203, 205, 190, 191, 211, 185, 207, 216, 205, 180, 200, 246, 249, ...
263, 237, 230, 252, 231, 248, 285, 220, 261, 220, 244, 270, 242, ...
248, 234, 243, 259, 246, 253, 225, 229, 250, 237, 257, 261, 248, ...
219, 244, 283, 293, 312, 272, 280, 298, 275, 297, 350, 260, 313, ...
273, 289, 326, 281, 288, 280, 283, 307, 286, 298, 267, 272, 285, ...
286, 303, 295, 289, 258, 286, 320, 354, 328, 297, 323, 331, 305, ...
338, 376, 296, 352, 314, 325, 358, 312, 324, 316, 317, 336, 321, ...
334, 302, 302, 323, 331, 345, 333, 316, 291, 324];
y = reshape(y,30,5);
x = [8 15 22 29 36];
rats_dat = struct('N',size(y,1),'TT',size(y,2),'x',x,'y',y,'xbar',mean(x));
params = struct('file','rats.stan','data',rats_dat,...
'chains',1,'warmup',100,'iter',100,...
'inc_warmup',true);
%% Default initialization is uniformly from (-2,+2)
fit = stan(params);
% Look at the first sample from the warmup for parameter alpha, which is a vector
fit.sim.warmup.alpha(1,:)
%% Initialization is uniformly from (-10,+10)
fit = stan(params,'init',10);
fit.sim.warmup.alpha(1,:)
%% Initialize all parameters to 0
fit = stan(params,'init',0);
% The first iteration is close to zero, since the initial conditions themselves
% are not saved
fit.sim.warmup.alpha(1,:)
%% Initialize one parameter (mu_beta) to zero, and the rest uniformly from (-2,+2)
fit = stan(params,'init',struct('mu_beta',0));
fit.sim.warmup.mu_beta(1,:)
fit.sim.warmup.alpha(1,:)
%% Initialize subset of parameters, and the rest uniformly from (-2,+2)
fit = stan(params,'init',struct('mu_beta',4,'alpha',10*ones(1,30)));
fit.sim.warmup.mu_beta(1,:)
fit.sim.warmup.alpha(1,:)
%% Initialize chains with different inits
fit = stan(params,'chains',2,'init',[0 10]);
fit.sim.warmup(1).alpha(1,:)
fit.sim.warmup(2).alpha(1,:)
%% Initialize chains with different inits using structs
init(1) = struct('mu_beta',4,'alpha',10*ones(1,30));
init(2) = struct('mu_beta',0,'alpha',zeros(1,30));
fit = stan(params,'chains',2,'init',init);
fit.sim.warmup(1).alpha(1,:)
fit.sim.warmup(2).alpha(1,:)