Debian Codegraph @

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

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