MATLAB Answers

histogram normalization not working

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