Debian Codegraph @

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

raw
1 function [imfpdf,imfpo,imfprob] = histpdfcnt(imfcnt,ndrawscnt,forep,shockp,nvar,ninv,invc,Am,...
2 ixdeng,findex,sindex,vindex,idxuniscl,lval,hval,ncom,gIndx)
3 % [imfpdf,imfpo,imfprob] = histpdfcnt(imfcnt,ndrawscnt,forep,shockp,nvar,ninv,invc,Am,...
4 % ixdeng,findex,sindex,vindex,idxuniscl,lval,hval,ncom,gIndx)
5 % From already ordered and binned (cnt: count) series (not pdf yet).
6 % Produce (1) the dataset for generating p.d.f., (2) the dataset for
7 % probability (NOT density) at each bin, (3) with option ixdeng=1,
8 % graphs of density functions
9 %
10 % imfcnt: 2+ninv-by-forep*shockp*nvar. Counted logcnt, yhatqgcnt, or yhatCalygcnt.
11 % In the case of impulse responses, forep=imstp and shockp=nvar.
12 % In case of of A0(Avhx), forep=1, shockp=1, nvar=nfp
13 % In case of forecasts, shockp=1
14 % ndrawscnt: a total number of draws used in imfcnt
15 % forep: forecast periods -- 1st dim (must be compatible with findex)
16 % shockp: number of shocks -- 2nd dim (must be compatible with sindex)
17 % nvar: number of responses (variables) -- 3rd dim (must be compatible with vindex)
18 % ninv: the number of small interior intervals for sorting.
19 % invc: 1-by-forep*shockp*nvar. Whole inverval lenghth from min to max for one of
20 % (yhat, yhatqg, or yhatCalyg)
21 % Am: 1-by-forep*shockp*nvar. Range5{i}(:,:,1) is lowest range for for one of
22 % (yhat, yhatqg, or yhatCalyg)
23 % ixdeng: index for density graphs. 1: enable; 0: disenable
24 % findex: index for selected forecast periods, 1st dim (c.f., forep)
25 % sindex: index for selected shocks, 2nd dim (c.f., shockp)
26 % vindex: variable index, 3rd dim (c.f., nvar)
27 % idxuniscl: 1: scale on each graph by using lval and hval; 0: disenable this
28 % lval: (length(findex),length(sindex),length(vindex)); lowest point on the axis;
29 % The number matches a total of findex, sindex, and vindex
30 % hval: (length(findex),length(sindex),length(vindex)); highest point on the axis;
31 % The number matches a total of findex, sindex, and vindex
32 % ncom: number of combined intervals. Large ncom implies large size of the bin.
33 % Must set ncom so that ninv can be divided
34 % gIndx: 1: plot graphs of pdf's; 0: no plot
35 %-----------------
36 % imfpdf: 2+ninv-by-forep*shockp*nvar. Density
37 % imfpo: 2+ninv-by-forep*shockp*nvar. Bin position (x-axis) in relation to imfs3
38 % imfprob: 2+ninv-by-forep*shockp*nvar. Probability (NOT density) at each bin
39 %
40 % 27 August 1998 Tao Zha
41 % Revised, October 1998, March 1999
42 % 3/24/99, added gIndx so that the previous programs may not be compatible.
43 %
44 % Copyright (C) 1997-2012 Tao Zha
45 %
46 % This free software: you can redistribute it and/or modify
47 % it under the terms of the GNU General Public License as published by
48 % the Free Software Foundation, either version 3 of the License, or
49 % (at your option) any later version.
50 %
51 % It is distributed in the hope that it will be useful,
52 % but WITHOUT ANY WARRANTY; without even the implied warranty of
53 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54 % GNU General Public License for more details.
55 %
56 % If you did not received a copy of the GNU General Public License
57 % with this software, see <http://www.gnu.org/licenses/>.
58 %
59
60 invlength = invc ./ ninv;
61 invlengthM = repmat(invlength,[2+ninv,1]);
62 invlengthM([1 2+ninv],:) = 1;
63 % first (-inf, low bound) and last (high bound, +inf), the interval is set
64 % to be 1. Of course, theoretically, the interval length should be set
65 % to infinity, but 1 is large enough compared with invlength.
66 imfprob = imfcnt ./ ndrawscnt; % probability (NOT density)
67 imfpdf = imfprob ./ invlengthM; % density
68
69 imfpo = [1:2+ninv]'; % positions for each forecast
70 imfpo = imfpo - 2; % the first step to put back to original form
71 imfpo = repmat(imfpo,[1,forep*shockp*nvar]);
72 invcM = repmat(invc,[2+ninv,1]);
73 AmM = repmat(Am,[2+ninv,1]);
74 imfpo = ((imfpo .* invcM) ./ ninv) + AmM; % 2+ninv-by-forep*shockp*nvar
75 % the final step to put back to original form of impulse responses
76
77
78 if mod(ninv,ncom)
79 warning('Set ncom so that ninv can be divided')
80 return
81 end
82 %
83 ninv2=ninv/ncom;
84 imfpdfn=zeros(2+ninv2,forep*shockp*nvar); %<<>>
85 imfpon=imfpdfn;
86 %
87 for ik=1:ninv2 % from 2 to 1+ninv2 for imfpdfn and imfpon
88 imfpdfn(1+ik,:) = sum(imfpdf(1+(ik-1)*ncom+1:1+ik*ncom,:))/ncom;
89 imfpon(1+ik,:) = mean(imfpo(1+(ik-1)*ncom+1:1+ik*ncom,:));
90 end
91
92 imfpdf4 = reshape(imfpdfn,2+ninv2,forep,nvar,shockp);
93 % imfpdf3: row--bin numbers, column--steps, 3rd dim--variables(responses), 4rd dime--shocks
94 imfpdf4s = permute(imfpdf4,[1 2 4 3]);
95 % imf3pdfs: permuted so that
96 % row--bin numbers, column--steps, 3rd dim--shocks, 4rd dime--variables (responses)
97 imfpo4 = reshape(imfpon,2+ninv2,forep,nvar,shockp);
98 imfpo4s = permute(imfpo4,[1 2 4 3]);
99
100
101 if gIndx
102 ck1=0;
103 for k1=findex
104 ck1=ck1+1;
105 ck2=0;
106 for k2=sindex
107 ck2=ck2+1;
108 ck3=0;
109 for k3=vindex
110 ck3=ck3+1;
111 %figure
112 if idxuniscl
113 lpos = min(find(imfpo4s(2:1+ninv2,k1,k2,k3)>lval(ck1,ck2,ck3))); % low position
114 hpos = max(find(imfpo4s(2:1+ninv2,k1,k2,k3)<hval(ck1,ck2,ck3))); % high position
115 plot(imfpo4s(lpos+1:hpos+1,k1,k2,k3),imfpdf4s(lpos+1:hpos+1,k1,k2,k3)) %
116 % push everything forward by 1 because lpos and hpos start at 2
117 set(gca,'XLim',[lval(ck1,ck2,ck3) hval(ck1,ck2,ck3)])
118 grid
119 else
120 plot(imfpo4s(2:1+ninv2,k1,k2,k3),imfpdf4s(2:1+ninv2,k1,k2,k3)) %
121 grid
122 end
123 end
124 end
125 end
126 end
127
128 %xlabel('The 1984 U Forecast') %The 1982 GDP Growth Forecast')
129 %set(gca,'XLim',[lval hval])
130 %xact = [7.508 7.508]; %[-2.13 -2.13];
131 %yact = [0 0.5]; %[0 0.5];
132 %set(line(xact,yact),'Linestyle','-')
133 %title('Figure 8')
134 %line([-2 -2],[0 400])