Unfixing the Random seed

This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location where you will have to reset your password.
Forum rules
This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location (https://forum.dynare.org) where you will have to reset your password.

Unfixing the Random seed

Postby robp » Mon May 05, 2014 3:51 pm

Hi,
I use the simulation coming from a .mod file as the DGP.
I need to do a MonteCarlo experiment, where I need to simulate the model M times.
Obviously, each of the M simulations should be different. Since now Dynare fixes automatically the seed for the RNG, I cannot see how to do it.
I have used the command set_dynare_seed('clock') and also I tried to use set_dynare_seed(randi(1000000,1)) (to give as a seed a random integer every time), but I do not obtain simulations that are different.

Techincally, I run a for loop calling each time the .m file associated with the .mod file that solves and simulate the model I am interested in, and for each iteration of the loop I store the simulated variable.

Thanks for your help
robp
 
Posts: 8
Joined: Fri Feb 13, 2009 8:50 pm

Re: Unfixing the Random seed

Postby jpfeifer » Wed May 07, 2014 9:34 am

Could you post an example? It is hard to track the interaction and which command is called in the mod-file and the wrapping m-file you wrote.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Unfixing the Random seed

Postby robp » Wed May 07, 2014 12:54 pm

The question is really simple.

Suppose I have a .mod file (model.mod) that at the end produce the stochastic simulation for T periods of one variable of interest (let's call it Y).
I want M different simulations of Y when running the model file M times. Let's call YMATRIX the matrix that gathers all the M time series for Y.

In a .m file I write the following loop:

M=1000;
for i = 1: M
model.m
YMATRIX(:,i)=Y.
end

where in the model.m fine I have delete the line that "clear all" the variables.

If I run this simple Loop, the YMATRIX is composed by M columns that are exactly the same, since every time that dynare does the stochastic simulation it fixes (in the new version) the seed for the random generator.

Instead, I would like to have M different time series for Y, which requires unfixing the random seed used by dynare.

Thanks
robp
 
Posts: 8
Joined: Fri Feb 13, 2009 8:50 pm

Re: Unfixing the Random seed

Postby jpfeifer » Wed May 07, 2014 1:30 pm

I see. When you tried to use
Code: Select all
set_dynare_seed('clock');

where did you put this command? Did you try to put it directly before the stoch_simul-command?
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Unfixing the Random seed

Postby robp » Wed May 07, 2014 1:40 pm

jpfeifer wrote:I see. When you tried to use
Code: Select all
set_dynare_seed('clock');

where did you put this command? Did you try to put it directly before the stoch_simul-command?


yes, I did.

I put it in the model.m file,
before the line options_.periods = 2000 (which is where dynare put it in the .m file created by running the .mod file where
Code: Select all
set_dynare_seed('clock');
is directly before stock_simul.

The results, when running that are weird:
The simulations are not all equal, but the 2nd and 3rd are the same. The 4th and 5th are the same. The 7th and 8th are the same (but different among each other)...
It seems that 'clock' is not random "enough"...
robp
 
Posts: 8
Joined: Fri Feb 13, 2009 8:50 pm

Re: Unfixing the Random seed

Postby jpfeifer » Wed May 07, 2014 1:47 pm

Maybe the the execution is too quick so that the clock-time is identical. Did you try
Code: Select all
set_dynare_seed(i);

instead, i.e. you use the exterier loop variable i to change the seed?
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: Unfixing the Random seed

Postby robp » Wed May 07, 2014 2:30 pm

jpfeifer wrote:Maybe the the execution is too quick so that the clock-time is identical. Did you try
Code: Select all
set_dynare_seed(i);

instead, i.e. you use the exterier loop variable i to change the seed?



That indeed works fine.
The funny thing was that I tried initially to assign a random integer in any iteration using [code]set_dynare_seed(randi(20000000,1))[\code] and that did not work fine.

Thanks a lot
robp
 
Posts: 8
Joined: Fri Feb 13, 2009 8:50 pm

Re: Unfixing the Random seed

Postby jpfeifer » Wed May 07, 2014 3:29 pm

Short explanation: When Dynare fixes the seed, you cannot use randi to draw different random numbers because the output from randi will always be the same.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany


Return to Dynare help

Who is online

Users browsing this forum: No registered users and 6 guests