Commit b261eb0b authored by Michel Juillard's avatar Michel Juillard
Browse files

integration of partial information in stoch_simul (thanks to George, Joe and Paul)

parent 436fdf97
function X=disclyap_fast(G,V,ch)
% function X=disclyap_fast(G,V,ch)
%
% Solve the discrete Lyapunov Equation
% X=G*X*G'+V
% Using the Doubling Algorithm
%
% If ch is defined then the code will check if the resulting X
% is positive definite and generate an error message if it is not
%
% Joe Pearlman and Alejandro Justiniano
% 3/5/2005
% =================================================================
if nargin == 2 | isempty( ch ) == 1
flag_ch = 0;
else
flag_ch = 1;
end
s=size(G,1);
tol = 1e-16;
P0=V;
A0=G;
matd=1;
while matd > tol
P1=P0+A0*P0*A0';
A1=A0*A0;
matd=max( max( abs( P1 - P0 ) ) );
P0=P1;
A0=A1;
end
clear A0 A1 P1;
X=(P0+P0')/2;
% Check that X is positive definite
if flag_ch==1
[C,p]=chol(X);
if p ~= 0
error('X is not positive definite')
end
end
\ No newline at end of file
......@@ -41,6 +41,7 @@ addpath([dynareroot '/distributions/'])
addpath([dynareroot '/kalman/'])
addpath([dynareroot '/kalman/likelihood'])
addpath([dynareroot '/AIM/'])
addpath([dynareroot '/partial_information/'])
% For functions that exist only under some Octave versions
% or some MATLAB versions, and for which we provide some replacement functions
......
......@@ -128,6 +128,7 @@ end
options_.aim_solver = 0; % i.e. by default do not use G.Anderson's AIM solver, use mjdgges instead
options_.k_order_solver=0; % by default do not use k_order_perturbation but mjdgges
options_.partial_information = 0;
options_.ACES_solver = 0;
options_.conditional_variance_decomposition = [];
% Ramsey policy
......
function [irfmat,irfst]=PCL_Part_info_irf( H, OBS, M_, dr, irfpers,ii)
function [irfmat,irfst]=PCL_Part_info_irf( H, varobs, M_, dr, irfpers,ii)
% sets up parameters and calls part-info kalman filter
% developed by G Perendia, July 2006 for implementation from notes by Prof. Joe Pearlman to
% suit partial information RE solution in accordance with, and based on, the
......@@ -23,12 +23,14 @@ function [irfmat,irfst]=PCL_Part_info_irf( H, OBS, M_, dr, irfpers,ii)
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% Recall that the state space is given by the
% predetermined variables s(t-1), x(t-1)
% and the jump variables x(t).
% The jump variables have dimension NETA
% Recall that the state space is given by the
% predetermined variables s(t-1), x(t-1)
% and the jump variables x(t).
% The jump variables have dimension NETA
OBS = ismember(varobs,M_.endo_names);
G1=dr.PI_ghx;
impact=dr.PI_ghu;
nmat=dr.PI_nmat;
......@@ -149,10 +151,10 @@ function [irfmat,irfst]=PCL_Part_info_irf( H, OBS, M_, dr, irfpers,ii)
%OVV = [ zeros( size(dr.PI_TT1,1), NX ) dr.PI_TT1 dr.PI_TT2];
VV = [ dr.PI_TT1 dr.PI_TT2];
stderr=diag(M_.Sigma_e^0.5);
irfmat=zeros(size(dr.PI_TT1 ,1),irfpers+1);
irfst=zeros(size(GG,1),irfpers+1);
irfmat=zeros(size(dr.PI_TT1 ,1),irfpers);
irfst=zeros(size(GG,1),irfpers);
irfst(:,1)=stderr(ii)*imp(:,ii);
for jj=2:irfpers+1;
for jj=2:irfpers;
irfst(:,jj)=GG*irfst(:,jj-1); % xjj=f irfstjj-2
irfmat(:,jj-1)=VV*irfst(NX+1:ss-FL_RANK,jj);
%irfmat(:,jj)=LL0*irfst(:,jj);
......
function [irfmat,irfst]=PCL_Part_info_irf( H, OBS, dr,ivar, var_list)
function [irfmat,irfst]=PCL_Part_info_irf( H, varobs, dr,ivar)
% sets up parameters and calls part-info kalman filter
% developed by G Perendia, July 2006 for implementation from notes by Prof. Joe Pearlman to
% suit partial information RE solution in accordance with, and based on, the
......@@ -30,6 +30,9 @@ function [irfmat,irfst]=PCL_Part_info_irf( H, OBS, dr,ivar, var_list)
global M_ options_ oo_
warning_old_state = warning;
warning off
OBS = ismember(varobs,M_.endo_names);
G1=dr.PI_ghx;
impact=dr.PI_ghu;
nmat=dr.PI_nmat;
......
function [dr,info]=resol(ys,check_flag)
% function [dr,info]=resol(ys,check_flag)
function [dr,info]=PCL_resol(ys,check_flag)
% function [dr,info]=PCL_resol(ys,check_flag)
% Computes first and second order approximations
%
% INPUTS
......
function [A,B,Q,Z] = qzdiv(stake,A,B,Q,Z)
%function [A,B,Q,Z] = qzdiv(stake,A,B,Q,Z)
%
% Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them
% so that all cases of abs(B(i,i)/A(i,i))>stake are in lower right
% corner, while preserving U.T. and orthonormal properties and Q'AZ' and
% Q'BZ'.
% Original file downloaded from:
% http://sims.princeton.edu/yftp/gensys/mfiles/qzdiv.m
% Copyright (C) 1993-2007 Christopher Sims
%
% 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/>.
[n jnk] = size(A);
root = abs([diag(A) diag(B)]);
root(:,1) = root(:,1)-(root(:,1)<1.e-13).*(root(:,1)+root(:,2));
root(:,2) = root(:,2)./root(:,1);
for i = n:-1:1
m=0;
for j=i:-1:1
if (root(j,2) > stake | root(j,2) < -.1)
m=j;
break
end
end
if (m==0)
return
end
for k=m:1:i-1
[A B Q Z] = qzswitch(k,A,B,Q,Z);
tmp = root(k,2);
root(k,2) = root(k+1,2);
root(k+1,2) = tmp;
end
end
function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z)
%function [A,B,Q,Z] = qzswitch(i,A,B,Q,Z)
%
% Takes U.T. matrices A, B, orthonormal matrices Q,Z, interchanges
% diagonal elements i and i+1 of both A and B, while maintaining
% Q'AZ' and Q'BZ' unchanged. If diagonal elements of A and B
% are zero at matching positions, the returned A will have zeros at both
% positions on the diagonal. This is natural behavior if this routine is used
% to drive all zeros on the diagonal of A to the lower right, but in this case
% the qz transformation is not unique and it is not possible simply to switch
% the positions of the diagonal elements of both A and B.
% Original file downloaded from:
% http://sims.princeton.edu/yftp/gensys/mfiles/qzswitch.m
% Copyright (C) 1993-2007 Christopher Sims
%
% 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/>.
realsmall=sqrt(eps)*10;
%realsmall=1e-3;
a = A(i,i); d = B(i,i); b = A(i,i+1); e = B(i,i+1);
c = A(i+1,i+1); f = B(i+1,i+1);
% A(i:i+1,i:i+1)=[a b; 0 c];
% B(i:i+1,i:i+1)=[d e; 0 f];
if (abs(c)<realsmall & abs(f)<realsmall)
if abs(a)<realsmall
% l.r. coincident 0's with u.l. of A=0; do nothing
return
else
% l.r. coincident zeros; put 0 in u.l. of a
wz=[b; -a];
wz=wz/sqrt(wz'*wz);
wz=[wz [wz(2)';-wz(1)'] ];
xy=eye(2);
end
elseif (abs(a)<realsmall & abs(d)<realsmall)
if abs(c)<realsmall
% u.l. coincident zeros with l.r. of A=0; do nothing
return
else
% u.l. coincident zeros; put 0 in l.r. of A
wz=eye(2);
xy=[c -b];
xy=xy/sqrt(xy*xy');
xy=[[xy(2)' -xy(1)'];xy];
end
else
% usual case
wz = [c*e-f*b, (c*d-f*a)'];
xy = [(b*d-e*a)', (c*d-f*a)'];
n = sqrt(wz*wz');
m = sqrt(xy*xy');
if m<eps*100
% all elements of A and B proportional
return
end
wz = n\wz;
xy = m\xy;
wz = [wz; -wz(2)', wz(1)'];
xy = [xy;-xy(2)', xy(1)'];
end
A(i:i+1,:) = xy*A(i:i+1,:);
B(i:i+1,:) = xy*B(i:i+1,:);
A(:,i:i+1) = A(:,i:i+1)*wz;
B(:,i:i+1) = B(:,i:i+1)*wz;
Z(:,i:i+1) = Z(:,i:i+1)*wz;
Q(i:i+1,:) = xy*Q(i:i+1,:);
\ No newline at end of file
function info=stoch_simul(var_list)
% Copyright (C) 2001-20010 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/>.
global M_ options_ oo_ it_
options_old = options_;
if options_.linear
options_.order = 1;
end
if (options_.order == 1)
options_.replic = 1;
elseif options_.order == 3
options_.k_order_solver = 1;
end
TeX = options_.TeX;
iter_ = max(options_.periods,1);
if M_.exo_nbr > 0
oo_.exo_simul= ones(iter_ + M_.maximum_lag + M_.maximum_lead,1) * oo_.exo_steady_state';
end
check_model;
[oo_.dr, info] = resol(oo_.steady_state,0);
if info(1)
options_ = options_old;
print_info(info, options_.noprint);
return
end
if ~options_.noprint
disp(' ')
disp('MODEL SUMMARY')
disp(' ')
disp([' Number of variables: ' int2str(M_.endo_nbr)])
disp([' Number of stochastic shocks: ' int2str(M_.exo_nbr)])
disp([' Number of state variables: ' ...
int2str(length(find(oo_.dr.kstate(:,2) <= M_.maximum_lag+1)))])
disp([' Number of jumpers: ' ...
int2str(length(find(oo_.dr.kstate(:,2) == M_.maximum_lag+2)))])
disp([' Number of static variables: ' int2str(oo_.dr.nstatic)])
my_title='MATRIX OF COVARIANCE OF EXOGENOUS SHOCKS';
labels = deblank(M_.exo_names);
headers = strvcat('Variables',labels);
lh = size(labels,2)+2;
dyntable(my_title,headers,labels,M_.Sigma_e,lh,10,6);
disp(' ')
if (options_.order <= 2 & options_.partial_information ==0 & options_.ACES_solver==0)
disp_dr(oo_.dr,options_.order,var_list);
end
end
if options_.irf
if size(var_list,1) == 0
var_list = M_.endo_names(1:M_.orig_endo_nbr, :);
if TeX
var_listTeX = M_.endo_names_tex(1:M_.orig_endo_nbr, :);
end
end;%else
n = size(var_list,1);
ivar=zeros(n,1);
if TeX
var_listTeX = [];
end
for i=1:n
i_tmp = strmatch(var_list(i,:),M_.endo_names,'exact');
if isempty(i_tmp)
error (['One of the specified variables does not exist']) ;
else
ivar(i) = i_tmp;
if TeX
var_listTeX = strvcat(var_listTeX,deblank(M_.endo_names_tex(i_tmp,:)));
end
end
end
end
if ( options_.partial_information ==0 & options_.ACES_solver==0)
if options_.periods == 0 && options_.nomoments == 0
disp_th_moments(oo_.dr,var_list);
elseif options_.periods ~= 0
if options_.periods < options_.drop
disp(['STOCH_SIMUL error: The horizon of simulation is shorter' ...
' than the number of observations to be DROPed'])
options_ =options_old;
return
end
oo_.endo_simul = simult(repmat(oo_.dr.ys,1,M_.maximum_lag),oo_.dr);
dyn2vec;
if options_.nomoments == 0
disp_moments(oo_.endo_simul,var_list);
end
end
else % part info or ACES
if ~isfield(options_,'varobs')
nobs =0;
else
if isempty(options_.varobs)
nobs =0;
else
varobs=options_.varobs;
nobs = size(varobs,1);
end
end
if nobs == 0
ivarobs=ivar;
else
ivarobs=zeros(nobs,1);
if TeX
varobs_listTeX = [];
end
for i=1:nobs
i_tmp = strmatch(varobs(i,:),M_.endo_names,'exact');
if isempty(i_tmp)
error (['One of the specified observed variables does not exist']) ;
else
ivarobs(i) = i_tmp;
if TeX
varobs_listTeX = strvcat(varobs_listTeX,deblank(M_.endo_names_tex(i_tmp,:)));
end
end
end
end
PCL_Part_info_moments (0, ivarobs', oo_.dr, ivar, var_list);
end
if options_.irf
if TeX
fidTeX = fopen([M_.fname '_IRF.TeX'],'w');
fprintf(fidTeX,'%% TeX eps-loader file generated by stoch_simul.m (Dynare).\n');
fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
fprintf(fidTeX,' \n');
end
olditer = iter_;% Est-ce vraiment utile ? Il y a la mme ligne dans irf...
SS(M_.exo_names_orig_ord,M_.exo_names_orig_ord)=M_.Sigma_e+1e-14*eye(M_.exo_nbr);
cs = transpose(chol(SS));
tit(M_.exo_names_orig_ord,:) = M_.exo_names;
if TeX
titTeX(M_.exo_names_orig_ord,:) = M_.exo_names_tex;
end
for i=1:M_.exo_nbr
if SS(i,i) > 1e-13
if ( options_.partial_information ==1 | options_.ACES_solver==1)
irfmat=PCL_Part_info_irf (0, ivarobs', M_, oo_.dr, options_.irf, i);
y=zeros(M_.endo_nbr,options_.irf);
y(ivar,:)=irfmat(ivar,1:options_.irf);
else
y=irf(oo_.dr,cs(M_.exo_names_orig_ord,i), options_.irf, options_.drop, ...
options_.replic, options_.order);
end
if options_.relative_irf
y = 100*y/cs(i,i);
end
irfs = [];
mylist = [];
if TeX
mylistTeX = [];
end
for j = 1:n
assignin('base',[deblank(M_.endo_names(ivar(j),:)) '_' deblank(M_.exo_names(i,:))],...
y(ivar(j),:)');
eval(['oo_.irfs.' deblank(M_.endo_names(ivar(j),:)) '_' ...
deblank(M_.exo_names(i,:)) ' = y(ivar(j),:);']);
if max(y(ivar(j),:)) - min(y(ivar(j),:)) > 1e-10
irfs = cat(1,irfs,y(ivar(j),:));
mylist = strvcat(mylist,deblank(var_list(j,:)));
if TeX
mylistTeX = strvcat(mylistTeX,deblank(var_listTeX(j,:)));
end
end
end
if options_.nograph == 0
number_of_plots_to_draw = size(irfs,1);
[nbplt,nr,nc,lr,lc,nstar] = pltorg(number_of_plots_to_draw);
if nbplt == 0
elseif nbplt == 1
if options_.relative_irf
hh = figure('Name',['Relative response to' ...
' orthogonalized shock to ' tit(i,:)]);
else
hh = figure('Name',['Orthogonalized shock to' ...
' ' tit(i,:)]);
end
for j = 1:number_of_plots_to_draw
subplot(nr,nc,j);
plot(1:options_.irf,transpose(irfs(j,:)),'-k','linewidth',1);
hold on
plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
hold off
xlim([1 options_.irf]);
title(deblank(mylist(j,:)),'Interpreter','none');
end
eval(['print -depsc2 ' M_.fname '_IRF_' deblank(tit(i,:)) '.eps']);
if ~exist('OCTAVE_VERSION')
eval(['print -dpdf ' M_.fname '_IRF_' deblank(tit(i,:))]);
saveas(hh,[M_.fname '_IRF_' deblank(tit(i,:)) '.fig']);
end
if TeX
fprintf(fidTeX,'\\begin{figure}[H]\n');
for j = 1:number_of_plots_to_draw
fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{$%s$}\n'],deblank(mylist(j,:)),deblank(mylistTeX(j,:)));
end
fprintf(fidTeX,'\\centering \n');
fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_IRF_%s}\n',M_.fname,deblank(tit(i,:)));
fprintf(fidTeX,'\\caption{Impulse response functions (orthogonalized shock to $%s$).}',titTeX(i,:));
fprintf(fidTeX,'\\label{Fig:IRF:%s}\n',deblank(tit(i,:)));
fprintf(fidTeX,'\\end{figure}\n');
fprintf(fidTeX,' \n');
end
% close(hh)
else
for fig = 1:nbplt-1
if options_.relative_irf == 1
hh = figure('Name',['Relative response to orthogonalized shock' ...
' to ' tit(i,:) ' figure ' int2str(fig)]);
else
hh = figure('Name',['Orthogonalized shock to ' tit(i,:) ...
' figure ' int2str(fig)]);
end
for plt = 1:nstar
subplot(nr,nc,plt);
plot(1:options_.irf,transpose(irfs((fig-1)*nstar+plt,:)),'-k','linewidth',1);
hold on
plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
hold off
xlim([1 options_.irf]);
title(deblank(mylist((fig-1)*nstar+plt,:)),'Interpreter','none');
end
eval(['print -depsc2 ' M_.fname '_IRF_' deblank(tit(i,:)) int2str(fig) '.eps']);
if ~exist('OCTAVE_VERSION')
eval(['print -dpdf ' M_.fname '_IRF_' deblank(tit(i,:)) int2str(fig)]);
saveas(hh,[M_.fname '_IRF_' deblank(tit(i,:)) int2str(fig) '.fig']);
end
if TeX
fprintf(fidTeX,'\\begin{figure}[H]\n');
for j = 1:nstar
fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{$%s$}\n'],deblank(mylist((fig-1)*nstar+j,:)),deblank(mylistTeX((fig-1)*nstar+j,:)));
end
fprintf(fidTeX,'\\centering \n');
fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_IRF_%s%s}\n',M_.fname,deblank(tit(i,:)),int2str(fig));
if options_.relative_irf
fprintf(fidTeX,['\\caption{Relative impulse response' ...
' functions (orthogonalized shock to $%s$).}'],deblank(titTeX(i,:)));
else
fprintf(fidTeX,['\\caption{Impulse response functions' ...
' (orthogonalized shock to $%s$).}'],deblank(titTeX(i,:)));
end
fprintf(fidTeX,'\\label{Fig:BayesianIRF:%s:%s}\n',deblank(tit(i,:)),int2str(fig));
fprintf(fidTeX,'\\end{figure}\n');
fprintf(fidTeX,' \n');
end
% close(hh);
end
hh = figure('Name',['Orthogonalized shock to ' tit(i,:) ' figure ' int2str(nbplt) '.']);
m = 0;
for plt = 1:number_of_plots_to_draw-(nbplt-1)*nstar;
m = m+1;
subplot(lr,lc,m);
plot(1:options_.irf,transpose(irfs((nbplt-1)*nstar+plt,:)),'-k','linewidth',1);
hold on
plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
hold off
xlim([1 options_.irf]);
title(deblank(mylist((nbplt-1)*nstar+plt,:)),'Interpreter','none');
end
eval(['print -depsc2 ' M_.fname '_IRF_' deblank(tit(i,:)) int2str(nbplt) '.eps']);
if ~exist('OCTAVE_VERSION')
eval(['print -dpdf ' M_.fname '_IRF_' deblank(tit(i,:)) int2str(nbplt)]);
saveas(hh,[M_.fname '_IRF_' deblank(tit(i,:)) int2str(nbplt) '.fig']);
end
if TeX
fprintf(fidTeX,'\\begin{figure}[H]\n');
for j = 1:m
fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{$%s$}\n'],deblank(mylist((nbplt-1)*nstar+j,:)),deblank(mylistTeX((nbplt-1)*nstar+j,:)));
end
fprintf(fidTeX,'\\centering \n');
fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_IRF_%s%s}\n',M_.fname,deblank(tit(i,:)),int2str(nbplt));
if options_.relative_irf
fprintf(fidTeX,['\\caption{Relative impulse response functions' ...
' (orthogonalized shock to $%s$).}'],deblank(titTeX(i,:)));
else
fprintf(fidTeX,['\\caption{Impulse response functions' ...
' (orthogonalized shock to $%s$).}'],deblank(titTeX(i,:)));
end
fprintf(fidTeX,'\\label{Fig:IRF:%s:%s}\n',deblank(tit(i,:)),int2str(nbplt));
fprintf(fidTeX,'\\end{figure}\n');
fprintf(fidTeX,' \n');
end
% close(hh);
end
end
end
iter_ = olditer;
if TeX
fprintf(fidTeX,' \n');
fprintf(fidTeX,'%% End Of TeX file. \n');
fclose(fidTeX);
end
end
end
if options_.SpectralDensity == 1
[omega,f] = UnivariateSpectralDensity(oo_.dr,var_list);
end
options_ = options_old;