《量化投资:以MATLAB为工具》

MATLAB技术论坛

 找回密码
 注册账号
查看: 1109|回复: 5
收起左侧

[讨论] 螺纹期货收盘价预测(基于信息粒化+SVM预测,附详细代码与运行结果)

[复制链接]
发表于 2017-7-17 14:13:44 | 显示全部楼层 |阅读模式
本帖最后由 zhanghaojoe 于 2017-7-17 14:30 编辑

最近利用“信息粒化+SVM”技术对螺纹期货价格进行了预测,发现预测期货价格的涨跌趋势还可以,但预测具体价格并不准确,现将具体代码以及运行结果共享出来,欢迎大家讨论,代码和运行结果如下:
>> load('螺纹期货数据.mat')
>> tic;
ts=close;
time=length(ts);
figure;
plot(ts,'LineWidth',2);
title('螺纹期货收盘价(2009.3.27-2017.7.10)','FontSize',12);
xlabel('交易日天数(2009.3.27-2017.7.10)','FontSize',12);
ylabel('收盘价','FontSize',12);
grid on;
win_num = floor(time/5);
tsx = 1:win_num;
tsx = tsx';
[Low,R,Up]=FIG_D(ts','triangle',win_num);
[low,low_ps] = mapminmax(Low);
low_ps.ymin = 100;
low_ps.ymax = 300;
[low,low_ps] = mapminmax(Low,low_ps);
low = low';
[bestmse1,bestc1,bestg1] = SVMcgForRegress(low,tsx,-8,8,-8,8);
str1 = sprintf( 'SVM parameters for Low:Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse1,bestc1,bestg1);
disp(str1);
[bestmse1,bestc1,bestg1] = SVMcgForRegress(low,tsx,-8,8,-8,8,3,0.5,0.5,0.05);
str1 = sprintf( 'SVM parameters for Low:Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse1,bestc1,bestg1);
disp(str1);
cmd1 = ['-c ', num2str(bestc1), ' -g ', num2str(bestg1) , ' -s 3 -p 0.1'];
low_model = svmtrain(low, tsx, cmd1);
[low_predict,low_mse,dec_value_low] = svmpredict(low,tsx,low_model);
low_predict = mapminmax('reverse',low_predict,low_ps);
predict_low = svmpredict(1,win_num+1,low_model);
predict_low = mapminmax('reverse',predict_low,low_ps);
predict_low
figure;
error1 = low_predict - Low';
plot(error1,'ro');
title('误差(predicted data-original data)','FontSize',12);
xlabel('粒化窗口数目','FontSize',12);
ylabel('误差量','FontSize',12);
grid on;
[r,r_ps] = mapminmax(R);
r_ps.ymin = 100;
r_ps.ymax = 300;
[r,r_ps] = mapminmax(R,r_ps);
figure;
plot(r,'r*');
title('r归一化后的图像','FontSize',12);
grid on;
r = r';
[bestmse2,bestc2,bestg2] = SVMcgForRegress(r,tsx,-8,8,-8,8);
str2 = sprintf( 'SVM parameters for R:Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse2,bestc2,bestg2);
disp(str2);
[bestmse2,bestc2,bestg2] = SVMcgForRegress(r,tsx,-8,8,-8,8,3,0.5,0.5,0.05);
str2 = sprintf( 'SVM parameters for R:Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse2,bestc2,bestg2);
disp(str2);
cmd2 = ['-c ', num2str(bestc2), ' -g ', num2str(bestg2) , ' -s 3 -p 0.1'];
r_model = svmtrain(r, tsx, cmd2);
[r_predict,r_mse,dec_value_r] = svmpredict(r,tsx,r_model);
r_predict = mapminmax('reverse',r_predict,r_ps);
predict_r = svmpredict(1,win_num+1,r_model);
predict_r = mapminmax('reverse',predict_r,r_ps);
predict_r
figure;  
error2 = r_predict - R';
plot(error2,'ro');
title('误差(predicted data-original data)','FontSize',12);
xlabel('粒化窗口数目','FontSize',12);
ylabel('误差量','FontSize',12);  
grid on;
[up,up_ps] = mapminmax(Up);
up_ps.ymin = 100;
up_ps.ymax = 300;
[up,up_ps] = mapminmax(Up,up_ps);
up = up';
[bestmse3,bestc3,bestg3] = SVMcgForRegress(up,tsx,-8,8,-8,8);
str3 = sprintf( 'SVM parameters for Up:Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse3,bestc3,bestg3);
disp(str3);
[bestmse3,bestc3,bestg3] = SVMcgForRegress(up,tsx,-4,8,-8,8,3,0.5,0.5,0.05);
str3 = sprintf( 'SVM parameters for Up:Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse3,bestc3,bestg3);
disp(str3);
cmd3 = ['-c ', num2str(bestc3), ' -g ', num2str(bestg3) , ' -s 3 -p 0.1'];
up_model = svmtrain(up, tsx, cmd3);
[up_predict,up_mse,dec_value_up] = svmpredict(up,tsx,up_model);
up_predict = mapminmax('reverse',up_predict,up_ps);
predict_up = svmpredict(1,win_num+1,up_model);
predict_up = mapminmax('reverse',predict_up,up_ps);
predict_up
figure;  
error3 = up_predict - Up';  
plot(error3,'ro');
title('误差(predicted data-original data)','FontSize',12);
xlabel('粒化窗口数目','FontSize',12);
ylabel('误差量','FontSize',12);
grid on;
toc;

SVM parameters for Low:Best Cross Validation MSE = 30.4484 Best c = 84.4485 Best g = 0.0358968
SVM parameters for Low:Best Cross Validation MSE = 29.8477 Best c = 256 Best g = 0.03125
Mean squared error = 14.9071 (regression)
Squared correlation coefficient = 0.994699 (regression)
Mean squared error = 44923 (regression)
Squared correlation coefficient = -1.#IND (regression)

predict_low =

   3.5311e+03

SVM parameters for R:Best Cross Validation MSE = 21.0496 Best c = 84.4485 Best g = 0.0625
SVM parameters for R:Best Cross Validation MSE = 23.9824 Best c = 128 Best g = 0.0441942
Mean squared error = 9.59775 (regression)
Squared correlation coefficient = 0.996472 (regression)
Mean squared error = 42992 (regression)
Squared correlation coefficient = -1.#IND (regression)

predict_r =

   3.5495e+03

SVM parameters for Up:Best Cross Validation MSE = 18.9305 Best c = 84.4485 Best g = 0.0625
SVM parameters for Up:Best Cross Validation MSE = 22.8052 Best c = 128 Best g = 0.0441942
Mean squared error = 8.79312 (regression)
Squared correlation coefficient = 0.996777 (regression)
Mean squared error = 43411.7 (regression)
Squared correlation coefficient = -1.#IND (regression)

predict_up =

   3.5801e+03

时间已过 145.169905 秒。
上述预测的期货价格区间为[3531,3580],平均值为3550,螺纹期货5日实际收盘价分别为:3565,3575,3657,3551?,最后一个数据是今天的收盘价,今天收盘价还没有出来,所以用?表示。
对比预测结果,5天中,实际有3天的价格在预测的区间范围内。前一个5天的均价为3413,下一个的5天均价预测值为3550,因此是一个涨的趋势,从实际来看,本周较上周却是涨了不少。
发表于 2017-7-19 15:58:20 | 显示全部楼层
感谢楼主分享,但是
FIG_D、SVMcgForRegress 这两个function,没有提供,能否分享一下。
感谢楼主~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-19 16:23:12 | 显示全部楼层
参照李洋先生的,你可以在网上搜索自行下载。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-19 16:24:44 | 显示全部楼层
ytiantian0505 发表于 2017-7-19 15:58
感谢楼主分享,但是
FIG_D、SVMcgForRegress 这两个function,没有提供,能否分享一下。
感谢楼主~

参照李洋先生的,你可以在网上搜索自行下载。
回复 支持 反对

使用道具 举报

发表于 2017-7-19 17:13:19 | 显示全部楼层
zhanghaojoe 发表于 2017-7-19 16:24
参照李洋先生的,你可以在网上搜索自行下载。

谢谢楼主~~~~~
回复 支持 反对

使用道具 举报

发表于 2018-4-22 19:57:17 | 显示全部楼层
兄弟,想请教一下FIG_D函数是怎么运行的,我找到了函数代码但运行不起来啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

QQ|网站地图|MATLAB技术论坛|Simulink仿真论坛 ( 蜀ICP备19014457号 

GMT+8, 2020-2-28 07:18 , Processed in 0.060496 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表