【时间序列】简单garch+arma模型,金融时间序列

matlab->garch+arma模型,金融时间序列模型

  • 1. 原理
    • 1.1 代码参考
  • 2. 代码脚本
    • 2.1 数据处理
    • 2.2 ARMA
    • 2.3 garch

1. 原理

在网上看到zhihu上有个很好的专栏在这里码一下:

  • 基础篇:https://zhuanlan.zhihu.com/p/38320827
  • 初级篇:https://zhuanlan.zhihu.com/p/38321845
  • 进阶篇:https://zhuanlan.zhihu.com/p/38322333
  • 应用篇:https://zhuanlan.zhihu.com/p/38322638
  • 补完篇:https://zhuanlan.zhihu.com/p/77307871

1.1 代码参考

使用ARMA做时间序列预测全流程:https://zhuanlan.zhihu.com/p/69630638

matlab官网的economic 工具箱的实例:
https://ww2.mathworks.cn/discovery/garch-models.html
https://ww2.mathworks.cn/help/releases/R2013a/econ/arima.forecast.html
https://ww2.mathworks.cn/help/econ/conditional-variance-models.html
https://ww2.mathworks.cn/help/econ/garch-model.html
等等

2. 代码脚本

2.1 数据处理


clear;
close all;

%% 一、数据处理
% 1.ascii2fts导入数据,要求数据大于100期,必须显示前五期时间序列数据;
% ascii2fts无法选择读取列,先处理了一下
dis = ascii2fts('000157.txt');
dis = chfield(dis, {'series1','series2','series3','series4'},{'Open','High','Low','Close'});

% 2.利用candle函数显示前100期数据;
figure;
% candle(dis(1:100));
dis.freq = 1;								%指明为日数据
ftsma5 = filter(ones(1,5)/5,1,dis);         %计算5日移动平均
ftsma10 = filter(ones(1,10)/10,1,dis);      %计算10日移动平均
ftsclose5 = ftsma5.Close;                   %提取5日收盘平均
ftsclose10 = ftsma10.Close;                 %提取10日收盘平均
candle(dis(10:100));						%绘制蜡烛图
hold on;									%保持图形窗口
plot(ftsclose5(10:100), 'k');			%绘制5日均线
plot(ftsclose10(10:100), 'r');			%绘制10日均线

title("中联重科(000157)");

% 3.利用fts2mat转换成矩阵,并提取收盘序列;
mat = fts2mat(dis);
close_data = mat(:,4);

% 4.利用price2ret转换成收益率序列,绘制收益率折线图;
[Returns,~] = price2ret(close_data);

figure;
plot(Returns, 'b');
axis on; grid on;
set(gca,'gridlinestyle',':','GridAlpha',.8);
title("Returns Plot");

% 5.单位根检验。
h_adf = adftest(Returns);
h_kpss = kpsstest(Returns);

% r_temp = diff(Returns);   % 一阶差分

2.2 ARMA


%% 二、ARMA模型分析
% 1、用autocorr进行自相关分析;
% 2、用parcor进行偏相关分析;
figure;
subplot(2,1,1);
autocorr(Returns);
subplot(2,1,2);
parcorr(Returns);

% 3、分析运行结果,进行定阶,如果不能直接定阶,利用函数armax进行估计,并利用函数
%   fpe计算最终预报误差,选择最小fpe的阶作为ARMA模型的阶;

% 4、估计ARMA模型参数,写出最终的ARMA模型
% data = iddata(Returns);
% modelAR = ar(data, na);
% modelMA = armax(data, nc);

[na,nc,m] = demoarmax(Returns,4,4);

Mdl = arima(na, 0, nc);
EstMdl = estimate(Mdl,Returns);
[res,~,logL] = infer(EstMdl,Returns);

stdr = res/sqrt(EstMdl.Variance);
figure('Name','残差检验');
subplot(2,3,1);
plot(stdr);
title('Standardized Residuals');
subplot(2,3,2);
histogram(stdr,10);
title('Standardized Residuals');
subplot(2,3,3);
autocorr(stdr);
subplot(2,3,4);
parcorr(stdr);
subplot(2,3,5);
qqplot(stdr);

m = armax(iddata(Returns),'na',na,'nc',nc);

%% 三、GARCH模型分析【不估计均值方程,估计方差方程,这就要选取好的数据(单位根检验做不下去,
%      可以重新找数据或直接做),不可能有Garch阶为0,ARCH为1,模型矫正为科幻为Garch(1,2)/Garch(2,1)】

