数据处理一共可以分为三个方面,一是数据的回归分类,而是时间序列数据,三是网络型数据处理。本文将要来讨论一下时间序列的应用。
一.ARMA 模型
Arma是用来讨论时间序列里面回报率的情况,假设t时刻的回报率与t时刻之前的回报率有关。同时,也与之前的误差有关。
这模型就是AR模型和MA模型的结合,非常好理解。我们在matlab中画出序列的ACF图和PACF图来找出具有相关性的时间节点。
(以ANZ公司为例)
超出两条线的部分就是具有相关性的部分,我们在这里就取ARMA(1,1)然后我们做一个LB test,为的是确定之前的return确实与t时刻具有相关性。
求出ARMA的表达式即可。
Matlab 实现:
% Plot series and ACF
figure;subplot(3,1,1);plot(ret_is(:,1));title('ANZ returns');xlim([0,length(ret_is)]);
subplot(3,1,2);autocorr(ret_is(:,1), 25);subplot(3,1,3);parcorr(ret_is(:,1), 20)
% LB test for AR effects on ANZ
[H5, pval5, Qs5, CV5] = lbqtest(ret_is(:,1), 5, 0.05);
[H10, pval10, Qs10, CV10] = lbqtest(ret_is(:,1), 10, 0.05);
[pval5 pval10]
% I choose ARMA(1,1) for ANZ, now fit it and forecast
Mdl=arima(1,0,1);
[EstMdl,EstParamCov,logL,info] = estimate(Mdl,ret_is(:,1));
[ret_f7(1), FMSE] = forecast(EstMdl,1,'Y0',ret_is(:,1));% 1-period forecasts
二.ARCH 模型
Arch模型与之前其他模型最大的不同是他是用来预测方差的。
关于阿尔法的取值,是由SIC和AIC来决定的。
SIC或AIC计算出最小的误差值的点,因此这里可以取阿尔法等于7。
同理,以ANZ为例计算出表达式出来:
算出计算结果后,我们要做两个验证。因为这个模型建立在一个假设条件上,即残差是iid和服从正态分布的(若不是正态分布就要服从t分布)。因此,我们要做两个test,一个是LB test.
LB test 用来检验残差是否服从iid (H0:p1=p2=p3=^=Pn=0 H1:至少有一个不等于0), 当p-value 越大,说明原假设成立,这说明是iid的。
JB test 用来检测残差是否服从正态性(H0:是正态分布的 H1:不是)当P-value越大越好,说明原假设成立,是正态分布的。
而当我们选取残差是t分布的时候,最后做检验的时候也是将残差的分布转化为正态分布来做检验。
Matlab的实现:
LLF=0;aic1=0;sic1=0;b=0.05
for p=1:20
Mdl = garch(0,p);Mdl.Offset=NaN;
[EstMdl,EstParamCov,logL,info] = estimate(Mdl,ANZ,'display','off');
% Adding 'display','off' to the previous command suppresses the estimation
% results from appearing in the output
aic1(p)=-2*logL+2*p;sic1(p)=-2*logL+log(length(ANZ))*p;
end
figure;plot(aic1,'b+-');title('AIC & SIC for ARCH models');hold on;plot(sic1,'r+-');
legend('AIC','SIC');
% Fit ARCH(7) model
Mdl = garch(0,7);Mdl.Offset=NaN;
EstMdl = estimate(Mdl,ANZ);
v7=infer(EstMdl,ANZ);s7=sqrt(v7); %infer the conditional variance and calculate standard deviations
a7 = ANZ-EstMdl.Offset; %calculate innovations
% Plot the innovations, conditional standard deviations and log returns
% above one another
figure;
subplot(3,1,1);
plot(ANZdates(2:end,1),a7);
%xlim([ANZdates(2) ANZdates(end)]); % set range of x-axis
title('ARCH(7) Innovations')
subplot(3,1,2);
plot(ANZdates(2:end,1),s7);
ylim([0,0.05]);
%xlim([BHPdates(2) BHPdates(end)]); % set range of x-axis
title('ARCH(7) Conditional Standard Deviations')
subplot(3,1,3);
plot(ANZdates(2:end,1),ANZ);
%xlim([BHPdates(2) BHPdates(end)]); % set range of x-axis
title('ANZ Log Returns')
figure;plot(ANZdates(2:end,1),ANZ,'c');hold on;
plot(ANZdates(2:end,1),s7)
%xlim([BHPdates(2) BHPdates(end)]); % set range of x-axis
title('Log Returns and ARCH(7) Conditional Standard Deviations')
legend('ANZ returns', 'conditional standard deviations','location','South','Orientation','horizontal' );
a1A=EstMdl.ARCH; % stores the 15 ARCH coefficients for later use
%% Q1(b) Assess the fit of the ARCH model
e7=a7./s7;
figure;subplot(2,1,1);plot(ANZdates(2:end,1),e7);
title('ARCH(7) Standardised Residuals');
subplot(2,1,2);autocorr(e7);
title('ACF of ARCH(7) Standardised Residuals');
figure;subplot(2,1,1);hist(e7,25)
title('Histogram of ARCH(7) Standardised Residuals');
subplot(2,1,2);qqplot(e7);
title('QQ plot ARCH(7) Standardised Residuals');
figure;autocorr(e7.^2);
title('ACF of ARCH(7) Squared Standardised Residuals');
% LB test on standardised residuals
[H, pValue, Qstat, CriticalValue] = lbqtest(e7, [20 25], 0.05, [13 18])
%LB test on squared standardised residuals
[H, pValue, Qstat, CriticalValue] = lbqtest(e7.^2, [20 25], 0.05, [13 18])
% JB test
[skewness(e7) kurtosis(e7)]
[h,p] = jbtest(e7)
三. GARCH 模型
类似于ARCH 模型,但是方差多加了一项,即t时刻之前的方差影响。
同理,先由sic和aic来确定P,q的值,然后开始建模和检验。