The histogram is not wrong, because pdf values can be more than 1. pdf values are normalized such that the total area under the blue bars is 1.0. Total area = sum(bar height x bar width). In this case some heights are greater than 1 because the bars are so narrow.
histogram normalization not working
7 次查看(过去 30 天)
显示 更早的评论
Dear MATLAB experts,
I have a problem normalizing my AR(1) model histogram to pdf. In fact, when I try cdf or probabiltiy it works fine, but pdf option does not try properly.
The code is as follows:
% AROLS function
function [beta, Sig2,Cov]=AROLS(y,p)
T=size(y,1);
Y=y(p+1:T,1);
X=ones(T-p,1);
for j=1:p
X=[X,y(p+1-j:T-j,1)];
end
beta=(X'*X)\(X'*Y);
U=Y-X*beta;
Sig2=(U'*U)/(T-p-1);
Cov=Sig2*inv(X'*X);
end
% AR(1) model histogram
Ns=20;
N=10000;
T=200;
a0=0;
a1=0.7;
beta0=[a0;a1];
Amat=zeros(Ns,2);
for idx=1:Ns
u=randn(N,1);
y=zeros(N,1);
for n=2:N
y(n,1)=a0+a1*y(n-1,1)+u(n,1);
end
y=y(N-T+1:N,1);
p=1;
[beta,Sig2,Cov]=AROLS(y,p);
Amat(idx,:)=((beta-beta0)./sqrt(diag(Cov)))';
end
figure(1)
h=histogram(Amat(:,2),50,'Normalization', 'pdf');
grids=-3:0.1:3;
f=pdf('Normal', grids,0,1);
hold on
plot(grids,f,'-r')
hold off
So when I execute this code, the plot shows like this. But the histogram obviously is wrong because certain bars are over than 1. I'm totaly lost..

0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!