Debian Codegraph @

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

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