% 1、GARCH(1,1)模型估计;
% 1.1、选择一个或多个模型,如garch(1,1)、garch(2,1)
% spec11 = garchset('P',1,'Q',1,'Display','off');

% spec11 = garch('Offset',NaN,'GARCHLags',1,'ARCHLags',1);
spec11 = garch(1,1);
% spec11.Offset = NaN;

spec21 = garch(2,1);
spec21.Offset = NaN;

% 1.2、估计模型参数根据数据对每个模型进行参数估计。
% [Coeff11,Errors11,LLF11,Innovations11,Sigmas11] = garchfit(spec11,Returns);
[est11,~,LogL11] = estimate(spec11, Returns);
[est21,~,LogL21] = estimate(spec21, Returns);
% garchplot(Innovations11,Sigmas11,dem2gbp);

% garchdisp(Coeff11,Errors11);
disp(est11);

% 1.3、利用合适的评估方法选择合适的模型
[h,p] = lratiotest(LogL21,LogL11,1,0.05);
[h,p] = lratiotest(LogL21,LogL11,1,0.02);

% 赤池信息准则(AIC)和贝叶斯信息准则(BIC)检验模型
% format long;
% n11 = garchcount(Coeff11);
% n21 = garchcount(Coeff21);

% n11 = sum(any(Coeff11));
% n21 = sum(any(Coeff21));
% 
% [AIC11,BIC11] = aicbic(LLF11,n11,1974);
% [AIC21,BIC21] = aicbic(LLF21,n21,1974);
% disp([AIC11,BIC11; AIC21,BIC21]);


2.3 garch

% 2、GARCH仿真;【一定要做对比】
%%
rng default; % For reproducibility
[V,Y] = simulate(est11,100,'NumPaths',20);

figure;
subplot(2,2,1);
plot(V);
title('est11-Simulated Conditional Variances');

subplot(2,2,2);
plot(Y);
title('est11-Simulated Responses');

[V,Y] = simulate(est21,100,'NumPaths',20);

subplot(2,2,3);
plot(V);
title('est21-Simulated Conditional Variances');

subplot(2,2,4);
plot(Y);
title('est21-Simulated Responses');

%% 
numObs = numel(Returns); % Sample size (T)
numPaths = 100;     % Number of paths to simulate
rng(1);             % For reproducibility
[VSim,YSim] = simulate(est21,numObs,'NumPaths',numPaths);

VSimBar = mean(VSim,2);
VSimCI = quantile(VSim,[0.025 0.975],2);
YSimBar = mean(YSim,2);
YSimCI = quantile(YSim,[0.025 0.975],2);

figure;
subplot(2,1,1);
h1 = plot(VSim,'Color',0.8*ones(1,3));
hold on;
h2 = plot(VSimBar,'k--','LineWidth',2);
h3 = plot(VSimCI,'r--','LineWidth',2);
hold off;
title('Simulated Conditional Variances');
ylabel('Cond. var.');
xlabel('Year');

subplot(2,1,2);
h1 = plot(YSim,'Color',0.8*ones(1,3));
hold on;
h2 = plot(YSimBar,'k--','LineWidth',2);
h3 = plot(YSimCI,'r--','LineWidth',2);
hold off;
title('Simulated Nominal Returns');
ylabel('Nominal return (%)');
xlabel('Year');
legend([h1(1) h2 h3(1)],{'Simulated path' 'Mean' 'Confidence bounds'},...
    'FontSize',7,'Location','NorthWest');
%%
% 3、GARCH预测。【最多预测五期】
% [v,y] = simulate(est11,100);
% vF1 = forecast(est11,30,'Y0',y);
% vF2 = forecast(est11,30);
% 
% figure;
% plot(v,'Color',[.7,.7,.7]);
% grid on;
% hold on;
% plot(101:130,vF1,'r','LineWidth',2);
% plot(101:130,vF2,':','LineWidth',2);
% title('Forecasted Conditional Variances');
% legend('Observed','Forecasts with Presamples',...
% 		'Forecasts without Presamples');
% hold off;

[v,y] = simulate(est11,100);
vF1 = forecast(est11,5,'Y0',y);
vF2 = forecast(est11,5);

figure;
plot(v,'Color',[.7,.7,.7]);
grid on;
hold on;
plot(101:105,vF1,'r','LineWidth',2);
plot(101:105,vF2,':','LineWidth',2);
title('Forecasted Conditional Variances');
legend('Observed','Forecasts with Presamples',...
		'Forecasts without Presamples');
hold off;

你可能感兴趣的:(金融,matlab,概率论)