File: dynare_simul.m

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (174 lines) | stat: -rw-r--r-- 5,110 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
%
% SYNOPSIS
% 
% r = dynare_simul(name, shocks)
% r = dynare_simul(name, prefix, shocks)
% r = dynare_simul(name, shocks, start)
% r = dynare_simul(name, prefix, shocks, start)
%
%     name     name of MAT-file produced by dynare++
%     prefix   prefix of variables in the MAT-file
%     shocks   matrix of shocks
%     start    zero period value
%
% Note that this file requires the dynare_simul_ DLL to be in the path.
% This DLL is distributed with Dynare, under the mex/matlab or mex/octave
% subdirectory.
%
% SEMANTICS
%
% The command reads a decision rule from the MAT-file having the given
% prefix. Then it starts simulating the decision rule with zero time value
% equal to the given start. It uses the given shocks for the simulation. If
% the start is not given, the state about which the decision rule is
% centralized is taken (called fix point, or stochastic steady state, take
% your pick).
%
%     prefix   Use the prefix with which you called dynare++, the default
%              prefix in dynare++ is 'dyn'.
%     shocks   Number of rows must be a number of exogenous shocks,
%              number of columns gives the number of simulated
%              periods. NaNs and Infs in the matrix are substitued by
%              draws from the normal distribution using the covariance
%              matrix given in the model file.
%     start    Vector of endogenous variables in the ordering given by
%              <prefix>_vars.
%
% Seed for random generator is derived from calling rand(1,1). Therefore,
% seeding can be controlled with rand('state') and rand('state',some_seed).
%
% EXAMPLES
%
% All examples suppose that the prefix is 'dyn' and that your_model.mat
% has been loaded into Matlab.
%
% 1. response to permanent negative shock to the third exo var EPS3 for
%    100 periods
%
%       shocks = zeros(4,100); % 4 exogenous variables in the model
%       shocks(dyn_i_EPS3,:) = -0.1; % the permanent shock to EPS3
%       r = dynare_simul('your_model.mat',shocks);
%
% 2. one stochastic simulation for 100 periods
%
%       shocks = zeros(4,100)./0; % put NaNs everywhere
%       r = dynare_simul('your_model.mat',shocks);
%
% 3. one stochastic simulation starting at 75% undercapitalized economy
%
%       shocks = zeros(4,100)./0; % put NaNs everywhere
%       ystart = dyn_ss; % get copy of DR fix point
%       ystart(dyn_i_K) = 0.75*dyn_ss(dyn_i_K); % scale down the capital
%       r = dynare_simul('your_model.mat',shocks,ystart);
%
% 
% SEE ALSO
%
%   "DSGE Models with Dynare++. A Tutorial.", Ondra Kamenik, 2005

% Copyright (C) 2005-2011, Ondra Kamenik

function r = dynare_simul(varargin)

if exist('dynare_simul_') ~= 3
    error('Can''t find dynare_simul_ DLL in the path. The simplest way to add it is to run Dynare once in this session.')
end

% get the file name and load data
fname = varargin{1};
eval(['load ' fname]);

% set prefix, shocks, ystart
if ischar(varargin{2})
  prefix = varargin{2};
  if length(varargin) == 3
    shocks = varargin{3};
    ystart = NaN;
  elseif length(varargin) == 4
    shocks = varargin{3};
    ystart = varargin{4};
  else
    error('Wrong number of parameters.');
  end
else
  prefix = 'dyn';
  if length(varargin) == 2
    shocks = varargin{2};
    ystart = NaN;
  elseif length(varargin) == 3
    shocks = varargin{2};
    ystart = varargin{3};
  else
    error('Wrong number of parameters.');
  end
end

% load all needed variables but prefix_g_*
if (exist([prefix '_nstat']))
  nstat = eval([prefix '_nstat']);
else
  error(['Could not find variable ' prefix '_nstat in workspace']);
end
if (exist([prefix '_npred']))
  npred = eval([prefix '_npred']);
else
  error(['Could not find variable ' prefix '_npred in workspace']);
end
if (exist([prefix '_nboth']))
  nboth = eval([prefix '_nboth']);
else
  error(['Could not find variable ' prefix '_nboth in workspace']);
end
if (exist([prefix '_nforw']))
  nforw = eval([prefix '_nforw']);
else
  error(['Could not find variable ' prefix '_nforw in workspace']);
end
if (exist([prefix '_ss']))
  ss = eval([prefix '_ss']);
else
  error(['Could not find variable ' prefix '_ss in workspace']);
end
if (exist([prefix '_vcov_exo']))
  vcov_exo = eval([prefix '_vcov_exo']);
else
  error(['Could not find variable ' prefix '_vcov_exo in workspace']);
end
nexog = size(vcov_exo,1);

if isnan(ystart)
  ystart = ss;
end

% newer version of dynare++ doesn't return prefix_g_0, we make it here if
% it does not exist in workspace
g_zero = [prefix '_g_0'];
if (~ exist(g_zero))
  eval([ g_zero '= zeros(nstat+npred+nboth+nforw,1);']);
end

% make derstr a string of comma seperated existing prefix_g_*
derstr = [',' g_zero];
order = 1;
cont = 1;
while cont == 1
  g_ord = [prefix '_g_' num2str(order)];
  if (exist(g_ord))
    derstr = [derstr ',' g_ord];
    order = order + 1;
  else
    cont = 0;
  end
end

% set seed
seed = ceil(10000*rand(1,1));

% call dynare_simul_
command = ['[err,r]=dynare_simul_(' num2str(order-1) ',nstat,npred,nboth,nforw,' ...
           'nexog,ystart,shocks,vcov_exo,seed,ss' derstr ');'];
eval(command);

if err
    error('Simulation failed')
end