How to create a pdf from the histogram

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.

 Respuesta aceptada

the cyclist
the cyclist el 4 de Mayo de 2025

1 voto

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 comentarios

MechenG
MechenG el 4 de Mayo de 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 el 4 de Mayo de 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 el 4 de Mayo de 2025
Editada: Torsten el 4 de Mayo de 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 el 4 de Mayo de 2025
Thanks!
the cyclist
the cyclist el 4 de Mayo de 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 el 4 de Mayo de 2025
Ok, I will have a look.
Adam Danz
Adam Danz el 4 de Mayo de 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 el 7 de Mayo de 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 el 7 de Mayo de 2025
What distribution did you specify in the 3rd argument to histfit?
MechenG
MechenG el 7 de Mayo de 2025

I used normal distribution

MechenG
MechenG el 7 de Mayo de 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..

the cyclist
the cyclist el 9 de Mayo de 2025
Editada: the cyclist el 9 de Mayo de 2025
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 el 9 de Mayo de 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?
the cyclist
the cyclist el 10 de Mayo de 2025
Editada: the cyclist el 10 de Mayo de 2025
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 el 10 de Mayo de 2025
Ok. Now I got it. Thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 4 de Mayo de 2025

Comentada:

el 10 de Mayo de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by