Computing CDF????
35 次查看(过去 30 天)
显示 更早的评论
Hi all,
Does anyone of you know how to calculate cdf plot using following data sets:
A = [10 10 10 5 3 3 3 2 0]; B = [10 10 10 5 5 3 3 2 2 2 1 0]; how can obtain cdf from A and B?
Best Regards Talha
0 个评论
采纳的回答
Steven Lord
2018-5-30
If you're using release R2014b or later (for histcounts and histogram) or release R2015b or later (for histcounts2 and histogram2) you can specify the 'Normalization' argument with value 'cdf' to display the CDF of the data that you binned using the appropriate function.
Note that this doesn't find parameters for a particular distribution that were determined by fitting the distribution to your data. If you want to do that, take a look at some of the functions on this page in the documentation for Statistics and Machine Learning Toolbox, specifically those whose names end in "fit".
1 个评论
Camilo Malagon Nieto
2018-5-31
Thank you for the update. Then the solution will be:
Given....
A = [10 10 10 5 3 3 3 2 0]; B = [10 10 10 5 5 3 3 2 2 2 1 0]
First merge the data in a new vector
X=[A,B]
The plot the Cumulative distribution histogram
histogram(X,'Normalization','cdf')
更多回答(4 个)
Image Analyst
2011-12-19
Try this code. It's actually just two calls - one to hist() and one to cumsum(), but it's a full-fledged demo with all kinds of fancy plotting and does it for both A and B, so it looks long and complicated but it's actually not. (It only took about 3 minutes to code up this demo). Don't be afraid - just copy, paste, and run:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
A = [10 10 10 5 3 3 3 2 0];
B = [10 10 10 5 5 3 3 2 2 2 1 0];
% Compute the histogram of A and B.
[countsA, binsA] = hist(A);
[countsB, binsB] = hist(B);
% Compute the cumulative distribution function of A and B.
cdfA = cumsum(countsA) / sum(countsA);
cdfB = cumsum(countsB) / sum(countsB);
% Plot the probability distribution of A.
subplot(2,2, 1);
bar(binsA, countsA);
title('Histogram of A', 'FontSize', fontSize);
ylabel('Count A', 'FontSize', fontSize);
xlabel('Values of A', 'FontSize', fontSize);
grid on;
% Plot the probability distribution of B.
subplot(2,2, 2);
bar(binsB, countsB);
title('Histogram of B', 'FontSize', fontSize);
ylabel('Count B', 'FontSize', fontSize);
xlabel('Values of B', 'FontSize', fontSize);
grid on;
% Plot the cumulative distribution function of A.
subplot(2,2, 3);
bar(binsA, cdfA); % You can use plot() if you want to.
title('CDF of A', 'FontSize', fontSize);
grid on;
ylabel('Percentage A (/100)', 'FontSize', fontSize);
xlabel('Values of A', 'FontSize', fontSize);
% Plot the cumulative distribution function of B.
subplot(2,2, 4);
bar(binsB, cdfB); % You can use plot() if you want to.
title('CDF of B', 'FontSize', fontSize);
ylabel('Percentage B (/100)', 'FontSize', fontSize);
xlabel('Values of B', 'FontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
0 个评论
Ssssssss fffgffggg
2017-7-9
how to plot exp cdf with histogram?????
2 个评论
Image Analyst
2017-7-9
This does not answer the original poster's question. To get a CDF and histogram they should call cumsum() and histogram(), not rand(). Anyway, they posted it 6 years ago so it's likely they don't need an answer anymore, despite the fact that they never accepted the answer that was offered.
Camilo Malagon Nieto
2018-5-30
编辑:Camilo Malagon Nieto
2018-5-30
This will be my answer:
There is not a function that automatically takes a data vector and creates a vector with CDF values.
You need first to see your data by plotting it with a histogram
hist(X)
then use the distribution tool to see what distributions may suit your data
disttool
then use the distribution fitting tool to see how that distribution actually fits your data
dfittool
Then create a new distribution Object that fits your data
NewDist=fitdist(X,'Normal');
Than use the new distibution to plot the CDF
cdfplot(NewDist)
if you need values from the CDF you will have to create a values vector and the values to plot and get the CDFs for them
t=0:0,1:10;
Xcdf = cdf(NewDist,t);
Then you can plot the same
plot(t,Xcdf);
1 个评论
Camilo Malagon Nieto
2018-5-30
Worth to notice that the data provided by the question does not have statistical meaning due to the poor amount of samples.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!