ms_sbvar_setup.m 16.5 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
function ms_sbvar_setup(options_)
2
% function ms_sbvar_setup(options_)
Houtan Bastani's avatar
Houtan Bastani committed
3
4
5
% does the general file initialization for ms sbvar
%
% INPUTS
Houtan Bastani's avatar
Houtan Bastani committed
6
%    options_:    (struct)    options
Houtan Bastani's avatar
Houtan Bastani committed
7
8
9
10
11
12
13
%
% OUTPUTS
%    none
%
% SPECIAL REQUIREMENTS
%    none

14
% Copyright (C) 2003-2012 Dynare Team
Houtan Bastani's avatar
Houtan Bastani committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
%
% 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/>.

options_.data = read_variables(options_.datafile, ...
    options_.varobs, [], options_.xls_sheet, options_.xls_range);
33
34
35
[final_year,final_subperiod] = check_datafile_years_assigned(options_);
if ~isempty(final_year)
    options_.ms.final_year = final_year;
36
    options_.ms.final_subperiod = final_subperiod;
37
end
Houtan Bastani's avatar
Houtan Bastani committed
38
39
40
41
42
43
44
45
46
47

if options_.ms.upper_cholesky
    if options_.ms.lower_cholesky
        error(['Upper Cholesky and lower Cholesky decomposition can''t be ' ...
               'requested at the same time!'])
    else
        options_.ms.restriction_fname = 'upper_cholesky';
    end
elseif options_.ms.lower_cholesky
    options_.ms.restriction_fname = 'lower_cholesky';
48
49
elseif ~isempty(options_.ms.Qi) && ~isempty(options_.ms.Ri)
    options_.ms.restriction_fname = 'exclusions';
Houtan Bastani's avatar
Houtan Bastani committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
else
    options_.ms.restriction_fname = 0;
end

%==========================================================================
%== Markov Process Specification File
%==========================================================================
markov_file = [options_.ms.output_file_tag '_markov_file.dat'];

%==========================================================================
%== BVAR prior
%==========================================================================

%=== The following mu is effective only if indxPrior==1.
Houtan Bastani's avatar
Houtan Bastani committed
64
%mu = zeros(6,1);   % hyperparameters
Houtan Bastani's avatar
Houtan Bastani committed
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
if length(options_.ms.coefficients_prior_hyperparameters) ~= 6
    error('When specifying the coefficients_prior_hyperparameters, you must pass a vector of 6 numbers')
end
mu = options_.ms.coefficients_prior_hyperparameters;
mu = reshape(mu,1,6);

