function [h,chi2,chi21]=chi2test0(X,sigma2,a,tail)
%CHI2TEST0 单个正态总体方差的检验(chi2检验)
%参考文献:《应用数理统计》(叶慈南编)p119.
%单个正态总体方差的检验(chi2检验)
%调用格式:[h,chi2,chi21]=chi2test0(X,sigma2,a,tail)
%X为样本,sigma2为检验方差,a为显著性水平
%TAIL must be ''both'', ''right'', or ''left'', or 0, 1, or -1.'
%By:Ji Lin
%Email: linji@live.com
%Blog: http://linji526.spaces.live.com
%Date: 2007,2008/08/18
if nargin tail=0;
end
if nargin a=0.05;
end
n=length(X);
chi2=(n-1)*var(X)/sigma2;
if tail == 0 % two-tailed test
chi21=chi2inv(1-a/2,n-1);chi22=chi2inv(a/2,n-1);
if (chi21 h = '拒绝H0';
else
h = '接受H0';
end
elseif tail == 1 % right one-tailed test
chi21 = chi2inv(1-a,n- 1);
if chi21 h = '拒绝H0';
else
h = '接受H0';
end
elseif tail == -1 % left one-tailed test
chi21 = chi2inv(a,n - 1);
if chi2 h = '拒绝H0';
else
h = '接受H0';
end
else
error('stats:chi2test:BadTail',...
'TAIL must be ''both'', ''right'', or ''left'', or 0, 1, or -1.');
end