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.
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:
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
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.
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. normshock.zip (1.8 KB)
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.
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?
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.
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.
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
I’ll try being more precise. My understanding is that the conditional expectation of a shock is always zero iff I’m integrating over the full distribution, that is from -inf to +inf. But if one integral’s boundary is an endogenous variable, the expected value of the epsilon-shock shouldn’t be zero. I think I can better explain myself if you have a look at the attached document from where it should be clearer what I would need to do.
The answer is not so easy. If you don’t have a recursive representation to get rid the integral by defining the whole thing as a variable (e.g. [How to include an integral in dynare code?)), you can only try a external function [Integration function - dynare v4?). Unfortunately, I don’t have experience with models like this. You may want to look at how Christiano/Motto/Rostagno (2014): RIsk shocks, AER dealt with this. They have replication files online.
Using external functions is actually what I did. It looked to be quite a successful method, in the sense that I think I can succeed to implement the first integral in this way:
Declaring the external function before the model block:
external_function(name=integral_afa2,nargs=1,first_deriv_provided);
Writing a function afa2.m that looks like
function f = afa2(epsilon)
f = epsilon.*normpdf(epsilon,0,1);
end
integral_afa_2 is then a function that integrates afa_2
My problem, however, is about the second integral. In particular, I am not able to write a function like the one in 2) but for the variable ‘w’ and associated to the epsilon-pdf.
Something like:
f = w.*normpdf(epsilon,0,1); (But this doesn’t work)
Thank you for your references, I will try to have a look.