Commit 6180bc22 authored by Houtan Bastani's avatar Houtan Bastani

Add TZcode submodule to repository

parent 9576255e
......@@ -4,3 +4,6 @@
[submodule "contrib/ms-sbvar/switch_dw"]
path = contrib/ms-sbvar/switch_dw
url = http://www.dynare.org/git/frbatlanta/switch_dw.git
[submodule "contrib/ms-sbvar/TZcode"]
path = contrib/ms-sbvar/TZcode
url = http://www.dynare.org/git/frbatlanta/TZcode.git
TZcode @ 1673c6f7
Subproject commit 1673c6f7170ac61ad3d3cb832eb3f038c8e4b3ef
......@@ -152,13 +152,83 @@ Copyright: 1996 Christopher Sims
2003 Karibzhanov, Waggoner and Zha
License: GPL-3+
Files: matlab/ms-sbvar/cstz/*
Copyright: 1993-2011 Tao Zha
Files: contrib/ms-sbvar/TZcode/*
Copyright: 1997-2012 Tao Zha
License: GPL-3+
Files: matlab/ms-sbvar/cstz/bfgsi.m matlab/ms-sbvar/cstz/csminit.m
matlab/ms-sbvar/cstz/csminwel.m
Copyright: 1993-2011 Tao Zha and Christopher Sims
Files: contrib/ms-sbvar/TZcode/MatlabFiles/szbvar.m
contrib/ms-sbvar/TZcode/MatlabFiles/phg233.m
contrib/ms-sbvar/TZcode/MatlabFiles/phg235.m
contrib/ms-sbvar/TZcode/MatlabFiles/pwf234.m
contrib/ms-sbvar/TZcode/MatlabFiles/pmddf235.m
contrib/ms-sbvar/TZcode/MatlabFiles/pmddf234.m
contrib/ms-sbvar/TZcode/MatlabFiles/pmddf236.m
contrib/ms-sbvar/TZcode/MatlabFiles/csminwel.m
contrib/ms-sbvar/TZcode/MatlabFiles/phg234.m
contrib/ms-sbvar/TZcode/MatlabFiles/pwf235.m
contrib/ms-sbvar/TZcode/MatlabFiles/mnpdf.m
contrib/ms-sbvar/TZcode/MatlabFiles/szasbvar.m
Copyright: 1997-2012 Christopher A. Sims and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/pmddf233.m
contrib/ms-sbvar/TZcode/MatlabFiles/csminit.m
contrib/ms-sbvar/TZcode/MatlabFiles/gensysoldversion.m
contrib/ms-sbvar/TZcode/MatlabFiles/gensys_z2new.m
contrib/ms-sbvar/TZcode/MatlabFiles/gensysct.m
contrib/ms-sbvar/TZcode/MatlabFiles/csminitworksuntiil0205.m
contrib/ms-sbvar/TZcode/MatlabFiles/gensys_z2.m
contrib/ms-sbvar/TZcode/MatlabFiles/qzdiv.m
contrib/ms-sbvar/TZcode/MatlabFiles/pwf233.m
Copyright: 1997-2012 Christopher A. Sims
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/srestrictrwzalg.m
Copyright: 1997-2012 Juan Rubio-Ramirez, Daniel Waggoner and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/clmonq.m
contrib/ms-sbvar/TZcode/MatlabFiles/clgls.m
Copyright: 1997-2012 Eric Leeper and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/a0lhfun.m
Copyright: 1997-2012 Eric Leeper
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/suptitle.m
Copyright: 1997-2012 Drea Thomas
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/fn_gibbsrvar.m
contrib/ms-sbvar/TZcode/MatlabFiles/gibbsvar.m
contrib/ms-sbvar/TZcode/MatlabFiles/simtanzphi.m
contrib/ms-sbvar/TZcode/MatlabFiles/fn_gibbsglb.m
contrib/ms-sbvar/TZcode/MatlabFiles/fn_gibbsrvar_setup.m
contrib/ms-sbvar/TZcode/MatlabFiles/gibbsglb.m
contrib/ms-sbvar/TZcode/MatlabFiles/fn_gibbsrvaroldworks.m
Copyright: 1997-2012 Daniel Waggoner and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/gensys.m
Copyright: 1996-2012 Christopher A. Sims
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/xydata.m
Copyright: 1997-2012 Lutz Kilian and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/bfgsi.m
Copyright: 1996 Christopher A. Sims
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/qplot2.m
contrib/ms-sbvar/TZcode/MatlabFiles/ellipse.m
Copyright: 1997-2012 Clark A. Burdick
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/chol2.m
Copyright: 1996-2012 Tao Zha
License: GPL-3+
License: GFDL-NIV-1.3+
......
......@@ -51,8 +51,8 @@ addpath([dynareroot '/kalman/likelihood'])
addpath([dynareroot '/AIM/'])
addpath([dynareroot '/partial_information/'])
addpath([dynareroot '/ms-sbvar/'])
addpath([dynareroot '/ms-sbvar/cstz/'])
addpath([dynareroot '/ms-sbvar/identification/'])
addpath([dynareroot '../contrib/ms-sbvar/TZcode/MatlabFiles/'])
addpath([dynareroot '/parallel/'])
addpath([dynareroot '/particle/'])
addpath([dynareroot '/gsa/'])
......
function H = bfgsi(H0,dg,dx)
% H = bfgsi(H0,dg,dx)
% dg is previous change in gradient; dx is previous change in x;
% 6/8/93 version that updates inverse hessian instead of hessian
% itself.
% Copyright by Christopher Sims 1996. This material may be freely
% reproduced and modified.
dispIndx = 0; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha)
% Copyright (C) 1996-2011 Tao Zha and 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/>.
if size(dg,2)>1
dg=dg';
end
if size(dx,2)>1
dx=dx';
end
Hdg = H0*dg;
dgdx = dg'*dx;
if (abs(dgdx) >1e-12)
H = H0 + (1+(dg'*Hdg)/dgdx)*(dx*dx')/dgdx - (dx*Hdg'+Hdg*dx')/dgdx;
else
if dispIndx
disp('bfgs update failed.')
disp(['|dg| = ' num2str(sqrt(dg'*dg)) '|dx| = ' num2str(sqrt(dx'*dx))]);
disp(['dg''*dx = ' num2str(dgdx)])
disp(['|H*dg| = ' num2str(Hdg'*Hdg)])
end
H=H0;
end
save H.dat H
function [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,varargin)
% [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,...
% P1,P2,P3,P4,P5,P6,P7,P8)
% retcodes: 0, normal step. 5, largest step still improves too fast.
% 4,2 back and forth adjustment of stepsize didn't finish. 3, smallest
% stepsize still improves too slow. 6, no improvement found. 1, zero
% gradient.
%---------------------
% Modified 7/22/96 to omit variable-length P list, for efficiency and compilation.
% Places where the number of P's need to be altered or the code could be returned to
% its old form are marked with ARGLIST comments.
%
% Fixed 7/17/93 to use inverse-hessian instead of hessian itself in bfgs
% update.
%
% Fixed 7/19/93 to flip eigenvalues of H to get better performance when
% it's not psd.
%
% Fixed 02/19/05 to correct for low angle problems.
%
%tailstr = ')';
%for i=nargin-6:-1:1
% tailstr=[ ',P' num2str(i) tailstr];
%end
% Copyright (C) 1993-2011 Tao Zha and 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/>.
dispIndx = 0; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha)
%ANGLE = .03; % when output of this variable becomes negative, we have wrong analytical graident
ANGLE = .005; % works for identified VARs and OLS
%THETA = .03;
THETA = .3; %(0<THETA<.5) THETA near .5 makes long line searches, possibly fewer iterations.
FCHANGE = 1000;
MINLAMB = 1e-9;
% fixed 7/15/94
% MINDX = .0001;
% MINDX = 1e-6;
MINDFAC = .01;
fcount=0;
lambda=1;
xhat=x0;
f=f0;
fhat=f0;
g = g0;
gnorm = norm(g);
%
if (gnorm < 1.e-12) && ~badg % put ~badg 8/4/94
retcode =1;
dxnorm=0;
% gradient convergence
else
% with badg true, we don't try to match rate of improvement to directional
% derivative. We're satisfied just to get some improvement in f.
%
%if(badg)
% dx = -g*FCHANGE/(gnorm*gnorm);
% dxnorm = norm(dx);
% if dxnorm > 1e12
% disp('Bad, small gradient problem.')
% dx = dx*FCHANGE/dxnorm;
% end
%else
% Gauss-Newton step;
%---------- Start of 7/19/93 mod ---------------
%[v d] = eig(H0);
%toc
%d=max(1e-10,abs(diag(d)));
%d=abs(diag(d));
%dx = -(v.*(ones(size(v,1),1)*d'))*(v'*g);
% toc
dx = -H0*g;
% toc
dxnorm = norm(dx);
if dxnorm > 1e12
if dispIndx, disp('Near-singular H problem.'), end
dx = dx*FCHANGE/dxnorm;
end
dfhat = dx'*g0;
%end
%
%
if ~badg
% test for alignment of dx with gradient and fix if necessary
a = -dfhat/(gnorm*dxnorm);
if a<ANGLE
dx = dx - (ANGLE*dxnorm/gnorm+dfhat/(gnorm*gnorm))*g;
% suggested alternate code: ---------------------
dx = dx*dxnorm/norm(dx); % Added 02/19/05 by CAS. This keeps scale invariant to the angle correction
% ------------------------------------------------
dfhat = dx'*g;
% dxnorm = norm(dx); % Removed 02/19/05 by CAS. This line unnecessary with modification that keeps scale invariant
if dispIndx, disp(sprintf('Correct for low angle: %g',a)), end
end
end
if dispIndx, disp(sprintf('Predicted improvement: %18.9f',-dfhat/2)), end
%
% Have OK dx, now adjust length of step (lambda) until min and
% max improvement rate criteria are met.
done=0;
factor=3;
shrink=1;
lambdaMin=0;
lambdaMax=inf;
lambdaPeak=0;
fPeak=f0;
lambdahat=0;
while ~done
if size(x0,2)>1
dxtest=x0+dx'*lambda;
else
dxtest=x0+dx*lambda;
end
% home
f = feval(fcn,dxtest,varargin{:});
%ARGLIST
%f = feval(fcn,dxtest,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13);
% f = feval(fcn,x0+dx*lambda,P1,P2,P3,P4,P5,P6,P7,P8);
if dispIndx, disp(sprintf('lambda = %10.5g; f = %20.7f',lambda,f )), end
%debug
%disp(sprintf('Improvement too great? f0-f: %g, criterion: %g',f0-f,-(1-THETA)*dfhat*lambda))
if f<fhat
fhat=f;
xhat=dxtest;
lambdahat = lambda;
end
fcount=fcount+1;
shrinkSignal = (~badg & (f0-f < max([-THETA*dfhat*lambda 0]))) | (badg & (f0-f) < 0) ;
growSignal = ~badg & ( (lambda > 0) & (f0-f > -(1-THETA)*dfhat*lambda) );
if shrinkSignal && ( (lambda>lambdaPeak) || (lambda<0) )
if (lambda>0) && ((~shrink) || (lambda/factor <= lambdaPeak))
shrink=1;
factor=factor^.6;
while lambda/factor <= lambdaPeak
factor=factor^.6;
end
%if (abs(lambda)*(factor-1)*dxnorm < MINDX) || (abs(lambda)*(factor-1) < MINLAMB)
if abs(factor-1)<MINDFAC
if abs(lambda)<4
retcode=2;
else
retcode=7;
end
done=1;
end
end
if (lambda<lambdaMax) && (lambda>lambdaPeak)
lambdaMax=lambda;
end
lambda=lambda/factor;
if abs(lambda) < MINLAMB
if (lambda > 0) && (f0 <= fhat)
% try going against gradient, which may be inaccurate
if dispIndx, lambda = -lambda*factor^6, end
else
if lambda < 0
retcode = 6;
else
retcode = 3;
end
done = 1;
end
end
elseif (growSignal && lambda>0) || (shrinkSignal && ((lambda <= lambdaPeak) && (lambda>0)))
if shrink
shrink=0;
factor = factor^.6;
%if ( abs(lambda)*(factor-1)*dxnorm< MINDX ) || ( abs(lambda)*(factor-1)< MINLAMB)
if abs(factor-1)<MINDFAC
if abs(lambda)<4
retcode=4;
else
retcode=7;
end
done=1;
end
end
if ( f<fPeak ) && (lambda>0)
fPeak=f;
lambdaPeak=lambda;
if lambdaMax<=lambdaPeak
lambdaMax=lambdaPeak*factor*factor;
end
end
lambda=lambda*factor;
if abs(lambda) > 1e20;
retcode = 5;
done =1;
end
else
done=1;
if factor < 1.2
retcode=7;
else
retcode=0;
end
end
end
end
if dispIndx, disp(sprintf('Norm of dx %10.5g', dxnorm)), end
This diff is collapsed.
function of = fn_a0freefun(b,Ui,nvar,n0,fss,H0inv)
% of = fn_a0freefun(b,Ui,nvar,n0,fss,H0inv)
%
% Negative logPosterior function for squeesed A0 free parameters, which are b's in the WZ notation
% Note: columns correspond to equations
%
% b: sum(n0)-by-1 vector of A0 free parameters
% Ui: nvar-by-1 cell. In each cell, nvar-by-qi orthonormal basis for the null of the ith
% equation contemporaneous restriction matrix where qi is the number of free parameters.
% With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector
% of total original parameters and bi is a vector of free parameters. When no
% restrictions are imposed, we have Ui = I. There must be at least one free
% parameter left for the ith equation.
% nvar: number of endogeous variables
% n0: nvar-by-1, ith element represents the number of free A0 parameters in ith equation
% fss: nSample-lags (plus ndobs if dummies are included)
% H0inv: cell(nvar,1). In each cell, posterior inverse of covariance inv(H0) for the ith equation,
% resembling old SpH in the exponent term in posterior of A0, but not divided by T yet.
%----------------
% of: objective function (negative logPosterior)
%
% Tao Zha, February 2000
% Copyright (C) 2000-2011 Tao Zha
%
% 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/>.
b=b(:); n0=n0(:);
A0 = zeros(nvar);
n0cum = [0;cumsum(n0)];
tra = 0.0;
for kj = 1:nvar
bj = b(n0cum(kj)+1:n0cum(kj+1));
A0(:,kj) = Ui{kj}*bj;
tra = tra + 0.5*bj'*H0inv{kj}*bj; % negative exponential term
end
[A0l,A0u] = lu(A0);
ada = -fss*sum(log(abs(diag(A0u)))); % negative log determinant of A0 raised to power T
of = ada + tra;
function [g,badg] = fn_a0freegrad(b,Ui,nvar,n0,fss,H0inv)
% [g,badg] = a0freegrad(b,Ui,nvar,n0,fss,H0inv)
% Analytical gradient for a0freefun.m in use of csminwel.m. See Dhrymes's book.
%
% b: sum(n0)-by-1 vector of A0 free parameters
% Ui: nvar-by-1 cell. In each cell, nvar-by-qi orthonormal basis for the null of the ith
% equation contemporaneous restriction matrix where qi is the number of free parameters.
% With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector
% of total original parameters and bi is a vector of free parameters. When no
% restrictions are imposed, we have Ui = I. There must be at least one free
% parameter left for the ith equation.
% nvar: number of endogeous variables
% n0: nvar-by-1, ith element represents the number of free A0 parameters in ith equation
% fss: nSample-lags (plus ndobs if dummies are included)
% H0inv: cell(nvar,1). In each cell, posterior inverse of covariance inv(H0) for the ith equation,
% resembling old SpH in the exponent term in posterior of A0, but not divided by T yet.
%---------------
% g: sum(n0)-by-1 analytical gradient for a0freefun.m
% badg: 0, the value that is used in csminwel.m
%
% Tao Zha, February 2000. Revised, August 2000
% Copyright (C) 2000-2011 Tao Zha
%
% 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/>.
b=b(:); n0 = n0(:);
A0 = zeros(nvar);
n0cum = [0;cumsum(n0)];
g = zeros(n0cum(end),1);
badg = 0;
%*** The derivative of the exponential term w.r.t. each free paramater
for kj = 1:nvar
bj = b(n0cum(kj)+1:n0cum(kj+1));
g(n0cum(kj)+1:n0cum(kj+1)) = H0inv{kj}*bj;
A0(:,kj) = Ui{kj}*bj;
end
B=inv(A0');
%*** Add the derivative of -Tlog|A0| w.r.t. each free paramater
for ki = 1:sum(n0)
n = max(find( (ki-n0cum)>0 )); % note, 1<=n<=nvar
g(ki) = g(ki) - fss*B(:,n)'*Ui{n}(:,ki-n0cum(n));
end
function [Myrqm,nMyrqm] = fn_calyrqm(q_m,Byrqm,Eyrqm)
% [Myrqm,nMyrqm] = fn_calyrqm(q_m,Byrqm,Eyrqm)
%
% Given the beginning and end years and quarters (months), export a matrix of all years and
% quarters (months) for these years and in between
%
% q_m: 4 if quarterly and 12 if monthly
% Byrqm: [year quarter(month)] -- all integers, the begining year and quarter (month)
% Eyrqm: [year quarter(month)] -- all integers, the end year and quarter (month)
%-------------------
% Myrqm: matrix of all years and quarters (months) between and incl. Byrqm and Eyrqm
% nMyrqm: number of data points incl. Byrqm and Eyrqm
%
% Tao Zha, April 2000
% Copyright (C) 2000-2011 Tao Zha
%
% 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/>.
if ~isempty(find(Byrqm-round(Byrqm))) | (q_m-round(q_m)) | ~isempty(find(Byrqm-round(Byrqm)))
error('argin qm, Byrqm, or Eyrqm must of integer')
elseif Byrqm(1)>Eyrqm(1)
error('Eyrqm(1) must be equal to or greater than Byrqm(1)')
elseif Byrqm(1)==Eyrqm(1)
if Byrqm(2)>Eyrqm(2)
error('Eyrqm(2) must be equal to or greater than Byrqm(2) because of the same year')
end
end
Yr = Byrqm(1)+[0:Eyrqm(1)-Byrqm(1)]';
if length(Yr)>=2 % there are years and quarters (months) between Byrqm and Eyrqm
n=length(Yr)-2;
C=zeros(n*q_m,2);
C(:,1) = kron(Yr(2:end-1),ones(q_m,1));
C(:,2) = kron(ones(n,1),[1:q_m]');
%* initialize a matrix of years and quarters (months) including Byrqm and Eyrqm
Myrqm = zeros((q_m-Byrqm(2)+1)+Eyrqm(2)+n*q_m,2);
%* Years in between
n1=q_m-Byrqm(2)+1;
n2=Eyrqm(2);
Myrqm(n1+1:end-n2,:) = C;
%* Beginning year
for k=1:n1
Myrqm(k,:) = [Byrqm(1) Byrqm(2)+k-1];
end
%* End year
for k=1:n2
Myrqm(end-Eyrqm(2)+k,:) = [Eyrqm(1) k];
end
else %* all the data are in the same calendar year
n1=Eyrqm(2)-Byrqm(2)+1;
Myrqm = zeros(n1,2);
for k=1:n1
Myrqm(k,:) = [Byrqm(1) Byrqm(2)+k-1];
end
end
nMyrqm = size(Myrqm,1);
function [xdsube,Brow,Erow] = fn_dataext(Byrqm,Eyrqm,xdatae)
% xdsube = dataext(Byrqm,Eyrqm,xdatae)
% Extract subset of xdatae, given Byrqm and Eyrqm
%
% Byrqm: [year quarter(month)]: beginning year and period. If Byqm(2)=0, we get annual data.
% Eyrqm: [year period]: end year and period. If Byrqm(2)=0, it must be Eyrqm(2)=0.
% xdatae: all data (some of which may be NaN) with the first 2 columns indicating years and periods.
%----------
% xdsube: subset of xdatae.
% Brow: the row number in xdatee that marks the first row of xdsube.
% Erow: the row number in xdatee that marks the last row of xdsube.
%
% Tao Zha, April 2000
% Copyright (C) 2000-2011 Tao Zha
%
% 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/>.
if (Byrqm(2)==0) && (Eyrqm(2)~=0)
error('If annual data, make sure both Byrqm(2) and Eyrqm(2) are zero')
end
Brow = min(find(xdatae(:,1)==Byrqm(1)));
if isempty(Brow)
error('Byrqm is outside the date range indicated by xdatae(:,1:2)!')
end
if Byrqm(2)>0
nadt=Byrqm(2)-xdatae(Brow,2);
if nadt<0
error('Byrqm is outside the date range indicated by xdatae(:,1:2)!')
end
Brow=Brow+nadt;
end
%
Erow = min(find(xdatae(:,1)==Eyrqm(1)));
if isempty(Erow)
error('Eyrqm is outside the date range indicated by xdatae(:,1:2)!')
end
if Eyrqm(2)>0
nadt2=Eyrqm(2)-xdatae(Erow,2);
if nadt<0
error('Eyrqm is outside the date range indicated by xdatae(:,1:2)!')
end
Erow=Erow+nadt2;
end
%
xdsube = xdatae(Brow:Erow,:); % with dates
function [yactyrge,yactyre,yactqmyge,yactqmge,yactqme] = fn_datana(xdatae,q_m,vlistlog,vlistper,Byrqm,Eyrqm)
% [yactyrge,yactyre,yactqmyge,yactqmge,yactqme] = fn_datana(Byrqm,Eyrqm,xdatae,q_m,vlistlog,vlistper)
%
% Generate prior period, period-to-period-in-last-year, and annual growth rates
% For annual rates, works for both calendar and any annual years, depending on Byrqm and Eyrqm
% If monthly data, we haven't got time to get quarterly growth rates yet.
%
% xdatae: all data (logged levels or interest rates/100, some of which may be NaN) with the first
% 2 columns indicating years and periods.
% q_m: quarter or month period
% vlistlog: sublist for logged variables
% vlistper: sublists for percent variables
% Byrqm: [year quarter(month)]: beginning year and period. If Byqm(2)~=1, we don't get
% calendar annual rates. In other words, the first column of yactyge (which
% indicates years) does not mean calendar years. Byqm(2) must be specified; in other
% words, it must be not set to 0 as in, say, fn_dataext.
% Eyrqm: [year period]: end year and period. Eyqm(2) must be specified; in other words, it
% must be not set to 0 as in, say, fn_dataext.
% NOTE: if no inputs Byrqm and Eyrqm are specified, all growth rates begin at xdatae(1,1:2).
%----------
% yactyrge: annual growth rates with dates in the first 2 columns.
% yactyre: annual average logged level with dates in the 1st 2 columns.
% yactqmyge: period-to-period-in-last-year annual growth rates with dates in the first 2 columns.
% yactqmge: prior-period annualized growth rates with dates in the first 2 columns.
% yactqme: data (logged levels or interest rates/100) with dates in the first 2 columns.
% Same as xdatae but with Brow:Erow.
%
% Tao Zha, April 2000.
% Copyright (C) 2000-2011 Tao Zha
%
% 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/>.
if size(xdatae,1)<2*q_m
error('We need at least two years of xdatae to get annual rates. Check xdatae!!')
end
if nargin==4
Brow=1; Erow=length(xdatae(:,1));
nyr = floor((Erow-Brow+1)/q_m);
yrsg = [xdatae(q_m+1,1):xdatae(q_m+1,1)+nyr-2]'; % for annual growth later on
else
if Byrqm(2)<1 || Eyrqm(2)<1
error('This function requires specifying both years and months (quarters) in Byrqm and Eyrqm')
end
Brow = min(find(xdatae(:,1)==Byrqm(1)));
if isempty(Brow)
error('Byrqm is outside the date range of xdatae(:,1:2)!')
end
nadt=Byrqm(2)-xdatae(Brow,2);
if nadt<0
error('Byrqm is outside the date range indicated by xdatae(:,1:2)!')
end
Brow=Brow+nadt;
%
Erow = min(find(xdatae(:,1)==Eyrqm(1)));
if isempty(Brow)
error('Eyrqm is outside the date range of xdatae(:,1:2)!')
end
nadt2=Eyrqm(2)-xdatae(Erow,2);
if nadt<0
error('Eyrqm is outside the date range indicated by xdatae(:,1:2)!')
end
Erow=Erow+nadt2;
nyr = floor((Erow-Brow+1)/q_m);
yrsg = [Byrqm(1)+1:Byrqm(1)+nyr-1]'; % for annual growth later on, which will
% start at Byrqm(1) instead of Byrqm(1)+1
end
%
yactqme = xdatae(Brow:Erow,:); % with dates
yactqm = yactqme(:,3:end); % only data
%======== prior period change (annaluized rate)
yactqmg = yactqm(2:end,:); % start at second period to get growth rate
yactqmg(:,vlistlog) = (yactqm(2:end,vlistlog) - yactqm(1:end-1,vlistlog)) .* q_m;
% monthly, 12*log(1+growth rate), annualized growth rate
yactqmg(:,vlistlog) = 100*(exp(yactqmg(:,vlistlog))-1);