chi2_str
例对1~7阶滞后相关系数进行检验。
function [sacf1,sacf2,sacf3,sacf4,sacf5,sacf6,sacf7]=acf_test(R) % 独立性的自相关AFC检验
R_mean=mean(R);R_var=var(R);n=length(R); for i=1:7
rou(i)=sum(((R(1:n-i).*R(i+1:n)-R_mean^2))/R_var)*sqrt(1/(n-i)); end rou
if abs(rou(1))<1.96 sacf1='pass'; else
sacf1='*'; end
if abs(rou(2))<1.96 sacf2='pass'; else
sacf2='*'; end
R=unifrnd(0,1,1000,2); N=2000 k=6
n=hist3(R,[k k]) % 产生每个小正方形落入的个数 ni=sum(n'); nj=sum(n); nij=ni'*nj;
n_sum=sum(sum(n.^2./nij))-1 chi2_2=N*n_sum
chi2_p=chi2cdf((k-1)^2,chi2_2); if chi2_p<0.95 chi2_str='pass'; else
chi2_str='*'; end chi2_str
例子:
function [striud_u,striud_chi2]=run_ud_test(R); % 升降连检验,包括正态和卡方检验 m1=[0.625 0.275 0.079167 0.020833]; n=length(R);R1=diff(R); AN=[0 0 0 0];
% 搜索并计算总连长 k=1;j=1;i=1;mk(1)=1; while i<=n-2
if R1(i)*R1(i+1)<0 mk(j+1)=i+1;j=j+1; end i=i+1; end
[mkmax,mki]=max(mk); mk=mk(1:mki); mk=diff(mk);
nn=hist(mk,5);
n1=[nn(1:3) sum(nn(4:5))];
Run_tol=sum(nn);m1=m1*Run_tol; % 计算正态和卡方检验检验
u=(3*Run_tol-2*n+4)/sqrt(1.6*n-2.9); chi_3=sum((n1-m1).^2./m1); % 检验
if abs(u)<1.96 striud_u='pass'; else
striud_u='*'; end
if chi2cdf(3,chi_3) < 0.95 striud_chi2='pass'; else
striud_chi2='*'; end
例:
% 扑克牌检验,2005.10.24 clear,clc
R=rand(1,1000); % poke test
n=length(R);kk=8; jj=1;
% 求125组数据的颜色数,并放入矩阵cc中。 for ii=1:kk:n
% 对每组数小数点后第一位取模为8运算 rr=10*R(ii:ii+7); pk=mod(fix(rr),8); % 计算每组的颜色数 pk=sort(pk); j=1; for i=1:7
if pk(i)~=pk(i+1); j=j+1; end end
cc(jj)= j+1;jj=jj+1; % 将颜色数输入到数组cc中 end
% 构造自由度为4的卡方分布,并进行检验。 nn(1)=sum(n_pk(1:3)); nn(2:4)=n_pk(4:6); nn(5)=sum(n_pk(7:8));
m=[0.02 0.17 0.4205 0.3195 0.0697]*n; chi_4=sum((m-nn).^2./m); % 构造统计量 p=chi2cdf(4,chi_4); if p<0.95
str_pk='pass'; else
str_pk='*'; end str_pk
注:随机数发生器(Random Number Generator)
betarnd Random numbers from the beta distribution binornd Random numbers from the binomial distribution
chi2rnd Random numbers from the chi-square (χ2) distribution evrnd Random matrices from the extreme value distribution
exprnd Generate random numbers from the exponential distribution frnd Random numbers from the F distribution
gamrnd Random numbers from the gamma distribution geornd Random numbers from the geometric distribution
gevrnd Random arrays from the generalized extreme value distribution gprnd Random arrays from the generalized Pareto distribution hygernd Random numbers from the hypergeometric distribution iwishrnd Generate inverse Wishart random matrix lhsdesign Generate a latin hypercube sample
lhsnorm Generate a latin hypercube sample with a normal distribution lognrnd Random matrices from the lognormal distribution
mvnrnd Random matrices from the multivariate normal distribution mvtrnd Random matrices from the multivariate t distribution nbinrnd Random matrices from a negative binomial distribution ncfrnd Random matrices from the noncentral F distribution nctrnd Random matrices from noncentral T distribution
ncx2rnd Random matrices from the noncentral chi-square distribution normrnd Generate random numbers from the normal distribution poissrnd Random numbers from the Poisson distribution random Random numbers from a specified distribution randsample Random sample, with or without replacement raylrnd Random matrices from the Rayleigh distribution trnd Random numbers from Student's t distribution
unidrnd Random numbers from the discrete uniform distribution unifrnd Random numbers from the continuous uniform distribution wblrnd Random numbers from the Weibull distribution wishrnd Generate Wishart random matrix 1.4 常用分位数函数 命令 chi2inv(P,V) finv(P,V1,V2) expinv(P, MU) poissinv(P,LMD) 含义 卡方分布,v是自由度 F分布,v1,v2,为自由度 指数分布,MU为参数 泊松分布,LMD为参数 norminv(P,MU,SIGMA) 正态分布 tinv(P,V) 学生分布,v是自由度 unifinv(P,A,B) 区间[A,B]上的均匀分布 举例:
(1)卡方分布的密度和分布图形
x=linspace(0,20,100); p=chi2pdf(x,4); P=chi2cdf(x,4);
subplot(1,2,1),plot(x,p),title('chi2inv') subplot(1,2,2),plot(x,P),title('chi2cdf') chi2inv0.20.180.160.140.120.10.080.060.040.0200510152010.90.80.70.60.50.40.30.20.1005101520chi2cdf (2) 产生卡方随机数,并估计均值和方差
产生1000个自由度为4的卡方随机数,并估计均值和方差。
R=chi2rnd(4,1,1000); % 产生自由度为4的卡方分布随机数 ER=mean(R) % 估计1000个样本的均值 Var=var(R) % 估计1000个样本的方差
(3)卡方检验:
for i=1:1000
R=normrnd(0,1,4,1); KA(i)=R'*R;
end %%以上抽1000个按公式计算的样本 hist(KA,20) %调用直方图命令作图
kstest(KA', [KA' chi2cdf(KA', 4)]) %检验数据是否来自卡方分布
(4)计算卡方下侧概率为0.05和0.95的分位点。
q1=chi2inv(0.05,4) q2=chi2inv(0.95,4)
注:分位数函数(Inverse Cumulative Distribution Function INV)
betainv Inverse of the beta cumulative distribution function
binoinv Inverse of the binomial cumulative distribution function (cdf)
chi2inv Inverse of the chi-square (χ) cumulative distribution function (cdf) evinv Inverse of the extreme value cumulative distribution function expinv Inverse of the exponential cumulative distribution function (cdf) finv Inverse of the F cumulative distribution function (cdf)
gaminv Inverse of the gamma cumulative distribution function (cdf) geoinv Inverse of the geometric cumulative distribution function (cdf)
gevinv Inverse of the generalized extreme value cumulative distribution function gpinv Inverse of the generalized Pareto cumulative distribution function (cdf) hygeinv Inverse of the hypergeometric cumulative distribution function (cdf) icdf Inverse of a specified cumulative distribution function (icdf) logninv Inverse of the lognormal cumulative distribution function (cdf)
nbininv Inverse of the negative binomial cumulative distribution function (cdf) ncfinv Inverse of the noncentral F cumulative distribution function (cdf) nctinv Inverse of the noncentral T cumulative distribution ncx2inv Inverse of the noncentral chi-square cdf
norminv Inverse of the normal cumulative distribution function (cdf) poissinv Inverse of the Poisson cumulative distribution function (cdf) raylinv Inverse of the Rayleigh cumulative distribution function
tinv Inverse of the Student's t cumulative distribution function (cdf) unidinv Inverse of the discrete uniform cumulative distribution function unifinv Inverse continuous uniform cumulative distribution function (cdf) wblinv Inverse of the Weibull cumulative distribution function 1.5 分布的矩
betastat binostat chi2stat evstat expstat fstat gamstat geostat gevstat gpstat hygestat lognstat nbinstat ncfstat nctstat ncx2stat
Mean and variance for the beta distribution
Mean and variance for the binomial distribution
Mean and variance for the chi-square (χ2) distribution Mean and variance of the extreme value distribution Mean and variance for the exponential distribution Mean and variance for the F distribution
Mean and variance for the gamma distribution Mean and variance for the geometric distribution
Mean and variance of the generalized extreme value distribution Mean and variance of the generalized Pareto distribution Mean and variance for the hypergeometric distribution Mean and variance for the lognormal distribution
Mean and variance of the negative binomial distribution Mean and variance of the noncentral F distribution Mean and variance for the noncentral t distribution
Mean and variance for the noncentral chi-square distribution