SVR预测第二天开盘趋势和正负统计分析

基本概念:支持向量机(SVM)

原始SVM算法是由弗拉基米尔·万普尼克和亚历克塞·泽范兰杰斯于1963年发明的。1992年,Bernhard E. Boser、Isabelle M. Guyon和弗拉基米尔·万普尼克提出了一种通过将核技巧应用于最大间隔超平面来创建非线性分类器的方法。当前标准的前身(软间隔)由Corinna Cortes和Vapnik于1993年提出,并于1995年发表。

上个世纪90年代,由于人工神经网络(RNN)的衰落,SVM在很长一段时间里都是当时的明星算法。被认为是一种理论优美且非常实用的机器学习算法。

在理论方面,SVM算法涉及到了非常多的概念:间隔(margin)、支持向量(support vector)、核函数(kernel)、对偶(duality)、凸优化等,相关知识可以参阅周志华的西瓜书、吴恩达机器学习课程及百度,在此不予列举

SVR--根据SVM做回归分析

支持向量回归模型(Support Vector Regression, SVR)是使用SVM来拟合曲线,做回归分析。分类和回归问题是有监督机器学习中最重要的两类任务。与分类的输出是有限个离散的值(例如上面的{−1,1}{−1,1})不同的是,回归模型的输出在一定范围内是连续的。

from sklearn.svm import SVR构造支持向量回归模型。

支持向量回归模型的一个例子通过添加不同的核函数拟合不同的回归模型,数据点随机产生

import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt
#import time


x = np.sort(5*np.random.rand(40, 1), axis=0)    #随机数据
y = np.sin(x).ravel()   #添加噪声

y[::5] += 3*(0.5-np.random.rand(8))

svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_lin = SVR(kernel='linear', C=1e3)
svr_poly = SVR(kernel='poly', C=1e3, degree=2)
y_rbf = svr_rbf.fit(x, y).predict(x)
y_lin = svr_lin.fit(x, y).predict(x)
y_poly = svr_poly.fit(x, y).predict(x)

plt.scatter(x, y, c='k', label='data')
plt.plot(x, y_rbf, c='g', label='RBF model')
plt.plot(x, y_lin, c='r', label='Linear model')
plt.plot(x, y_poly, c='b', label='Polynomial model')
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.show()

得到三种不同的结果

SVR预测第二天开盘趋势和正负统计分析_第1张图片

就这个例子而言,RBF模型拟合效果好于线性模型和多项式模型

 

下面通过优矿的数据集DataAPI提取股票数据进行回归建模

import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt


start='20160101' #开始日期
end='20170101'   #截至日期
code='600000'    
df=DataAPI.MktEqudAdjAfGet(ticker=code,beginDate=start,endDate=end,field=u'tradeDate,closePrice',pandas='1')
df=df.set_index('tradeDate').copy() 

#dates = df.index
X=np.mat(range(1,len(df.values)+1)).T 
y=df.values 


svr_rbf=SVR(kernel='rbf',C=1e3,gamma=0.1)  
clf=svr_rbf.fit(X,y)            #训练得到分类器
y_rbf=clf.predict(X)            #预测输出

_,ax=plt.subplots(figsize=[14,7])
#dates = df.index
ax.scatter(X,y,c='r',label='data')               
ax.plot(X,y_rbf,c='b',label='RBF model')                  
#ticks = ax.get_xticks()
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.show()

通过RBF模型得到拟合结果如下

SVR预测第二天开盘趋势和正负统计分析_第2张图片

总体拟合程度比较高,只存在少量数据误差

以上内容实现均基于公众号 量化投资与机器学习

你可能感兴趣的:(SVR预测第二天开盘趋势和正负统计分析)