Hi,
My data is stored in x3 array (also attachesd here). where, x3 (1,:) - input; x3 (2,:) - output. I need to generate pdf from this data. May I know how to generate histogram and pdf from this data.

 Akzeptierte Antwort

the cyclist
the cyclist am 4 Mai 2025

1 Stimme

You can use the histogram function to create and plot a histogram.
You can use the ksdensity function to estimate a non-parametric fit to the probability density function, and then the plot function to plot it.

17 Kommentare

MechenG
MechenG am 4 Mai 2025
Okay, Thanks!. For the histogram, I need to plot the number distribution as function of my input, how to do that?
+1
Here's an example that plots the histogram using pdf normalization along with the fitted pdf curve.
data = pearsrnd(7,1.2,0.9,4,1000,1);
[f,xi] = ksdensity(data);
histogram(data, Normalization='pdf')
hold on
plot(xi,f, 'r-', LineWidth=2)
MechenG
MechenG am 4 Mai 2025
Ok. I sorted out.
I tired as follows, here how to make edges symetric, for e.g. for the attached data edges starts from -4.65 and ends at 4.45, how to make this to be symetric i.e. - 4.65 to 4.65
figure
h=histogram((b(1,:)),edges);
binE=[-5:0.7:5];
[N,edges] = histcounts(b(1,:),binE);
edges = edges(2:end) - (edges(2)-edges(1))/2;
ax = gca;
pdfout=[edges;N];
plot (pdfout(1,:),pdfout(2,:)./sum(pdfout(2,:)),'color','r','LineWidth',1.5)
Torsten
Torsten am 4 Mai 2025
Bearbeitet: Torsten am 4 Mai 2025
If you use
binE = [-a:0.7:a]
and you want the edges to be symmetric, 0.7 must divide a. So a = 4.9 or a = 5.6 are possible options.
MechenG
MechenG am 4 Mai 2025
Thanks!
the cyclist
the cyclist am 4 Mai 2025
I almost forgot ...
There is also the handy histfit function, that will do both a histogram and a fit to a normal distribution. (I did not look at the data you posted, to see if the normality assumption is sensible.)
MechenG
MechenG am 4 Mai 2025
Ok, I will have a look.
Adam Danz
Adam Danz am 4 Mai 2025
Thanks for pointing out the usefulness of histfit @the cyclist! Just a quick note—while the normality assumption is important if you’re specifically fitting a normal distribution, histfit actually supports a variety of distributions via the 3rd argument in histfit(data,nbins,dist), many of which don’t require normality but instead have their own underlying assumptions.
MechenG
MechenG am 7 Mai 2025
Hi,
@the cyclist@Adam Danz For the attached data, I have the discripancy in the distribution between ksdensity and hisfit. Could you please suggest which one I have to use here.
Adam Danz
Adam Danz am 7 Mai 2025
What distribution did you specify in the 3rd argument to histfit?
MechenG
MechenG am 7 Mai 2025

I used normal distribution

MechenG
MechenG am 7 Mai 2025

Just a note. Does ksdensity work only with equally spaced x values. As seen in the attached data, my x values are not equally spaced..

I'm not quite sure what you mean. Your data in the b vector could be anything, as they are just a sample of data that be fit by either method. The fact that they happen to be at discrete values (equally spaced or not) does not matter.
But, surely you notice that your data are not even close to normally distributed. The K-S density fit is not assuming normality, and gets the shape of the data right.
If what you meant was not about the shape, but about the scale, I'm guessing you did not scale the K-S density fit so that it matched the assumptions of histfit. I did that below, too.
% Load the data
load matlab.mat
% Non-parametric fit to the data
[f,x] = ksdensity(b);
% Calculate the normalization that will convert the KS density
% to the same scale as histfit()
dx = x(2) - x(1);
count = numel(b);
% Plot
figure
hold on
histfit(b)
plot(x,f*dx*count,"LineWidth",3)
legend(["data","histfit","ksdensity"])
MechenG
MechenG am 9 Mai 2025
Thanks!, on the ksdensity plot (yellow color), the peak on the right hand side is slightly higher than left side despite the counts being smaller. Is this because the data is denser on the right side than the left side?
You need to read about and understand the method more. The KS curve is going to be incorporating data density from more than one of your discrete values.
How far left and right each point looks is determined by the kernel bandwidth. For example, look what happens when I make it very narrow:
% Load the data
load matlab.mat
% Non-parametric fit to the data
[f,x] = ksdensity(b,"BandWidth",0.3);
% Calculate the normalization that will convert the KS density
% to the same scale as histfit()
dx = x(2) - x(1);
count = numel(b);
% Plot
figure
hold on
histfit(b)
plot(x,f*dx*count,"LineWidth",2)
legend(["data","histfit","ksdensity"])
MechenG
MechenG am 10 Mai 2025
Ok. Now I got it. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by