Histogram from pdf doesn't match histogram from data
조회 수: 5 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2016년 12월 27일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!