Debian Codegraph @

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

raw
1 function [A0n,a0dpindx,nswitch,A0inn] = fn_nmlzvar(A0u,A0xhat,A0inxhat,IndxNmlr,nswitch,A0inu)
2 % [A0n,nswitch,A0inn] = nmlzvar(A0u,A0xhat,A0inxhat,IndxNmlr,nswitch,A0inu)
3 % Export normalized new draw A0 (and inv(A0) only if IndxNmlr(5)) and the number of sign switches
4 % Ref.: D.F. Waggoner and T.A. Zha: "Does Normalization Matter for Inference?"
5 % See Note Forecast (2) pp. 52-53
6 %
7 % A0u: unnormalized A0; column--equation
8 % A0xhat: ML estimate or posterior mode of A0
9 % A0inxhat: inv(A0xhat)
10 % IndxNmlr: index for which normalization rule to choose
11 % Only one of the elments in IndxNmlr can be non-zero
12 % IndxNmlr(1): ML A distance rule (supposed to be the best)
13 % IndxNmlr(2): ML Ahat distance rule (to approximate IndxNmlr(1))
14 % IndxNmlr(3): ML Euclidean distance rule (not invariant to scale)
15 % IndxNmlr(4): Positive diagonal rule
16 % IndxNmlr(5): Positive inv(A) diagonal rule (if ~IndxNmlr(5), no need for A0inu,
17 % so we set A0inn=[])
18 % IndxNmlr(6): Assigned postive rule (such as off-diagonal elements). Added 1/3/00
19 % nswitch: # of sign switches
20 % A0inu: unnormalized inv(A0); used only if IndxNmlr(5)
21 %-----------------
22 % A0n: normalized new A0; column--equation
23 % a0dpindx: Index of the columns in A0 or A+ whose signs need to be switched.
24 % nswitch: updated # of sign switches
25 % A0inn: normalized inv(A0); used only if IndxNmlr(5)
26 %
27 % Written by Tao Zha
28 % 1/3/00: added IndxNmlr(6) so that previous programs may not be compatible.
29 % 10/11/01: added a0dpindx in front of nswitch as an output argument so that previous programs may not be compatible.
30
31 %
32 % Copyright (C) 1997-2012 Tao Zha
33 %
34 % This free software: you can redistribute it and/or modify
35 % it under the terms of the GNU General Public License as published by
36 % the Free Software Foundation, either version 3 of the License, or
37 % (at your option) any later version.
38 %
39 % It is distributed in the hope that it will be useful,
40 % but WITHOUT ANY WARRANTY; without even the implied warranty of
41 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 % GNU General Public License for more details.
43 %
44 % If you did not received a copy of the GNU General Public License
45 % with this software, see <http://www.gnu.org/licenses/>.
46 %
47
48
49 A0inn = []; % no output for normalized A0in unless IndxNmlr(5)
50
51 if (length(find(IndxNmlr))>1)
52 warning('You cannot choose more than one normalization rule at a time')
53 disp('Press ctrl-c to abort')
54 pause
55 elseif isempty(find(IndxNmlr)) % no normalization
56 A0n=A0u; nswitch=0;
57 elseif IndxNmlr(1)
58 a0dpindx = find(diag(A0u\A0xhat)<0);
59 A0n = A0u;
60 if ~isempty(a0dpindx)
61 A0n(:,a0dpindx) = -A0u(:,a0dpindx); % normalized new draws
62 nswitch = nswitch + 1; %<<>> # of sign switches
63 end
64 elseif IndxNmlr(2)
65 a0dpindx = find(diag(A0inxhat*A0u)<0);
66 A0n = A0u;
67 if ~isempty(a0dpindx)
68 A0n(:,a0dpindx) = -A0u(:,a0dpindx); % normalized new draws
69 nswitch = nswitch + 1; %<<>> # of sign switches
70 end
71 elseif IndxNmlr(3)
72 Adiff = (A0u - A0xhat).^2; % distance that may be far from axhat or A0xhat
73 Adiffn = (-A0u - A0xhat).^2; % distance by chaning the sign of Atem
74 cAdiff = sum(Adiff); % each column summed up
75 cAdiffn = sum(Adiffn); % each column summed up
76 cAindx = find(cAdiffn<cAdiff); % index for shorter distance
77 A0n = A0u;
78 if ~isempty(cAindx)
79 A0n(:,cAindx) = -A0u(:,cAindx); % find the shortest or nearest distance
80 nswitch = nswitch + 1; %<<>> # of sign switches
81 end
82 elseif IndxNmlr(4)
83 a0dpindx = find(diag(A0u)<0);
84 A0n = A0u;
85 if ~isempty(a0dpindx)
86 A0n(:,a0dpindx) = -A0u(:,a0dpindx); % normalized new draws
87 nswitch = nswitch + 1; %<<>> # of sign switches
88 end
89 elseif IndxNmlr(5)
90 a0dpindx = find(diag(A0inu)<0);
91 A0n = A0u;
92 A0inn = A0inu;
93 if ~isempty(a0dpindx)
94 A0n(:,a0dpindx) = -A0u(:,a0dpindx);
95 A0inn(a0dpindx,:) = -A0inu(a0dpindx,:);
96 nswitch = nswitch + 1; %<<>> # of sign switches
97 end
98 elseif IndxNmlr(6) %*** This one has to be MANUALLY handled
99 [jnk,nvar]=size(A0u);
100 A0dummy=A0u;
101 A0dummy(:,1:2)=-A0u(:,1:2); % keep it to a sign that coincide with Brooking paper
102 a0dpindx = find(A0dummy(nvar,:)<0); % the last row
103 A0n = A0u;
104 if ~isempty(a0dpindx)
105 A0n(:,a0dpindx) = -A0u(:,a0dpindx); % normalized new draws
106 nswitch = nswitch + 1; %<<>> # of sign switches
107 end
108 end