dr_block.m 28.3 KB
Newer Older
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
function [dr,info,M_,options_,oo_] = dr_block(dr,task,M_,options_,oo_)
% function [dr,info,M_,options_,oo_] = dr_block(dr,task,M_,options_,oo_)
% computes the reduced form solution of a rational expectation model (first
% approximation of the stochastic model around the deterministic steady state). 
%
% INPUTS
%   dr         [matlab structure] Decision rules for stochastic simulations.
%   task       [integer]          if task = 0 then dr1 computes decision rules.
%                                 if task = 1 then dr1 computes eigenvalues.
%   M_         [matlab structure] Definition of the model.           
%   options_   [matlab structure] Global options.
%   oo_        [matlab structure] Results 
%    
% OUTPUTS
%   dr         [matlab structure] Decision rules for stochastic simulations.
%   info       [integer]          info=1: the model doesn't define current variables uniquely
%                                 info=2: problem in mjdgges.dll info(2) contains error code. 
%                                 info=3: BK order condition not satisfied info(2) contains "distance"
%                                         absence of stable trajectory.
%                                 info=4: BK order condition not satisfied info(2) contains "distance"
%                                         indeterminacy.
%                                 info=5: BK rank condition not satisfied.
%                                 info=6: The jacobian matrix evaluated at the steady state is complex.        
%   M_         [matlab structure]            
%   options_   [matlab structure]
%   oo_        [matlab structure]
%  
% ALGORITHM
%   first order block relaxation method applied to the model
30
%    E[A Yt-1 + B Yt + C Yt+1 + ut] = 0
31
32
33
34
35
%    
% SPECIAL REQUIREMENTS
%   none.
%  

36
% Copyright (C) 2010-2011 Dynare Team
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
%
% 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/>.

info = 0;
verbose = 0;
55
56
57
58
if options_.order > 1
    error('2nd and 3rd order approximation not implemented with block option')
end

59
60
61
62
63
64
65
66
67
z = repmat(dr.ys,1,M_.maximum_lead + M_.maximum_lag + 1);
if (isfield(M_,'block_structure'))
    data = M_.block_structure.block;
    Size = length(M_.block_structure.block);
else
    data = M_;
    Size = 1;
end;
if (options_.bytecode)
68
    [chck, zz, data]= bytecode('dynamic','evaluate',z,[oo_.exo_simul oo_.exo_det_simul], M_.params, dr.ys, 1, data);
