matlab-stan and matlab random number generator

217 views
Skip to first unread message

Giorgio Corani

unread,
Dec 17, 2014, 11:32:40 AM12/17/14
to stan-...@googlegroups.com
Hi all,

my stan file run correctly from the command line.

Sorry for disturbing again, but I do get an error while calling it
from matlabStan.

Error using rng (line 96)
The current random number generator is the legacy generator. This is
because you have executed a command such as rand('state',0), which
activates
MATLAB's legacy random number behavior. You may not use RNG to reseed
the legacy random number generator.

Use rng('default') to reinitialize the random number generator to its
startup configuration, or call RNG using a specific generator type,
such as
rng(seed,'twister').
Error in mcmc/set.rng_state (line 92)
rng(r);
Error in mcmc (line 37)
self.rng_state = seed;
Error in StanFit (line 82)
self.sim_ = mcmc(self.model.seed);
Error in StanModel/sampling (line 744)
fit = StanFit('model',copy(self),'processes',p,...
Error in stan (line 137)
fit = model.sampling(p.Unmatched);



To reproduce the bug, my matlab code is:

corr_sign_data = struct('N',10,...
'D',2,...
'r', 0.31622776601,...
'pos',[7 1]);

fit1 = stan('file','bernoulli_hier_corr.stan','data',corr_sign_data,'iter',1000,'chains',1);

while the stan file is below:

https://db.tt/WIkzeDmT

regards, Giorgio

Brian Lau

unread,
Dec 18, 2014, 2:27:10 AM12/18/14
to stan-...@googlegroups.com
Hi Giorgio,

This runs fine on my machine. The error suggests that you have caused Matlab to use it's legacy random number generator sometime before running stan. Can you try running rng('default') and then try your example again?

-b

Giorgio Corani

unread,
Dec 18, 2014, 8:11:40 AM12/18/14
to stan-...@googlegroups.com
Hi Brian,

the example now works fine. Thanks.

I have yet another issue - sorry to bother you. I need to run many
experiments on a cluster, thus I am using your
listener.

After adding the listener, I should somehow stop matlab until the
listener has been notified.
I am unable to do this.
I get the message "Stan is still working. You can either: Come back
later, or Attach a listener to the StanFit object."
Eventually the listener gets correctly notified, but in the meanwhile
matlab has exit on error.

Could you provide some hints?
I could not find an easy solution from the manual you linked.

fit = stan('file','bernoulli_hier_corr.stan','data',corr_sign_data);
//fit is fine
addlistener(fit,'exit',@exitHandler); //listener is fine
mu=fit.extract('permuted',true).mu;//before arriving here matlab exits
with the above message.

regards, Giorgio

Brian Lau

unread,
Dec 18, 2014, 9:25:07 AM12/18/14
to stan-...@googlegroups.com
Hi Giorgio

It looks like you put the line:

mu=fit.extract('permuted',true).mu;

after adding the listener. However since the call to stan is not blocking, you will get an error because the sampling is not finished. You need to put the code or functions you want run inside the exitHandler, so that it gets called when the notification is sent. Change the wiki example to something like this:

function exitHandler(src,data) 
   fprintf('Stan finished. \n');
   mu = src.extract('permuted',true).mu; 
   fprintf('mean of mu = %1.2f\n', mean(mu));
end

-b

Giorgio Corani

unread,
Dec 18, 2014, 11:13:16 AM12/18/14
to stan-...@googlegroups.com
Hi Brian,

thanks for the hint.

best, Giorgio
Reply all
Reply to author
Forward
0 new messages