Debian Codegraph @

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

raw
1 function [A0n,nswitch,A0inn] = 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 % Or Euclidean distance rule for A0inv (not invariant to scale)
19 % IndxNmlr(6): Assigned postive rule (such as off-diagonal elements). Added 1/3/00
20 % nswitch: # of sign switches
21 % A0inu: unnormalized inv(A0); used only if IndxNmlr(5)
22 %-----------------
23 % A0n: normalized new A0; column--equation
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 %
30 % Copyright (C) 1997-2012 Tao Zha
31 %
32 % This free software: you can redistribute it and/or modify
33 % it under the terms of the GNU General Public License as published by
34 % the Free Software Foundation, either version 3 of the License, or
35 % (at your option) any later version.
36 %
37 % It is distributed in the hope that it will be useful,
38 % but WITHOUT ANY WARRANTY; without even the implied warranty of
39 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 % GNU General Public License for more details.
41 %
42 % If you did not received a copy of the GNU General Public License
43 % with this software, see <http://www.gnu.org/licenses/>.
44 %
45
46
47
48 A0inn = []; % no output for normalized A0in unless IndxNmlr(5)
49
50 if (length(find(IndxNmlr))>1)
51 warning('You cannot choose more than one normalization rule at a time')
52 disp('Press ctrl-c to abort')
53 pause
54 elseif isempty(find(IndxNmlr)) % no normalization
55 A0n=A0u; nswitch=0;
56 elseif IndxNmlr(1)
57 a0dpindx = find(diag(A0u\A0xhat)<0);
58 A0n = A0u;
59 if ~isempty(a0dpindx)
60 A0n(:,a0dpindx) = -A0u(:,a0dpindx); % normalized new draws
61 nswitch = nswitch + 1; %<<>> # of sign switches
62 end
63 elseif IndxNmlr(2)
64 a0dpindx = find(diag(A0inxhat*A0u)<0);
65 A0n = A0u;
66 if ~isempty(a0dpindx)
67 A0n(:,a0dpindx) = -A0u(:,a0dpindx); % normalized new draws
68 nswitch = nswitch + 1; %<<>> # of sign switches
69 end
70 elseif IndxNmlr(3)
71 Adiff = (A0u - A0xhat).^2; % distance that may be far from axhat or A0xhat
72 Adiffn = (-A0u - A0xhat).^2; % distance by chaning the sign of Atem
73 cAdiff = sum(Adiff); % each column summed up
74 cAdiffn = sum(Adiffn); % each column summed up
75 cAindx = find(cAdiffn<cAdiff); % index for shorter distance
76 A0n = A0u;
77 if ~isempty(cAindx)
78 A0n(:,cAindx) = -A0u(:,cAindx); % find the shortest or nearest distance
79 nswitch = nswitch + 1; %<<>> # of sign switches
80 end
81 elseif IndxNmlr(4)
82 a0dpindx = find(diag(A0u)<0);
83 A0n = A0u;
84 if ~isempty(a0dpindx)
85 A0n(:,a0dpindx) = -A0u(:,a0dpindx); % normalized new draws
86 nswitch = nswitch + 1; %<<>> # of sign switches
87 end
88 elseif IndxNmlr(5)
89 if (0)
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 else
99 A0n = [];
100 Aindiff = (A0inu - A0inxhat).^2; % distance that may be far from A0inxhat
101 Aindiffn = (-A0inu - A0inxhat).^2; % distance by chaning the sign of A0inu
102 cAindiff = sum(Aindiff,2); % each row summed up
103 cAindiffn = sum(Aindiffn, 2); % each row summed up
104 cAinindx = find(cAindiffn<cAindiff); % index for shorter distance
105 A0inn = A0inu;
106 if ~isempty(cAinindx)
107 A0inn(:,cAinindx) = -A0inu(:,cAinindx); % find the shortest or nearest distance
108 nswitch = nswitch + 1; %<<>> # of sign switches
109 end
110 end
111 elseif IndxNmlr(6) %*** This one has to be MANUALLY handled
112 [jnk,nvar]=size(A0u);
113 A0dummy=A0u;
114 A0dummy(:,1:2)=-A0u(:,1:2); % keep it to a sign that coincide with Brooking paper
115 a0dpindx = find(A0dummy(nvar,:)<0); % the last row
116 A0n = A0u;
117 if ~isempty(a0dpindx)
118 A0n(:,a0dpindx) = -A0u(:,a0dpindx); % normalized new draws
119 nswitch = nswitch + 1; %<<>> # of sign switches
120 end
121 end