MATLAB Answers

histogram normalization not working

조회 수: 30(최근 30일)
인식 장
인식 장 8 Apr 2021
답변: Jeff Miller 9 Apr 2021
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..

답변(1개)

Jeff Miller
Jeff Miller 9 Apr 2021
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.

Community Treasure Hunt

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

Start Hunting!

Translated by