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