-
Notifications
You must be signed in to change notification settings - Fork 47
Optimization in Stan
brian-lau edited this page Sep 26, 2014
·
5 revisions
MatlabStan provides an interface to Stan’s optimization methods to obtain a point estimate by maximizing the posterior function defined for a model. The following example estimates the mean and variance from samples assumed to be drawn from normal distribution:
stdnorm = {
'data {'
' int N;'
' real y[N];'
'}'
'parameters {'
' real mu;'
' real<lower=0> sigma;'
'}'
'model {'
' mu ~ normal(0, 5);'
' sigma ~ normal(0, 5);'
' y ~ normal(mu, sigma);'
'}'
};
% data
y2 = randn(20,1);
mean(y2)
dat = struct('N',length(y2),'y',y2);
% Explicitly declare model & and optimize via method call
sm = StanModel('model_code',stdnorm);
op = sm.optimizing('data',dat,'verbose',true);
op.extract
print(op);
% Alternatively, call stan()
op2 = stan('model_code',stdnorm,'method','optimize','data',dat);
op2.extract
print(op2);
% Change algorithm, without recompiling model (default is LBFGS)
op3 = stan('fit',op2,'method','optimize','data',dat,'algorithm','bfgs');
op3.extract
print(op3);