Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/errors.m

raw
1 function [vd,str,imf] = errors(Bh,swish,nn)
2 % Computing variance decompositions and impulse functions with
3 % [vd,str,imf] = errors(Bh,swish,nn)
4 % where imf and vd is of the same format as in RATS, that is to say:
5 % Column: nvar responses to 1st shock,
6 % nvar responses to 2nd shock, and so on.
7 % Row: steps of impulse responses.
8 % vd: variance decompositions
9 % str: standard errors of each variable, steps-by-nvar
10 % imf: impulse response functions
11 % Bh is the estimated reduced form coefficient in the form
12 % Y(T*nvar) = XB + U, X: T*k, B: k*nvar. The matrix
13 % form or dimension is the same as "Bh" from the function "sye";
14 % swish is the inv(A0) in the structural model A(L)y(t) = e(t).
15 % nn is the numbers of inputs [nvar,lags,# of impulse responses].
16 %
17 % Copyright (C) 1997-2012 Tao Zha
18 %
19 % This free software: you can redistribute it and/or modify
20 % it under the terms of the GNU General Public License as published by
21 % the Free Software Foundation, either version 3 of the License, or
22 % (at your option) any later version.
23 %
24 % It is distributed in the hope that it will be useful,
25 % but WITHOUT ANY WARRANTY; without even the implied warranty of
26 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 % GNU General Public License for more details.
28 %
29 % If you did not received a copy of the GNU General Public License
30 % with this software, see <http://www.gnu.org/licenses/>.
31 %
32
33 nvar = nn(1);
34 lags = nn(2);
35 imstep = nn(3); % number of steps for impulse responses
36
37 Ah = Bh';
38 % Row: nvar equations
39 % Column: 1st lag (with nvar variables) to lags (with nvar variables) + const = k.
40
41 imf = zeros(imstep,nvar*nvar);
42 vd = imf;
43 % Column: nvar responses to 1st shock, nvar responses to 2nd shock, and so on.
44 % Row: steps of impulse responses.
45 str = zeros(imstep,nvar); % initializing standard errors of each equation
46 M = zeros(nvar*(lags+1),nvar);
47 % Stack lags M's in the order of, e.g., [Mlags, ..., M2,M1;M0]
48 M(1:nvar,:) = swish';
49 Mtem = M(1:nvar,:); % temporary M -- impulse responses.
50 %
51 Mvd = Mtem.^2; % saved for the cumulative sum later
52 Mvds = (sum(Mvd'))';
53 str(1,:) = sqrt(Mvds'); % standard errors of each equation
54 Mvds = Mvds(:,ones(size(Mvds,1),1));
55 Mvdtem = (100.0*Mvd) ./ Mvds; % tempoary Mvd -- variance decompositions
56 % first or initial responses to
57 % one standard deviation shock (or forecast errors).
58 % Row: responses; Column: shocks
59 %
60 % * put in the form of "imf"
61 imf(1,:) = Mtem(:)';
62 vd(1,:) = Mvdtem(:)';
63
64 t = 1;
65 ims1 = min([imstep-1 lags]);
66 while t <= ims1
67 Mtem = Ah(:,1:nvar*t)*M(1:nvar*t,:);
68 % Row: nvar equations, each for the nvar variables at tth lag
69 M(nvar+1:nvar*(t+1),:)=M(1:nvar*t,:);
70 M(1:nvar,:) = Mtem;
71 % ** impulse response functions
72 imf(t+1,:) = Mtem(:)';
73 % stack imf with each step, Row: 6 var to 1st shock, 6 var to 2nd shock, etc.
74 % ** variance decompositions
75 Mvd = Mvd + Mtem.^2; % saved for the cumulative sum later
76 Mvds = (sum(Mvd'))';
77 str(t+1,:) = sqrt(Mvds'); % standard errors of each equation
78 Mvds = Mvds(:,ones(size(Mvds,1),1));
79 Mvdtem = (100.0*Mvd) ./ Mvds; % tempoary Mvd -- variance decompositions
80 vd(t+1,:) = Mvdtem(:)';
81 % stack vd with each step, Row: 6 var to 1st shock, 6 var to 2nd shock, etc.
82 t= t+1;
83 end
84
85 for t = lags+1:imstep-1
86 Mtem = Ah(:,1:nvar*lags)*M(1:nvar*lags,:);
87 % Row: nvar equations, each for the nvar variables at tth lag
88 M(nvar+1:nvar*(t+1),:) = M(1:nvar*t,:);
89 M(1:nvar,:)=Mtem;
90 % ** impulse response functions
91 imf(t+1,:) = Mtem(:)';
92 % stack imf with each step, Row: 6 var to 1st shock, 6 var to 2nd shock, etc.
93 % ** variance decompositions
94 Mvd = Mvd + Mtem.^2; % saved for the cumulative sum later
95 Mvds = (sum(Mvd'))';
96 str(t+1,:) = sqrt(Mvds'); % standard errors of each equation
97 Mvds = Mvds(:,ones(size(Mvds,1),1));
98 Mvdtem = (100.0*Mvd) ./ Mvds; % tempoary Mvd -- variance decompositions
99 vd(t+1,:) = Mvdtem(:)';
100 % stack vd with each step, Row: 6 var to 1st shock, 6 var to 2nd shock, etc.
101 end
102