File: nmlzvar.m

package info (click to toggle)
dynare 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 40,640 kB
  • sloc: fortran: 82,231; cpp: 72,734; ansic: 28,874; pascal: 13,241; sh: 4,300; objc: 3,281; yacc: 2,833; makefile: 1,288; lex: 1,162; python: 162; lisp: 54; xml: 8
file content (121 lines) | stat: -rw-r--r-- 4,944 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function [A0n,nswitch,A0inn] = nmlzvar(A0u,A0xhat,A0inxhat,IndxNmlr,nswitch,A0inu)
% [A0n,nswitch,A0inn] = nmlzvar(A0u,A0xhat,A0inxhat,IndxNmlr,nswitch,A0inu)
%    Export normalized new draw A0 (and inv(A0) only if IndxNmlr(5)) and the number of sign switches
%    Ref.:  D.F. Waggoner and T.A. Zha: "Does Normalization Matter for Inference?"
%    See Note Forecast (2) pp. 52-53
%
% A0u:  unnormalized A0; column--equation
% A0xhat:  ML estimate or posterior mode of A0
% A0inxhat:  inv(A0xhat)
% IndxNmlr: index for which normalization rule to choose
%     Only one of the elments in IndxNmlr can be non-zero
%     IndxNmlr(1): ML A distance rule (supposed to be the best)
%     IndxNmlr(2): ML Ahat distance rule (to approximate IndxNmlr(1))
%     IndxNmlr(3): ML Euclidean distance rule (not invariant to scale)
%     IndxNmlr(4): Positive diagonal rule
%     IndxNmlr(5): Positive inv(A) diagonal rule (if ~IndxNmlr(5), no need for A0inu,
%                                      so we set A0inn=[])
%                  Or Euclidean distance rule for A0inv (not invariant to scale)
%     IndxNmlr(6): Assigned postive rule (such as off-diagonal elements).  Added 1/3/00
% nswitch:  # of sign switches
% A0inu:   unnormalized inv(A0); used only if IndxNmlr(5)
%-----------------
% A0n:  normalized new A0; column--equation
% nswitch:  updated # of sign switches
% A0inn:   normalized inv(A0); used only if IndxNmlr(5)
%
% Written by Tao Zha
% 1/3/00: added IndxNmlr(6) so that previous programs may not be compatible.
%
% Copyright (C) 1997-2012 Tao Zha
%
% This 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.
%
% It 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.
%
% If you did not received a copy of the GNU General Public License
% with this software, see <http://www.gnu.org/licenses/>.
%



A0inn = [];    % no output for normalized A0in unless IndxNmlr(5)

if (length(find(IndxNmlr))>1)
   warning('You cannot choose more than one normalization rule at a time')
   disp('Press ctrl-c to abort')
   pause
elseif isempty(find(IndxNmlr))      % no normalization
   A0n=A0u; nswitch=0;
elseif IndxNmlr(1)
   a0dpindx = find(diag(A0u\A0xhat)<0);
   A0n = A0u;
   if ~isempty(a0dpindx)
      A0n(:,a0dpindx) = -A0u(:,a0dpindx);   % normalized new draws
      nswitch = nswitch + 1;  %<<>> # of sign switches
   end
elseif IndxNmlr(2)
   a0dpindx = find(diag(A0inxhat*A0u)<0);
   A0n = A0u;
   if ~isempty(a0dpindx)
      A0n(:,a0dpindx) = -A0u(:,a0dpindx);   % normalized new draws
      nswitch = nswitch + 1;  %<<>> # of sign switches
   end
elseif IndxNmlr(3)
   Adiff = (A0u - A0xhat).^2;  % distance that may be far from axhat or A0xhat
   Adiffn = (-A0u - A0xhat).^2; % distance by chaning the sign of Atem
   cAdiff = sum(Adiff);    % each column summed up
   cAdiffn = sum(Adiffn);  % each column summed up
   cAindx = find(cAdiffn<cAdiff); % index for shorter distance
   A0n = A0u;
   if ~isempty(cAindx)
      A0n(:,cAindx) = -A0u(:,cAindx); % find the shortest or nearest distance
      nswitch = nswitch + 1;  %<<>> # of sign switches
   end
elseif IndxNmlr(4)
   a0dpindx = find(diag(A0u)<0);
   A0n = A0u;
   if ~isempty(a0dpindx)
      A0n(:,a0dpindx) = -A0u(:,a0dpindx);   % normalized new draws
      nswitch = nswitch + 1;  %<<>> # of sign switches
   end
elseif IndxNmlr(5)
   if (0)
      a0dpindx = find(diag(A0inu)<0);
      A0n = A0u;
      A0inn = A0inu;
      if ~isempty(a0dpindx)
         A0n(:,a0dpindx) = -A0u(:,a0dpindx);
         A0inn(a0dpindx,:) = -A0inu(a0dpindx,:);
         nswitch = nswitch + 1;  %<<>> # of sign switches
      end
   else
      A0n = [];
      Aindiff = (A0inu - A0inxhat).^2;  % distance that may be far from A0inxhat
      Aindiffn = (-A0inu - A0inxhat).^2; % distance by chaning the sign of A0inu
      cAindiff = sum(Aindiff,2);    % each row summed up
      cAindiffn = sum(Aindiffn, 2);  % each row summed up
      cAinindx = find(cAindiffn<cAindiff); % index for shorter distance
      A0inn = A0inu;
      if ~isempty(cAinindx)
         A0inn(:,cAinindx) = -A0inu(:,cAinindx); % find the shortest or nearest distance
         nswitch = nswitch + 1;  %<<>> # of sign switches
      end
   end
elseif IndxNmlr(6)         %*** This one has to be MANUALLY handled
   [jnk,nvar]=size(A0u);
   A0dummy=A0u;
   A0dummy(:,1:2)=-A0u(:,1:2);   % keep it to a sign that coincide with Brooking paper
   a0dpindx = find(A0dummy(nvar,:)<0);   % the last row
   A0n = A0u;
   if ~isempty(a0dpindx)
      A0n(:,a0dpindx) = -A0u(:,a0dpindx);   % normalized new draws
      nswitch = nswitch + 1;  %<<>> # of sign switches
   end
end