Debian Codegraph @

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

raw
1 function [xp,xpd,xc,w,bw] = histwpdfg(s,nbin,gIdx,w,xlabstr,ylabstr,titstr)
2 % [xp,xpd,xc,w,bw] = histwpdfg(s,nbin,gIdx,w,xlabstr,ylabstr,titstr)
3 % Generate probabilities and plot scaled densitys, given the unsorted draws and weights
4 %
5 % s: ndraws-by-n draws where ndraws is # of draws and n is # of series
6 % nbin: the number of bins (the maximum is ndraws)
7 % gIdx: 1 if plotting the pdf; 0 if no graph
8 % w (optional): ndraws-by-n (unscaled) weights where ndraws is # of draws and n is # of series
9 % xlabstr (optional): xlabel string
10 % ylabstr (optional): ylabel string
11 % titstr (optional): title string
12 %-------------
13 % xp: nbin-by-n: probability (not density) in the centered bin on x-axis.
14 % NOTE: sum(xp) must be 1 for each column
15 % xpd: nbin-by-n: density (not probability) in the centered bin on x-axis.
16 % xc: nbin-by-n: the position of centered bin on x-axis, from top to bottom.
17 % All columns are identical
18 % w: ndraws-by-n scaled weights so that sum(w)=1 for each column
19 % bw: bandwidth
20 %
21 % August 1999 by Tao Zha
22 % July 18 2000. Place w after gIdx so that previous programs need modifications accordingly.
23 %
24 % Copyright (C) 1997-2012 Tao Zha
25 %
26 % This free software: you can redistribute it and/or modify
27 % it under the terms of the GNU General Public License as published by
28 % the Free Software Foundation, either version 3 of the License, or
29 % (at your option) any later version.
30 %
31 % It is distributed in the hope that it will be useful,
32 % but WITHOUT ANY WARRANTY; without even the implied warranty of
33 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 % GNU General Public License for more details.
35 %
36 % If you did not received a copy of the GNU General Public License
37 % with this software, see <http://www.gnu.org/licenses/>.
38 %
39
40 if (nargin<=4), titstr=[]; xlabstr=[]; ylabstr=[]; end
41 [m,n] = size(s);
42 if (nargin<=3)
43 w=ones(m,n)/m;
44 else
45 %** normalized to 1 for the probability
46 wsum = repmat(sum(w), [m 1]);
47 w = w ./ wsum;
48 end
49
50 %if min(size(s))==1, s=s(:); w=w(:); end % making sure they are column vectors
51
52 %*** the position of the center of the bin on the x-axis
53 mins = min(min(s));
54 maxs = max(max(s));
55 bw = (maxs-mins)/nbin; % binwidth for x-axis
56 x = mins + bw*(0:nbin);
57 x(end) = maxs; % in case nbin is not an integer
58 xc = x(1:end-1) + bw/2; % the position of the center of the x-bin
59 xc = xc'; % nbin-by-1 row vector: from top to bottom
60 xc = repmat(xc,[1 n]); % nbin-by-n, same for each column
61
62
63 %*** the probability at each bin on the x-axis
64 nbin = nbin+1; % + 1 to get the difference for getting probability xp
65 nn = zeros(nbin,n);
66 for i=2:nbin
67 for k=1:n
68 xidx = find(s(:,k) <= x(i)); % index for the positions
69 nn(i,k) = sum(w(xidx,k));
70 end
71 end
72 xp = nn(2:nbin,:) - nn(1:nbin-1,:);
73 xpd = xp/bw; % the density, NOT the probability as xp
74
75 if gIdx
76 plot(xc,xpd)
77 title(titstr), xlabel(xlabstr), ylabel(ylabstr);
78 end