Debian Codegraph @

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

raw
1 function [xpd,xc,xp,w,bw] = fn_histwpdfg(s,nbin,gIdx,w,xlabstr,ylabstr,titstr)
2 % [xpd,xc,xp,w,bw] = fn_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 % xpd: nbin-by-n: density or pdf (not probability) in the centered bin on x-axis.
14 % xc: nbin-by-n: the position of centered bin on x-axis, from top to bottom.
15 % All columns are identical
16 % xp: nbin-by-n: probability (not density) in the centered bin on x-axis.
17 % NOTE: sum(xp) must be 1 for each column
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 % Oct 1 2000. Change the order of xpd, xc, and xp so that previous programs need modifications accordingly.
24 %
25 % Copyright (C) 1997-2012 Tao Zha
26 %
27 % This free software: you can redistribute it and/or modify
28 % it under the terms of the GNU General Public License as published by
29 % the Free Software Foundation, either version 3 of the License, or
30 % (at your option) any later version.
31 %
32 % It is distributed in the hope that it will be useful,
33 % but WITHOUT ANY WARRANTY; without even the implied warranty of
34 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 % GNU General Public License for more details.
36 %
37 % If you did not received a copy of the GNU General Public License
38 % with this software, see <http://www.gnu.org/licenses/>.
39 %
40
41 if (nargin<=4), titstr=[]; xlabstr=[]; ylabstr=[]; end
42 [m,n] = size(s);
43 if (nargin<=3)
44 w=ones(m,n)/m;
45 else
46 %** normalized to 1 for the probability
47 wsum = repmat(sum(w), [m 1]);
48 w = w ./ wsum;
49 end
50
51 %if min(size(s))==1, s=s(:); w=w(:); end % making sure they are column vectors
52
53 %*** the position of the center of the bin on the x-axis
54 mins = min(min(s));
55 maxs = max(max(s));
56 bw = (maxs-mins)/nbin; % binwidth for x-axis
57 x = mins + bw*(0:nbin);
58 x(end) = maxs; % in case nbin is not an integer
59 xc = x(1:end-1) + bw/2; % the position of the center of the x-bin
60 xc = xc'; % nbin-by-1 row vector: from top to bottom
61 xc = repmat(xc,[1 n]); % nbin-by-n, same for each column
62
63
64 %*** the probability at each bin on the x-axis
65 nbin = nbin+1; % + 1 to get the difference for getting probability xp
66 nn = zeros(nbin,n);
67 for i=2:nbin
68 for k=1:n
69 xidx = find(s(:,k) <= x(i)); % index for the positions
70 nn(i,k) = sum(w(xidx,k));
71 end
72 end
73 xp = nn(2:nbin,:) - nn(1:nbin-1,:);
74 if (bw<eps)
75 xpd=Inf*ones(size(xp));
76 else
77 xpd = xp/bw; % the density, NOT the probability as xp
78 end
79
80 if gIdx
81 plot(xc,xpd)
82 %set(gca,'XLim',[-5 5]) % put the limit only on the y-axis
83 title(titstr), xlabel(xlabstr), ylabel(ylabstr);
84 end