Histogram from pdf doesn't match histogram from data
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jorge
am 27 Dez. 2016
Kommentiert: Jorge
am 28 Dez. 2016
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');
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 27 Dez. 2016
gmdistribution needs the covariance, which in the case of a single component would be the variance. You are passing it the standard deviation, so pass sigma.^2 instead.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Histograms finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!