% mu(1): overall tightness for A0 and Aplus
% mu(2): relative tightness for Aplus
% mu(3): relative tightness for the constant term
% mu(4): tightness on lag decay.  (1.2 - 1.5 faster decay produces better
% inflation forecasts
% mu(5): weight on nvar sums of coeffs dummy observations (unit roots).
% mu(6): weight on single dummy initial observation including constant
% (cointegration, unit roots, and stationarity).

% Alpha on p. 66 for squared time-varying structural shock lambda.
galp = options_.ms.alpha;

% Beta on p. 66 for squared time-varying structural shock lambda.
gbeta = options_.ms.beta;

% Case 3 (no state change across options_.ms.nlags (l) but allows all variables for a
% given lag to switch states). Normal prior variance for glamda
% (nvar-by-nvar for each state) for different variables in lagged D+.  See
% p.71v.
gsig2_lmdm = options_.ms.gsig2_lmdm;


%==========================================================================
%== Data
%==========================================================================
% Read in data to produce rectangular array named xdd.  Each column is one
% data series.
xdd=options_.data;

% Information about timing of the data for consistancy checks
% quarters (4) or months (12)
q_m = options_.ms.freq;
% beginning year in data set
yrBin=options_.ms.initial_year;
% beginning quarter or month in data set
%options_.ms.initial_subperiod = 1;
qmBin=options_.ms.initial_subperiod;
% final year in data set
yrFin=options_.ms.final_year;
% final month or quarter in data set
qmFin=options_.ms.final_subperiod;
% first year to use in estimation
yrStart=options_.ms.initial_year;
% first quarter or month to use in estimation
qmStart=options_.ms.initial_subperiod;
% last year to use in estimation
yrEnd=options_.ms.final_year;
% last quater or month to use in estimation
qmEnd=options_.ms.final_subperiod;
% Log variables in xdd
logindx = [];

% Convert percent to decimal in xdd
pctindx = [];

% Select the variable to use and rearrange columns if desired
%vlist = [3 1 2];
%options_.ms.vlist = [1 2 3];
Houtan Bastani's avatar
Houtan Bastani committed
129
options_.ms.vlist = 1:size(options_.varobs,1);
Houtan Bastani's avatar
Houtan Bastani committed
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
vlist1=options_.ms.vlist;

%==========================================================================
%==========================================================================
%==========================================================================
%== Beginning of code.  Modify below at own risk.
%==========================================================================

% options that may at some point become user specified
%indxC0Pres = 0;    % 1: cross-A0-and-A+ restrictions; 0: idfile_const is all we have
indxC0Pres =options_.ms.cross_restrictions;
% Example for indxOres==1: restrictions of the form P(t) = P(t-1).
%Rform = 0;         % 1: contemporaneous recursive reduced form; 0: restricted (non-recursive) form
Rform =options_.ms.contemp_reduced_form;
% % % Pseudo = 0;  % 1: Pseudo forecasts; 0: real time forecasts
%indxPrior = 1;     % 1: Bayesian prior; 0: no prior
indxPrior =options_.ms.bayesian_prior;
%indxDummy = indxPrior;  % 1: add dummy observations to the data; 0: no dummy added.
indxDummy = options_.ms.bayesian_prior;
%ndobs = 0;         % No dummy observations for xtx, phi, fss, xdatae, etc.  Dummy observations are used as an explicit prior in fn_rnrprior_covres_dobs.m.
%ndobs =options_.ms.dummy_obs;
%if indxDummy
%   ndobs=nvar+1;         % number of dummy observations
%else
%   ndobs=0;    % no dummy observations
%end

%
hpmsmd = [0.0; 0.0];
indxmsmdeqn = [0; 0; 0; 0];  %This option disenable using this in fn_rnrprior_covres_dobs.m

nStates = -1;

%==========================================================================
%== Create initialization file
%==========================================================================

%======================================================================
%== Check and setup data
%======================================================================
% log data
xdd(:,logindx) = log(xdd(:,logindx));

% convert percentage to decimal
xdd(:,pctindx)=.01*xdd(:,pctindx);

if (q_m ~= 12) && (q_m ~= 4)
    disp('Warning: data must be monthly or quarterly!')
    return
end

% number of data points
nData=(yrFin-yrBin)*q_m + (qmFin-qmBin+1);
% number of data points in estimation sample
nSample=(yrEnd-yrStart)*q_m + (qmEnd-qmEnd+1);
% number of periods not used at beginning of data (non-negative number)
nStart=(yrStart-yrBin)*q_m + (qmStart-qmBin);
% number of periods not used at end of data (non-positive number)
nEnd=(yrEnd-yrFin)*q_m + (qmEnd-qmFin);

if (nEnd > 0) || (nStart < 0)
    disp('Warning: desired estimation period not in data set!')
    return
end
if (nSample <= 0)
    disp('Warning: no data points in estimation period!')
    return
end

% reorder variables and create estimation data set
xdgel=xdd(nStart+1:nData+nEnd,vlist1);

% bad data points
baddata = find(isnan(xdgel));
if ~isempty(baddata)
    disp('Warning: some data for estimation period are unavailable.')
    return
end

% set nvar and nexo
nvar=size(xdgel,2);
nexo=1;

% Arranged data information, WITHOUT dummy obs when 0 after mu is used.
% See fn_rnrprior_covres_dobs.m for using the dummy observations as part of
% an explicit prior.
[xtx,xty,yty,fss,phi,y,ncoef,xr,Bh] = fn_dataxy(nvar,options_.ms.nlags,xdgel,mu,0,nexo);


%======================================================================
%== Linear Restrictions
%======================================================================
if Rform
    Ui=cell(nvar,1); Vi=cell(ncoef,1);
    for kj=1:nvar
        Ui{kj} = eye(nvar);  Vi{kj} = eye(ncoef);
    end
else
    [Ui,Vi,n0,np,ixmC0Pres] = feval(options_.ms.restriction_fname,nvar,nexo,options_.ms);
    if min(n0)==0
        disp('A0: restrictions give no free parameters in one of equations')
        return
    elseif min(np)==0
        disp('Aplus: Restrictions in give no free parameters in one of equations')
        return
    end
end


%======================================================================
%== Estimation
%======================================================================
if indxPrior
    %*** Obtains asymmetric prior (with no linear restrictions) with dummy observations as part of an explicit prior (i.e,
    %      reflected in Hpmulti and Hpinvmulti).  See Forecast II, pp.69a-69b for details.
    if 1  % Liquidity effect prior on both MS and MD equations.
        [Pi,H0multi,Hpmulti,H0invmulti,Hpinvmulti] = fn_rnrprior_covres_dobs(nvar,q_m,options_.ms.nlags,xdgel,mu,indxDummy,hpmsmd,indxmsmdeqn);
    else
        [Pi,H0multi,Hpmulti,H0invmulti,Hpinvmulti] = fn_rnrprior(nvar,q_m,options_.ms.nlags,xdgel,mu);
    end
    
    %*** Combines asymmetric prior with linear restrictions
    [Ptld,H0invtld,Hpinvtld] = fn_rlrprior(Ui,Vi,Pi,H0multi,Hpmulti,nvar);
    
    %*** Obtains the posterior matrices for estimation and inference
    [Pmat,H0inv,Hpinv] = fn_rlrpostr(xtx,xty,yty,Ptld,H0invtld,Hpinvtld,Ui,Vi);
else
    %*** Obtain the posterior matrices for estimation and inference
    [Pmat,H0inv,Hpinv] = fn_dlrpostr(xtx,xty,yty,Ui,Vi);
end

if Rform
    %*** Obtain the ML estimate
    A0hatinv = chol(H0inv{1}/fss);   % upper triangular but lower triangular choleski
    A0hat=inv(A0hatinv);
    
    Aphat = Pmat{1}*A0hat;
else
    %*** Obtain the ML estimate
    %   load idenml
    x = 10*rand(sum(n0),1);
    H0 = eye(sum(n0));
    crit = 1.0e-9;
    nit = 10000;
    %
    [fhat,xhat,grad,Hhat,itct,fcount,retcodehat] = csminwel('fn_a0freefun',x,H0,'fn_a0freegrad',crit,nit,Ui,nvar,n0,fss,H0inv);
276

Houtan Bastani's avatar
Houtan Bastani committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
    A0hat = fn_tran_b2a(xhat,Ui,nvar,n0);
    
    xhat = fn_tran_a2b(A0hat,Ui,nvar,n0);
    [Aphat,ghat] = fn_gfmean(xhat,Pmat,Vi,nvar,ncoef,n0,np);
    if indxC0Pres
        Fhatur0P = Fhat;  % ur: unrestriced across A0 and A+
        for ki = 1:size(ixmC0Pres,1)   % loop through the number of equations in which
            % cross-A0-A+ restrictions occur. See St. Louis Note p.5.
            ixeq = ixmC0Pres{ki}(1,1);   % index for the jth equation in consideration.
            Lit = Vi{ixeq}(ixmC0Pres{ki}(:,2),:);  % transposed restriction matrix Li
            % V_j(i,:) in f_j(i) = V_j(i,:)*g_j
            ci = ixmC0Pres{ki}(:,4) .* A0hat(ixmC0Pres{ki}(:,3),ixeq);
            % s * a_j(h) in the restriction f_j(i) = s * a_j(h).
            LtH = Lit/Hpinv{ixeq};
            HLV = LtH'/(LtH*Lit');
            gihat = Vi{ixeq}'*Fhatur0P(:,ixeq);
            Aphat(:,ixeq) = Vi{ixeq}*(gihat + HLV*(ci-Lit*gihat));
        end
    end
