ADF检验MATLAB程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ADF-situation1-statistic1-2.m
%ADF检验法p取2.
%AR(2)过程临界值的确定
T=5000;
N=200;
w1=zeros(1,T);
w2=zeros(1,T);
for t=1:T
sita1=0.7;
sita2=0.3;
ru=sita1+sita2;
labuda1=-sita2;
%p=2;
e=randn(1,N);
y1(1)=e(1);
y1(2)=sita1*y1(1)+e(2);
for j=3:N
y1(j)=sita1*y1(j-1)+sita2*y1(j-2)+e(j);
end
dertay1(1)=y1(1);
for i=2:N
dertay1(i)=y1(i)-y1(i-1);
end
%数据的生成过程
y(1)=e(1);
for k=2:N
y(k)=ru*y(k-1)+labuda1*dertay1(k-1)+e(k);
end
dertay(1)=y(1);
for h=2:N
dertay(h)=y(h)-y(h-1);
end
A=[sum(dertay(1,1:N-1).^2) sum(dertay(1,1:N-1).*y(1,1:N-1));
sum(dertay(1,1:N-1).*y(1,1:N-1)) sum(y(1,1:N-1).^2)];
B=[sum(dertay(1,1:N-1).*y(1,2:N)) sum(y(1,1:N-1).*y(1,2:N))]';
C=inv(A)*B;
ruhat=C(2,1);
labuda1hat=C(1,1);
w1(t)=N*(ruhat-1)/(1-labuda1hat);
%样本方差计算SigmaSquare
episilon1(1)=y(1)-arfahat;
for i1=2:N
episilon1(i1)=y(i1)-ruhat*y(i1-1)-labuda1hat*dertay1(i1-1);%Situation1与Situation2再此处不一样。
end
SigmaSquare=(N-3)^(-1)*sum(episilon1(1,1:N).^2);%有三个参数需要估计,所以是N-3。
YitahatSquare=SigmaSquare.*[0 1]*inv(A)*[0 1]';%在计算YitaSquare时采用了《小书》p66结论。
%SigmaSquare/sum(y(1,1:N-1).^2);
%Statistic2
w2(t)=(ruhat-1)/(YitahatSquare^(1/2));
end
a1=sort(w1);
a2=sort(w2);
b1=a1(fix(T*0.05))
b2=a2(fix(T*0.05))
%在备择假设下的检验
for t=1:T
sita1=0.3;
sita2=0.3;
ru=sita1+sita2;
labuda1=-sita2;
%p=2;
e=randn(1,N);
y1(1)=e(1);
y1(2)=sita1*y1(1)+e(2);
for j=3:N
y1(j)=sita1*y1(j-1)+sita2*y1(j-2)+e(j);
end
dertay1(1)=y1(1);
for i=2:N
dertay1(i)=y1(i)-y1(i-1);
end
%数据的生成过程
y(1)=e(1);
for k=2:N
y(k)=ru*y(k-1)+labuda1*dertay1(k-1)+e(k);
end
dertay(1)=y(1);
for h=2:N
dertay(h)=y(h)-y(h-1);
end
A=[sum(dertay(1,1:N-1).^2) sum(dertay(1,1:N-1))
sum(dertay(1,1:N-1).*y(1,1:N-1));
sum(dertay(1,1:N-1)) N sum(y(1,1:N-1));
sum(dertay(1,1:N-1).*y(1,1:N-1)) sum(y(1,1:N-1)) sum(y(1,1:N-1).^2)]; B=[sum(dertay(1,1:N-1).*y(1,2:N)) sum(y(1,1:N))
sum(y(1,1:N-1).*y(1,2:N))]';
C=inv(A)*B;
ruhat=C(3,1);
labuda1hat=C(1,1);
arfahat=C(2,1);
w1(t)=N*(ruhat-1)/(1-labuda1hat);
%样本方差计算SigmaSquare
episilon1(1)=y(1)-arfahat;
for i1=2:N
episilon1(i1)=y(i1)-ruhat*y(i1-1)-labuda1hat*dertay1(i1-1);%Situation1与Situation2再此处不一样。
end
SigmaSquare=(N-3)^(-1)*sum(episilon1(1,1:N).^2);%有三个参数需要估计,所以是