ARMA模型程序求助!
给位前辈,我在网上找到了一段ARMA模型的MATLAB程序,但是里面有错误,我自己尝试了很多次都没有能够调出来,还请给位前辈不吝赐教,帮我看看这个程序如何修改,不胜感激!!!!!x = [5272.814;5319.861;5361.574;5393.343;5386.531;5435.807;5456.541;5484.677;5497.901;...
5443.791;5290.606;5151.626;5180.514;4914.435;4559.751;4703.047;4717.734;4761.688;...
4419.294;4457.944;4417.849;4383.393;4320.767;4672.17;4599.696;4490.721;4552.316;...
4497.127;4568.151;4664.295;4567.026;4527.177;4370.285;4192.533;4238.179;4334.047;...
4299.513;4348.543;4438.265;4335.446;4292.654;4360.986;4300.515;4146.299;4165.878;...
4070.116;3971.257;3962.673;3820.048;3668.897;3761.605;3804.054;3796.576;3626.188;...
3629.619;3606.857;3411.493;3580.146;3472.713;3329.162;3347.882;3446.244;3599.618;...
3612.539;3413.907;3471.743;3492.893;3296.672;3348.353;3291.599;3222.741;3094.668;...
3116.977;3147.793;3278.33;3583.028;3557.749;3474.722;3523.405;3693.106;3761.009;...
3733.503;3579.147;3656.839;3613.494;3626.982;3560.243;3657.432;3637.324;3624.233;...
3604.761;3443.162;3544.186;3485.63;3473.091;3364.544;3375.407;3459.026;3401.437;...
3433.354;3459.044;3436.398;3369.913;3351.645;3329.67;3072.333;3024.24;2957.532;...
2868.8;2874.103;2794.751;2941.115;2748.874;2831.736;2760.417;2803.019;2905.014;...
2901.85;2748.432;2736.103];
figure(1)
plot(x);
%扩充ADF单位根检验
= dfARDTest(x,[],0.05,'T') %Augmented Dickey-Fuller unit root test based on AR model withdrift
z = price2ret(x); %股价序列非平稳,一阶差分z(i) = log(x(i)/x(i-1))/(i-(i-1))
figure(2)
plot(z);
%一元线性回归,求残差residuals
X = z(1:length(z)-1,1);
Y = z(2:length(z),1);
lxx=0;
lxy=0;
for k=1:length(X)
lxx=lxx+(X(k)-mean(X))^2;
lxy=lxy+(X(k)-mean(X))*(Y(k)-mean(Y));
end
b=lxy/lxx;
a=mean(Y)-b*mean(X);
for k = 1:length(X)
Y1(k,1) = a+b*X(k); %估计值
residuals (k,1) = Y(k)-Y1(k); %残差
end
figure(3)
subplot(2,1,1)
autocorr(z); %一阶差分序列z自相关函数图MA(q),观察系数是否在区间(-2T^(1/2),-2T^(1/2))内
subplot(2,1,2)
parcorr(z); %一阶差分序列z偏相关函数图AR(p),观察系数是否在区间(-2T^(1/2),-2T^(1/2))内
%---------------------------------------------------模型定阶或识别-------------------
%扩充ADF单位根检验,检验序列z平稳性
= ... %Augmented Dickey-Fuller unit root test based on AR model withdrift
dfARDTest(z,[],0.05,'T')
%确定ARMA模型阶数,选择相关度大、下降幅度大的阶数作测试,在此选择|ACF|>0.05的阶为q,|PACF|>0.05的阶为p,邻近的阶数也参与测试
u = iddata(z);
test = [];
%p = ; %自回归对应PACF,给定滞后长度上限p和q,一般取为T/10、ln(T)或T^(1/2),这里取T/10=12
%q = ; %移动平均对应ACF
for p = 0:12 %自回归对应PACF,给定滞后长度上限p和q,一般取为T/10、ln(T)或T^(1/2),这里取T/10=12
for q = 0:12 %移动平均对应ACF
m = armax(u,)
AIC = aic(m) %armax(p,q),选择对应FPE最小,AIC值最小的模型
% = lbqtest(z, , 0.05) %Ljung-Box Q-statistic lack-of-fit hypothesis test
test = ;
end
end
for k = 1:size(test,1)
if test(k,3) == min(test(:,3)) %选择AIC值最小的模型
p_test = test(k,1)
q_test = test(k,2)
break;
end
end
%------------------------------------------模型参数估计--------------------------
m = armax(u,) %armax(p,q),对应AIC值最小
AIC = aic(m) %选择AIC值最小的模型
%----------------------------------------这段拟合程序有问题??????????????
uu = iddata([],idinput(size(z,1)));
y = sim(m,uu); %ARIMA拟合
yh = y.OutputData;
%----------------------------------------这段拟合程序有问题??????????????
%-----------------------------------------检验-------------------------------
figure(4)
e = resid(m,u); %残差分析
plot(e);
%检验残差的自相关和偏相关函数
figure(5)
subplot(2,1,1)
autocorr(e.OutputData) %一阶差分序列z自相关函数图MA(q),置信水平0.95
subplot(2,1,2)
parcorr(e.OutputData) %一阶差分序列z偏相关函数图AR(p),置信水平0.95
errors = z-yh; %误差
%errors = pe(m,u);
%perf(i) = mse(errors); %均方误差
%figure(3)
%plot(y);
%compare(data,m,1) %1步预测
figure(6)
subplot(1,2,1)
plot(u);
hold on ;
plot(y,'-.r') ;
hold off
title('拟合效果');
xlabel('采样点');ylabel('收盘价');
legend('实际值','拟合值')
subplot(1,2,2)
bar(errors);
hold off
title('拟合误差');
xlabel('采样点');ylabel('误差');
%---------------------------------------反差分-------------------------------------
figure(7);
plot(ret2price(z,x(1)));
hold on ;
plot(ret2price(yh,x(2)),'-.r') ;
hold off
title('拟合效果');
xlabel('采样点');ylabel('收盘价');
legend('实际值','拟合值')
编辑过程中,出错代码为:
u = iddata(z);
test = [];
%p = ; %自回归对应PACF,给定滞后长度上限p和q,一般取为T/10、ln(T)或T^(1/2),这里取T/10=12
%q = ; %移动平均对应ACF
for p = 0:12 %自回归对应PACF,给定滞后长度上限p和q,一般取为T/10、ln(T)或T^(1/2),这里取T/10=12
for q = 0:12 %移动平均对应ACF
m = armax(u,)
AIC = aic(m) %armax(p,q),选择对应FPE最小,AIC值最小的模型
% = lbqtest(z, , 0.05) %Ljung-Box Q-statistic lack-of-fit hypothesis test
test = ;
end
end
出错提示为:
??? Error using ==> armax at 76
There are no free parameters in the model to estimate.
Error in ==> Untitled at 54
m = armax(u,)
[ 本帖最后由 wuygoon 于 2010-4-8 00:04 编辑 ]
页:
[1]