69
else
70
    [r, data] = feval([M_.fname '_dynamic'], z', [oo_.exo_simul oo_.exo_det_simul], M_.params, dr.ys, M_.maximum_lag+1, data);
71
72
73
74
75
76
77
78
79
80
    chck = 0;
end;
mexErrCheck('bytecode', chck);
dr.rank = 0;
dr.eigval = [];
dr.nstatic = 0;
dr.nfwrd = 0;
dr.npred = 0;
dr.nboth = 0;
dr.nd = 0;
81

82
83
dr.ghx = [];
dr.ghu = [];
84
85
86
87
88
89
90
91
92
93
94
95
%Determine the global list of state variables:
dr.state_var = M_.state_var;
M_.block_structure.state_var = dr.state_var;
n_sv = size(dr.state_var, 2);
dr.ghx = zeros(M_.endo_nbr, length(dr.state_var));
dr.exo_var = 1:M_.exo_nbr;
dr.ghu = zeros(M_.endo_nbr, M_.exo_nbr);
dr.nstatic = M_.nstatic;
dr.nfwrd = M_.nfwrd;
dr.npred = M_.npred;
dr.nboth = M_.nboth;
dr.nyf = 0;
96
97
98
99
for i = 1:Size;
    ghx = [];
    indexi_0 = 0;
    if (verbose)
100
        disp('======================================================================');
101
102
103
        disp(['Block ' int2str(i)]);
        disp('-----------');
        data(i)
104
105
106
107
108
    end;
    n_pred = data(i).n_backward;
    n_fwrd = data(i).n_forward;
    n_both = data(i).n_mixed;
    n_static = data(i).n_static;
109
    dr.nyf = dr.nyf  + n_fwrd + n_both;
110
111
112
113
114
    nd = n_pred + n_fwrd + 2*n_both;
    dr.nd = dr.nd + nd;
    n_dynamic = n_pred + n_fwrd + n_both;
    exo_nbr = M_.block_structure.block(i).exo_nbr;
    exo_det_nbr = M_.block_structure.block(i).exo_det_nbr;
115
    other_endo_nbr = M_.block_structure.block(i).other_endo_nbr;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    jacob = full(data(i).g1);
    lead_lag_incidence = data(i).lead_lag_incidence;
    endo = data(i).variable;
    exo = data(i).exogenous;
    if (verbose)
        disp('jacob');
        disp(jacob);
        disp('lead_lag_incidence');
        disp(lead_lag_incidence);
    end;
    maximum_lag = data(i).maximum_endo_lag;
    maximum_lead = data(i).maximum_endo_lead;
    n = n_dynamic + n_static;
    
130
131
132
133
134
135
    block_type = M_.block_structure.block(i).Simulation_Type;
    if task ~= 1
        if block_type == 2 || block_type == 4 || block_type == 6 
            block_type = 8;
        end;
    end;
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
136
    if maximum_lag > 0 && (n_pred > 0  || n_both > 0) && block_type ~= 1 
137
138
139
        indexi_0 = min(lead_lag_incidence(2,:));
    end;
    switch block_type
140
      case 1
141
      %% ------------------------------------------------------------------
142
143
144
145
146
        %Evaluate Forward
        if maximum_lag > 0 && n_pred > 0
            indx_r = find(M_.block_structure.block(i).lead_lag_incidence(1,:));
            indx_c = M_.block_structure.block(i).lead_lag_incidence(1,indx_r);
            data(i).eigval = diag(jacob(indx_r, indx_c));
147
            data(i).rank = 0;
148
149
150
151
152
        else
            data(i).eigval = [];
            data(i).rank = 0;
        end
        dr.eigval = [dr.eigval ; data(i).eigval];
153
        dr.rank = dr.rank + data(i).rank;
154
155
        %First order approximation
        if task ~= 1
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
            [tmp1, tmp2, indx_c] = find(M_.block_structure.block(i).lead_lag_incidence(2,:));
            B = jacob(:,indx_c);
            if (maximum_lag > 0 && n_pred > 0)
                [indx_r, tmp1, indx_r_v]  = find(M_.block_structure.block(i).lead_lag_incidence(1,:));
                ghx = - B \ jacob(:,indx_r_v);
            end;
            if other_endo_nbr
                fx = data(i).g1_o;
                % retrieves the derivatives with respect to endogenous
                % variable belonging to previous blocks
                fx_tm1 = zeros(n,other_endo_nbr);
                fx_t = zeros(n,other_endo_nbr);
                fx_tp1 = zeros(n,other_endo_nbr);
                % stores in fx_tm1 the lagged values of fx
                [r, c, lag] = find(data(i).lead_lag_incidence_other(1,:));
                fx_tm1(:,c) = fx(:,lag);
                % stores in fx the current values of fx
                [r, c, curr] = find(data(i).lead_lag_incidence_other(2,:));
                fx_t(:,c) = fx(:,curr);
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
175
                % stores in fx_tp1 the leaded values of fx
176
177
178
179
180
181
182
183
184
185
                [r, c, lead] = find(data(i).lead_lag_incidence_other(3,:));
                fx_tp1(:,c) = fx(:,lead);

                l_x = dr.ghx(data(i).other_endogenous,:);
                l_x_sv = dr.ghx(dr.state_var, 1:n_sv);

                selector_tm1 = M_.block_structure.block(i).tm1;
               
                ghx_other = - B \ (fx_t * l_x + (fx_tp1 * l_x * l_x_sv) + fx_tm1 * selector_tm1);
                dr.ghx(endo, :) = dr.ghx(endo, :) + ghx_other;
186
            end;
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
            
            if exo_nbr
                fu = data(i).g1_x;
                exo = dr.exo_var;
                if other_endo_nbr > 0
                    l_u_sv = dr.ghu(dr.state_var,:);
                    l_x = dr.ghx(data(i).other_endogenous,:);
                    l_u = dr.ghu(data(i).other_endogenous,:);
                    fu_complet = zeros(n, M_.exo_nbr);
                    fu_complet(:,data(i).exogenous) = fu;
                    ghu = - B \ (fu_complet + fx_tp1 * l_x * l_u_sv + (fx_t) * l_u );
                else
                    fu_complet = zeros(n, M_.exo_nbr);
                    fu_complet(:,data(i).exogenous) = fu;
                    ghu = - B \ fu_complet;
                end;
            else
                exo = dr.exo_var;
                if other_endo_nbr > 0
                     l_u_sv = dr.ghu(dr.state_var,:);
                     l_x = dr.ghx(data(i).other_endogenous,:);
                     l_u = dr.ghu(data(i).other_endogenous,:);
                     ghu = -B \ (fx_tp1 * l_x * l_u_sv + (fx_t) * l_u );
                else
                    ghu = [];
                end
            end
214
215
        end
      case 2
216
      %% ------------------------------------------------------------------
217
218
        %Evaluate Backward
        if maximum_lead > 0 && n_fwrd > 0
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
219
220
            indx_r = find(M_.block_structure.block(i).lead_lag_incidence(3,:));
            indx_c = M_.block_structure.block(i).lead_lag_incidence(3,indx_r);
221
            data(i).eigval = 1 ./ diag(jacob(indx_r, indx_c));
222
223
224
225
226
227
            data(i).rank = sum(abs(data(i).eigval) > 0);
        else
            data(i).eigval = [];
            data(i).rank = 0;
        end
        dr.eigval = [dr.eigval ; data(i).eigval];
228
229
230
231
232
233
234
235
236
237
        dr.rank = dr.rank + data(i).rank;
        %First order approximation
        if task ~= 1
            if (maximum_lag > 0)
                indx_r = find(M_.block_structure.block(i).lead_lag_incidence(3,:));
                indx_c = M_.block_structure.block(i).lead_lag_incidence(3,indx_r);
                ghx = - inv(jacob(indx_r, indx_c));
            end;
            ghu =  - inv(jacob(indx_r, indx_c)) * data(i).g1_x;
        end
238
      case 3
239
240
      %% ------------------------------------------------------------------
        %Solve Forward single equation
241
242
        if maximum_lag > 0 && n_pred > 0
            data(i).eigval = - jacob(1 , 1 : n_pred) / jacob(1 , n_pred + n_static + 1 : n_pred + n_static + n_pred + n_both);
243
            data(i).rank = 0;
244
245
246
247
248
249
250
251
        else
            data(i).eigval = [];
            data(i).rank = 0;
        end;
        dr.eigval = [dr.eigval ; data(i).eigval];
        %First order approximation
        if task ~= 1
            if (maximum_lag > 0)
252
253
254
                 ghx = - jacob(1 , 1 : n_pred) / jacob(1 , n_pred + n_static + 1 : n_pred + n_static + n_pred + n_both);
            else
                 ghx = 0;
255
            end;
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
            if other_endo_nbr
                fx = data(i).g1_o;
                % retrieves the derivatives with respect to endogenous
                % variable belonging to previous blocks
                fx_tm1 = zeros(n,other_endo_nbr);
                fx_t = zeros(n,other_endo_nbr);
                fx_tp1 = zeros(n,other_endo_nbr);
                % stores in fx_tm1 the lagged values of fx
                [r, c, lag] = find(data(i).lead_lag_incidence_other(1,:));
                fx_tm1(:,c) = fx(:,lag);
                % stores in fx the current values of fx
                [r, c, curr] = find(data(i).lead_lag_incidence_other(2,:));
                fx_t(:,c) = fx(:,curr);
                % stores in fx_tm1 the leaded values of fx
                [r, c, lead] = find(data(i).lead_lag_incidence_other(3,:));
                fx_tp1(:,c) = fx(:,lead);

                l_x = dr.ghx(data(i).other_endogenous,:);
                l_x_sv = dr.ghx(dr.state_var, 1:n_sv);
                
                selector_tm1 = M_.block_structure.block(i).tm1;
                ghx_other = - (fx_t * l_x + (fx_tp1 * l_x * l_x_sv) + fx_tm1 * selector_tm1) / jacob(1 , n_pred + 1 : n_pred + n_static + n_pred + n_both);
                dr.ghx(endo, :) = dr.ghx(endo, :) + ghx_other;

            end;
            if exo_nbr
                fu = data(i).g1_x;
                if other_endo_nbr > 0
                    l_u_sv = dr.ghu(dr.state_var,:);
                    l_x = dr.ghx(data(i).other_endogenous,:);
                    l_u = dr.ghu(data(i).other_endogenous,:);
                    fu_complet = zeros(n, M_.exo_nbr);
                    fu_complet(:,data(i).exogenous) = fu;
                    ghu = -(fu_complet + fx_tp1 * l_x * l_u_sv + (fx_t) * l_u ) / jacob(1 , n_pred + 1 : n_pred + n_static + n_pred + n_both);
                    exo = dr.exo_var;
                else
                    ghu = - fu  / jacob(1 , n_pred + 1 : n_pred + n_static + n_pred + n_both);
                end;
            else
                 if other_endo_nbr > 0
                     l_u_sv = dr.ghu(dr.state_var,:);
                     l_x = dr.ghx(data(i).other_endogenous,:);
                     l_u = dr.ghu(data(i).other_endogenous,:);
                     ghu = -(fx_tp1 * l_x * l_u_sv + (fx_t) * l_u ) / jacob(1 , n_pred + 1 : n_pred + n_static + n_pred + n_both);
                     exo = dr.exo_var;
                 else
                     ghu = [];
                 end
            end
305
306
        end
      case 4
307
308
      %% ------------------------------------------------------------------
        %Solve Backward single equation
309
310
311
312
313
314
315
316
317
        if maximum_lead > 0 && n_fwrd > 0
            data(i).eigval = - jacob(1 , n_pred + n - n_fwrd + 1 : n_pred + n) / jacob(1 , n_pred + n + 1 : n_pred + n + n_fwrd) ;
            data(i).rank = sum(abs(data(i).eigval) > 0);
        else
            data(i).eigval = [];
            data(i).rank = 0;
        end;
        dr.rank = dr.rank + data(i).rank;
        dr.eigval = [dr.eigval ; data(i).eigval];
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
318
      case 6
319
      %% ------------------------------------------------------------------
320
321
322
323
        %Solve Forward complete
        if maximum_lag > 0 && n_pred > 0
            data(i).eigval = eig(- jacob(: , 1 : n_pred) / ...
                                 jacob(: , (n_pred + n_static + 1 : n_pred + n_static + n_pred )));
324
            data(i).rank = 0;
325
326
327
328
329
        else
            data(i).eigval = [];
            data(i).rank = 0;
        end;
        dr.eigval = [dr.eigval ; data(i).eigval];
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
        if task ~= 1
            if (maximum_lag > 0)
                 ghx = - jacob(1 , 1 : n_pred) / jacob(1 , n_pred + n_static + 1 : n_pred + n_static + n_pred + n_both);
            else
                 ghx = 0;
            end;
            if other_endo_nbr
                fx = data(i).g1_o;
                % retrieves the derivatives with respect to endogenous
                % variable belonging to previous blocks
                fx_tm1 = zeros(n,other_endo_nbr);
                fx_t = zeros(n,other_endo_nbr);
                fx_tp1 = zeros(n,other_endo_nbr);
                % stores in fx_tm1 the lagged values of fx
                [r, c, lag] = find(data(i).lead_lag_incidence_other(1,:));
                fx_tm1(:,c) = fx(:,lag);
                % stores in fx the current values of fx
                [r, c, curr] = find(data(i).lead_lag_incidence_other(2,:));
                fx_t(:,c) = fx(:,curr);
                % stores in fx_tm1 the leaded values of fx
                [r, c, lead] = find(data(i).lead_lag_incidence_other(3,:));
                fx_tp1(:,c) = fx(:,lead);

                l_x = dr.ghx(data(i).other_endogenous,:);
                l_x_sv = dr.ghx(dr.state_var, 1:n_sv);
                
                selector_tm1 = M_.block_structure.block(i).tm1;
                ghx_other = - (fx_t * l_x + (fx_tp1 * l_x * l_x_sv) + fx_tm1 * selector_tm1) / jacob(1 , n_pred + 1 : n_pred + n_static + n_pred + n_both);
                dr.ghx(endo, :) = dr.ghx(endo, :) + ghx_other;
            end;
            if exo_nbr
                fu = data(i).g1_x;
                if other_endo_nbr > 0
                    l_u_sv = dr.ghu(dr.state_var,:);
                    l_x = dr.ghx(data(i).other_endogenous,:);
                    l_u = dr.ghu(data(i).other_endogenous,:);
                    fu_complet = zeros(n, M_.exo_nbr);
                    fu_complet(:,data(i).exogenous) = fu;
                    ghu = -(fu_complet + fx_tp1 * l_x * l_u_sv + (fx_t) * l_u ) / jacob(1 , n_pred + 1 : n_pred + n_static + n_pred + n_both);
                    exo = dr.exo_var;
                else
                    ghu = - fu  / jacob(1 , n_pred + 1 : n_pred + n_static + n_pred + n_both);
                end;
            else
                 if other_endo_nbr > 0
                     l_u_sv = dr.ghu(dr.state_var,:);
                     l_x = dr.ghx(data(i).other_endogenous,:);
                     l_u = dr.ghu(data(i).other_endogenous,:);
                     ghu = -(fx_tp1 * l_x * l_u_sv + (fx_t) * l_u ) / jacob(1 , n_pred + 1 : n_pred + n_static + n_pred + n_both);
                     exo = dr.exo_var;
                 else
                     ghu = [];
                 end
            end
        end
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
385
      case 7
386
      %% ------------------------------------------------------------------
387
388
389
390
391
392
393
394
395
396
397
        %Solve Backward complete
        if maximum_lead > 0 && n_fwrd > 0
            data(i).eigval = eig(- jacob(: , n_pred + n - n_fwrd + 1: n_pred + n))/ ...
                jacob(: , n_pred + n + 1 : n_pred + n + n_fwrd);
            data(i).rank = sum(abs(data(i).eigval) > 0);
        else
            data(i).eigval = [];
            data(i).rank = 0;
        end;
        dr.rank = dr.rank + data(i).rank;
        dr.eigval = [dr.eigval ; data(i).eigval];
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
398
      case {5,8}
399
400
      %% ------------------------------------------------------------------
        %The lead_lag_incidence contains columns in the following order:
401
402
        %  static variables, backward variable, mixed variables and forward variables
        %  
403
        %Proceeds to a QR decomposition on the jacobian matrix in order to reduce the problem size
404
405
406
        index_c = lead_lag_incidence(2,:);             % Index of all endogenous variables present at time=t
        index_s = lead_lag_incidence(2,1:n_static);    % Index of all static endogenous variables present at time=t
        if n_static > 0
407
            [Q, junk] = qr(jacob(:,index_s));
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
            aa = Q'*jacob;
        else
            aa = jacob;
        end;
        index_0m = (n_static+1:n_static+n_pred) + indexi_0 - 1;
        index_0p = (n_static+n_pred+1:n) + indexi_0 - 1;
        index_m = 1:(n_pred+n_both);
        indexi_p = max(lead_lag_incidence(2,:))+1;
        index_p = indexi_p:size(jacob, 2);
        nyf = n_fwrd + n_both;
        A = aa(:,index_m);  % Jacobain matrix for lagged endogeneous variables
        B = aa(:,index_c);  % Jacobian matrix for contemporaneous endogeneous variables
        C = aa(:,index_p);  % Jacobain matrix for led endogeneous variables

        row_indx = n_static+1:n;
        
        D = [[aa(row_indx,index_0m) zeros(n_dynamic,n_both) aa(row_indx,index_p)] ; [zeros(n_both, n_pred) eye(n_both) zeros(n_both, n_both + n_fwrd)]];
        E = [-aa(row_indx,[index_m index_0p])  ; [zeros(n_both, n_both + n_pred) eye(n_both, n_both + n_fwrd) ] ];

        [err, ss, tt, w, sdim, data(i).eigval, info1] = mjdgges(E,D,options_.qz_criterium);
428

429
430
431
432
433
434
435
436
437
438
439
440
        if (verbose)
            disp('eigval');
            disp(data(i).eigval);
        end;
        if info1
            info(1) = 2;
            info(2) = info1;
            return
        end
        nba = nd-sdim;
        if task == 1
            data(i).rank = rank(w(nd-nyf+1:end,nd-nyf+1:end));
441
            dr.rank = dr.rank + data(i).rank;
442
443
444
            if ~exist('OCTAVE_VERSION','builtin')
                data(i).eigval = eig(E,D);
            end
445
            dr.eigval = [dr.eigval ; data(i).eigval];
446
447
448
449
450
451
452
453
454
455
456
457
458
459
        end
        if (verbose)
            disp(['sum eigval > 1 = ' int2str(sum(abs(data(i).eigval) > 1.)) ' nyf=' int2str(nyf) ' and dr.rank=' int2str(data(i).rank)]);
            disp(['data(' int2str(i) ').eigval']);
            disp(data(i).eigval);
        end;
        
        %First order approximation
        if task ~= 1
            if nba ~= nyf
                sorted_roots = sort(abs(dr.eigval));
                if isfield(options_,'indeterminacy_continuity')
                    if options_.indeterminacy_msv == 1
                        [ss,tt,w,q] = qz(e',d');
460
                        [ss,tt,w,junk] = reorder(ss,tt,w,q);
461
462
463
                        ss = ss';
                        tt = tt';
                        w  = w';
464
                        %nba = nyf;
465
466
467
468
469
470
471
472
473
474
475
476
477
                    end
                else
                    if nba > nyf
                        temp = sorted_roots(nd-nba+1:nd-nyf)-1-options_.qz_criterium;
                        info(1) = 3;
                    elseif nba < nyf;
                        temp = sorted_roots(nd-nyf+1:nd-nba)-1-options_.qz_criterium;
                        info(1) = 4;
                    end
                    info(2) = temp'*temp;
                    return
                end
            end
478
479
            indx_stable_root = 1: (nd - nyf);     %=> index of stable roots
            indx_explosive_root = n_pred + n_both + 1:nd;  %=> index of explosive roots
480
481
482
483
484
485
486
487
488
489
490
491
            % derivatives with respect to dynamic state variables
            % forward variables
            Z = w';
            Z11t = Z(indx_stable_root,    indx_stable_root)';
            Z21  = Z(indx_explosive_root, indx_stable_root);
            Z22  = Z(indx_explosive_root, indx_explosive_root);
            if ~isfloat(Z21) && (condest(Z21) > 1e9)
                % condest() fails on a scalar under Octave
                info(1) = 5;
                info(2) = condest(Z21);
                return;
            else
492
493
                %gx = -inv(Z22) * Z21;
                gx = - Z22 \ Z21;
494
            end
495

496
497
            % predetermined variables
            hx =  Z11t * inv(tt(indx_stable_root, indx_stable_root)) * ss(indx_stable_root, indx_stable_root) * inv(Z11t);
498
            
499
500
            k1 = 1:(n_pred+n_both);
            k2 = 1:(n_fwrd+n_both);
501

502
503
504
505
            ghx = [hx(k1,:); gx(k2(n_both+1:end),:)];
            
            %lead variables actually present in the model
            
506
507
508
509
510
511
            j4 = n_static+n_pred+1:n_static+n_pred+n_both+n_fwrd;   % Index on the forward and both variables
            j3 = nonzeros(lead_lag_incidence(2,j4)) - n_static - 2 * n_pred - n_both;  % Index on the non-zeros forward and both variables
            j4 = find(lead_lag_incidence(2,j4)); 
            
            if n_static > 0
                B_static = B(:,1:n_static);  % submatrix containing the derivatives w.r. to static variables
512
            else
513
514
515
516
517
                B_static = [];
            end;
            %static variables, backward variable, mixed variables and forward variables
            B_pred = B(:,n_static+1:n_static+n_pred+n_both);
            B_fyd = B(:,n_static+n_pred+n_both+1:end);
518
519
520
521
522
523
524
525
526
527
528
529

            % static variables
            if n_static > 0
                temp = - C(1:n_static,j3)*gx(j4,:)*hx;
                j5 = index_m;
                b = aa(:,index_c);
                b10 = b(1:n_static, 1:n_static);
                b11 = b(1:n_static, n_static+1:n);
                temp(:,j5) = temp(:,j5)-A(1:n_static,:);
                temp = b10\(temp-b11*ghx);
                ghx = [temp; ghx];
                temp = [];
530
531
            end;
            
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
532
533
            A_ = real([B_static C(:,j3)*gx+B_pred B_fyd]); % The state_variable of the block are located at [B_pred B_both]
            
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
            if other_endo_nbr
                if n_static > 0
                     fx = Q' * data(i).g1_o;
                else
                    fx = data(i).g1_o;
                end;
                % retrieves the derivatives with respect to endogenous
                % variable belonging to previous blocks
                fx_tm1 = zeros(n,other_endo_nbr);
                fx_t = zeros(n,other_endo_nbr);
                fx_tp1 = zeros(n,other_endo_nbr);
                % stores in fx_tm1 the lagged values of fx
                [r, c, lag] = find(data(i).lead_lag_incidence_other(1,:));
                fx_tm1(:,c) = fx(:,lag);
                % stores in fx the current values of fx
                [r, c, curr] = find(data(i).lead_lag_incidence_other(2,:));
                fx_t(:,c) = fx(:,curr);
                % stores in fx_tp1 the leaded values of fx
                [r, c, lead] = find(data(i).lead_lag_incidence_other(3,:));
                fx_tp1(:,c) = fx(:,lead);

                l_x = dr.ghx(data(i).other_endogenous,:);
                
                l_x_sv = dr.ghx(dr.state_var, :);
                
                selector_tm1 = M_.block_structure.block(i).tm1; 

                B_ = [zeros(size(B_static)) zeros(n,n_pred) C(:,j3) ];
                C_ = l_x_sv;
                D_ = (fx_t * l_x + fx_tp1 * l_x * l_x_sv + fx_tm1 * selector_tm1 );
                % Solve the Sylvester equation:
                % A_ * gx + B_ * gx * C_ + D_ = 0
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
566
567
568
569
570
571
                if block_type == 5
                    vghx_other = - inv(kron(eye(size(D_,2)), A_) + kron(C_', B_)) * vec(D_);
                    ghx_other = reshape(vghx_other, size(D_,1), size(D_,2));
                else
                    [err, ghx_other] = gensylv(1, A_, B_, C_, -D_);
                end;
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
                if options_.aim_solver ~= 1 && options_.use_qzdiv
                   % Necessary when using Sims' routines for QZ
                   ghx_other = real(ghx_other);
                end
                
                dr.ghx(endo, :) = dr.ghx(endo, :) + ghx_other;
            end;

            if exo_nbr
                if n_static > 0
                    fu = Q' * data(i).g1_x;
                else
                    fu = data(i).g1_x;
                end;

                if other_endo_nbr > 0
                    l_u_sv = dr.ghu(dr.state_var,:);
                    l_x = dr.ghx(data(i).other_endogenous,:);
                    l_u = dr.ghu(data(i).other_endogenous,:);
                    fu_complet = zeros(n, M_.exo_nbr);
                    fu_complet(:,data(i).exogenous) = fu;
                    % Solve the equation in ghu:
                    % A_ * ghu + (fu_complet + fx_tp1 * l_x * l_u_sv + (fx_t + B_ * ghx_other) * l_u ) = 0
                    
                    ghu = -A_\ (fu_complet + fx_tp1 * l_x * l_u_sv + fx_t * l_u + B_ * ghx_other  * l_u_sv  );
                    exo = dr.exo_var;
                else
Ferhat Mihoubi's avatar
Ferhat Mihoubi committed
599
                    ghu = - A_ \ fu;
600
601
602
603
604
605
606
607
608
609
610
611
                end;
            else
                if other_endo_nbr > 0
                    l_u_sv = dr.ghu(dr.state_var,:);
                    l_x = dr.ghx(data(i).other_endogenous,:);
                    l_u = dr.ghu(data(i).other_endogenous,:);
                    % Solve the equation in ghu:
                    % A_ * ghu + (fx_tp1 * l_x * l_u_sv + (fx_t + B_ * ghx_other) * l_u ) = 0
                    ghu = -real(A_)\ (fx_tp1 * l_x * l_u_sv + (fx_t * l_u + B_ * ghx_other * l_u_sv) );
                    exo = dr.exo_var;
                else
                    ghu = [];
612
613
                end;
            end
614
615


616
            
617
            if options_.loglinear == 1
618
619
620
621
622
623
624
625
                error('log linear option is for the moment not supported in first order approximation for a block decomposed mode');
%                 k = find(dr.kstate(:,2) <= M_.maximum_endo_lag+1);
%                 klag = dr.kstate(k,[1 2]);
%                 k1 = dr.order_var;
%                 
%                 ghx = repmat(1./dr.ys(k1),1,size(ghx,2)).*ghx.* ...
%                       repmat(dr.ys(k1(klag(:,1)))',size(ghx,1),1);
%                 ghu = repmat(1./dr.ys(k1),1,size(ghu,2)).*ghu;
626
            end
627

628

629
630
631
632
633
            if options_.aim_solver ~= 1 && options_.use_qzdiv
                % Necessary when using Sims' routines for QZ
                ghx = real(ghx);
                ghu = real(ghu);
            end
634

635
636
            %exogenous deterministic variables
            if exo_det_nbr > 0
637
638
639
640
641
642
643
644
645
646
647
                error('deterministic exogenous are not yet implemented in first order approximation for a block decomposed model');
%                 f1 = sparse(jacobia_(:,nonzeros(M_.lead_lag_incidence(M_.maximum_endo_lag+2:end,order_var))));
%                 f0 = sparse(jacobia_(:,nonzeros(M_.lead_lag_incidence(M_.maximum_endo_lag+1,order_var))));
%                 fudet = data(i).g1_xd;
%                 M1 = inv(f0+[zeros(n,n_static) f1*gx zeros(n,nyf-n_both)]);
%                 M2 = M1*f1;
%                 dr.ghud = cell(M_.exo_det_length,1);
%                 dr.ghud{1} = -M1*fudet;
%                 for i = 2:M_.exo_det_length
%                     dr.ghud{i} = -M2*dr.ghud{i-1}(end-nyf+1:end,:);
%                 end
648
            end
649
        end
650
651
    end;
    if task ~=1
652
653
654
655
656
657
658
        if (maximum_lag > 0 && n_pred > 0)
            sorted_col_dr_ghx = M_.block_structure.block(i).sorted_col_dr_ghx;
            dr.ghx(endo, sorted_col_dr_ghx) = dr.ghx(endo, sorted_col_dr_ghx) + ghx;
            data(i).ghx = ghx;
            data(i).pol.i_ghx = sorted_col_dr_ghx;
        else
            data(i).pol.i_ghx = [];
659
        end;
660
661
662
663
664
665
666
667
668
669
670
671
        data(i).ghu = ghu;
        dr.ghu(endo, exo) = ghu;
        data(i).pol.i_ghu = exo;
    end;
    
   if (verbose)
        disp('dr.ghx');
        dr.ghx
        disp('dr.ghu');
        dr.ghu
   end; 
   
672
end;
673
M_.block_structure.block = data ;
674
if (verbose)
675
676
677
678
679
        disp('dr.ghx');
        disp(real(dr.ghx));
        disp('dr.ghu');
        disp(real(dr.ghu));
end; 
680
if (task == 1)
681
    return;
682
end;