You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to get a cdf plot of my data
4 views (last 30 days)
Show older comments
I have run Flower Pollination Algorithm 100 times and have got readings of fitness values. The values of my fitness are as follows:
fitness=[
4.E+00
0.E+00
0.E+00
0.E+00
4.E+00
0.E+00
0.E+00
5.E-10
4.E+00
0.E+00
2.E-16
0.E+00
0.E+00
4.E+00
0.E+00
0.E+00
2.E-13
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
4.E+00
4.E-27
0.E+00
4.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
2.E-11
1.E-10
0.E+00
0.E+00
0.E+00
0.E+00
2.E-10
1.E-14
0.E+00
7.E-08
2.E-19
0.E+00
0.E+00
0.E+00
6.E-10
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
2.E-07
0.E+00
0.E+00
0.E+00
0.E+00
2.E-08
0.E+00
2.E-07
0.E+00
0.E+00
6.E-06
0.E+00
0.E+00
9.E-14
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
3.E-13
0.E+00
2.E-10
0.E+00
0.E+00
0.E+00
0.E+00
2.E-08
0.E+00
0.E+00
0.E+00
7.E-21
0.E+00
0.E+00
4.E-07
0.E+00
0.E+00
0.E+00
5.E-10
0.E+00
0.E+00
3.E-06
2.E-16
5.E-07
0.E+00 ];
How can I get its CDF plot.
Accepted Answer
Star Strider
on 18 Oct 2020
Edited: Star Strider
on 18 Oct 2020
Try this:
[f,x,flo,fup] = ecdf(fitness);
figure
loglog(x, f)
hold on
plot(x, [flo fup], '--r')
hold off
grid
legend('f', '95% CI', 'Location','SE')
xlabel('x')
ylabel('CDF')
EDIT — (17 Oct 2020 at 3:35)
Added plot figure:
.
19 Comments
Star Strider
on 18 Oct 2020
Sadiq Akbar posted this as an Answer 6 hours earlier —
Thank you very much Star strider for your help. I ran your program, it works very well. I liked it. It seems too good. But I don't understand the output arguments? i.e. what do you mean by f,x,flo,fup. Does x mean my fitness values? Likewise what is meant by f, flo and fup? Further, you have written 95% CI and f inside legend. What do you mean by that.
Moreover, where is the iteration on the graph? i.e if I want to show that this cdf is for 100 runs, then as we show in normal graph that fitness values are on y-axis and iterations are on x-axis, can we show the same here. i.e. if
iterations=1:100;
and fitness is as given before, then can we get a cdf showing both?
Star Strider
on 18 Oct 2020
My pleasure!
‘i.e. what do you mean by f,x,flo,fup.’
They are all explained in the documentaiton for ecdf that I linked to in my Answer.
‘Further, you have written 95% CI and f inside legend. What do you mean by that.’
The ‘95%CI’ lines referred to in the legend are the 95% confidence intervals. This is also explained in the documentation.
‘Moreover, where is the iteration on the graph?’
This is a display of the statistical empirical distribution function. Iterations have no meaning in this context.
‘i.e if I want to show that this cdf is for 100 runs, then as we show in normal graph that fitness values are on y-axis and iterations are on x-axis, can we show the same here. i.e. if
iterations=1:100;
and fitness is as given before, then can we get a cdf showing both?’
If you want to display several cumulative distribtion functions in one plot, it would be possible to do that, either as a 2D or 3D plot. We only have one vector here, so only one plot.
Sadiq Akbar
on 18 Oct 2020
Thank you very much dear Star Strider for your kind step by step explanation. Indded I understood it now. But again i have a question. You have shown the lower and upper bound lines also in the graph? Is it necessary? Because as you told if i want to display several plots in one, and each has its own lower and upper bound lines also, then it will be very congested. Isn't that?
Star Strider
on 18 Oct 2020
My pleasure.
It is not necesary to display the confidence interval lines. I did simply to demonstrate how to calculate them and how to plot them.
If you do not want to plot them, the code changes to:
figure
loglog(x, f)
grid
xlabel('x')
ylabel('CDF')
I eliminated the legend call since there is no need for it if there is only one item depicted on the plot. If you have several lines, you might want to incluide it and refer to each line in it.
Sadiq Akbar
on 18 Oct 2020
Thank you very much dear Star Strider. Now its clear to me. If you don't mind tell me how can I plot histogram, box plot and pdf plot for the same fitness values.
Star Strider
on 18 Oct 2020
My pleasure!
These are all the plots:
[f,x,flo,fup] = ecdf(fitness);
figure
loglog(x, f)
hold on
% plot(x, [flo fup], '--r')
hold off
grid
% legend('f', '95% CI', 'Location','SE')
xlabel('x')
ylabel('CDF')
pdfv = gradient(f) ./ gradient(x);
figure
loglog(x, pdfv)
grid
xlabel('x')
ylabel('PDF')
figure
boxplot(fitness)
grid
Ax = gca;
Q2 = Ax.YLabel;
Ax.YLabel.String = 'fitness';
Ax.YScale = 'log';
Ax.YLim = [1E-15 1E-4];
figure
histogram(log10(fitness),100)
grid
Ax = gca;
xt = Ax.XTick;
Ax.XTickLabel = compose('10^{%d}',xt);
Ax.XLabel.String = 'fitness (Logarithmic Scale)';
Ax.YLabel.String = 'Count';
I doubt that your data match any known defined distribution, and some (such as lognormal are absolutely not applicable because of multiple 0 values). If you are interested in exploring these ideas, using the histfit function is likely the best option, since it automatically displays the results. If you find one that works, and that is appropriate to your data (your data met the assumptions of the distribution), you can then get the parameters using the fitdist function.
Sadiq Akbar
on 18 Oct 2020
Thank you very much dear Star Strider for your consistant help. I ran your program, it works very good. Then I myself converted it for three fitnesses. Then it displays CDF and PDF plots, but 3rd figure window is empty and gives error in the command window as follows:
SWITCH expression must be a scalar or character vector constant.
Error in boxplot>parseArgs (line 569)
switch notch
Error in boxplot (line 256)
= parseArgs(varargin);
Error in AllSourcesFourPlots (line 57)
boxplot(fitness2sn0,fitness3sn0,fitness4sn0)
Further, it doesn't display the histogram plot and its window.
Can you correct it if you don't mind. I am attaching the mat files. My converted code is as follows:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This code displays Four Plots of fitness namely
% (1) CDF Plot
% (2) PDF Plot
% (3) Box Plot
% (4) Histogram
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc
load 2sn0_sorted
fitness2sn0=one; % fitness of 2s
load 3sn0_sorted
fitness3sn0=one; % fitness of 3s
load 4sn0_sorted
fitness4sn0=one; % fitness of 4s
%%%%%%%%%%%%%%%%%%%%%
% (1) CDF Plot of All
%%%%%%%%%%%%%%%%%%%%%
[f2sn0,x2sn0,flo2sn0,fup2sn0] = ecdf(fitness2sn0);
[f3sn0,x3sn0,flo3sn0,fup3sn0] = ecdf(fitness3sn0);
[f4sn0,x4sn0,flo4sn0,fup4sn0] = ecdf(fitness4sn0);
figure
loglog(x2sn0, f2sn0,x3sn0,f3sn0,x4sn0,f4sn0,'linewidth',2)
hold on
% plot(x, [flo fup], '--r')
hold off
grid
% legend('f', '95% CI', 'Location','SE')
xlabel('x')
ylabel('CDF')
legend('2s CDF','3s CDF','4s CDF')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (2) PDF Plot of All
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pdfv2sn0 = gradient(f2sn0) ./ gradient(x2sn0);
pdfv3sn0 = gradient(f3sn0) ./ gradient(x3sn0);
pdfv4sn0 = gradient(f4sn0) ./ gradient(x4sn0);
figure
loglog(x2sn0, pdfv2sn0,x3sn0,pdfv3sn0,x4sn0,pdfv4sn0,'linewidth',2)
grid
xlabel('x')
ylabel('PDF')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (3) Box Plot of All
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
boxplot(fitness2sn0,fitness3sn0,fitness4sn0)
grid
Ax = gca;
Q2 = Ax.YLabel;
Ax.YLabel.String = 'fitness';
Ax.YScale = 'log';
Ax.YLim = [1E-15 1E-4];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (4) Histogram of All
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
histogram(log10(fitness2sn0),100,log10(fitness3sn0),100,log10(fitness4sn0),100)
grid
Ax = gca;
xt = Ax.XTick;
Ax.XTickLabel = compose('10^{%d}',xt);
Ax.XLabel.String = 'fitness (Logarithmic Scale)';
Ax.YLabel.String = 'Count';
Star Strider
on 18 Oct 2020
Try this:
boxplot([fitness2sn0,fitness3sn0,fitness4sn0])
They all appear to be column vectors, so horizontally concatenate them.
Beyond that, I am lost. I have no idea what you are doing.
Sadiq Akbar
on 19 Oct 2020
Thank you very much dear Star Strider. Actually the 1st fitness that I had given in the 1st post was fitness values of one signal. Then when you talked about several graphs, then I took the fitnesses of three signals, and tried the same box plots and histogram for them, but its not displayed. Let me tell you that the fitnesses that I have given in the last code are fitness2sn0, fitness3sn0 and fitness4sn0 are fitnesses of three signals. I want to draw a box plot of all of them as shown in the attachements.
Star Strider
on 19 Oct 2020
My pleasure!
Try this:
figure
boxplot([fitness2sn0; fitness3sn0; fitness4sn0].') % Row Vectors, So Vertically Concatenate Them & Transpose Concatenated Matrix
grid
Ax = gca;
Ax.XLabel.String = 'No. of sources';
Ax.YLabel.String = 'fitness';
Ax.YScale = 'log';
Ax.XTickLabel = compose('%dsn0_sorted',2:4); % Display New ‘XTickLabel’
Ax.YLim = [1E-16 10];
Make appropriate changes to get the result you want.
.
Sadiq Akbar
on 19 Oct 2020
Thank you very much dear Star Strider. It works and I see about exactly like that graph in the attachment. Indeed I put you in a lot of trouble. I beg your parden for that. I woudn't have done that but since I am not too much technical like you people, So I request you people for help. I am thankful to Mathworks too that they have this facility for us. And I am thankful to all of you who help me here. Indeed everyone is very helpful here on this site.
Now my last request if possible is that If I have the same three fitnesses and I want to draw three histograms for them on a single figure simultaneously so that I can see what is going on. I will be very thankful to you, though I am still thankful to you as you did a lot for me like @Ameer Hamza
Star Strider
on 19 Oct 2020
The same general approach should work for the histogram as for the boxplot:
histogram([fitness2sn0; fitness3sn0; fitness4sn0].')
There are several histogram functions, so choose the one that does what you want.
If my Answer helped you solve your problem, please Accept it!
.
Sadiq Akbar
on 19 Oct 2020
Yes I always accept the answer when my task is done. I ran this but it doesn't give me what I want.
Actually I want plots of this type as I am attaching here. As you can see:
In Figure (a), one fitness is there i.e. fitness2sn0 only and fitness values are on x-axis and Runs values are on y-axis. similarly
in figure(b), two fitnesses are there, i.e. fitness2sn0, fitness3sn0 while
in figure (c) three fitnesses are there i.e. fitness2sn0, fitness3sn0 and fitness4sn0.
Further a small graph is also shown as a preview pane.
So please do this last task. You do it for three fitnesses vs Runs, then I will try to do the other two my self onece I understand your code.
Star Strider
on 19 Oct 2020
Use the subplot function.
Sadiq Akbar
on 19 Oct 2020
As I told you I am not too much technical.Please do it.Its last request. Thanks in advance
Sadiq Akbar
on 19 Oct 2020
Sorry these all figures are not on single figure window. Figure (a) is on separate figure. Figure (b) is on separate figure and son on. The details are as I told you above, i.e.
in figure(a), one fitness is there and its fitness vs Runs,
in figure(b), two fitnesses are there i.e fitness2sn0 and fitness3sn0.
in Figure (c), three fitnesses i.e. fitness2sn0, fitness3sn0 and fitness4sn0 are there vs runs. You do figure © for me only. Thanks
Star Strider
on 19 Oct 2020
I have absolutely no idea what those are plotting, since no details are provided. That figure is a subplot plot, with several plots in a single figure, and additional axes within each figure The axes documentation demonstrates how to put smaller axes (and the plot associated with them) inside an existing axes.
Sadiq Akbar
on 19 Oct 2020
Ok dear Star Strider. Thank you very much for your so much help. Doesn't matter. I will float that problem. May be some other brother solves that.
I am indeed very thankful to you and @Ameer Hamza who also has also helped me alot like you. So thank you all. God bless you.
Star Strider
on 19 Oct 2020
As always, my pleasure!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)