t分布计算函数:
function t=t_func(y,x)
n=length(x);
b=pinv(x'*x)*x'*y;
beta2=0.9;
e=y-x*b;
se=(e'*e)*pinv(x'*x)./(n-2);
se=se(2,2)^(1/2);
t=(b(2,2)-beta2)./se;
end
T分布检验主函数:
p为最终回报的p值序列
sample_size=[10,14,20,28,40,56,80,113,160,226,320,452,640,905,1280];
p=zeros(15,1);
for i=1:15
len=sample_size(i);
% sample的大小
y=zeros(10000,1);
for t=(1:10000)
x = zeros(len+100,2);
x(:,1)=1;
% 定义向量x的行数
for n=(2:len+100)
x(n,2)=0.9.*x(n-1,2)+1+randn(1,1);
% x的值
end
y(t,1)=t_func(x(100:len+100,:),x(99:len+99,:));
end
p(i,1)=sum(abs(y)>1.96)/10000;
end
函数主体:
p_las为最终回报的p值序列
sample_size=[10,14,20,28,40,56,80,113,160,226,320,452,640,905,1280];
p_las=zeros(15,1);
for i=1:15
len=sample_size(i);
% sample的大小
p=zeros(10000,1);
for t=(1:100)
x = zeros(len+100,2);
x(:,1)=1;
% 定义向量x的行数
for n=(2:len+100)
x(n,2)=0.9.*x(n-1,1)+1+randn(1,1);
% 数列的值
end
OLS=OLS_func(x(100:len+100,:),x(99:len+99,:));
e_res=e_rescale(x(100:len+100,:),x(99:len+99,:));
% 重整残差
boot_p=zeros(399,1);
for boot=(1:399)
c=randperm(numel(e_res));
boot_u=e_res(c(1:len));
% 抽残差
boot_y=zeros(len+1,2);
boot_y(:,1)=1;
for b=(2:len+1)
boot_y(b,2)=0.9.*boot_y(b-1,2)+1+boot_u(b-1);
end
% bootstrap样本
boot_p(boot,1)=t_func(boot_y(2:len+1,:),boot_y(1:len,:));
end
p(t,1)=sum(abs(boot_p)>1.96)/399;
end
p_las(i,1)=sum(p)/10000;
end
其中,残差重整的函数如下
function e_res=e_rescale(y,x)
n=length(x);
b=pinv(x'*x)*x'*y;
%b=Sxy/Sxx;
e=y-x*b;
e_res=(n/(n-2))^(1/2).*e;
e_res=e_res(:,2);
函数主体与(2)一致
sample_size=[10,14,20,28,40,56,80,113,160,226,320,452,640,905,1280];
p_las=zeros(15,1);
for i=1:15
len=sample_size(i);
% sample的大小
p=zeros(10000,1);
for t=(1:10000)
x = zeros(len+100,2);
x(:,1)=1;
% 定义向量x的行数
for n=(2:len+100)
x(n,2)=0.9.*x(n-1,1)+1+randn(1,1);
% 数列的值
end
OLS=OLS_func(x(100:len+100,:),x(99:len+99,:));
e_res=e_rescale3(x(100:len+100,:),x(99:len+99,:));
% 重整残差
boot_p=zeros(399,1);
for boot=(1:399)
c=randperm(numel(e_res));
boot_u=e_res(c(1:len));
% 抽残差
boot_y=zeros(len+1,2);
boot_y(:,1)=1;
for b=(2:len+1)
boot_y(b,2)=0.9.*boot_y(b-1,2)+1+boot_u(b-1);
end
% bootstrap样本
boot_p(boot,1)=t_func(boot_y(2:len+1,:),boot_y(1:len,:));
end
p(t,1)=sum(abs(boot_p)>1.96)/399;
end
p_las(i,1)=sum(p)/10000;
end
重整残差的方法与(2)不同,函数如下,V函数由(0,1)均匀分布随机数转变成。
function e_res=e_rescale3(y,x)
n=length(x);
b=pinv(x'*x)*x'*y;
%b=Sxy/Sxx;
e=y-x*b;
v=rand(1,n);
for i=1:n
if v(i)>(sqrt(5)+1)/(2*sqrt(5))
v(i)=-(sqrt(5)-1)/2;
else
v(i)=(sqrt(5)+1)/2;
end
end
e_res=v'.*e;
e_res=e_res(:,2);