%LMcs100302_4th.m
% Benford's law p 609-611 in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu 3/14/11
O=[111 60 46 29 26 22 21 20 20]';
BP=zeros(9,1);
for i=1:9
    BP(i)=log10(i+1)-log10(i);
end
% or the same (thanks Wikipedia for equation)
i=[1:9]';BP=log10(1+1./i)
N=sum(O);
E=BP*N;
Table100302=[[1:9]' O E (O-E).^2./E]
CHI2=sum((O-E).^2./E)
df=length(E)-1;
alpha=0.05
pvalue=1-chi2cdf(CHI2,df)
criticalval=chi2inv(1-alpha,df)
if CHI2>=criticalval
    disp('Reject Ho')
else
    disp('Fail to Reject Ho')
end