Verified Commit 5525a7c5 authored by Willi Mutschler's avatar Willi Mutschler
Browse files

🏇 Better minimal state space handling and unit tests

parent 1aa3dda4
......@@ -442,14 +442,16 @@ if ~no_identification_minimal
dMINIMAL = [];
else
% Derive and check minimal state vector of first-order
[CheckCO,minnx,minA,minB,minC,minD,dminA,dminB,dminC,dminD] = get_minimal_state_representation(oo.dr.ghx(oo.dr.pruned.indx,:),... %A
oo.dr.ghu(oo.dr.pruned.indx,:),... %B
oo.dr.ghx(oo.dr.pruned.indy,:),... %C
oo.dr.ghu(oo.dr.pruned.indy,:),... %D
oo.dr.derivs.dghx(oo.dr.pruned.indx,:,:),... %dA
oo.dr.derivs.dghu(oo.dr.pruned.indx,:,:),... %dB
oo.dr.derivs.dghx(oo.dr.pruned.indy,:,:),... %dC
oo.dr.derivs.dghu(oo.dr.pruned.indy,:,:)); %dD
SYS.A = oo.dr.ghx(oo.dr.pruned.indx,:);
SYS.dA = oo.dr.derivs.dghx(oo.dr.pruned.indx,:,:);
SYS.B = oo.dr.ghu(oo.dr.pruned.indx,:);
SYS.dB = oo.dr.derivs.dghu(oo.dr.pruned.indx,:,:);
SYS.C = oo.dr.ghx(oo.dr.pruned.indy,:);
SYS.dC = oo.dr.derivs.dghx(oo.dr.pruned.indy,:,:);
SYS.D = oo.dr.ghu(oo.dr.pruned.indy,:);
SYS.dD = oo.dr.derivs.dghu(oo.dr.pruned.indy,:,:);
[CheckCO,minnx,SYS] = get_minimal_state_representation(SYS,1);
if CheckCO == 0
warning_KomunjerNg = 'WARNING: Komunjer and Ng (2011) failed:\n';
warning_KomunjerNg = [warning_KomunjerNg ' Conditions for minimality are not fullfilled:\n'];
......@@ -457,6 +459,10 @@ if ~no_identification_minimal
fprintf(warning_KomunjerNg); %use sprintf to have line breaks
dMINIMAL = [];
else
minA = SYS.A; dminA = SYS.dA;
minB = SYS.B; dminB = SYS.dB;
minC = SYS.C; dminC = SYS.dC;
minD = SYS.D; dminD = SYS.dD;
%reshape into Magnus-Neudecker Jacobians, i.e. dvec(X)/dp
dminA = reshape(dminA,size(dminA,1)*size(dminA,2),size(dminA,3));
dminB = reshape(dminB,size(dminB,1)*size(dminB,2),size(dminB,3));
......
function [CheckCO,minnx,minA,minB,minC,minD,dminA,dminB,dminC,dminD] = get_minimal_state_representation(A,B,C,D,dA,dB,dC,dD)
% [CheckCO,minnx,minA,minB,minC,minD,dminA,dminB,dminC,dminD] = get_minimal_state_representation(A,B,C,D,dA,dB,dC,dD)
% Derives and checks the minimal state representation of the ABCD
% representation of a state space model
function [CheckCO,minns,minSYS] = get_minimal_state_representation(SYS, derivs_flag)
% Derives and checks the minimal state representation
% Let x = A*x(-1) + B*u and y = C*x(-1) + D*u be a linear state space
% system, then this function computes the following representation
% xmin = minA*xmin(-1) + minB*u and and y=minC*xmin(-1) + minD*u
%
% -------------------------------------------------------------------------
% INPUTS
% A: [endo_nbr by endo_nbr] Transition matrix from Kalman filter
% for all endogenous declared variables, in DR order
% B: [endo_nbr by exo_nbr] Transition matrix from Kalman filter
% mapping shocks today to endogenous variables today, in DR order
% C: [obs_nbr by endo_nbr] Measurement matrix from Kalman filter
% linking control/observable variables to states, in DR order
% D: [obs_nbr by exo_nbr] Measurement matrix from Kalman filter
% mapping shocks today to controls/observables today, in DR order
% dA: [endo_nbr by endo_nbr by totparam_nbr] in DR order
% SYS [structure]
% with the following necessary fields:
% A: [nspred by nspred] in DR order
% Transition matrix for all state variables
% B: [nspred by exo_nbr] in DR order
% Transition matrix mapping shocks today to states today
% C: [varobs_nbr by nspred] in DR order
% Measurement matrix linking control/observable variables to states
% D: [varobs_nbr by exo_nbr] in DR order
% Measurement matrix mapping shocks today to controls/observables today
% and optional fields:
% dA: [nspred by nspred by totparam_nbr] in DR order
% Jacobian (wrt to all parameters) of transition matrix A
% dB: [endo_nbr by exo_nbr by totparam_nbr] in DR order
% dB: [nspred by exo_nbr by totparam_nbr] in DR order
% Jacobian (wrt to all parameters) of transition matrix B
% dC: [obs_nbr by endo_nbr by totparam_nbr] in DR order
% dC: [varobs_nbr by nspred by totparam_nbr] in DR order
% Jacobian (wrt to all parameters) of measurement matrix C
% dD: [obs_nbr by exo_nbr by totparam_nbr] in DR order
% dD: [varobs_nbr by exo_nbr by totparam_nbr] in DR order
% Jacobian (wrt to all parameters) of measurement matrix D
% derivs_flag [scalar]
% (optional) indicator whether to output parameter derivatives
% -------------------------------------------------------------------------
% OUTPUTS
% CheckCO: [scalar] indicator, equals to 1 if minimal state representation is found
% minnx: [scalar] length of minimal state vector
% minA: [minnx by minnx] Transition matrix A for evolution of minimal state vector
% minB: [minnx by exo_nbr] Transition matrix B for evolution of minimal state vector
% minC: [obs_nbr by minnx] Measurement matrix C for evolution of controls, depending on minimal state vector only
% minD: [obs_nbr by minnx] Measurement matrix D for evolution of controls, depending on minimal state vector only
% dminA: [minnx by minnx by totparam_nbr] in DR order
% CheckCO: [scalar]
% equals to 1 if minimal state representation is found
% minns: [scalar]
% length of minimal state vector
% SYS [structure]
% with the following fields:
% minA: [minns by minns] in DR-order
% transition matrix A for evolution of minimal state vector
% minB: [minns by exo_nbr] in DR-order
% transition matrix B for evolution of minimal state vector
% minC: [varobs_nbr by minns] in DR-order
% measurement matrix C for evolution of controls, depending on minimal state vector only
% minD: [varobs_nbr by minns] in DR-order
% measurement matrix D for evolution of controls, depending on minimal state vector only
% dminA: [minns by minns by totparam_nbr] in DR order
% Jacobian (wrt to all parameters) of transition matrix minA
% dminB: [minnx by exo_nbr by totparam_nbr] in DR order
% dminB: [minns by exo_nbr by totparam_nbr] in DR order
% Jacobian (wrt to all parameters) of transition matrix minB
% dminC: [obs_nbr by minnx by totparam_nbr] in DR order
% dminC: [varobs_nbr by minns by totparam_nbr] in DR order
% Jacobian (wrt to all parameters) of measurement matrix minC
% dminD: [obs_nbr by exo_nbr by totparam_nbr] in DR order
% dminD: [varobs_nbr by u_nbr by totparam_nbr] in DR order
% Jacobian (wrt to all parameters) of measurement matrix minD
% -------------------------------------------------------------------------
% This function is called by
......@@ -42,8 +57,9 @@ function [CheckCO,minnx,minA,minB,minC,minD,dminA,dminB,dminC,dminD] = get_minim
% -------------------------------------------------------------------------
% This function calls
% * check_minimality (embedded)
% * minrealold (embedded)
% =========================================================================
% Copyright (C) 2019 Dynare Team
% Copyright (C) 2019-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -60,102 +76,123 @@ function [CheckCO,minnx,minA,minB,minC,minD,dminA,dminB,dminC,dminD] = get_minim
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% =========================================================================
nx = size(A,2);
ny = size(C,1);
nu = size(B,2);
if nargin == 1
derivs_flag = 0;
end
realsmall = 1e-7;
[nspred,exo_nbr] = size(SYS.B);
varobs_nbr = size(SYS.C,1);
% Check controllability and observability conditions for full state vector
CheckCO = check_minimality(A,B,C);
if CheckCO == 1 % If model is already minimal
minnx = nx;
minA = A;
minB = B;
minC = C;
minD = D;
if nargout > 6
dminA = dA;
dminB = dB;
dminC = dC;
dminD = dD;
end
CheckCO = check_minimality(SYS.A,SYS.B,SYS.C);
if CheckCO == 1 % If model is already minimal, we are finished
minns = nspred;
minSYS = SYS;
else
%Model is not minimal
realsmall = 1e-7;
% create indices for unnecessary states
endogstateindex = find(abs(sum(A,1))<realsmall);
exogstateindex = find(abs(sum(A,1))>realsmall);
minnx = length(exogstateindex);
% remove unnecessary states from solution matrices
A_1 = A(endogstateindex,exogstateindex);
A_2 = A(exogstateindex,exogstateindex);
B_2 = B(exogstateindex,:);
C_1 = C(:,endogstateindex);
C_2 = C(:,exogstateindex);
D = D;
ind_A1 = endogstateindex;
ind_A2 = exogstateindex;
% minimal representation
minA = A_2;
minB = B_2;
minC = C_2;
minD = D;
% Check controllability and observability conditions
CheckCO = check_minimality(minA,minB,minC);
if CheckCO ~=1
j=1;
while (CheckCO==0 && j<minnx)
combis = nchoosek(1:minnx,j);
i=1;
while i <= size(combis,1)
ind_A2 = exogstateindex;
ind_A1 = [endogstateindex ind_A2(combis(j,:))];
ind_A2(combis(j,:)) = [];
% remove unnecessary states from solution matrices
A_1 = A(ind_A1,ind_A2);
A_2 = A(ind_A2,ind_A2);
B_2 = B(ind_A2,:);
C_1 = C(:,ind_A1);
C_2 = C(:,ind_A2);
D = D;
% minimal representation
minA = A_2;
minB = B_2;
minC = C_2;
minD = D;
% Check controllability and observability conditions
CheckCO = check_minimality(minA,minB,minC);
if CheckCO == 1
minnx = length(ind_A2);
break;
%Model is not minimal
try
minreal_flag = 1;
% In future we will use SLICOT TB01PD.f mex file [to do @wmutschl], currently use workaround
[mSYS,U] = minrealold(SYS,realsmall);
minns = size(mSYS.A,1);
CheckCO = check_minimality(mSYS.A,mSYS.B,mSYS.C);
if CheckCO
minSYS.A = mSYS.A;
minSYS.B = mSYS.B;
minSYS.C = mSYS.C;
minSYS.D = mSYS.D;
if derivs_flag
totparam_nbr = size(SYS.dA,3);
minSYS.dA = zeros(minns,minns,totparam_nbr);
minSYS.dB = zeros(minns,exo_nbr,totparam_nbr);
minSYS.dC = zeros(varobs_nbr,minns,totparam_nbr);
% Note that orthogonal matrix U is such that (U*dA*U',U*dB,dC*U') is a Kalman decomposition of (dA,dB,dC) %
for jp=1:totparam_nbr
dA_tmp = U*SYS.dA(:,:,jp)*U';
dB_tmp = U*SYS.dB(:,:,jp);
dC_tmp = SYS.dC(:,:,jp)*U';
minSYS.dA(:,:,jp) = dA_tmp(1:minns,1:minns);
minSYS.dB(:,:,jp) = dB_tmp(1:minns,:);
minSYS.dC(:,:,jp) = dC_tmp(:,1:minns);
end
i=i+1;
minSYS.dD = SYS.dD;
end
j=j+1;
end
catch
minreal_flag = 0; % if something went wrong use below procedure
end
if nargout > 6
dminA = dA(ind_A2,ind_A2,:);
dminB = dB(ind_A2,:,:);
dminC = dC(:,ind_A2,:);
dminD = dD;
if minreal_flag == 0
fprintf('Use naive brute-force approach to find minimal state space system.\n These computations may be inaccurate/wrong as ''minreal'' is not available, please raise an issue on GitLab or the forum\n')
% create indices for unnecessary states
exogstateindex = find(abs(sum(SYS.A,1))>realsmall);
minns = length(exogstateindex);
% remove unnecessary states from solution matrices
A_2 = SYS.A(exogstateindex,exogstateindex);
B_2 = SYS.B(exogstateindex,:);
C_2 = SYS.C(:,exogstateindex);
D = SYS.D;
ind_A2 = exogstateindex;
% minimal representation
minSYS.A = A_2;
minSYS.B = B_2;
minSYS.C = C_2;
minSYS.D = D;
% Check controllability and observability conditions
CheckCO = check_minimality(minSYS.A,minSYS.B,minSYS.C);
if CheckCO ~=1
% do brute-force search
j=1;
while (CheckCO==0 && j<minns)
combis = nchoosek(1:minns,j);
i=1;
while i <= size(combis,1)
ind_A2 = exogstateindex;
ind_A2(combis(j,:)) = [];
% remove unnecessary states from solution matrices
A_2 = SYS.A(ind_A2,ind_A2);
B_2 = SYS.B(ind_A2,:);
C_2 = SYS.C(:,ind_A2);
D = SYS.D;
% minimal representation
minSYS.A = A_2;
minSYS.B = B_2;
minSYS.C = C_2;
minSYS.D = D;
% Check controllability and observability conditions
CheckCO = check_minimality(minSYS.A,minSYS.B,minSYS.C);
if CheckCO == 1
minns = length(ind_A2);
break;
end
i=i+1;
end
j=j+1;
end
end
if derivs_flag
minSYS.dA = SYS.dA(ind_A2,ind_A2,:);
minSYS.dB = SYS.dB(ind_A2,:,:);
minSYS.dC = SYS.dC(:,ind_A2,:);
minSYS.dD = SYS.dD;
end
end
end
function CheckCO = check_minimality(A,B,C)
function CheckCO = check_minimality(a,b,c)
%% This function computes the controllability and the observability matrices of the ABCD system and checks if the system is minimal
%
% Inputs: Solution matrices A,B,C of ABCD representation of a DSGE model
% Outputs: CheckCO: equals 1, if both rank conditions for observability and controllability are fullfilled
n = size(A,1);
CC = B; % Initialize controllability matrix
OO = C; % Initialize observability matrix
n = size(a,1);
CC = b; % Initialize controllability matrix
OO = c; % Initialize observability matrix
if n >= 2
for indn = 1:1:n-1
CC = [CC, (A^indn)*B]; % Set up controllability matrix
OO = [OO; C*(A^indn)]; % Set up observability matrix
CC = [CC, (a^indn)*b]; % Set up controllability matrix
OO = [OO; c*(a^indn)]; % Set up observability matrix
end
end
CheckC = (rank(full(CC))==n); % Check rank of controllability matrix
......@@ -163,4 +200,87 @@ CheckO = (rank(full(OO))==n); % Check rank of observability matrix
CheckCO = CheckC&CheckO; % equals 1 if minimal state
end % check_minimality end
function [mSYS,U] = minrealold(SYS,tol)
% This is a temporary replacement for minreal, will be replaced by a mex file from SLICOT TB01PD.f soon
a = SYS.A;
b = SYS.B;
c = SYS.C;
[ns,nu] = size(b);
[am,bm,cm,U,k] = ControllabilityStaircaseRosenbrock(a,b,c,tol);
kk = sum(k);
nu = ns - kk;
nn = nu;
am = am(nu+1:ns,nu+1:ns);
bm = bm(nu+1:ns,:);
cm = cm(:,nu+1:ns);
ns = ns - nu;
if ns
[am,bm,cm,t,k] = ObservabilityStaircaseRosenbrock(am,bm,cm,tol);
kk = sum(k);
nu = ns - kk;
nn = nn + nu;
am = am(nu+1:ns,nu+1:ns);
bm = bm(nu+1:ns,:);
cm = cm(:,nu+1:ns);
end
mSYS.A = am;
mSYS.B = bm;
mSYS.C = cm;
mSYS.D = SYS.D;
end
function [abar,bbar,cbar,t,k] = ObservabilityStaircaseRosenbrock(a,b,c,tol)
%Observability staircase form
[aa,bb,cc,t,k] = ControllabilityStaircaseRosenbrock(a',c',b',tol);
abar = aa'; bbar = cc'; cbar = bb';
end
function [abar,bbar,cbar,t,k] = ControllabilityStaircaseRosenbrock(a, b, c, tol)
% Controllability staircase algorithm of Rosenbrock, 1968
[ra,ca] = size(a);
[rb,cb] = size(b);
ptjn1 = eye(ra);
ajn1 = a;
bjn1 = b;
rojn1 = cb;
deltajn1 = 0;
sigmajn1 = ra;
k = zeros(1,ra);
if nargin == 3
tol = ra*norm(a,1)*eps;
end
for jj = 1 : ra
[uj,sj,vj] = svd(bjn1);
[rsj,csj] = size(sj);
p = flip(eye(rsj),2);
p = permute(p,[2 1 3:ndims(eye(rsj))]);
uj = uj*p;
bb = uj'*bjn1;
roj = rank(bb,tol);
[rbb,cbb] = size(bb);
sigmaj = rbb - roj;
sigmajn1 = sigmaj;
k(jj) = roj;
if roj == 0, break, end
if sigmaj == 0, break, end
abxy = uj' * ajn1 * uj;
aj = abxy(1:sigmaj,1:sigmaj);
bj = abxy(1:sigmaj,sigmaj+1:sigmaj+roj);
ajn1 = aj;
bjn1 = bj;
[ruj,cuj] = size(uj);
ptj = ptjn1 * ...
[uj zeros(ruj,deltajn1); ...
zeros(deltajn1,cuj) eye(deltajn1)];
ptjn1 = ptj;
deltaj = deltajn1 + roj;
deltajn1 = deltaj;
end
t = ptjn1';
abar = t * a * t';
bbar = t * b;
cbar = c * t';
end
end % Main function end
% this is the Smets and Wouters (2007) model for which Komunjer and Ng (2011)
% derived the minimal state space system. In Dynare, however, we use more
% powerful minreal function
% created by Willi Mutschler (@wmutschl, willi@mutschler.eu)
% =========================================================================
% Copyright (C) 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/>.
% =========================================================================
var y R g z c dy p YGR INFL INT;
varobs y R p c YGR INFL INT;
varexo e_r e_g e_z;
parameters tau phi psi1 psi2 rhor rhog rhoz rrst pist gamst nu cyst;
rrst = 1.0000;
pist = 3.2000;
gamst= 0.5500;
tau = 2.0000;
nu = 0.1000;
kap = 0.3300;
phi = tau*(1-nu)/nu/kap/exp(pist/400)^2;
cyst = 0.8500;
psi1 = 1.5000;
psi2 = 0.1250;
rhor = 0.7500;
rhog = 0.9500;
rhoz = 0.9000;
model;
#pist2 = exp(pist/400);
#rrst2 = exp(rrst/400);
#bet = 1/rrst2;
#gst = 1/cyst;
#cst = (1-nu)^(1/tau);
#yst = cst*gst;
1 = exp(-tau*c(+1)+tau*c+R-z(+1)-p(+1));
(1-nu)/nu/phi/(pist2^2)*(exp(tau*c)-1) = (exp(p)-1)*((1-1/2/nu)*exp(p)+1/2/nu) - bet*(exp(p(+1))-1)*exp(-tau*c(+1)+tau*c+dy(+1)+p(+1));
exp(c-y) = exp(-g) - phi*pist2^2*gst/2*(exp(p)-1)^2;
R = rhor*R(-1) + (1-rhor)*psi1*p + (1-rhor)*psi2*(y-g) + e_r;
g = rhog*g(-1) + e_g;
z = rhoz*z(-1) + e_z;
YGR = gamst+100*(dy+z);
INFL = pist+400*p;
INT = pist+rrst+4*gamst+400*R;
dy = y - y(-1);
end;
shocks;
var e_r; stderr 0.2/100;
var e_g; stderr 0.6/100;
var e_z; stderr 0.3/100;
end;
steady_state_model;
z=0; g=0; c=0; y=0; p=0; R=0; dy=0;
YGR=gamst; INFL=pist; INT=pist+rrst+4*gamst;
end;
stoch_simul(order=1,irf=0,periods=0);
options_.qz_criterium = 1;
indx = [M_.nstatic+(1:M_.nspred)]';
indy = 1:M_.endo_nbr';
SS.A = oo_.dr.ghx(indx,:);
SS.B = oo_.dr.ghu(indx,:);
SS.C = oo_.dr.ghx(indy,:);
SS.D = oo_.dr.ghu(indy,:);
[CheckCO,minnx,minSS] = get_minimal_state_representation(SS,0);
Sigmax_full = lyapunov_symm(SS.A, SS.B*M_.Sigma_e*SS.B', options_.lyapunov_fixed_point_tol, options_.qz_criterium, options_.lyapunov_complex_threshold, 1, options_.debug);
Sigmay_full = SS.C*Sigmax_full*SS.C' + SS.D*M_.Sigma_e*SS.D';
Sigmax_min = lyapunov_symm(minSS.A, minSS.B*M_.Sigma_e*minSS.B', options_.lyapunov_fixed_point_tol, options_.qz_criterium, options_.lyapunov_complex_threshold, 1, options_.debug);
Sigmay_min = minSS.C*Sigmax_min*minSS.C' + minSS.D*M_.Sigma_e*minSS.D';
([Sigmay_full(:) - Sigmay_min(:)]')
sqrt(([diag(Sigmay_full), diag(Sigmay_min)]'))
dx = norm( Sigmay_full - Sigmay_min, Inf);
if dx > 1e-12
error('something wrong with minimal state space computations')
else
fprintf('numerical error for moments computed from minimal state system is %d\n',dx)
end
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment