Histogram from pdf doesn't match histogram from data

5 vues (au cours des 30 derniers jours)
Jorge
Jorge le 27 Déc 2016
Commenté : Jorge le 28 Déc 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');

Réponse acceptée

Walter Roberson
Walter Roberson le 27 Déc 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.
  1 commentaire
Jorge
Jorge le 28 Déc 2016
Thank you, you've been very helpful.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by