【预测模型-SVM预测】基于SVM实现股票趋势预测matlab源码

261 阅读2分钟

1 模型

 基于数据的机器学习就是由观测样本数据得出目前尚不能通过原理分析得到的规律,利用其对未来数据进行预测.神经网络以其优越的函数逼近性能广泛用于建立时间序列过去与未来数据之间某种确定的映射关系,实现预测.首先分析了以经验风险最小化为准则的神经网络的局限性,以及针对此提出的结构风险最小化准则的优点;其次引出支持向量机;最后利用支持向量机对股票数据做较准确的多步预测.

2 部分代码

clear all;

clc ;

close all

%得到文件路径,找到所有.dat格式的文件

sh = xlsread('bdata1.xls','Sheet1','G2:G98');

addpath('./libsvm-3.20');%%添加工具箱

%extract data

[m,n] = size(sh);

n1 = round(length(sh)*0.9);%训练样本大小

T=n1+1:m;%预测天数

ts = sh(1:n1,1);%训练数据

tsx = sh(n1+1:m,1);%测试数据

original = sh(n1+1:end,:);%原始测试数据

%归一化处理

[TS,TSps] = mapminmax(ts);

[TSX,TSXps] = mapminmax(tsx);

[TSX_zong,TSXps_zong] = mapminmax(sh);

%split the data into training and testing

train_label = TS(1:n1,:);%训练数据标签

train_data = TS(1:n1,:);%训练数据

test_label = TSX(1:end,:);%测试数据标签

test_data = TSX(1:end,:);%测试数据

test_label_zong = TSX_zong(1:end,:);%测试数据标签

test_data_zong = TSX_zong(1:end,:);%测试数据

% Find the optimize value of c,g paramter

% Approximately choose the parameters:

% the scale of c is 2^(-5),2^(-4),...,2^(10)

% the scale of g is 2^(-5),2^(-4),...,2^(5)

[bestmse,bestc,bestg] = svmregress(train_label,train_label,-5,10,-5,5,3,1,1,0.0005);

% Display the approximate result

disp('Display the approximate result');

str = sprintf( 'Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse,bestc,bestg);

disp(str);

%Do training by using svmtrain of libsvm

cmd = ['-c ', num2str(bestc), ' -g ', num2str(bestg) , ' -s 3 -p 0.01'];

model = svmtrain(train_label,train_data,cmd);%训练

%Do predicting by using svmpredict of libsvm

predict= svmpredict(test_label,test_data,model);%预测

predict = mapminmax('reverse',predict,TSXps);%反归一化

predict_zong= svmpredict(test_label_zong,test_data_zong,model);%预测

predict_zong = mapminmax('reverse',predict_zong,TSXps_zong);%反归一化

% Display the result of SVM Regression

% str = sprintf( '误差均方差MSE = %g R = %g%%',mse(2),mse(3)*100);

disp(str);

figure(1);

plot(sh,'k*-','LineWidth',2);

hold on;

plot(predict_zong,'ro-','LineWidth',2);

legend('原始数据','预测数据');

xlabel('时刻')

ylabel('用电负荷')

title('总体预测')

hold off;

figure(2);

plot(T,original,'b*-','LineWidth',1);

hold on;

plot(T,predict,'ro-','LineWidth',1);

xlabel('时刻')

ylabel('用电负荷')

legend('原始数据','预测数据');

title('未来几天预测')

hold off;

figure(3);

plot(T,original-predict,'bo-','LineWidth',1);

title('未来几天预测误差图')

xlabel('时刻')

ylabel('用电负荷')

hold off;

3 仿真结果

4 参考文献

[1]杨一文, and 杨朝军. "基于支持向量机的金融时间序列预测." 系统管理学报 14.2(2005):176-181.

5 MATLAB代码与数据下载地址

见博客主页