www.gusucode.com > stats 源码程序 matlab案例代码 > stats/CompareHistogramwithKnownProbabilityDistributionFunctionExample.m
%% Compare Histogram with Known Probability Distribution Function % Generate right-censored survival data and compare the histogram from cumulative % distribution function with the known probability distribution function. % Copyright 2015 The MathWorks, Inc. %% % Generate failure times from an exponential distribution with mean failure % time of 15. rng default; % For reproducibility y = exprnd(15,75,1); %% % Generate drop-out times from an exponential distribution with mean failure % time of 30. d = exprnd(30,75,1); %% % Record the minimum of these times as the observed failure times. t = min(y,d); %% % Generate censoring by finding the generated failure times that are greater % than the drop-out times. censored = (y>d); %% % Calculate the empirical cdf and plot a histogram using the empirical cumulative % distribution function. [f,x] = ecdf(t,'censoring',censored); ecdfhist(f,x) h = findobj(gca,'Type','patch'); h.FaceColor = [.8 .8 1]; hold on %% % Superimpose a plot of the known population pdf. xx = 0:.1:max(t); yy = exp(-xx/15)/15; plot(xx,yy,'r-','LineWidth',2) hold off