| 1 |
function Q = SRestrictRWZalg(A0hatinv,Bhat,nvar,lags,irs) |
| 2 |
% Rubio-Waggoner-Zha (RWZ) method of sign restrictions. For related methods, see Canova, Faust, and Uhlig. |
| 3 |
% The detailed theoretical foundation of this algorithm can be found in Theorem 3 of Rubio, Waggoner, and Zha (RWZ)'s article "Regime Changes in the Euro Area." |
| 4 |
% Other M functions called by this function can be downloaded by clicking on Archived Matlab Library ZhaZippedCode on http://home.earthlink.net/~tzha02/programCode.html |
| 5 |
% Strcutural VAR form: Y*A0hat = X*Aphat + E, X where Y is T-by-nvar, A0hat is nvar-by-nvar, X is T-by-k (including the constant term and all other exogenous terms), |
| 6 |
% Aphat is k-by-nvar, and E is T-by-nvar. Rows are in the order of 1st lag (with nvar variables) to lags (with nvar variables) plus the exogenous terms. |
| 7 |
% Note that columns of A0hat or Aphat correspond to equations. |
| 8 |
% Inputs: |
| 9 |
% A0hatinv = inv(A0hat). |
| 10 |
% Bhat = Aphat*inv(A0hat). |
| 11 |
% nvar = number of endogenous variables. |
| 12 |
% lags = lag length. |
| 13 |
% irs = maximum number of periods in which sign restrictions are imposed. |
| 14 |
% Outputs: |
| 15 |
% Q: orthogonal rotation matrix so that Q*A0hatinv or A0hat*Q' gives impulse responses that will satisfy |
| 16 |
% sign restrictions of Canova, Faust, and Uhlig. |
| 17 |
% |
| 18 |
% Modified Nov 2004 by T. Zha to |
| 19 |
% (1) correct the existing bugs; |
| 20 |
% (2) make the signs explicit to avoid the normalization problem when computing error bands; |
| 21 |
% (3) construct efficient way (i.e., earlier exit) to make all restrictions satisfied; |
| 22 |
% (4) construct efficient way to normalize. |
| 23 |
% In this example, we have |
| 24 |
% Variables are in the following order: 1: y; 2: P; 3: R; 4: M3; 5: Exec (per $). |
| 25 |
% Shocks are in the following order: 1: AS; 2: AD; 3: MP; 4: MD; 5: Exec. |
| 26 |
% |
| 27 |
% |
| 28 |
% Copyright (C) 1997-2012 Juan Rubio-Ramirez, Daniel Waggoner and Tao Zha |
| 29 |
% |
| 30 |
% This free software: you can redistribute it and/or modify |
| 31 |
% it under the terms of the GNU General Public License as published by |
| 32 |
% the Free Software Foundation, either version 3 of the License, or |
| 33 |
% (at your option) any later version. |
| 34 |
% |
| 35 |
% It is distributed in the hope that it will be useful, |
| 36 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 |
% GNU General Public License for more details. |
| 39 |
% |
| 40 |
% If you did not received a copy of the GNU General Public License |
| 41 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 42 |
% |
| 43 |
|
| 44 |
|
| 45 |
nn = [nvar lags irs]; |
| 46 |
control=0; |
| 47 |
|
| 48 |
|
| 49 |
Q=eye(nvar); |
| 50 |
|
| 51 |
while control==0 |
| 52 |
newmatrix=normrnd(0,1,nvar,nvar); |
| 53 |
[Q,R]=qr(newmatrix); |
| 54 |
for i=1:nvar; |
| 55 |
if R(i,i)<0 |
| 56 |
Q(:,i)=-Q(:,i); |
| 57 |
end |
| 58 |
end |
| 59 |
imfhat = fn_impulse(Bhat,Q*A0hatinv,nn); |
| 60 |
%In the form that is congenial to RATS |
| 61 |
imf3hat=reshape(imfhat,size(imfhat,1),nvar,nvar); |
| 62 |
%imf3hat: row--steps, column--nvar responses, 3rd dimension--nvar shocks |
| 63 |
|
| 64 |
%=== Responses to a moneaty policy (MP) shock. |
| 65 |
% R>0, M<0, y<0, and P<0 for the irs periods. |
| 66 |
a = (imf3hat(1:irs,3,3) > 0) .* (imf3hat(1:irs,4,3) < 0) .* (imf3hat(1:irs,1,3) < 0) .* (imf3hat(1:irs,2,3) < 0); |
| 67 |
if (max(a)==0) |
| 68 |
%--- Swiching the sign of the shock. |
| 69 |
am = (imf3hat(1:irs,3,3) < 0) .* (imf3hat(1:irs,4,3) > 0) .* (imf3hat(1:irs,1,3) > 0) .* (imf3hat(1:irs,2,3) > 0); |
| 70 |
if (min(am)==0) |
| 71 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 72 |
else |
| 73 |
%--- Normalizing according to the switched sign. |
| 74 |
Q(3,:) = -Q(3,:); |
| 75 |
end |
| 76 |
elseif (min(a)==0) |
| 77 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 78 |
end |
| 79 |
%--- R>0 and M<0 for the irs periods. |
| 80 |
% a = (imf3hat(1:irs,3,3) > 0) .* (imf3hat(1:irs,4,3) < 0); |
| 81 |
% if (max(a)==0) |
| 82 |
% %--- Swiching the sign of the shock. |
| 83 |
% am = (imf3hat(1:irs,3,3) < 0) .* (imf3hat(1:irs,4,3) > 0); |
| 84 |
% if (min(am)==0) |
| 85 |
% continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 86 |
% else |
| 87 |
% %--- Normalizing according to the switched sign. |
| 88 |
% Q(3,:) = -Q(3,:); |
| 89 |
% end |
| 90 |
% elseif (min(a)==0) |
| 91 |
% continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 92 |
% end |
| 93 |
|
| 94 |
%=== Responses to an money demand (MD) shock. |
| 95 |
% R>0 and M>0 for the irs periods. |
| 96 |
a = (imf3hat(1:irs,3,4) > 0) .* (imf3hat(1:irs,4,4) > 0); |
| 97 |
if (max(a)==0) |
| 98 |
%--- Swiching the sign of the shock and normalize. |
| 99 |
am = (imf3hat(1:irs,3,4) < 0) .* (imf3hat(1:irs,4,4) < 0); |
| 100 |
if (min(am)==0) |
| 101 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 102 |
else |
| 103 |
%--- Normalizing according to the switched sign. |
| 104 |
Q(4,:) = -Q(4,:); |
| 105 |
end |
| 106 |
elseif (min(a)==0) |
| 107 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 108 |
end |
| 109 |
|
| 110 |
%=== Responses to an aggregate demand (AD) shock. |
| 111 |
% P>0 and y>0 for the irs periods. |
| 112 |
a = (imf3hat(1:irs,1,2) > 0) .* (imf3hat(1:irs,2,2) > 0); |
| 113 |
if (max(a)==0) |
| 114 |
%--- Swiching the sign of the shock and normalize. |
| 115 |
am = (imf3hat(1:irs,1,2) < 0) .* (imf3hat(1:irs,2,2) < 0); |
| 116 |
if (min(am)==0) |
| 117 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 118 |
else |
| 119 |
%--- Normalizing according to the switched sign. |
| 120 |
Q(2,:) = -Q(2,:); |
| 121 |
end |
| 122 |
elseif (min(a)==0) |
| 123 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 124 |
end |
| 125 |
|
| 126 |
%=== Responses to an aggregate supply (AS) shock. |
| 127 |
% P>0 and y<0 for the irs periods. |
| 128 |
a = (imf3hat(1:irs,1,1) < 0) .* (imf3hat(1:irs,2,1) > 0); |
| 129 |
if (max(a)==0) |
| 130 |
%--- Swiching the sign of the shock and normalize. |
| 131 |
am = (imf3hat(1:irs,1,1) > 0) .* (imf3hat(1:irs,2,1) < 0); |
| 132 |
if (min(am)==0) |
| 133 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 134 |
else |
| 135 |
%--- Normalizing according to the switched sign. |
| 136 |
Q(1,:) = -Q(1,:); |
| 137 |
end |
| 138 |
elseif (min(a)==0) |
| 139 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 140 |
end |
| 141 |
|
| 142 |
%=== Responses to an exchange-rate shock (depreciation ==> exports >0 ==> y>0); |
| 143 |
% Ex>0 and y>0 for the irs periods. |
| 144 |
a= (imf3hat(1:irs,1,5) > 0) .* (imf3hat(1:irs,5,5) > 0); |
| 145 |
if (max(a)==0) |
| 146 |
%--- Swiching the sign of the shock and normalize. |
| 147 |
am = (imf3hat(1:irs,1,5) < 0) .* (imf3hat(1:irs,5,5) < 0); |
| 148 |
if (min(am)==0) |
| 149 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 150 |
else |
| 151 |
%--- Normalizing according to the switched sign. |
| 152 |
Q(5,:) = -Q(5,:); |
| 153 |
end |
| 154 |
elseif (min(a)==0) |
| 155 |
continue; %The restrictions are not satisfied. Go the beginning to redraw. |
| 156 |
end |
| 157 |
|
| 158 |
%--- Terminating condition: all restrictions are satisfied. |
| 159 |
control=1; |
| 160 |
end |