Page 1 of 2
Dynare 4.2.0 Error using Rand
Posted:
Sun Feb 27, 2011 4:11 pm
by xuduan
Hi guys. I was trying to replicate an example with external_funtion. However, with Dynare 4.2.0, I kept receving the following message:
?? Error using ==> rand
Unknown command option.
Error in ==> set_dynare_seed at 80
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
Error in ==> global_initialization at 319
set_dynare_seed('default');
Error in ==> example1_no_deriv_functions_provided at 15
global_initialization;
Error in ==> dynare at 132
evalin('base',fname) ;
Anybody knows what's going on here? Is it my matlab version is too low( version 7.0)?
Attached please find the mod file and external function.
Thanks a lot for your help!
Duan
Re: Dynare 4.2.0 Error using Rand
Posted:
Sun Feb 27, 2011 4:31 pm
by jpfeifer
Hi,
Your mod-file runs fine on my computer. Which Matlab version are you using? This seems to be a problem with the rand-function. Probably the syntax in your Matlab version is different. Or you have a name conflict so that not the original Matlab-file is used when calling rand.
Re: Dynare 4.2.0 Error using Rand
Posted:
Sun Feb 27, 2011 4:44 pm
by xuduan
Thanks a lot jpfeifer! Well , I have matlab 7.0
Do you happen to know what I can do except getting new version of matlab?
Thanks again
Duan
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Mar 02, 2011 8:37 am
by StephaneAdjemian
Hi, The bug (related to the random number generators in old versions of matlab) has been fixed in branches 4.2 and master of our Git repository. This fix will published in the next release of Dynare. Meanwhile, as a workaround, you can replace the content of the file
set_dynare_seed.m (you will find this file in the matlab subfolder of Dynare) by:
- Code: Select all
function set_dynare_seed(a,b)
% Set seeds depending on matlab (octave) version. This routine is called in dynare_config and can be by the
% user in the mod file.
%
% Copyright (C) 2010-2011 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global options_
if ~nargin
error('set_dynare_seed:: I need at least one input argument!')
end
matlab_random_streams = ~(exist('OCTAVE_VERSION') || matlab_ver_less_than('7.7'));
if matlab_random_streams% Use new matlab interface.
if nargin==1
if ischar(a) && strcmpi(a,'default')
options_.DynareRandomStreams.algo = 'mt19937ar';
options_.DynareRandomStreams.seed = 0;
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setDefaultStream(s));
return
end
if ischar(a) && strcmpi(a,'reset')
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setDefaultStream(s));
return
end
if ischar(a)
error('set_dynare_seed:: something is wrong in the calling sequence!')
end
if ~ischar(a)
options_.DynareRandomStreams.algo = 'mt19937ar';
options_.DynareRandomStreams.seed = a;
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setDefaultStream(s));
return
end
elseif nargin==2
if ~ischar(a) || ~( strcmpi(a,'mcg16807') || ...
strcmpi(a,'mlfg6331_64') || ...
strcmpi(a,'mrg32k3a') || ...
strcmpi(a,'mt19937ar') || ...
strcmpi(a,'shr3cong') || ...
strcmpi(a,'swb2712') )
disp('set_dynare_seed:: First argument must be string designing the uniform random number algorithm!')
RandStream.list
disp(' ')
disp('set_dynare_seed:: Change the first input accordingly...')
disp(' ')
error(' ')
end
if ~isint(b)
error('set_dynare_seed:: The second input argument must be an integer!')
end
options_.DynareRandomStreams.algo = a;
options_.DynareRandomStreams.seed = b;
s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
reset(RandStream.setDefaultStream(s));
end
else% Use old matlab interface.
if nargin==1
if ischar(a) && strcmpi(a,'default')
if exist('OCTAVE_VERSION') || matlab_ver_less_than('7.4')
options_.DynareRandomStreams.algo = 'state';
else
% Twister was introduced in MATLAB 7.4
options_.DynareRandomStreams.algo = 'twister';
end
options_.DynareRandomStreams.seed = 0;
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
return
end
if ischar(a) && strcmpi(a,'reset')
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
return
end
if ~ischar(a) && isint(a)
options_.DynareRandomStreams.seed = a;
rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
randn('state',options_.DynareRandomStreams.seed);
else
error('set_dynare_seed:: Something is wrong in the calling sequence!')
end
else
error('set_dynare_seed:: Cannot use more than one input argument with your version of Matlab/Octave!')
end
end
Best,
Stéphane.
Re: Dynare 4.2.0 Error using Rand
Posted:
Thu Mar 03, 2011 10:42 am
by xuduan
Thanks Stephane! It works fine now!
Re: Dynare 4.2.0 Error using Rand
Posted:
Mon Feb 24, 2014 2:44 am
by cyepez
I'm using Dynare (4.2?) on Ubuntu with the latest Matlab Release (2014a) and got the same issue of the random generator.
I replaced problematic method 'setDefaultStream' with the Matlab's 'setGlobalStream' on the set_dynare_seed.m script and it did the trick.
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Oct 29, 2014 11:26 am
by hlt215
Hi,
I define a function
function f = epsilon(~,~)
av=0;
sd=1;
f = normrnd(av,sd);
end
declaring it as an external function as:
external_function(name=epsilon, nargs=2);
and then I define it as a new variable for convenience, like:
epsi = epsilon(av,sd);
After running my dynare code a get a non-zero static residual on this last equation, do you have an idea of why?
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Oct 29, 2014 12:01 pm
by jpfeifer
Please provide the full files needed to replicate the problem. Attach them as a zip or rar archive.
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Oct 29, 2014 12:49 pm
by hlt215
I attach only the component related to the generation of the normal shock, in order to isolate other potential problems coming from the rest of the code which is still preliminary. Maybe I'm wrong, but I would expect the residual of this equation to be zero independently of the rest of the model. I also tried to assume that in the steady-state this random number should be to the unconditional mean, so that I have set epsi_ss = 0 as initial value, but I still don't get zero residuals.
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Oct 29, 2014 3:45 pm
by jpfeifer
I don't understand what you are doing. epsilon will always return a different random number. Conceptually, what is the steady state? You are defining epsi as an endogenous variable, but actually it is purely exogenous. Moreover, what is the purpose of this function? It just does the same as any var_exo defined in Dynare.
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Oct 29, 2014 4:20 pm
by hlt215
Yes, it is an exogenous shock. It's an additive term to some model's equations.
For example:
w = a - epsilon + beta*x;
where epsilon is the exogenous shock drowned from a normal pdf(0,1). I thought to implement it via an external function that gives me this random number satisfying my statistical distributional properties. Conceptually in the steady state shocks are zero, so I would put zero in the initial value.
If this is not the right procedure, are you saying that I could simply do the following?
varexo epsilon;
shocks;
var epsilon = 1;
But how do I make sure that it is drowned from a normal pdf?
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Oct 29, 2014 4:24 pm
by jpfeifer
Dynare always uses perturbation if you use stoch_simul. It assumes that exogenous variables are mean 0 and have a specified variance. That is, the distribution is specified by the first two moments. Higher moments do not play a role. This is isomorphic to shocks being normal.
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Oct 29, 2014 4:43 pm
by hlt215
Okay, thanks a lot. I will try trough the standard varexo block of Dynare then.
Re: Dynare 4.2.0 Error using Rand
Posted:
Mon Nov 03, 2014 12:09 pm
by hlt215
May I ask you one additional thing:
If I want to compute the conditional expectations of that epsilon-shock, I would write a matlab function that looks like:
f = epsilon.*normpdf(epsilon, mu,std) and then integrate this function over some interval.
Now, I am in the position of wanting to compute the expectation of another variable, say "w", but conditional to the same epsilon-distribution. I would write something like:
f = w.*normpdf(epsilon,mu,std). Clearly it doesn't work, but it was to show you what ideally I would like to do.
Any suggestion?
Thanks.
Re: Dynare 4.2.0 Error using Rand
Posted:
Wed Nov 05, 2014 3:52 pm
by jpfeifer
I don't get it. The conditional expectation of a shock is always 0. Every expression dated (+1) or higher in Dynare's stochastic simulations obtains a conditional expectations. For example, if x is an AR1-process with stochastic shock epsilon, the conditional expectation will simply be y in
- Code: Select all
x=0.9*x(-1)+epsilon;
y=x(+1);