end


%======================================================================
%== Create matlab initialization file
%======================================================================
matlab_filename = ['matlab_',options_.ms.output_file_tag,'.prn'];
fidForC = fopen(matlab_filename,'w');

fprintf(fidForC,'\n%s\n','//== gxia: alpha parameter for gamma prior of xi ==//');
fprintf(fidForC,' %20.15f ', galp);
fprintf(fidForC, '\n\n');

fprintf(fidForC,'\n%s\n','//== gxib: beta parameter for gamma prior of xi ==//');
fprintf(fidForC,' %20.15f ', gbeta);
fprintf(fidForC, '\n\n');

fprintf(fidForC,'\n%s\n','//== glamdasig: sigma parameter for normal prior of lamda ==//');
fprintf(fidForC,' %20.15f ', sqrt(gsig2_lmdm));
fprintf(fidForC, '\n\n');

%=== lags, nvar, nStates, sample size (excluding options_.ms.nlags where, with dummyies, fss=nSample-options_.ms.nlags+ndobs).
fprintf(fidForC,'\n%s\n','//== lags, nvar, nStates, T ==//');
fprintf(fidForC,' %d  %d  %d  %d\n\n\n',options_.ms.nlags, nvar, nStates, fss);

%=== A0hat nvar-by-nvar from the constant VAR.
fprintf(fidForC,'\n%s\n','//== A0hat: nvar-by-nvar ==//');
indxFloat = 1;
xM = A0hat;
nrows = nvar;
ncols = nvar;
fn_fprintmatrix(fidForC, xM, nrows, ncols, indxFloat)

