Debian Codegraph @

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

raw
1 function [xc,yc,z,zn] = histpdfg2D(w,n,ditp,xlabstr,ylabstr)
2 % histpdfg2D(w,n,ditp,xlabstr,ylabstr)
3 % Given 2D draws, generate 2D pdf by scaling 2D histogram
4 %
5 % w: ndraws-by-2 matrix where ndraws is # of draws and 2 variables
6 % n: 1-by-2 vector -- # of bins (integers in general) for both x-axis and y-axis
7 % ditp: number -- degrees of bicubic interpolation
8 % xlabstr: xlabel string; if [], no xlab
9 % ylabstr: ylabel string; if [], no ylab
10 %---------------------------------
11 % xc: the position of centered bin on x-axis, from left to right. All rows are identical
12 % yc: the position of centered bin on y-axis, from top to bottom. All columns are identical
13 % z: size(xc,2)-by-size(yc,1) -- the pdf value in each rectangular cell
14 % zn: size(xc,2)-by-size(yc,1) -- the probability value in each rectangular cell
15 % if nargout==0, plot 2D p.d.f. graphics
16 %
17 % January 1999 by Tao Zha
18 %
19 % Copyright (C) 1997-2012 Tao Zha
20 %
21 % This free software: you can redistribute it and/or modify
22 % it under the terms of the GNU General Public License as published by
23 % the Free Software Foundation, either version 3 of the License, or
24 % (at your option) any later version.
25 %
26 % It is distributed in the hope that it will be useful,
27 % but WITHOUT ANY WARRANTY; without even the implied warranty of
28 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 % GNU General Public License for more details.
30 %
31 % If you did not received a copy of the GNU General Public License
32 % with this software, see <http://www.gnu.org/licenses/>.
33 %
34
35 if (size(w,2)~=2)
36 error('The size of 1st argument must be *-by-2 (i.e., 2 columns)')
37 end
38
39 if (nargin==3), xlabstr=[]; ylabstr=[]; end
40
41 if (length(n(:))~=2)
42 error('2nd argument must have exactly 2 elements')
43 end
44
45 if (min(n)<3)
46 error('2nd argument -- bin size -- must have at least 3 for each axis')
47 end
48
49 ndraws=size(w,1);
50
51 %*** x-axis
52 minx = min(min(w(:,1)));
53 maxx = max(max(w(:,1)));
54 h1 = (maxx-minx)/n(1); % binwidth for x-axis
55 x = minx + h1*(0:n(1));
56 xlen = length(x); % n(1)+1
57 x(xlen) = maxx; % in case n(1) is not an integer
58 xc = x(1:xlen-1) + h1/2; % the position of the center of the x-bin
59 % 1-by-n(1) row vector: from left to right
60 xc = repmat(xc,[n(2) 1]);
61
62 %*** y-axis
63 miny = min(min(w(:,2)));
64 maxy = max(max(w(:,2)));
65 h2 = (maxy-miny)/n(2); % binwidth for y-axis
66 y = miny + h2*(0:n(2));
67 ylen = length(y);
68 y(ylen) = maxy; % in case n(2) is not an integer
69 yc = y(1:ylen-1) + h2/2; % the position of the center of the y-bin
70 yc = yc(:); % n(2)-by-1 column vector: from top to bottom
71 yc = repmat(yc,[1 n(1)]);
72
73
74 zn = zeros(n(2),n(1)); % the reverse of x and y is designed for mesh.
75 % see meshgrid to understand this.
76 tic
77 for draws=1:ndraws
78 k1 = floor((w(draws,1)-minx)/h1)+1;
79 k2 = floor((w(draws,2)-miny)/h2)+1;
80 %
81 % if k1==0
82 % k1=1;
83 % end
84 % if k2==0;
85 % k2=1;
86 % end
87 %
88 if k1>n(1)
89 k1=n(1);
90 end
91 if k2>n(2)
92 k2=n(2)
93 end
94 zn(k2,k1) = zn(k2,k1)+1;
95 end
96 timeloop=toc;
97 disp(['Loop time -- minutes ' num2str(timeloop/60)])
98
99 zn = zn/ndraws; % probability in each rectangular cell
100
101 z=zn/(h1*h2); % converted to the height of the p.d.f.
102
103
104 %*** scaled 2D histogram or p.d.f.
105 if (nargout==0)
106 %figure(1)
107 colormap(zeros(1,3)); % set color to black
108 mesh(xc,yc,z)
109 title('Scaled histogram or p.d.f.')
110 xlabel(xlabstr)
111 ylabel(ylabstr)
112 end
113
114 %*** interpolation
115 if (ditp & nargout==0)
116 [xi,yi]=meshgrid(minx:h1/ditp:maxx,miny:h2/ditp:maxy);
117 zi = interp2(xc,yc,z,xi,yi,'bicubic');
118 figure(30)
119 colormap(zeros(1,3)); % set color to black
120 mesh(xi,yi,zi)
121 title('Interpolated p.d.f.')
122 xlabel(xlabstr)
123 ylabel(ylabstr)
124 end