pruned_state_space_system.m 70 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
function pruned_state_space = pruned_state_space_system(M, options, dr, indy, nlags, useautocorr, compute_derivs)
% Set up the pruned state space ABCD representation:
%   z =      c + A*z(-1) + B*inov
%   y = ys + d + C*z(-1) + D*inov
% References: 
% - Andreasen, Martin M., Jesús Fernández-Villaverde and Juan F. Rubio-Ramírez (2018):
%   "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications",
%   Review of Economic Studies, Volume 85, Issue 1, Pages 1–49.
% - Mutschler, Willi (2018): "Higher-order statistics for DSGE models",
%   Econometrics and Statistics, Volume 6, Pages 44-56.
% =========================================================================
% INPUTS
13
14
15
16
17
18
19
%   M:              [structure] storing the model information
%   options:        [structure] storing the options
%   dr:             [structure] storing the results from perturbation approximation
%   indy:           [vector]    index of control variables in DR order
%   nlags:          [integer]   number of lags in autocovariances and autocorrelations
%   useautocorr:    [boolean]   true: compute autocorrelations
%   compute_derivs: [boolean]   true: compute derivatives
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
% -------------------------------------------------------------------------
% OUTPUTS
% pruned_state_space: [structure] with the following fields:
%   indx:       [x_nbr by 1]
%                 index of state variables
%   indy:       [y_nbr by 1]
%                 index of control variables
%   A:          [z_nbr by z_nbr]
%                 state space transition matrix A mapping previous states to current states
%   B:          [z_nbr by inov_nbr]
%                 state space transition matrix B mapping current inovations to current states
%   c:          [z_nbr by 1]
%                 state space transition matrix c mapping constants to current states
%   C:          [y_nbr by z_nbr]
%                 state space measurement matrix C mapping previous states to current controls
%   D:          [y_nbr by inov_nbr]
%                 state space measurement matrix D mapping current inovations to current controls
%   d:          [y_nbr by 1]
%                 state space measurement matrix d mapping constants to current controls
%   Var_inov    [inov_nbr by inov_nbr]
%                 contemporenous covariance matrix of innovations, i.e. E[inov*inov']
%   Var_z       [z_nbr by z_nbr]
%                 contemporenous covariance matrix of states z
%   Var_y       [y_nbr by y_nbr]
%                 contemporenous covariance matrix of controls y
%   Var_yi      [y_nbr by y_nbr by nlags]
%                 autocovariance matrix of controls y
%   Corr_y      [y_nbr by y_nbr]
%                 contemporenous correlation matrix of controls y
%   Corr_yi     [y_nbr by y_nbr by nlags]
%                 autocorrelation matrix of controls y
%   E_y         [y_nbr by 1] 
%                 unconditional theoretical mean of control variables y
%
% if compute_derivs == 1, then the following additional fields are outputed:
%   dA:         [z_nbr by z_nbr by totparam_nbr]
%                 parameter Jacobian of A
%   dB:         [z_nbr by inov_nbr by totparam_nbr]
%                 parameter Jacobian of B
%   dc:         [z_nbr by totparam_nbr]
%                 parameter Jacobian of c
%   dC:         [y_nbr by z_nbr by totparam_nbr]
%                 parameter Jacobian of C
%   dD:         [y_nbr by inov_nbr by totparam_nbr]
%                 parameter Jacobian of D
%   dd:         [y_nbr by totparam_nbr]
%                 parameter Jacobian of d
%   dVar_inov   [inov_nbr by inov_nbr by totparam_nbr]
%                 parameter Jacobian of Var_inov
%   dVar_z      [z_nbr by z_nbr by totparam_nbr]
%                 parameter Jacobian of Var_z
%   dVar_y      [y_nbr by y_nbr by totparam_nbr]
%                 parameter Jacobian of Var_y
%   dVar_yi     [y_nbr by y_nbr by nlags by totparam_nbr]
%                 parameter Jacobian of Var_yi
%   dCorr_y     [y_nbr by y_nbr by totparam_nbr]
%                 parameter Jacobian of Corr_y
%   dCorr_yi    [y_nbr by y_nbr by nlags by totparam_nbr]
%                 parameter Jacobian of Corr_yi
%   dE_y        [y_nbr by totparam_nbr]
%                 parameter Jacobian of E_y
% -------------------------------------------------------------------------
% This function is called by
%   * get_identification_jacobians.m
%   * identification_numerical_objective.m
% -------------------------------------------------------------------------
% This function calls
%   * allVL1.m
%   * commutation.m
89
%   * disclyap_fast (MEX)
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
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
%   * duplication.m
%   * lyapunov_symm.m
%   * prodmom
%   * prodmom_deriv
%   * Q6_plication
%   * quadruplication.m
%   * vec.m
% =========================================================================
% Copyright (C) 2019-2020 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/>.
% =========================================================================

%% MAIN IDEA:
%   Decompose the state vector x into first-order effects xf, second-order 
%   effects xs, and third-order effects xrd, i.e. x=xf+xs+xrd. Then, Dynare's
%   perturbation approximation for the state vector up to third order
%   (with Gaussian innovations u, i.e. no odd moments, hxxs=huus=hxus=hsss=0) is:
%   x = hx*( xf(-1)+xs(-1)+xrd(-1) )
%     + hu*u
%     + 1/2*hxx*kron( xf(-1)+xs(-1)+xrd(-1) , xf(-1)+xs(-1)+xrd(-1) )
%     + hxu*kron( xf(-1)+xs(-1)+xrd(-1) , u )
%     + 1/2*huu*kron( u , u )
%     + 1/2*hss*sig^2
%     + 1/6*hxxx*kron( kron( xf(-1)+xs(-1)+xrd(-1) , xf(-1)+xs(-1)+xrd(-1) ) , xf(-1)+xs(-1)+xrd(-1) )
%     + 1/6*huuu*kron( kron( u , u ) , u )
%     + 3/6*hxxu*kron( kron( xf(-1)+xs(-1)+xrd(-1) , xf(-1)+xs(-1)+xrd(-1) ) , u )
%     + 3/6*hxuu*kron( kron( xf(-1)+xs(-1)+xrd(-1) , u ) , u)
%     + 3/6*hxss*( xf(-1)+xs(-1)+xrd(-1) )*sig^2
%     + 3/6*huss*u*sig^2
%   where:
%     hx  = dr.ghx(indx,:);    hu  = dr.ghu(indx,:);
%     hxx = dr.ghxx(indx,:);   hxu = dr.ghxu(indx,:);   huu = dr.ghuu(indx,:);   hss = dr.ghs2(indx,:);
%     hxxx = dr.ghxxx(indx,:); huuu = dr.ghuuu(indx,:); hxxu = dr.ghxxu(indx,:); hxuu = dr.ghxxu(indx,:); hxss = dr.ghxss(indx,:); huss = dr.ghuss(indx,:);
%     and similarly for control variables:
%   y = gx*( xf(-1)+xs(-1)+xrd(-1) )
%     + gu*u
%     + 1/2*gxx*kron( xf(-1)+xs(-1)+xrd(-1) , xf(-1)+xs(-1)+xrd(-1) )
%     + gxu*kron( xf(-1)+xs(-1)+xrd(-1) , u )
%     + 1/2*guu*kron( u , u )
%     + 1/2*gss*sig^2
%     + 1/6*gxxx*kron( kron( xf(-1)+xs(-1)+xrd(-1) , xf(-1)+xs(-1)+xrd(-1) ) , xf(-1)+xs(-1)+xrd(-1) )
%     + 1/6*guuu*kron( kron( u , u ) , u )
%     + 3/6*gxxu*kron( kron( xf(-1)+xs(-1)+xrd(-1) , xf(-1)+xs(-1)+xrd(-1) ) , u )
%     + 3/6*gxuu*kron( kron( xf(-1)+xs(-1)+xrd(-1) , u ) , u)
%     + 3/6*gxss*( xf(-1)+xs(-1)+xrd(-1) )*sig^2
%     + 3/6*guss*u*sig^2
%   where:
%     gx  = dr.ghx(indy,:);    gu  = dr.ghu(indy,:);
%     gxx = dr.ghxx(indy,:);   gxu = dr.ghxu(indy,:);   guu = dr.ghuu(indy,:);   gss = dr.ghs2(indy,:);
%     gxxx = dr.ghxxx(indy,:); guuu = dr.ghuuu(indy,:); gxxu = dr.ghxxu(indy,:); gxuu = dr.ghxxu(indy,:); gxss = dr.ghxss(indy,:); guss = dr.ghuss(indy,:);
%
%   PRUNING means getting rid of terms higher than the approximation order, i.e.
%         - involving fourth-order effects:  kron(xf,xrd), kron(xs,xs), kron(xrd,xf), kron(xrd,u), 
%                                            kron(kron(xf,xf),xs), kron(kron(xf,xs),xf), kron(kron(xs,xf),xf)
%                                            kron(kron(xf,xs),u), kron(kron(xs,xf),u)
%                                            kron(kron(xs,u),u)
%                                            xs*sig^2
%         - involving fifth-order effects:   kron(xs,xrd), kron(xrd,xs),
%                                            kron(kron(xf,xf),xrd), kron(kron(xf,xs),xs), kron(kron(xf,xrd),xf), kron(kron(xs,xf),xs), kron(kron(xs,xs),xf), kron(kron(xrd,xf),xf)
%                                            kron(kron(xf,xrd),u), kron(kron(xs,xs),u), kron(kron(xrd,xf),u)
%                                            kron(kron(xrd,u),u)
%                                            xrd*sig^2
%         - involving sixth-order effects:   kron(xrd,xrd),
%                                            kron(kron(xf,xs),xrd), kron(kron(xf,xrd),xs), kron(kron(xs,xrd),xrd), kron(kron(xs,xs),xs), kron(kron(xs,xrd),xf), kron(kron(xf,xf),xs), kron(kron(xrd,xs),xf)
%                                            kron(kron(xs,xrd),u), kron(kron(xrd,xs),u)
%         - involving seventh-order effects: kron(kron(xf,xrd),xrd), kron(kron(xs,xs),xrd), kron(kron(xs,xrd),xs), kron(kron(xrd,xf),xrd), kron(kron(xrd,xs),xs), kron(kron(xrd,xrd),xf)
%                                            kron(kron(xrd,xrd),u)
%         - involving eighth-order effects:  kron(kron(xs,xrd),xrd), kron(kron(xrd,xs),xrd), kron(kron(xrd,xrd),xs)
%         - involving ninth-order effects:   kron(kron(xrd,xrd),xrd)
%   Note that u is treated as a first-order effect and the perturbation parameter sig as a variable.
%
% SUMMARY OF LAW OF MOTIONS: Set up the law of motions for the individual effects, but keep only effects of same order
%   Notation: I_n=eye(n) and K_m_n=commutation(m,n)
%
%   First-order effects: keep xf and u
%       xf = hx*xf(-1) + hu*u
%       Note that we 
%
%   Second-order effects: keep xs, kron(xf,xf), kron(u,u), kron(xf,u), and sig^2
%       xs = hx*xs(-1) + 1/2*hxx*kron(xf(-1),xf(-1)) + 1/2*huu*(kron(u,u)-Sigma_e(:)+Sigma_e(:)) + hxu*kron(xf(-1),u) + 1/2*hss*sig^2%     
%
%   Third-order effects: keep xrd, kron(xf,xs), kron(xs,xf), kron(xs,u), kron(kron(xf,xf),xf), kron(kron(u,u),u), kron(kron(xf,xf),u), kron(kron(xf,u),u), xf*sig^2, u*sig^2
%       xrd = hx*xrd(-1) + 1/2*hxx*(kron(xf(-1),xs(-1))+kron(xs(-1),xf(-1))) + hxu*kron(xs(-1),u) + 1/6*hxxx*kron(xf(-1),kron(xf(-1),xf(-1))) + 1/6*huuu*kron(u,kron(u,u)) + 3/6*hxxu*kron(xf(-1),kron(xf(-1),u)) + 3/6*hxuu*kron(xf(-1),kron(u,u)) + 3/6*hxss*xf(-1)*sig^2 + 3/6*huss*u*sig^2
%     Simplified (due to symmetry in hxx):
%       xrd = hx*xrd(-1) + hxx*(kron(xf(-1),xs(-1)) + hxu*kron(xs(-1),u) + 1/6*hxxx*kron(xf(-1),kron(xf(-1),xf(-1))) + 1/6*huuu*kron(u,kron(u,u)) + 3/6*hxxu*kron(xf(-1),kron(xf(-1),u)) + 3/6*hxuu*kron(xf(-1),kron(u,u)) + 3/6*hxss*xf(-1)*sig^2 + 3/6*huss*u*sig^2%     
%
%   Auxiliary equation kron(xf,xf) to set up the VAR(1) pruned state space system
%       kron(xf,xf) = kron(hx,hx)*kron(xf(-1),xf(-1)) + kron(hu,hu)*(kron(u,u)-Sigma_e(:)+Sigma_e(:)) + kron(hx,u)*kron(xf(-1),u) + kron(u,hx)*kron(u,xf(-1))
%     Simplified using commutation matrix:
%       kron(xf,xf) = kron(hx,hx)*kron(xf(-1),xf(-1)) + (I_xx+K_x_x)*kron(hx,hu)*kron(xf(-1),u) + kron(hu,hu)*kron(u,u)
%
%   Auxiliary equation kron(xf,xs) to set up the VAR(1) pruned state space system
%       kron(xf,xs) = kron(hx,hx)*kron(xf(-1),xs(-1)) + kron(hu,hx)*kron(u,xs(-1))
%                   + kron(hx,1/2*hxx)*kron(kron(xf(-1),xf(-1)),xf(-1)) + kron(hu,1/2*hxx)*kron(kron(u,xf(-1)),xf(-1))
%                   + kron(hx,1/2*huu)*kron(kron(xf(-1),u),u) + kron(hu,1/2*huu)*kron(kron(u,u),u)
%                   + kron(hx,hxu)*kron(kron(xf(-1),xf(-1)),u) + kron(hu,hxu)*kron(kron(u,xf(-1)),u)
%                   + kron(hx,1/2*hss)*xf(-1)*sig^2 + kron(hu,1/2*hss)*u*sig^2
%     Simplified using commutation matrix:
%       kron(xf,xs) = kron(hx,hx)*kron(xf(-1),xs(-1))
%                   + K_x_x*kron(hx,hu)*kron(xs(-1),u)
%                   + kron(hx,1/2*hxx)*kron(kron(xf(-1),xf(-1)),xf(-1))
%                   + ( kron(hx,hxu) + K_x_x*kron(1/2*hxx,hu) )*kron(kron(xf(-1),xf(-1)),u)
%                   + ( kron(hx,1/2*huu) + K_x_x*kron(hxu,hu) )*kron(kron(xf(-1),u),u)
%                   + kron(hu,1/2*huu)*kron(kron(u,u),u)
%                   + kron(hx,1/2*hss)*xf(-1)*sig^2
%                   + kron(hu,1/2*hss)*u*sig^2
%
%   Auxiliary equation kron(kron(xf,xf),xf) to set up the VAR(1) pruned state space system
%       kron(kron(xf,xf),xf) = kron(kron(hx,hx),hx)*kron(kron(xf(-1),xf(-1)),xf(-1))
%                            + kron(kron(hx,hu),hx)*kron(kron(xf(-1),u),xf(-1))
%                            + kron(kron(hx,hx),hu)*kron(kron(xf(-1),xf(-1)),u)
%                            + kron(kron(hx,hu),hu)*kron(kron(xf(-1),u),u)
%                            + kron(kron(hu,hx),hx)*kron(kron(u,xf(-1)),xf(-1))
%                            + kron(kron(hu,hu),hx)*kron(kron(u,u),xf(-1))
%                            + kron(kron(hu,hx),hu)*kron(kron(u,xf(-1)),u)
%                            + kron(kron(hu,hu),hu)*kron(kron(u,u),u)
%     Simplified using commutation matrix:
%       kron(kron(xf,xf),xf) = kron(kron(hx,hx),hx)*kron(kron(xf(-1),xf(-1)),xf(-1))
%                            + ( kron(kron(hx,hx),hu) + K_xx_x*kron(hx,(I_xx+K_x_x)*kron(hx,hu)) )*kron(kron(xf(-1),xf(-1)),u)
%                            + ( kron((I_xx+K_x_x)*kron(hx,hu),hu) + K_xx_x*kron(kron(hx,hu),hu) )*kron(kron(xf(-1),u),u)
%                            + kron(kron(hu,hu),hu)*kron(kron(u,u),u)
%
%   Law of motion for control variables y (either VAROBS variables or if no VAROBS statement is given then for all endogenous variables)
%       y = steady_state(y)
%         + gx*( xf(-1)+xs(-1)+xrd(-1) )
%         + gu*u
%         + 1/2*gxx*kron(xf(-1),xf(-1)) + gxx*kron(xf(-1),xs(-1))
%         + gxu*kron(xf(-1),u) + gxu*kron(xs(-1),u)
%         + 1/2*guu*(kron(u,u)-Sigma_e+Sigma_e)
%         + 1/2*gss*sig^2
%         + 1/6*gxxx*kron(kron(xf(-1),xf(-1)),xf(-1))
%         + 1/6*guuu*kron(kron(u,u),u)
%         + 3/6*gxxu*kron(kron(xf(-1),xf(-1),u)
%         + 3/6*gxuu*kron(kron(xf(-1),u),u)
%         + 3/6*gxss*xf(-1)*sig^2
%         + 3/6*guss*u*sig^2
%
% See code below how z and inov are defined at first, second, and third order,
% and how to set up A, B, C, D and compute unconditional first and second moments of inov, z and y

244
persistent QPu COMBOS4 Q6Pu COMBOS6 K_u_xx K_u_ux K_xx_x
245
246

%% Auxiliary indices and objects
247
order = options.order;
248
249
250
251
252
253
254
if isempty(options.qz_criterium)
    % set default value for qz_criterium: if there are no unit roots one can use 1.0
    % If they are possible, you may have have multiple unit roots and the accuracy 
    % decreases when computing the eigenvalues in lyapunov_symm. Hence, we normally use 1+1e-6
    % Note that unit roots are only possible at first-order, at higher order we set it to 1
    options.qz_criterium = 1+1e-6;
end
255
indx = [M.nstatic+(1:M.nspred) M.endo_nbr+(1:size(dr.ghx,2)-M.nspred)]';
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
if isempty(indy)
    indy = (1:M.endo_nbr)'; %by default select all variables in DR order
end
u_nbr    = M.exo_nbr;
x_nbr    = length(indx);
y_nbr    = length(indy);
Yss      = dr.ys(dr.order_var);
hx       = dr.ghx(indx,:);
gx       = dr.ghx(indy,:);
hu       = dr.ghu(indx,:);
gu       = dr.ghu(indy,:);
E_uu     = M.Sigma_e; %this is E[u*u']

if compute_derivs
    stderrparam_nbr = length(dr.derivs.indpstderr);
    corrparam_nbr   = size(dr.derivs.indpcorr,1);
    modparam_nbr    = length(dr.derivs.indpmodel);
273
    totparam_nbr    = stderrparam_nbr+corrparam_nbr+modparam_nbr;
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
305
306
307
308
309
    dYss   = dr.derivs.dYss;
	dhx    = dr.derivs.dghx(indx,:,:);
    dgx    = dr.derivs.dghx(indy,:,:);
	dhu    = dr.derivs.dghu(indx,:,:);
    dgu    = dr.derivs.dghu(indy,:,:);
	dE_uu  = dr.derivs.dSigma_e;
end

% first-order approximation indices for extended state vector z and extended innovations vector inov 
id_z1_xf    = (1:x_nbr);
id_inov1_u  = (1:u_nbr);
if order > 1
    % second-order approximation indices for extended state vector z and extended innovations vector inov 
    id_z2_xs      = id_z1_xf(end)     + (1:x_nbr);
    id_z3_xf_xf   = id_z2_xs(end)     + (1:x_nbr*x_nbr);
    id_inov2_u_u  = id_inov1_u(end)   + (1:u_nbr*u_nbr);
    id_inov3_xf_u = id_inov2_u_u(end) + (1:x_nbr*u_nbr);

    hxx = dr.ghxx(indx,:);
    gxx = dr.ghxx(indy,:);
    hxu = dr.ghxu(indx,:);
    gxu = dr.ghxu(indy,:);
    huu = dr.ghuu(indx,:);
    guu = dr.ghuu(indy,:);
    hss = dr.ghs2(indx,:);
    gss = dr.ghs2(indy,:);
    if compute_derivs        
        dhxx = dr.derivs.dghxx(indx,:,:);
        dgxx = dr.derivs.dghxx(indy,:,:);
        dhxu = dr.derivs.dghxu(indx,:,:);
        dgxu = dr.derivs.dghxu(indy,:,:);
        dhuu = dr.derivs.dghuu(indx,:,:);
        dguu = dr.derivs.dghuu(indy,:,:);
        dhss = dr.derivs.dghs2(indx,:);
        dgss = dr.derivs.dghs2(indy,:);
    end
310
end
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
if order > 2
    % third-order approximation indices for extended state vector z and extended innovations vector inov 
    id_z4_xrd        = id_z3_xf_xf(end)      + (1:x_nbr);
    id_z5_xf_xs      = id_z4_xrd(end)        + (1:x_nbr*x_nbr);
    id_z6_xf_xf_xf   = id_z5_xf_xs(end)      + (1:x_nbr*x_nbr*x_nbr);
    id_inov4_xs_u    = id_inov3_xf_u(end)    + (1:x_nbr*u_nbr);
    id_inov5_xf_xf_u = id_inov4_xs_u(end)    + (1:x_nbr*x_nbr*u_nbr);
    id_inov6_xf_u_u  = id_inov5_xf_xf_u(end) + (1:x_nbr*u_nbr*u_nbr);
    id_inov7_u_u_u   = id_inov6_xf_u_u(end)  + (1:u_nbr*u_nbr*u_nbr);

    hxxx = dr.ghxxx(indx,:);
    gxxx = dr.ghxxx(indy,:);
    hxxu = dr.ghxxu(indx,:);
    gxxu = dr.ghxxu(indy,:);
    hxuu = dr.ghxuu(indx,:);
    gxuu = dr.ghxuu(indy,:);
    huuu = dr.ghuuu(indx,:);
    guuu = dr.ghuuu(indy,:);
    hxss = dr.ghxss(indx,:);
    gxss = dr.ghxss(indy,:);
    huss = dr.ghuss(indx,:);
    guss = dr.ghuss(indy,:);
    if compute_derivs
        dhxxx = dr.derivs.dghxxx(indx,:,:);
        dgxxx = dr.derivs.dghxxx(indy,:,:);
        dhxxu = dr.derivs.dghxxu(indx,:,:);
        dgxxu = dr.derivs.dghxxu(indy,:,:);
       	dhxuu = dr.derivs.dghxuu(indx,:,:);
        dgxuu = dr.derivs.dghxuu(indy,:,:);
        dhuuu = dr.derivs.dghuuu(indx,:,:);
        dguuu = dr.derivs.dghuuu(indy,:,:);
        dhxss = dr.derivs.dghxss(indx,:,:);
        dgxss = dr.derivs.dghxss(indy,:,:);
        dhuss = dr.derivs.dghuss(indx,:,:);
        dguss = dr.derivs.dghuss(indy,:,:);
    end
347
end
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

%% First-order state space system
% Auxiliary state vector z is defined by:          z    = [xf]
% Auxiliary innovations vector inov is defined by: inov = [u]
z_nbr       = x_nbr;
inov_nbr    = M.exo_nbr;
A           = hx;
B           = hu;
c           = zeros(x_nbr,1);
C           = gx;
D           = gu;
d           = zeros(y_nbr,1);
Varinov     = E_uu;
E_inovzlag1  = zeros(inov_nbr,z_nbr); %at first-order E[inov*z(-1)'] = 0
Om_z        = B*Varinov*B';
E_xf        = zeros(x_nbr,1);

lyapunov_symm_method = 1; %method=1 to initialize persistent variables
[Var_z,Schur_u] = lyapunov_symm(A, Om_z,... %at first-order this algorithm is well established and also used in th_autocovariances.m
                                options.lyapunov_fixed_point_tol, options.qz_criterium, options.lyapunov_complex_threshold,...
                                lyapunov_symm_method,...       
                                options.debug); %we use Schur_u to take care of (possible) nonstationary VAROBS variables in moment computations
%find stationary vars
stationary_vars = (1:y_nbr)';
if ~isempty(Schur_u)
    %base this only on first order, because if first-order is stable so are the higher-order pruned systems
    x = abs(gx*Schur_u);
    stationary_vars = find(all(x < options.Schur_vec_tol,2));
376
377
378
end

if compute_derivs == 1
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
    dA          = dhx;
    dB          = dhu;
    dc          = zeros(x_nbr,totparam_nbr);
    dC          = dgx;
    dD          = dgu;
    dd          = zeros(y_nbr,totparam_nbr);
    dVarinov    = dE_uu;
    dE_xf       = zeros(x_nbr,totparam_nbr);
    dE_inovzlag1 = zeros(z_nbr,inov_nbr,totparam_nbr);
    dVar_z   = zeros(z_nbr,z_nbr,totparam_nbr);
    lyapunov_symm_method = 2;%to spare a lot of computing time while not repeating Schur every time
    for jp1 = 1:totparam_nbr
        if jp1 <= stderrparam_nbr+corrparam_nbr
            dOm_z_jp1 = B*dVarinov(:,:,jp1)*B';
            dVar_z(:,:,jp1) = lyapunov_symm(A, dOm_z_jp1,...
                                           options.lyapunov_fixed_point_tol,options.qz_criterium,options.lyapunov_complex_threshold,...
                                           lyapunov_symm_method,...
                                           options.debug);
        else
            dOm_z_jp1  = dB(:,:,jp1)*Varinov*B' + B*Varinov*dB(:,:,jp1)';
            dVar_z(:,:,jp1) = lyapunov_symm(A, dA(:,:,jp1)*Var_z*A' + A*Var_z*dA(:,:,jp1)' + dOm_z_jp1,...
                                           options.lyapunov_fixed_point_tol,options.qz_criterium,options.lyapunov_complex_threshold,...
                                           lyapunov_symm_method,...
                                           options.debug);
        end
    end
405
406
407
end

if order > 1
408
    options.qz_criterium = 1; %pruned state space has no unit roots
409
    % Some common and useful objects for order > 1
410
411
412
413
414
415
416
417
418
419
420
421
422
    E_xfxf   = Var_z;
    if compute_derivs
        dE_xfxf = dVar_z;
    end
    hx_hx    = kron(hx,hx);
    hx_hu    = kron(hx,hu);
    hu_hu    = kron(hu,hu);
    I_xx     = eye(x_nbr^2);
    K_x_x    = commutation(x_nbr,x_nbr,1);
    invIx_hx = (eye(x_nbr)-hx)\eye(x_nbr);

    %Compute unique fourth order product moments of u, i.e. unique(E[kron(kron(kron(u,u),u),u)],'stable')
    u_nbr4    = u_nbr*(u_nbr+1)/2*(u_nbr+2)/3*(u_nbr+3)/4;
423
424
425
426
    if isempty(QPu)
        QPu       = quadruplication(u_nbr);
        COMBOS4   = flipud(allVL1(u_nbr, 4)); %all possible (unique) combinations of powers that sum up to four
    end
427
    E_u_u_u_u = zeros(u_nbr4,1); %only unique entries
428
    if compute_derivs && (stderrparam_nbr+corrparam_nbr>0)
429
        dE_u_u_u_u = zeros(u_nbr4,stderrparam_nbr+corrparam_nbr);
430
    end
431
    for j4 = 1:size(COMBOS4,1)
432
        if compute_derivs && (stderrparam_nbr+corrparam_nbr>0)
433
434
435
            [E_u_u_u_u(j4), dE_u_u_u_u(j4,:)] = prodmom_deriv(E_uu, 1:u_nbr, COMBOS4(j4,:), dE_uu(:,:,1:(stderrparam_nbr+corrparam_nbr)), dr.derivs.dCorrelation_matrix(:,:,1:(stderrparam_nbr+corrparam_nbr)));
        else
            E_u_u_u_u(j4) = prodmom(E_uu, 1:u_nbr, COMBOS4(j4,:));
436
437
        end
    end
438
439
440
441
442
443
444
    E_xfxf_uu = kron(E_xfxf,E_uu');

%% Second-order pruned state space system
% Auxiliary state vector z is defined by:          z    = [xf;xs;kron(xf,xf)]
% Auxiliary innovations vector inov is defined by: inov = [u;kron(u,u)-E_uu(:);kron(xf,u)]
    z_nbr    = x_nbr + x_nbr + x_nbr^2;
    inov_nbr = u_nbr + u_nbr^2 + x_nbr*u_nbr;
445
446

    A = zeros(z_nbr, z_nbr);
447
448
449
450
451
452
453
454
455
456
457
458
    A(id_z1_xf    , id_z1_xf   ) = hx;
    A(id_z2_xs    , id_z2_xs   ) = hx;
    A(id_z2_xs    , id_z3_xf_xf) = 1/2*hxx;
    A(id_z3_xf_xf , id_z3_xf_xf) = hx_hx;

    B = zeros(z_nbr, inov_nbr);
    B(id_z1_xf    , id_inov1_u   ) = hu;
    B(id_z2_xs    , id_inov2_u_u ) = 1/2*huu;
    B(id_z2_xs    , id_inov3_xf_u) = hxu;
    B(id_z3_xf_xf , id_inov2_u_u ) = hu_hu;
    B(id_z3_xf_xf , id_inov3_xf_u) = (I_xx+K_x_x)*hx_hu;

459
    c = zeros(z_nbr, 1);
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    c(id_z2_xs    , 1) = 1/2*hss + 1/2*huu*E_uu(:);
    c(id_z3_xf_xf , 1) = hu_hu*E_uu(:);

    C = zeros(y_nbr, z_nbr);
    C(: , id_z1_xf   ) = gx;
    C(: , id_z2_xs   ) = gx;
    C(: , id_z3_xf_xf) = 1/2*gxx;

    D = zeros(y_nbr, inov_nbr);
    D(: , id_inov1_u   ) = gu;
    D(: , id_inov2_u_u ) = 1/2*guu;
    D(: , id_inov3_xf_u) = gxu;

    d = 1/2*gss + 1/2*guu*E_uu(:);

    Varinov = zeros(inov_nbr,inov_nbr);
    Varinov(id_inov1_u    , id_inov1_u)    = E_uu;
   %Varinov(id_inov1_u    , id_inov2_u_u ) = zeros(u_nbr,u_nbr^2);
   %Varinov(id_inov1_u    , id_inov3_xf_u) = zeros(u_nbr,x_nbr*u_nbr);
   %Varinov(id_inov2_u_u  , id_inov1_u   ) = zeros(u_nbr^2,u_nbr);
    Varinov(id_inov2_u_u  , id_inov2_u_u ) = reshape(QPu*E_u_u_u_u,u_nbr^2,u_nbr^2)-E_uu(:)*E_uu(:)';
   %Varinov(id_inov2_u_u  , id_inov3_xf_u) = zeros(u_nbr^2,x_nbr*u_nbr);
   %Varinov(id_inov3_xf_u , id_inov1_u   ) = zeros(x_nbr*u_nbr,u_nbr);
   %Varinov(id_inov3_xf_u , id_inov2_u_u ) = zeros(x_nbr*u_nbr,u_nbr^2);
    Varinov(id_inov3_xf_u , id_inov3_xf_u) = E_xfxf_uu;

    E_xs        = invIx_hx*(1/2*hxx*E_xfxf(:) + c(id_z2_xs,1));
    E_inovzlag1 = zeros(inov_nbr,z_nbr); %at second-order E[z(-1)*inov'] = 0
    Om_z        = B*Varinov*transpose(B);

    lyapunov_symm_method = 1; %method=1 to initialize persistent variables (if errorflag)
    [Var_z, errorflag] = disclyap_fast(A,Om_z,options.lyapunov_doubling_tol);
    if errorflag %use Schur-based method
        fprintf('PRUNED_STATE_SPACE_SYSTEM: error flag in disclyap_fast at order=2, use lyapunov_symm\n');
        Var_z = lyapunov_symm(A,Om_z,...
                              options.lyapunov_fixed_point_tol,options.qz_criterium,options.lyapunov_complex_threshold,...
                              lyapunov_symm_method,...
                              options.debug);
        lyapunov_symm_method = 2; %in the following we can reuse persistent variables
    end
    % Make sure some stuff is zero due to Gaussianity
    Var_z(id_z1_xf    , id_z2_xs   ) = zeros(x_nbr,x_nbr);
    Var_z(id_z1_xf    , id_z3_xf_xf) = zeros(x_nbr,x_nbr^2);
    Var_z(id_z2_xs    , id_z1_xf   ) = zeros(x_nbr,x_nbr);
    Var_z(id_z3_xf_xf , id_z1_xf   ) = zeros(x_nbr^2,x_nbr);

    if compute_derivs
        dA           = zeros(z_nbr,z_nbr,totparam_nbr);
        dB           = zeros(z_nbr,inov_nbr,totparam_nbr);
        dc           = zeros(z_nbr,totparam_nbr);
        dC           = zeros(y_nbr,z_nbr,totparam_nbr);
        dD           = zeros(y_nbr,inov_nbr,totparam_nbr);
        dd           = zeros(y_nbr,totparam_nbr);
        dVarinov     = zeros(inov_nbr,inov_nbr,totparam_nbr);
        dE_xs        = zeros(x_nbr,totparam_nbr);
        dE_inovzlag1 = zeros(inov_nbr,z_nbr,totparam_nbr);
        dVar_z       = zeros(z_nbr,z_nbr,totparam_nbr);
        
        for jp2 = 1:totparam_nbr
            if jp2 <= (stderrparam_nbr+corrparam_nbr)
                dE_uu_jp2      = dE_uu(:,:,jp2);
                dE_u_u_u_u_jp2 = QPu*dE_u_u_u_u(:,jp2);                
            else
                dE_uu_jp2      = zeros(u_nbr,u_nbr);
                dE_u_u_u_u_jp2 = zeros(u_nbr^4,1);            
525
            end
526
527
528
529
530
531
532
533
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
566
567
568
569
570
            dhx_jp2        = dhx(:,:,jp2);
            dhu_jp2        = dhu(:,:,jp2);
            dhxx_jp2       = dhxx(:,:,jp2);
            dhxu_jp2       = dhxu(:,:,jp2);
            dhuu_jp2       = dhuu(:,:,jp2);
            dhss_jp2       = dhss(:,jp2);            
            dgx_jp2        = dgx(:,:,jp2);
            dgu_jp2        = dgu(:,:,jp2);
            dgxx_jp2       = dgxx(:,:,jp2);
            dgxu_jp2       = dgxu(:,:,jp2);
            dguu_jp2       = dguu(:,:,jp2);
            dgss_jp2       = dgss(:,jp2);
            dhx_hx_jp2     = kron(dhx_jp2,hx) + kron(hx,dhx_jp2);
            dhu_hu_jp2     = kron(dhu_jp2,hu) + kron(hu,dhu_jp2);
            dhx_hu_jp2     = kron(dhx_jp2,hu) + kron(hx,dhu_jp2);
            dE_xfxf_jp2    = dE_xfxf(:,:,jp2);
            dE_xfxf_uu_jp2 = kron(dE_xfxf_jp2,E_uu) + kron(E_xfxf,dE_uu_jp2);

            dA(id_z1_xf    , id_z1_xf    , jp2) = dhx_jp2;
            dA(id_z2_xs    , id_z2_xs    , jp2) = dhx_jp2;
            dA(id_z2_xs    , id_z3_xf_xf , jp2) = 1/2*dhxx_jp2;
            dA(id_z3_xf_xf , id_z3_xf_xf , jp2) = dhx_hx_jp2;

            dB(id_z1_xf    , id_inov1_u    , jp2) = dhu_jp2;
            dB(id_z2_xs    , id_inov2_u_u  , jp2) = 1/2*dhuu_jp2;
            dB(id_z2_xs    , id_inov3_xf_u , jp2) = dhxu_jp2;
            dB(id_z3_xf_xf , id_inov2_u_u  , jp2) = dhu_hu_jp2;
            dB(id_z3_xf_xf , id_inov3_xf_u , jp2) = (I_xx+K_x_x)*dhx_hu_jp2;

            dc(id_z2_xs    , jp2) = 1/2*dhss_jp2 + 1/2*dhuu_jp2*E_uu(:) + 1/2*huu*dE_uu_jp2(:);
            dc(id_z3_xf_xf , jp2) = dhu_hu_jp2*E_uu(:) + hu_hu*dE_uu_jp2(:);

            dC(: , id_z1_xf    , jp2) = dgx_jp2;
            dC(: , id_z2_xs    , jp2) = dgx_jp2;
            dC(: , id_z3_xf_xf , jp2) = 1/2*dgxx_jp2;

            dD(: , id_inov1_u    , jp2) = dgu_jp2;
            dD(: , id_inov2_u_u  , jp2) = 1/2*dguu_jp2;
            dD(: , id_inov3_xf_u , jp2) = dgxu_jp2;

            dd(:,jp2) = 1/2*dgss_jp2 + 1/2*guu*dE_uu_jp2(:) + 1/2*dguu_jp2*E_uu(:);

            dVarinov(id_inov1_u    , id_inov1_u    , jp2) = dE_uu_jp2;
            dVarinov(id_inov2_u_u  , id_inov2_u_u  , jp2) = reshape(dE_u_u_u_u_jp2,u_nbr^2,u_nbr^2) - dE_uu_jp2(:)*E_uu(:)' - E_uu(:)*dE_uu_jp2(:)';
            dVarinov(id_inov3_xf_u , id_inov3_xf_u , jp2) = dE_xfxf_uu_jp2;
571
            
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
            dE_xs(:,jp2) = invIx_hx*( dhx_jp2*E_xs + 1/2*dhxx_jp2*E_xfxf(:) + 1/2*hxx*dE_xfxf_jp2(:) + dc(id_z2_xs,jp2) );
            dOm_z_jp2    = dB(:,:,jp2)*Varinov*B' + B*dVarinov(:,:,jp2)*B' + B*Varinov*dB(:,:,jp2)';
            
            [dVar_z(:,:,jp2), errorflag] = disclyap_fast(A, dA(:,:,jp2)*Var_z*A' + A*Var_z*dA(:,:,jp2)' + dOm_z_jp2, options.lyapunov_doubling_tol);
            if errorflag
                dVar_z(:,:,jp2) = lyapunov_symm(A, dA(:,:,jp2)*Var_z*A' + A*Var_z*dA(:,:,jp2)' + dOm_z_jp2,...
                                               options.lyapunov_fixed_point_tol,options.qz_criterium,options.lyapunov_complex_threshold,...
                                               lyapunov_symm_method,...
                                               options.debug);
                if lyapunov_symm_method == 1
                    lyapunov_symm_method = 2; %now we can reuse persistent schur
                end
            end
            % Make sure some stuff is zero due to Gaussianity
            dVar_z(id_z1_xf    , id_z2_xs    , jp2) = zeros(x_nbr,x_nbr);
            dVar_z(id_z1_xf    , id_z3_xf_xf , jp2) = zeros(x_nbr,x_nbr^2);    
            dVar_z(id_z2_xs    , id_z1_xf    , jp2) = zeros(x_nbr,x_nbr);    
            dVar_z(id_z3_xf_xf , id_z1_xf    , jp2) = zeros(x_nbr^2,x_nbr);
590
591
592
593
        end
    end

    if order > 2
594
        % Some common and useful objects for order > 2
595
596
597
598
599
        if isempty(K_u_xx)
            K_u_xx   = commutation(u_nbr,x_nbr^2,1);
            K_u_ux   = commutation(u_nbr,u_nbr*x_nbr,1);
            K_xx_x   = commutation(x_nbr^2,x_nbr);
        end
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
        hx_hss2  = kron(hx,1/2*hss);
        hu_hss2  = kron(hu,1/2*hss);
        hx_hxx2  = kron(hx,1/2*hxx);
        hxx2_hu  = kron(1/2*hxx,hu);
        hx_hxu   = kron(hx,hxu);
        hxu_hu   = kron(hxu,hu);
        hx_huu2  = kron(hx,1/2*huu);
        hu_huu2  = kron(hu,1/2*huu);
        hx_hx_hx = kron(hx,hx_hx);
        hx_hx_hu = kron(hx_hx,hu);
        hu_hx_hx = kron(hu,hx_hx);
        hu_hu_hu = kron(hu_hu,hu);
        hx_hu_hu = kron(hx,hu_hu);
        hu_hx_hu = kron(hu,hx_hu);
        invIxx_hx_hx = (eye(x_nbr^2)-hx_hx)\eye(x_nbr^2);

        % Reuse second-order results
       %E_xfxf       = Var_z(id_z1_xf, id_z1_xf      );                        %this is E[xf*xf'], we already have that
       %E_xfxs       = Var_z(id_z1_xf, id_z2_xs      );                        %this is E[xf*xs']=0 due to gaussianity
       %E_xfxf_xf    = Var_z(id_z1_xf, id_z3_xf_xf   );                        %this is E[xf*kron(xf_xf)']=0 due to gaussianity
       %E_xsxf       = Var_z(id_z2_xs, id_z1_xf      );                        %this is E[xs*xf']=0 due to gaussianity
        E_xsxs       = Var_z(id_z2_xs, id_z2_xs      ) + E_xs*transpose(E_xs); %this is E[xs*xs']
        E_xsxf_xf    = Var_z(id_z2_xs, id_z3_xf_xf   ) + E_xs*E_xfxf(:)';      %this is E[xs*kron(xf,xf)']
       %E_xf_xfxf    = Var_z(id_z3_xf_xf, id_z1_xf   );                        %this is E[kron(xf,xf)*xf']=0 due to gaussianity
        E_xf_xfxs    = Var_z(id_z3_xf_xf, id_z2_xs   ) + E_xfxf(:)*E_xs';      %this is E[kron(xf,xf)*xs']
        E_xf_xfxf_xf = Var_z(id_z3_xf_xf, id_z3_xf_xf) + E_xfxf(:)*E_xfxf(:)'; %this is E[kron(xf,xf)*kron(xf,xf)']
        E_xrdxf = reshape(invIxx_hx_hx*vec(...
                                             hxx*reshape( commutation(x_nbr^2,x_nbr,1)*E_xf_xfxs(:), x_nbr^2,x_nbr)*hx'...
                                           + hxu*kron(E_xs,E_uu)*hu'...
                                           + 1/6*hxxx*reshape(E_xf_xfxf_xf,x_nbr^3,x_nbr)*hx'...
                                           + 1/6*huuu*reshape(QPu*E_u_u_u_u,u_nbr^3,u_nbr)*hu'...
                                           + 3/6*hxxu*kron(E_xfxf(:),E_uu)*hu'...
                                           + 3/6*hxuu*kron(E_xfxf,E_uu(:))*hx'...
                                           + 3/6*hxss*E_xfxf*hx'...
                                           + 3/6*huss*E_uu*hu'...
                                           ),...
                         x_nbr,x_nbr); %this is E[xrd*xf']
        if compute_derivs
            dE_xsxs       = zeros(x_nbr,x_nbr,totparam_nbr);
            dE_xsxf_xf    = zeros(x_nbr,x_nbr^2,totparam_nbr);
            dE_xf_xfxs    = zeros(x_nbr^2,x_nbr,totparam_nbr);
            dE_xf_xfxf_xf = zeros(x_nbr^2,x_nbr^2,totparam_nbr);
            dE_xrdxf      = zeros(x_nbr,x_nbr,totparam_nbr);
            for jp2 = 1:totparam_nbr
                if jp2 < (stderrparam_nbr+corrparam_nbr)
                    dE_u_u_u_u_jp2 = QPu*dE_u_u_u_u(:,jp2);
                else
                    dE_u_u_u_u_jp2 = zeros(u_nbr^4,1);
                end
                dE_xsxs(:,:,jp2)       = dVar_z(id_z2_xs    , id_z2_xs    , jp2) + dE_xs(:,jp2)*transpose(E_xs) + E_xs*transpose(dE_xs(:,jp2));
                dE_xsxf_xf(:,:,jp2)    = dVar_z(id_z2_xs    , id_z3_xf_xf , jp2) + dE_xs(:,jp2)*E_xfxf(:)' + E_xs*vec(dE_xfxf(:,:,jp2))';
                dE_xf_xfxs(:,:,jp2)    = dVar_z(id_z3_xf_xf , id_z2_xs    , jp2) + vec(dE_xfxf(:,:,jp2))*E_xs' + E_xfxf(:)*dE_xs(:,jp2)';
                dE_xf_xfxf_xf(:,:,jp2) = dVar_z(id_z3_xf_xf , id_z3_xf_xf , jp2) + vec(dE_xfxf(:,:,jp2))*E_xfxf(:)' + E_xfxf(:)*vec(dE_xfxf(:,:,jp2))';
                dE_xrdxf(:,:,jp2) = reshape(invIxx_hx_hx*vec(...
                    dhx(:,:,jp2)*E_xrdxf*hx' + hx*E_xrdxf*dhx(:,:,jp2)'...
                  + dhxx(:,:,jp2)*reshape( commutation(x_nbr^2,x_nbr,1)*E_xf_xfxs(:), x_nbr^2,x_nbr)*hx' + hxx*reshape( commutation(x_nbr^2,x_nbr,1)*vec(dE_xf_xfxs(:,:,jp2)), x_nbr^2,x_nbr)*hx' + hxx*reshape( commutation(x_nbr^2,x_nbr,1)*E_xf_xfxs(:), x_nbr^2,x_nbr)*dhx(:,:,jp2)'...
                  + dhxu(:,:,jp2)*kron(E_xs,E_uu)*hu' + hxu*kron(dE_xs(:,jp2),E_uu)*hu' + hxu*kron(E_xs,dE_uu(:,:,jp2))*hu' + hxu*kron(E_xs,E_uu)*dhu(:,:,jp2)'...
                  + 1/6*dhxxx(:,:,jp2)*reshape(E_xf_xfxf_xf,x_nbr^3,x_nbr)*hx' + 1/6*hxxx*reshape(dE_xf_xfxf_xf(:,:,jp2),x_nbr^3,x_nbr)*hx' + 1/6*hxxx*reshape(E_xf_xfxf_xf,x_nbr^3,x_nbr)*dhx(:,:,jp2)'...
                  + 1/6*dhuuu(:,:,jp2)*reshape(QPu*E_u_u_u_u,u_nbr^3,u_nbr)*hu' + 1/6*huuu*reshape(dE_u_u_u_u_jp2,u_nbr^3,u_nbr)*hu' + 1/6*huuu*reshape(QPu*E_u_u_u_u,u_nbr^3,u_nbr)*dhu(:,:,jp2)'...
                  + 3/6*dhxxu(:,:,jp2)*kron(E_xfxf(:),E_uu)*hu' + 3/6*hxxu*kron(vec(dE_xfxf(:,:,jp2)),E_uu)*hu' + 3/6*hxxu*kron(E_xfxf(:),dE_uu(:,:,jp2))*hu' + 3/6*hxxu*kron(E_xfxf(:),E_uu)*dhu(:,:,jp2)'...
                  + 3/6*dhxuu(:,:,jp2)*kron(E_xfxf,E_uu(:))*hx' + 3/6*hxuu*kron(dE_xfxf(:,:,jp2),E_uu(:))*hx' + 3/6*hxuu*kron(E_xfxf,vec(dE_uu(:,:,jp2)))*hx' + 3/6*hxuu*kron(E_xfxf,E_uu(:))*dhx(:,:,jp2)'...
                  + 3/6*dhxss(:,:,jp2)*E_xfxf*hx' + 3/6*hxss*dE_xfxf(:,:,jp2)*hx' + 3/6*hxss*E_xfxf*dhx(:,:,jp2)'...
                  + 3/6*dhuss(:,:,jp2)*E_uu*hu' + 3/6*huss*dE_uu(:,:,jp2)*hu' + 3/6*huss*E_uu*dhu(:,:,jp2)'...
                  ), x_nbr, x_nbr);
            end
        end

        % Compute unique sixth-order product moments of u, i.e. unique(E[kron(kron(kron(kron(kron(u,u),u),u),u),u)],'stable')
668
669
670
671
672
        u_nbr6        = u_nbr*(u_nbr+1)/2*(u_nbr+2)/3*(u_nbr+3)/4*(u_nbr+4)/5*(u_nbr+5)/6;       
        if isempty(Q6Pu)
            Q6Pu          = Q6_plication(u_nbr);
            COMBOS6       = flipud(allVL1(u_nbr, 6)); %all possible (unique) combinations of powers that sum up to six
        end
673
        E_u_u_u_u_u_u = zeros(u_nbr6,1); %only unique entries
674
        if compute_derivs && (stderrparam_nbr+corrparam_nbr>0)
675
            dE_u_u_u_u_u_u = zeros(u_nbr6,stderrparam_nbr+corrparam_nbr);
676
        end
677
        for j6 = 1:size(COMBOS6,1)
678
            if compute_derivs && (stderrparam_nbr+corrparam_nbr>0)
679
680
681
                [E_u_u_u_u_u_u(j6), dE_u_u_u_u_u_u(j6,:)] = prodmom_deriv(E_uu, 1:u_nbr, COMBOS6(j6,:), dE_uu(:,:,1:(stderrparam_nbr+corrparam_nbr)), dr.derivs.dCorrelation_matrix(:,:,1:(stderrparam_nbr+corrparam_nbr)));
            else
                E_u_u_u_u_u_u(j6) = prodmom(E_uu, 1:u_nbr, COMBOS6(j6,:));
682
683
684
            end
        end

685
686
687
688
689
%% Third-order pruned state space system
% Auxiliary state vector z is defined by:          z    = [xf; xs; kron(xf,xf); xrd; kron(xf,xs); kron(kron(xf,xf),xf)]
% Auxiliary innovations vector inov is defined by: inov = [u; kron(u,u)-E_uu(:); kron(xf,u); kron(xs,u); kron(kron(xf,xf),u); kron(kron(xf,u),u); kron(kron(u,u),u))]
        z_nbr    = x_nbr + x_nbr + x_nbr^2 + x_nbr + x_nbr^2 + x_nbr^3;
        inov_nbr = u_nbr + u_nbr^2 + x_nbr*u_nbr + x_nbr*u_nbr + x_nbr^2*u_nbr + x_nbr*u_nbr^2 + u_nbr^3;
690
691

        A = zeros(z_nbr,z_nbr);
692
693
694
        A(id_z1_xf       , id_z1_xf      ) = hx;
        A(id_z2_xs       , id_z2_xs      ) = hx;
        A(id_z2_xs       , id_z3_xf_xf   ) = 1/2*hxx;
695
        A(id_z3_xf_xf    , id_z3_xf_xf   ) = hx_hx;
696
697
698
699
700
        A(id_z4_xrd      , id_z1_xf      ) = 3/6*hxss;
        A(id_z4_xrd      , id_z4_xrd     ) = hx;
        A(id_z4_xrd      , id_z5_xf_xs   ) = hxx;
        A(id_z4_xrd      , id_z6_xf_xf_xf) = 1/6*hxxx;
        A(id_z5_xf_xs    , id_z1_xf      ) = hx_hss2;
701
        A(id_z5_xf_xs    , id_z5_xf_xs   ) = hx_hx;
702
703
        A(id_z5_xf_xs    , id_z6_xf_xf_xf) = hx_hxx2;
        A(id_z6_xf_xf_xf , id_z6_xf_xf_xf) = hx_hx_hx;
704

705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
        B = zeros(z_nbr,inov_nbr);
        B(id_z1_xf       , id_inov1_u      ) = hu;
        B(id_z2_xs       , id_inov2_u_u    ) = 1/2*huu;
        B(id_z2_xs       , id_inov3_xf_u   ) = hxu;
        B(id_z3_xf_xf    , id_inov2_u_u    ) = hu_hu;
        B(id_z3_xf_xf    , id_inov3_xf_u   ) = (I_xx+K_x_x)*hx_hu;
        B(id_z4_xrd      , id_inov1_u      ) = 3/6*huss;
        B(id_z4_xrd      , id_inov4_xs_u   ) = hxu;
        B(id_z4_xrd      , id_inov5_xf_xf_u) = 3/6*hxxu;
        B(id_z4_xrd      , id_inov6_xf_u_u ) = 3/6*hxuu;
        B(id_z4_xrd      , id_inov7_u_u_u  ) = 1/6*huuu;
        B(id_z5_xf_xs    , id_inov1_u      ) = hu_hss2;
        B(id_z5_xf_xs    , id_inov4_xs_u   ) = K_x_x*hx_hu;
        B(id_z5_xf_xs    , id_inov5_xf_xf_u) = hx_hxu + K_x_x*hxx2_hu;
        B(id_z5_xf_xs    , id_inov6_xf_u_u ) = hx_huu2 + K_x_x*hxu_hu;
        B(id_z5_xf_xs    , id_inov7_u_u_u  ) = hu_huu2;
        B(id_z6_xf_xf_xf , id_inov5_xf_xf_u) = hx_hx_hu + kron(hx,K_x_x*hx_hu) + hu_hx_hx*K_u_xx;
        B(id_z6_xf_xf_xf , id_inov6_xf_u_u ) = hx_hu_hu + hu_hx_hu*K_u_ux + kron(hu,K_x_x*hx_hu)*K_u_ux;
        B(id_z6_xf_xf_xf , id_inov7_u_u_u  ) = hu_hu_hu;

        c = zeros(z_nbr, 1);
        c(id_z2_xs    , 1) = 1/2*hss + 1/2*huu*E_uu(:);
727
728
        c(id_z3_xf_xf , 1) = hu_hu*E_uu(:);

729
730
731
732
733
734
735
        C = zeros(y_nbr,z_nbr);
        C(: , id_z1_xf      ) = gx + 3/6*gxss;
        C(: , id_z2_xs      ) = gx;
        C(: , id_z3_xf_xf   ) = 1/2*gxx;
        C(: , id_z4_xrd     ) = gx;
        C(: , id_z5_xf_xs   ) = gxx;
        C(: , id_z6_xf_xf_xf) = 1/6*gxxx;
736

737
738
739
740
741
742
743
744
        D = zeros(y_nbr,inov_nbr);
        D(: , id_inov1_u      ) = gu + 3/6*guss;
        D(: , id_inov2_u_u    ) = 1/2*guu;
        D(: , id_inov3_xf_u   ) = gxu;
        D(: , id_inov4_xs_u   ) = gxu;
        D(: , id_inov5_xf_xf_u) = 3/6*gxxu;
        D(: , id_inov6_xf_u_u)  = 3/6*gxuu;
        D(: , id_inov7_u_u_u )  = 1/6*guuu;
745

746
        d = 1/2*gss + 1/2*guu*E_uu(:);
747

748
749
750
751
752
753
754
755
        Varinov = zeros(inov_nbr,inov_nbr);
        Varinov(id_inov1_u       , id_inov1_u      ) = E_uu;
       %Varinov(id_inov1_u       , id_inov2_u_u    ) = zeros(u_nbr,u_nbr^2);
       %Varinov(id_inov1_u       , id_inov3_xf_u   ) = zeros(u_nbr,x_nbr*u_nbr);
        Varinov(id_inov1_u       , id_inov4_xs_u   ) = kron(E_xs',E_uu);
        Varinov(id_inov1_u       , id_inov5_xf_xf_u) = kron(E_xfxf(:)',E_uu);
       %Varinov(id_inov1_u       , id_inov6_xf_u_u ) = zeros(u_nbr,x_nbr*u_nbr^2);
        Varinov(id_inov1_u       , id_inov7_u_u_u  ) = reshape(QPu*E_u_u_u_u,u_nbr,u_nbr^3);
756

757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
       %Varinov(id_inov2_u_u     , id_inov1_u      ) = zeros(u_nbr^2,u_nbr);
        Varinov(id_inov2_u_u     , id_inov2_u_u    ) = reshape(QPu*E_u_u_u_u,u_nbr^2,u_nbr^2)-E_uu(:)*E_uu(:)';
       %Varinov(id_inov2_u_u     , id_inov3_xf_u   ) = zeros(u_nbr^2,x_nbr*u_nbr);
       %Varinov(id_inov2_u_u     , id_inov4_xs_u   ) = zeros(u_nbr^2,x_nbr*u_nbr);
       %Varinov(id_inov2_u_u     , id_inov5_xf_xf_u) = zeros(u_nbr^2,x_nbr^2,u_nbr);
       %Varinov(id_inov2_u_u     , id_inov6_xf_u_u ) = zeros(u_nbr^2,x_nbr*u_nbr^2);
       %Varinov(id_inov2_u_u     , id_inov7_u_u_u  ) = zeros(u_nbr^2,u_nbr^3);

       %Varinov(id_inov3_xf_u    , id_inov1_u      ) = zeros(x_nbr*u_nbr,u_nbr);
       %Varinov(id_inov3_xf_u    , id_inov2_u_u    ) = zeros(x_nbr*u_nbr,u_nbr^2);
        Varinov(id_inov3_xf_u    , id_inov3_xf_u   ) = E_xfxf_uu;
       %Varinov(id_inov3_xf_u    , id_inov4_xs_u   ) = zeros(x_nbr*u_nbr,x_nbr*u_nbr);
       %Varinov(id_inov3_xf_u    , id_inov5_xf_xf_u) = zeros(x_nbr*u_nbr,x_nbr^2*u_nbr);
       %Varinov(id_inov3_xf_u    , id_inov6_xf_u_u ) = zeros(x_nbr*u_nbr,x_nbr*u_nbr^2);
       %Varinov(id_inov3_xf_u    , id_inov7_u_u_u   ) = zeros(x_nbr*u_nbr,u_nbr^3);

        Varinov(id_inov4_xs_u    , id_inov1_u      ) = kron(E_xs,E_uu);
       %Varinov(id_inov4_xs_u    , id_inov2_u_u    ) = zeros(x_nbr*u_nbr,u_nbr^2);
       %Varinov(id_inov4_xs_u    , id_inov3_xf_u   ) = zeros(x_nbr*u_nbr,x_nbr*u_nbr);
        Varinov(id_inov4_xs_u    , id_inov4_xs_u   ) = kron(E_xsxs,E_uu);
        Varinov(id_inov4_xs_u    , id_inov5_xf_xf_u) = kron(E_xsxf_xf, E_uu);
       %Varinov(id_inov4_xs_u    , id_inov6_xf_u_u ) = zeros(x_nbr*u_nbr,x_nbr*u_nbr^2);
        Varinov(id_inov4_xs_u    , id_inov7_u_u_u  ) = kron(E_xs,reshape(QPu*E_u_u_u_u,u_nbr,u_nbr^3));

        Varinov(id_inov5_xf_xf_u , id_inov1_u      ) = kron(E_xfxf(:),E_uu);
       %Varinov(id_inov5_xf_xf_u , id_inov2_u_u    ) = zeros(x_nbr^2*u_nbr,u_nbr^2);
       %Varinov(id_inov5_xf_xf_u , id_inov3_xf_u   ) = zeros(x_nbr^2*u_nbr,x_nbr*u_nbr);
        Varinov(id_inov5_xf_xf_u , id_inov4_xs_u   ) = kron(E_xf_xfxs,E_uu);
        Varinov(id_inov5_xf_xf_u , id_inov5_xf_xf_u) = kron(E_xf_xfxf_xf,E_uu);
       %Varinov(id_inov5_xf_xf_u , id_inov6_xf_u_u ) = zeros(x_nbr^2*u_nbr,x_nbr*u_nbr^2);
        Varinov(id_inov5_xf_xf_u , id_inov7_u_u_u  ) = kron(E_xfxf(:),reshape(QPu*E_u_u_u_u,u_nbr,u_nbr^3));

       %Varinov(id_inov6_xf_u_u  , id_inov1_u      ) = zeros(x_nbr*u_nbr^2,u_nbr);
       %Varinov(id_inov6_xf_u_u  , id_inov2_u_u    ) = zeros(x_nbr*u_nbr^2,u_nbr^2);
       %Varinov(id_inov6_xf_u_u  , id_inov3_xf_u   ) = zeros(x_nbr*u_nbr^2,x_nbr*u_nbr);
       %Varinov(id_inov6_xf_u_u  , id_inov4_xs_u   ) = zeros(x_nbr*u_nbr^2,x_nbr*u_nbr);
       %Varinov(id_inov6_xf_u_u  , id_inov5_xf_xf_u) = zeros(x_nbr*u_nbr^2,x_nbr^2*u_nbr);
        Varinov(id_inov6_xf_u_u  , id_inov6_xf_u_u ) = kron(E_xfxf,reshape(QPu*E_u_u_u_u,u_nbr^2,u_nbr^2));
       %Varinov(id_inov6_xf_u_u  , id_inov7_u_u_u  ) = zeros(x_nbr*u_nbr^2,u_nbr^3);

        Varinov(id_inov7_u_u_u   , id_inov1_u      ) = reshape(QPu*E_u_u_u_u,u_nbr^3,u_nbr);
       %Varinov(id_inov7_u_u_u   , id_inov2_u_u    ) = zeros(u_nbr^3,u_nbr^2);
       %Varinov(id_inov7_u_u_u   , id_inov3_xf_u   ) = zeros(u_nbr^3,x_nbr*u_nbr);
        Varinov(id_inov7_u_u_u   , id_inov4_xs_u   ) = kron(E_xs',reshape(QPu*E_u_u_u_u,u_nbr^3,u_nbr));
        Varinov(id_inov7_u_u_u   , id_inov5_xf_xf_u) = kron(transpose(E_xfxf(:)),reshape(QPu*E_u_u_u_u,u_nbr^3,u_nbr));
       %Varinov(id_inov7_u_u_u   , id_inov6_xf_u_u ) = zeros(u_nbr^3,x_nbr*u_nbr^2);
        Varinov(id_inov7_u_u_u   , id_inov7_u_u_u  ) = reshape(Q6Pu*E_u_u_u_u_u_u,u_nbr^3,u_nbr^3);

        E_xrd = zeros(x_nbr,1);%due to gaussianity

        E_inovzlag1 = zeros(inov_nbr,z_nbr); % Attention: E[inov*z(-1)'] is not equal to zero for a third-order approximation due to kron(kron(xf(-1),u),u)
        E_inovzlag1(id_inov6_xf_u_u , id_z1_xf       ) = kron(E_xfxf,E_uu(:));
        E_inovzlag1(id_inov6_xf_u_u , id_z4_xrd      ) = kron(E_xrdxf',E_uu(:));
810
        E_inovzlag1(id_inov6_xf_u_u , id_z5_xf_xs    ) = kron(reshape(K_xx_x*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(E_uu)) ;
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
        E_inovzlag1(id_inov6_xf_u_u , id_z6_xf_xf_xf ) = kron(reshape(E_xf_xfxf_xf,x_nbr,x_nbr^3),E_uu(:));

        Binovzlag1A= B*E_inovzlag1*transpose(A);
        Om_z = B*Varinov*transpose(B) + Binovzlag1A + transpose(Binovzlag1A);

        lyapunov_symm_method = 1; %method=1 to initialize persistent variables
        [Var_z, errorflag] = disclyap_fast(A,Om_z,options.lyapunov_doubling_tol);        
        if errorflag %use Schur-based method
            fprintf('PRUNED_STATE_SPACE_SYSTEM: error flag in disclyap_fast at order=3, use lyapunov_symm\n');
            Var_z = lyapunov_symm(A,Om_z,...
                                    options.lyapunov_fixed_point_tol,options.qz_criterium,options.lyapunov_complex_threshold,...
                                    lyapunov_symm_method,...
                                    options.debug);
            lyapunov_symm_method = 2; %we can now make use of persistent variables from shur
        end
        %make sure some stuff is zero due to Gaussianity
        Var_z(id_z1_xf       , id_z2_xs)       = zeros(x_nbr,x_nbr);
        Var_z(id_z1_xf       , id_z3_xf_xf)    = zeros(x_nbr,x_nbr^2);
        Var_z(id_z2_xs       , id_z1_xf)       = zeros(x_nbr,x_nbr);
        Var_z(id_z2_xs       , id_z4_xrd)      = zeros(x_nbr,x_nbr);
        Var_z(id_z2_xs       , id_z5_xf_xs)    = zeros(x_nbr,x_nbr^2);
        Var_z(id_z2_xs       , id_z6_xf_xf_xf) = zeros(x_nbr,x_nbr^3);
        Var_z(id_z3_xf_xf    , id_z1_xf)       = zeros(x_nbr^2,x_nbr);
        Var_z(id_z3_xf_xf    , id_z4_xrd)      = zeros(x_nbr^2,x_nbr);
        Var_z(id_z3_xf_xf    , id_z5_xf_xs)    = zeros(x_nbr^2,x_nbr^2);
        Var_z(id_z3_xf_xf    , id_z6_xf_xf_xf) = zeros(x_nbr^2,x_nbr^3);
        Var_z(id_z4_xrd      , id_z2_xs)       = zeros(x_nbr,x_nbr);
        Var_z(id_z4_xrd      , id_z3_xf_xf)    = zeros(x_nbr,x_nbr^2);
        Var_z(id_z5_xf_xs    , id_z2_xs)       = zeros(x_nbr^2,x_nbr);
        Var_z(id_z5_xf_xs    , id_z3_xf_xf)    = zeros(x_nbr^2,x_nbr^2);
        Var_z(id_z6_xf_xf_xf , id_z2_xs)       = zeros(x_nbr^3,x_nbr);
        Var_z(id_z6_xf_xf_xf , id_z3_xf_xf)    = zeros(x_nbr^3,x_nbr^2);
        
        if compute_derivs
            dA           = zeros(z_nbr,z_nbr,totparam_nbr);
            dB           = zeros(z_nbr,inov_nbr,totparam_nbr);
            dc           = zeros(z_nbr,totparam_nbr);
            dC           = zeros(y_nbr,z_nbr,totparam_nbr);
            dD           = zeros(y_nbr,inov_nbr,totparam_nbr);
            dd           = zeros(y_nbr,totparam_nbr);
            dVarinov     = zeros(inov_nbr,inov_nbr,totparam_nbr);
            dE_xrd       = zeros(x_nbr,totparam_nbr);
            dE_inovzlag1 = zeros(inov_nbr,z_nbr,totparam_nbr);
            dVar_z       = zeros(z_nbr,z_nbr,totparam_nbr);
            
            for jp3 = 1:totparam_nbr
                if jp3 <= (stderrparam_nbr+corrparam_nbr)
                    dE_uu_jp3          = dE_uu(:,:,jp3);
                    dE_u_u_u_u_jp3     = QPu*dE_u_u_u_u(:,jp3);
                    dE_u_u_u_u_u_u_jp3 = Q6Pu*dE_u_u_u_u_u_u(:,jp3);
861
                else
862
863
864
                    dE_uu_jp3          = zeros(u_nbr,u_nbr);
                    dE_u_u_u_u_jp3     = zeros(u_nbr^4,1);
                    dE_u_u_u_u_u_u_jp3 = zeros(u_nbr^6,1);
865
                end
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
                dhx_jp3       = dhx(:,:,jp3);
                dhu_jp3       = dhu(:,:,jp3);
                dhxx_jp3      = dhxx(:,:,jp3);
                dhxu_jp3      = dhxu(:,:,jp3);
                dhuu_jp3      = dhuu(:,:,jp3);
                dhss_jp3      = dhss(:,jp3);
                dhxxx_jp3     = dhxxx(:,:,jp3);
                dhxxu_jp3     = dhxxu(:,:,jp3);
                dhxuu_jp3     = dhxuu(:,:,jp3);
                dhuuu_jp3     = dhuuu(:,:,jp3);
                dhxss_jp3     = dhxss(:,:,jp3);
                dhuss_jp3     = dhuss(:,:,jp3);
                dgx_jp3       = dgx(:,:,jp3);
                dgu_jp3       = dgu(:,:,jp3);
                dgxx_jp3      = dgxx(:,:,jp3);
                dgxu_jp3      = dgxu(:,:,jp3);
                dguu_jp3      = dguu(:,:,jp3);
                dgss_jp3      = dgss(:,jp3);
                dgxxx_jp3     = dgxxx(:,:,jp3);
                dgxxu_jp3     = dgxxu(:,:,jp3);
                dgxuu_jp3     = dgxuu(:,:,jp3);
                dguuu_jp3     = dguuu(:,:,jp3);
                dgxss_jp3     = dgxss(:,:,jp3);
                dguss_jp3     = dguss(:,:,jp3);
890
                
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
                dhx_hx_jp3    = kron(dhx_jp3,hx) + kron(hx,dhx_jp3);
                dhx_hu_jp3    = kron(dhx_jp3,hu) + kron(hx,dhu_jp3);                
                dhu_hu_jp3    = kron(dhu_jp3,hu) + kron(hu,dhu_jp3);
                dhx_hss2_jp3  = kron(dhx_jp3,1/2*hss) + kron(hx,1/2*dhss_jp3);
                dhu_hss2_jp3  = kron(dhu_jp3,1/2*hss) + kron(hu,1/2*dhss_jp3);
                dhx_hxx2_jp3  = kron(dhx_jp3,1/2*hxx) + kron(hx,1/2*dhxx_jp3);
                dhxx2_hu_jp3  = kron(1/2*dhxx_jp3,hu) + kron(1/2*hxx,dhu_jp3);
                dhx_hxu_jp3   = kron(dhx_jp3,hxu) + kron(hx,dhxu_jp3);
                dhxu_hu_jp3   = kron(dhxu_jp3,hu) + kron(hxu,dhu_jp3);
                dhx_huu2_jp3  = kron(dhx_jp3,1/2*huu) + kron(hx,1/2*dhuu_jp3);
                dhu_huu2_jp3  = kron(dhu_jp3,1/2*huu) + kron(hu,1/2*dhuu_jp3);
                dhx_hx_hx_jp3 = kron(dhx_jp3,hx_hx) + kron(hx,dhx_hx_jp3);
                dhx_hx_hu_jp3 = kron(dhx_hx_jp3,hu) + kron(hx_hx,dhu_jp3);
                dhu_hx_hx_jp3 = kron(dhu_jp3,hx_hx) + kron(hu,dhx_hx_jp3);
                dhu_hu_hu_jp3 = kron(dhu_hu_jp3,hu) + kron(hu_hu,dhu_jp3);
                dhx_hu_hu_jp3 = kron(dhx_jp3,hu_hu) + kron(hx,dhu_hu_jp3);
                dhu_hx_hu_jp3 = kron(dhu_jp3,hx_hu) + kron(hu,dhx_hu_jp3);

                dE_xs_jp3         = dE_xs(:,jp3);
                dE_xfxf_jp3       = dE_xfxf(:,:,jp3);
                dE_xsxs_jp3       = dE_xsxs(:,:,jp3);
                dE_xsxf_xf_jp3    = dE_xsxf_xf(:,:,jp3);
                dE_xfxf_uu_jp3    = kron(dE_xfxf_jp3,E_uu) + kron(E_xfxf,dE_uu_jp3);
                dE_xf_xfxs_jp3    = dE_xf_xfxs(:,:,jp3);
                dE_xf_xfxf_xf_jp3 = dE_xf_xfxf_xf(:,:,jp3);
                dE_xrdxf_jp3      = dE_xrdxf(:,:,jp3);
917
                
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
                dA(id_z1_xf       , id_z1_xf       , jp3) = dhx_jp3;
                dA(id_z2_xs       , id_z2_xs       , jp3) = dhx_jp3;
                dA(id_z2_xs       , id_z3_xf_xf    , jp3) = 1/2*dhxx_jp3;
                dA(id_z3_xf_xf    , id_z3_xf_xf    , jp3) = dhx_hx_jp3;
                dA(id_z4_xrd      , id_z1_xf       , jp3) = 3/6*dhxss_jp3;
                dA(id_z4_xrd      , id_z4_xrd      , jp3) = dhx_jp3;
                dA(id_z4_xrd      , id_z5_xf_xs    , jp3) = dhxx_jp3;
                dA(id_z4_xrd      , id_z6_xf_xf_xf , jp3) = 1/6*dhxxx_jp3;
                dA(id_z5_xf_xs    , id_z1_xf       , jp3) = dhx_hss2_jp3;
                dA(id_z5_xf_xs    , id_z5_xf_xs    , jp3) = dhx_hx_jp3;
                dA(id_z5_xf_xs    , id_z6_xf_xf_xf , jp3) = dhx_hxx2_jp3;
                dA(id_z6_xf_xf_xf , id_z6_xf_xf_xf , jp3) = dhx_hx_hx_jp3;

                dB(id_z1_xf       , id_inov1_u       , jp3) = dhu_jp3;
                dB(id_z2_xs       , id_inov2_u_u     , jp3) = 1/2*dhuu_jp3;
                dB(id_z2_xs       , id_inov3_xf_u    , jp3) = dhxu_jp3;
                dB(id_z3_xf_xf    , id_inov2_u_u     , jp3) = dhu_hu_jp3;
                dB(id_z3_xf_xf    , id_inov3_xf_u    , jp3) = (I_xx+K_x_x)*dhx_hu_jp3;
                dB(id_z4_xrd      , id_inov1_u       , jp3) = 3/6*dhuss_jp3;
                dB(id_z4_xrd      , id_inov4_xs_u    , jp3) = dhxu_jp3;
                dB(id_z4_xrd      , id_inov5_xf_xf_u , jp3) = 3/6*dhxxu_jp3;
                dB(id_z4_xrd      , id_inov6_xf_u_u  , jp3) = 3/6*dhxuu_jp3;
                dB(id_z4_xrd      , id_inov7_u_u_u   , jp3) = 1/6*dhuuu_jp3;
                dB(id_z5_xf_xs    , id_inov1_u       , jp3) = dhu_hss2_jp3;
                dB(id_z5_xf_xs    , id_inov4_xs_u    , jp3) = K_x_x*dhx_hu_jp3;
                dB(id_z5_xf_xs    , id_inov5_xf_xf_u , jp3) = dhx_hxu_jp3 + K_x_x*dhxx2_hu_jp3;
                dB(id_z5_xf_xs    , id_inov6_xf_u_u  , jp3) = dhx_huu2_jp3 + K_x_x*dhxu_hu_jp3;
                dB(id_z5_xf_xs    , id_inov7_u_u_u   , jp3) = dhu_huu2_jp3;
                dB(id_z6_xf_xf_xf , id_inov5_xf_xf_u , jp3) = dhx_hx_hu_jp3 + kron(dhx_jp3,K_x_x*hx_hu) + kron(hx,K_x_x*dhx_hu_jp3) + dhu_hx_hx_jp3*K_u_xx;
                dB(id_z6_xf_xf_xf , id_inov6_xf_u_u  , jp3) = dhx_hu_hu_jp3 + dhu_hx_hu_jp3*K_u_ux + kron(dhu_jp3,K_x_x*hx_hu)*K_u_ux + kron(hu,K_x_x*dhx_hu_jp3)*K_u_ux;
                dB(id_z6_xf_xf_xf , id_inov7_u_u_u   , jp3) = dhu_hu_hu_jp3;

                dc(id_z2_xs    , jp3) = 1/2*dhss_jp3 + 1/2*dhuu_jp3*E_uu(:) + 1/2*huu*dE_uu_jp3(:);
                dc(id_z3_xf_xf , jp3) = dhu_hu_jp3*E_uu(:) + hu_hu*dE_uu_jp3(:);

                dC(: , id_z1_xf       , jp3) = dgx_jp3 + 3/6*dgxss_jp3;
                dC(: , id_z2_xs       , jp3) = dgx_jp3;
                dC(: , id_z3_xf_xf    , jp3) = 1/2*dgxx_jp3;
                dC(: , id_z4_xrd      , jp3) = dgx_jp3;
                dC(: , id_z5_xf_xs    , jp3) = dgxx_jp3;
                dC(: , id_z6_xf_xf_xf , jp3) = 1/6*dgxxx_jp3;

                dD(: , id_inov1_u       , jp3) = dgu_jp3 + 3/6*dguss_jp3;
                dD(: , id_inov2_u_u     , jp3) = 1/2*dguu_jp3;
                dD(: , id_inov3_xf_u    , jp3) = dgxu_jp3;
                dD(: , id_inov4_xs_u    , jp3) = dgxu_jp3;
                dD(: , id_inov5_xf_xf_u , jp3) = 3/6*dgxxu_jp3;
                dD(: , id_inov6_xf_u_u  , jp3) = 3/6*dgxuu_jp3;
                dD(: , id_inov7_u_u_u   , jp3) = 1/6*dguuu_jp3;

                dd(:,jp3) = 1/2*dgss_jp3 + 1/2*dguu_jp3*E_uu(:) + 1/2*guu*dE_uu_jp3(:);

                dVarinov(id_inov1_u       , id_inov1_u       , jp3) = dE_uu_jp3;
                dVarinov(id_inov1_u       , id_inov4_xs_u    , jp3) = kron(dE_xs_jp3',E_uu) + kron(E_xs',dE_uu_jp3);
                dVarinov(id_inov1_u       , id_inov5_xf_xf_u , jp3) = kron(dE_xfxf_jp3(:)',E_uu) + kron(E_xfxf(:)',dE_uu_jp3);
                dVarinov(id_inov1_u       , id_inov7_u_u_u   , jp3) = reshape(dE_u_u_u_u_jp3,u_nbr,u_nbr^3);
                dVarinov(id_inov2_u_u     , id_inov2_u_u     , jp3) = reshape(dE_u_u_u_u_jp3,u_nbr^2,u_nbr^2) - dE_uu_jp3(:)*E_uu(:)' - E_uu(:)*dE_uu_jp3(:)';
                dVarinov(id_inov3_xf_u    , id_inov3_xf_u    , jp3) = dE_xfxf_uu_jp3;
                dVarinov(id_inov4_xs_u    , id_inov1_u       , jp3) = kron(dE_xs_jp3,E_uu) + kron(E_xs,dE_uu_jp3);
                dVarinov(id_inov4_xs_u    , id_inov4_xs_u    , jp3) = kron(dE_xsxs_jp3,E_uu) + kron(E_xsxs,dE_uu_jp3);
                dVarinov(id_inov4_xs_u    , id_inov5_xf_xf_u , jp3) = kron(dE_xsxf_xf_jp3, E_uu) + kron(E_xsxf_xf, dE_uu_jp3);
                dVarinov(id_inov4_xs_u    , id_inov7_u_u_u   , jp3) = kron(dE_xs_jp3,reshape(QPu*E_u_u_u_u,u_nbr,u_nbr^3)) + kron(E_xs,reshape(dE_u_u_u_u_jp3,u_nbr,u_nbr^3));
                dVarinov(id_inov5_xf_xf_u , id_inov1_u       , jp3) = kron(dE_xfxf_jp3(:),E_uu) + kron(E_xfxf(:),dE_uu_jp3);
                dVarinov(id_inov5_xf_xf_u , id_inov4_xs_u    , jp3) = kron(dE_xf_xfxs_jp3,E_uu) + kron(E_xf_xfxs,dE_uu_jp3);
                dVarinov(id_inov5_xf_xf_u , id_inov5_xf_xf_u , jp3) = kron(dE_xf_xfxf_xf_jp3,E_uu) + kron(E_xf_xfxf_xf,dE_uu_jp3);
                dVarinov(id_inov5_xf_xf_u , id_inov7_u_u_u   , jp3) = kron(dE_xfxf_jp3(:),reshape(QPu*E_u_u_u_u,u_nbr,u_nbr^3)) + kron(E_xfxf(:),reshape(dE_u_u_u_u_jp3,u_nbr,u_nbr^3));
                dVarinov(id_inov6_xf_u_u  , id_inov6_xf_u_u  , jp3) = kron(dE_xfxf_jp3,reshape(QPu*E_u_u_u_u,u_nbr^2,u_nbr^2)) + kron(E_xfxf,reshape(dE_u_u_u_u_jp3,u_nbr^2,u_nbr^2));
                dVarinov(id_inov7_u_u_u   , id_inov1_u       , jp3) = reshape(dE_u_u_u_u_jp3,u_nbr^3,u_nbr);
                dVarinov(id_inov7_u_u_u   , id_inov4_xs_u    , jp3) = kron(dE_xs_jp3',reshape(QPu*E_u_u_u_u,u_nbr^3,u_nbr)) + kron(E_xs',reshape(dE_u_u_u_u_jp3,u_nbr^3,u_nbr));
                dVarinov(id_inov7_u_u_u   , id_inov5_xf_xf_u , jp3) = kron(transpose(dE_xfxf_jp3(:)),reshape(QPu*E_u_u_u_u,u_nbr^3,u_nbr)) + kron(transpose(E_xfxf(:)),reshape(dE_u_u_u_u_jp3,u_nbr^3,u_nbr));
                dVarinov(id_inov7_u_u_u   , id_inov7_u_u_u   , jp3) = reshape(dE_u_u_u_u_u_u_jp3,u_nbr^3,u_nbr^3);

                dE_inovzlag1(id_inov6_xf_u_u , id_z1_xf       , jp3) = kron(dE_xfxf_jp3,E_uu(:)) + kron(E_xfxf,dE_uu_jp3(:));
                dE_inovzlag1(id_inov6_xf_u_u , id_z4_xrd      , jp3) = kron(dE_xrdxf_jp3',E_uu(:)) + kron(E_xrdxf',dE_uu_jp3(:));
992
                dE_inovzlag1(id_inov6_xf_u_u , id_z5_xf_xs    , jp3) = kron(reshape(K_xx_x*vec(dE_xsxf_xf_jp3),x_nbr,x_nbr^2),vec(E_uu)) + kron(reshape(K_xx_x*vec(E_xsxf_xf),x_nbr,x_nbr^2),vec(dE_uu_jp3)) ;
993
994
995
996
                dE_inovzlag1(id_inov6_xf_u_u , id_z6_xf_xf_xf , jp3) = kron(reshape(dE_xf_xfxf_xf_jp3,x_nbr,x_nbr^3),E_uu(:)) + kron(reshape(E_xf_xfxf_xf,x_nbr,x_nbr^3),dE_uu_jp3(:));

                dBinovzlag1A_jp3 = dB(:,:,jp3)*E_inovzlag1*transpose(A) + B*dE_inovzlag1(:,:,jp3)*transpose(A) + B*E_inovzlag1*transpose(dA(:,:,jp3));
                dOm_z_jp3 = dB(:,:,jp3)*Varinov*transpose(B) + B*dVarinov(:,:,jp3)*transpose(B) + B*Varinov*transpose(dB(:,:,jp3)) + dBinovzlag1A_jp3 + transpose(dBinovzlag1A_jp3);
997
                
998
999
1000
                [dVar_z(:,:,jp3), errorflag] = disclyap_fast(A, dA(:,:,jp3)*Var_z*A' + A*Var_z*dA(:,:,jp3)' + dOm_z_jp3, options.lyapunov_doubling_tol);
                if errorflag
                    dVar_z(:,:,jp3) = lyapunov_symm(A, dA(:,:,jp3)*Var_z*A' + A*Var_z*dA(:,:,jp3)' + dOm_z_jp3,...