comparison m-toolbox/classes/+utils/@math/cdfplot.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % CDFPLOT makes cumulative distribution plot
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % h = cdfplot(y1,[],ops) Plot an empirical cumulative distribution function
5 % against a theoretical cdf.
6 %
7 % h = cdfplot(y1,y2,ops) Plot two empirical cumulative distribution
8 % functions. Cdf for y1 is compared against cdf for y2 with confidence
9 % bounds.
10 %
11 % ops is a cell aray of options
12 % - 'ProbDist' -> theoretical distribution. Available distributions are:
13 % - 'Fdist' -> F cumulative distribution function. In this case the
14 % parameter 'params' should be a vector with distribution degrees of
15 % freedoms [dof1 dof2]
16 % - 'Normdist' -> Normal cumulative distribution function. In this case
17 % the parameter 'params' should be a vector with distribution mean and
18 % standard deviation [mu sigma]
19 % - 'Chi2dist' -> Chi square cumulative distribution function. In this
20 % case the parameter 'params' should be a number indicating
21 % distribution degrees of freedom
22 % - 'GammaDist' -> Gamma distribution. 'params' should contain the
23 % shape and scale parameters
24 % - 'ShapeParam' -> In the case of comparison of a data series with a
25 % theoretical distribution and the data series is composed of correlated
26 % elements. K can be adjusted with a shape parameter in order to recover
27 % test fairness. In such a case the test is performed for K* = Phi *K.
28 % Phi is the corresponding Shape parameter. The shape parameter depends
29 % on the correlations and on the significance value. It does not depend
30 % on data length.
31 % - 'params' -> Probability distribution parameters
32 % - 'conflevel' -> requiered confidence for confidence bounds evaluation.
33 % Default 0.95 (95%)
34 % - 'FontSize' -> Font size for axis. Default 22
35 % - 'LineWidth' -> line width. Default 2
36 % - 'axis' -> set axis properties of the plot. refer to help axis for
37 % further details
38 %
39 % Luigi Ferraioli 10-02-2011
40 %
41 % % $Id: cdfplot.m,v 1.8 2011/07/08 09:45:48 luigi Exp $
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 function h = cdfplot(y1,y2,ops)
44
45 %%% check and set imput options
46 % Default input struct
47 defaultparams = struct(...
48 'ProbDist','Fdist',...
49 'ShapeParam',1,...
50 'params',[1 1],...
51 'conflevel',0.95,...
52 'FontSize',22,...
53 'LineWidth',2,...
54 'axis',[]);
55
56 names = {'ProbDist','ShapeParam','params','conflevel','FontSize','LineWidth','axis'};
57
58 % collecting input and default params
59 if nargin == 3
60 if ~isempty(ops)
61 for jj=1:length(names)
62 if isfield(ops, names(jj))
63 defaultparams.(names{1,jj}) = ops.(names{1,jj});
64 end
65 end
66 end
67 end
68
69 pdist = defaultparams.ProbDist; % check theoretical distribution
70 shp = defaultparams.ShapeParam;
71 dof = defaultparams.params; % distribution parameters
72 conf = defaultparams.conflevel; % confidence level for confidence bounds calculation
73 if conf>1
74 conf = conf/100;
75 end
76 fontsize = defaultparams.FontSize;
77 lwidth = defaultparams.LineWidth;
78 axvect = defaultparams.axis;
79
80
81 %%% check data input
82 if isempty(y2) % do theoretical comparison
83 % get empirical distribution for input data
84 [eCD,ex]=utils.math.ecdf(y1);
85 % switch between input theoretical distributions
86 switch lower(pdist)
87 case 'fdist'
88 CD = utils.math.Fcdf(ex,dof(1),dof(2));
89 case 'normdist'
90 CD = utils.math.Normcdf(ex,dof(1),dof(2));
91 case 'chi2dist'
92 CD = utils.math.Chi2cdf(ex,dof(1));
93 case 'gammadist'
94 CD = gammainc(ex./dof(2),dof(1));
95 end
96 % get confidence levels with Kolmogorow - Smirnov test
97 alp = (1-conf)/2;
98 cVal = utils.math.SKcriticalvalues(numel(ex)*shp,[],alp);
99 % get confidence levels
100 CDu = CD+cVal;
101 CDl = CD-cVal;
102
103 figure;
104 h = stairs(ex,[eCD CD CDu CDl]);
105 grid on
106 xlabel('x','FontSize',fontsize);
107 ylabel('F(x)','FontSize',fontsize);
108 set(h(3:4), 'Color','b', 'LineStyle',':','LineWidth',lwidth);
109 set(h(1), 'Color','r', 'LineStyle','-','LineWidth',lwidth);
110 set(h(2), 'Color','k', 'LineStyle','--','LineWidth',lwidth);
111 legend([h(1),h(2),h(3)],{'eCDF','CDF','Conf. Bounds'});
112 if ~isempty(axvect)
113 axis(axvect);
114 else
115 % get limit for quantiles corresponding to 0 and 0.99 prob
116 xlw = interp1(eCD,ex,0.001,'linear');
117 if isnan(xlw)
118 xlw = min(ex);
119 end
120 xup = interp1(eCD,ex,0.999,'linear');
121 axis([xlw xup 0 1]);
122 end
123
124 else % do empirical comparison
125 % get empirical distribution for input data
126 [eCD1,ex1]=utils.math.ecdf(y1);
127 [eCD2,ex2]=utils.math.ecdf(y2);
128
129 % get confidence levels with Kolmogorow - Smirnov test
130 alp = (1-conf)/2;
131 cVal = utils.math.SKcriticalvalues(numel(ex1),numel(ex2),alp);
132 % get confidence levels
133 CDu = eCD2+cVal;
134 CDl = eCD2-cVal;
135
136 figure;
137 h1 = stairs(ex1,eCD1);
138 grid on
139 hold on
140 h2 = stairs(ex2,[eCD2 CDu CDl]);
141 xlabel('x','FontSize',fontsize);
142 ylabel('F(x)','FontSize',fontsize);
143 set(h2(2:3), 'Color','b', 'LineStyle',':','LineWidth',lwidth);
144 set(h1(1), 'Color','r', 'LineStyle','-','LineWidth',lwidth);
145 set(h2(1), 'Color','k', 'LineStyle','--','LineWidth',lwidth);
146 legend([h1(1),h2(1),h2(2)],{'eCDF1','eCDF2','Conf. Bounds'});
147 if ~isempty(axvect)
148 axis(axvect);
149 else
150 % get limit for quantiles corresponding to 0 and 0.99 prob
151 xlw = interp1(eCD2,ex2,0.001,'linear');
152 if isnan(xlw)
153 xlw = min(ex2);
154 end
155 xup = interp1(eCD2,ex2,0.999,'linear');
156 axis([xlw xup 0 1]);
157 end
158 h = [h1; h2];
159 end
160
161 end