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.

 Accepted Answer

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 Comments

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)
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)
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.
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.)
Ok, I will have a look.
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.
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.
What distribution did you specify in the 3rd argument to histfit?

I used normal distribution

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"])
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"])
Ok. Now I got it. Thanks.

Sign in to comment.

More Answers (0)

Asked:

on 4 May 2025

Commented:

on 10 May 2025

Community Treasure Hunt

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

Start Hunting!