Histogram from pdf doesn't match histogram from data
Show older comments
I have generated a vector with normally distributed random numbers (original data). I estimated the pdf by fitting a gaussian. Then, I generated another vector with random data from the pdf (more_data). When I plot the histograms of the original data and the more_data, they are different. Why are they different? Don't they share the same pdf?
This is what I get:

This is my code:
data=[0.5*randn(1,100000)]'; %original data
x=min(data):(max(data)-min(data))/10000:max(data);
%Normalized Histogram of original data
[counts,edges]=histcounts(data,500, 'Normalization', 'pdf');
bw=edges(2)-edges(1);
centers=edges(1:end-1)+bw;
H = bar(centers,counts,'hist');
hold on
%fitting pdf by computing mean and sigma. You can see that the gaussian fits the data.
mu=mean(data);
sigma=std(data);
pdf=normpdf(x,mu,sigma);
plot(x,pdf,'r');
%generate more data from the above pdf.
gm = gmdistribution(mu,sigma,1);
more_data = random(gm,100000);
%Normalized Histogram of more data is different from the original data.
[counts,edges]=histcounts(more_data,500,'Normalization', 'pdf');
bw=edges(2)-edges(1);
centers=edges(1:end-1)+bw;
H_more_data = bar(centers,counts,'hist');
Accepted Answer
More Answers (0)
Categories
Find more on Histograms in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!