Skip to content

Using listeners to notify when sampling is complete

Brian Lau edited this page Feb 23, 2014 · 12 revisions

StanFit objects issue a notification when sampling is complete. This is useful, for example, when something should be done when sampling is finished. Below is a minimal example of listening for the exit notification:

model_code = {
'data {'
'    int<lower=0> N;'
'    int<lower=0,upper=1> y[N];'
'}'
'parameters {'
'    real<lower=0,upper=1> theta;'
'}'
'model {'
'for (n in 1:N)'
'    y[n] ~ bernoulli(theta);'
'}'
};

data = struct('N',10,'y',[0, 1, 0, 0, 0, 0, 0, 0, 0, 1]);

fit = stan('model_code',model_code,'data',data,'verbose',false,'iter',50000);

% Attach the listener
addlistener(fit,'exit',@exitHandler);

where the listener callback is defined as:

function exitHandler(src,data)
   fprintf('\n');
   beep;
   fprintf('Listener notified!\n');
   fprintf('Stan finished. Chains exited with exitValue = \n');
   disp(src.exit_value)
   fprintf('\n');
end

Currently, notifications are sent only when sampling completes successfully. This will expanded in the future to include different notifications. More information can be found in the Matlab documentation.

Clone this wiki locally