% LMcs100401_4th.m
% p 616-617 in
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
obs=[1280 1717 915 167 17];hits=[0:4]';Total=obs*hits;
estP=Total/(4096*4)
expc=binopdf(hits,4,estP)'*4096
d1=sum((obs-expc).^2./expc)
df=length(expc)-1-1;
P = 1-chi2cdf(d1,df);
fprintf(...
   'The chi-square statistic,%6.1f, with %2.0f d.f. has pvalue=%6.4f\n',...
   d1,df,P)
alpha=0.05;
criticalval=chi2inv(1-alpha,df);
fprintf('The critical value for alpha=%4.2f is %5.3f\n',alpha,criticalval)
if d1 >=       criticalval
    disp('Reject null')
else
    disp('Fail to reject null')
end