%=== Aphat ncoef-by-nvar from the constant VAR.
%=== Each column of Aphat is in the order of [nvar variables for 1st lag, ..., nvar variables for last lag, constant term].
fprintf(fidForC,'\n%s\n','//== Aphat: ncoef(lags*nvar+1)-by-nvar ==//');
indxFloat = 1;
xM = Aphat;
nrows = ncoef;
ncols = nvar;
fn_fprintmatrix(fidForC, xM, nrows, ncols, indxFloat)

%=== n0const: nvar-by-1, whose ith element represents the number of free A0 parameters in ith equation for the case of constant parameters.
fprintf(fidForC,'\n%s\n','//== n0const: nvar-by-1 ==//');
indxFloat = 0;
xM = n0;
nrows = 1;
ncols = nvar;
fn_fprintmatrix(fidForC, xM', nrows, ncols, indxFloat)

%=== npconst: nvar-by-1, whose ith element represents the number of free A+ parameters in ith equation for the case of constant parameters.
fprintf(fidForC,'\n%s\n','//== npconst: nvar-by-1 ==//');
indxFloat = 0;
xM = np;
nrows = 1;
ncols = nvar;
fn_fprintmatrix(fidForC, xM', nrows, ncols, indxFloat)

354
355
356
357
%=== Specification
fprintf(fidForC,'\n%s','//== Specification (0=default  1=Sims-Zha  2=Random Walk) ==//');
fprintf(fidForC,'\n%d\n\n',options_.ms.specification);

Houtan Bastani's avatar
Houtan Bastani committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
%=== Uiconst: nvar-by-1 cell.  In each cell, nvar-by-qi orthonormal basis for the null of the ith
%           equation contemporaneous restriction matrix where qi is the number of free parameters.
%           With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector
%           of total original parameters and bi is a vector of free parameters. When no
%           restrictions are imposed, we have Ui = I.  There must be at least one free
%           parameter left for the ith equation.
fprintf(fidForC,'\n%s\n','//== Uiconst: cell(nvar,1) and nvar-by-n0const(i) for the ith cell (equation) ==//');
for i_=1:nvar
    fn_fprintmatrix(fidForC, Ui{i_}, nvar, n0(i_), 1);
end

%=== Viconst: nvar-by-1 cell.  In each cell, k-by-ri orthonormal basis for the null of the ith
%           equation lagged restriction matrix where k is a total of exogenous variables and
%           ri is the number of free parameters. With this transformation, we have fi = Vi*gi
%           or Vi'*fi = gi where fi is a vector of total original parameters and gi is a
%           vector of free parameters. There must be at least one free parameter left for
%           the ith equation.
fprintf(fidForC,'\n%s\n','//== Viconst: cell(nvar,1) and ncoef-by-n0const(i) for the ith cell (equation) ==//');
for i_=1:nvar
    fn_fprintmatrix(fidForC, Vi{i_}, ncoef, np(i_), 1);
end

%=== H0barconstcell: cell(nvar,1) (equations) and n-by-n for each cell (equaiton).
%=== H0barconst:  prior covariance matrix for each column of A0 under asymmetric prior (including SZ dummy obs.) with NO linear restrictions imposed yet.
fprintf(fidForC,'\n%s\n','//== H0barconstcell: cell(nvar,1) and n-by-n for the ith cell (equation) ==//');
for i_=1:nvar
    fn_fprintmatrix(fidForC, H0multi(:,:,i_), nvar, nvar, 1);
end

%=== Hpbarconstcell: cell(nvar,1) (equations) and ncoef-by-ncoef for each cell (equaiton).
%=== Hpbarconst:  prior covariance matrix for each column of A+ under asymmetric prior (including SZ dummy obs.) with NO linear restrictions imposed yet.
fprintf(fidForC,'\n%s\n','//== Hpbarconstcell: cell(nvar,1) and ncoef-by-ncoef for the ith cell (equation) ==//');
for i_=1:nvar
    fn_fprintmatrix(fidForC, Hpmulti(:,:,i_), ncoef, ncoef, 1);
end

%=== phi:  X; T-by-k; column: [nvar for 1st lag, ..., nvar for last lag, other exogenous terms, const term]
fprintf(fidForC,'\n%s\n','//== Xright -- X: T-by-ncoef ==//');
xM = phi;
nrows = fss;
ncols = ncoef;
for ki=1:nrows
    for kj=1:ncols
        fprintf(fidForC,' %20.15f ',xM((kj-1)*nrows+ki));
        if (kj==ncols)
            fprintf(fidForC,'\n');
        end
    end
    if (ki==nrows)
        fprintf(fidForC,'\n\n');
    end
end

%=== y:    Y: T-by-nvar where T=fss
fprintf(fidForC,'\n%s\n','//== Yleft -- Y: T-by-nvar ==//');
xM = y;
nrows = fss;
ncols = nvar;
for ki=1:nrows
    for kj=1:ncols
        fprintf(fidForC,' %20.15f ',xM((kj-1)*nrows+ki));
        if (kj==ncols)
            fprintf(fidForC,'\n');
        end
    end
    if (ki==nrows)
        fprintf(fidForC,'\n\n');
    end
end

fclose(fidForC);

%======================================================================
%== Create C initialization filename
%======================================================================
ms_write_markov_file(markov_file,options_)
434
create_init_file = [matlab_filename,' ',markov_file,' ',options_.ms.file_tag];
Houtan Bastani's avatar
Houtan Bastani committed
435
436
[err] = ms_sbvar_create_init_file(create_init_file);
mexErrCheck('ms_sbvar_create_init_file',err);
437
end