"Stove-refining AI" machine learning 045-Modeling of stock data by hidden Markov model

Source: Internet
Author: User

"Stove-refining AI" machine learning 045-Modeling of stock data by hidden Markov model

(Python libraries and version numbers used in this article: Python 3.6, Numpy 1.14, Scikit-learn 0.19, matplotlib 2.2)

Stock data is very very typical timing data, the data are arranged by date, and the stock price is what we can observe the observation sequence, and the underlying change mechanism behind the stock price is that we can not see the hidden state and state transition probability, so it is possible to use hidden Markov to model the stock, And predict the stock follow-up changes, if in the stock data research a little break, then, the silver is a lot of money into the pocket.


1. Prepare the data set

Here I use Tushare to extract a stock data, and then use the stock's daily gain and volume to model, to see what results can be predicted.

# 1, 准备数据集,使用tushare来获取股票数据import tushare as tsstock_df=ts.get_k_data(‘600123‘,start=‘2008-10-01‘,end=‘2018-10-01‘) # 获取600123这只股票的近十年数据print(stock_df.info()) # 查看没有错误print(stock_df.head())

It just downloads the last 10 years of the 600123 stock, but what we're going to get is the rise in the closing price, and we need to do further processing of the data.

# 准备数据集,此次我们用两个指标来计算HMM模型,股价涨幅和成交量close=stock_df.close.valuesfeature1=100*np.diff(close)/close[:-1] # 股票涨幅的计算print(close[:10])print(feature1[:10]) # 查看涨幅计算有没问题

-------------------------------------lose-----------------------------------------

[6.775 6.291 6.045 5.899 5.361 5.436 5.299 4.994 4.494 4.598]
[ -7.14391144-3.91034812-2.41521919-9.12018986 1.39899273
-2.52023547-5.75580298-10.01201442 2.31419671 9.98260113]

--------------------------------------------finished-------------------------------------

# 由于计算涨幅之后的序列比原来的收盘价序列少一个(最开始的股价没法计算涨幅),故而需要减去一个feature2=stock_df.volume.values[1:]dataset_X=np.c_[feature1,feature2]print(dataset_X[:5]) # 检查没错


2. Create a HMM model

For the HMM model, I have already explained in detail in the previous article, please refer to "Furnace refining AI" machine learning 044-Create hidden Markov model

# 创建HMM模型,并训练from hmmlearn.hmm import GaussianHMMmodel=GaussianHMM(n_components=5,n_iter=1000) # 暂时假设该股票有5个隐含状态model.fit(dataset_X)

How do we know if the model is good or bad after using hmm modeling? Then it is necessary to compare the results of their predictions with the actual results to see if they are consistent.

# 使用该模型查看一下效果N=500samples,_=model.sample(N)# 由于此处我使用涨幅作为第一个特征,成交量作为第二个特征进行建模,# 故而得到的模型第一列就是预测的涨幅,第二列就是成交量plt.plot(feature1[:N],c=‘red‘,label=‘Rise%‘) # 将实际涨幅和预测的涨幅绘制到一幅图中方便比较plt.plot(samples[:,0],c=‘blue‘,label=‘Predicted%‘)plt.legend()

Looks like the match result is not very good, again to look at the turnover forecast:

plt.plot(feature2[:N],c=‘red‘,label=‘volume‘)plt.plot(samples[:,1],c=‘blue‘,label=‘Predicted‘)plt.legend()

These two results are not good, the predicted value and the actual value are relatively large, indicating that the model is difficult to solve the project. We think in a different way, if it is so simple to predict the movements of the stock, then everyone will be from the stock market, and eventually the stock market can only be closed. Readers are interested in optimizing the HMM model's implied state number, may get a better matching results, but also may have happened to fit, so I think the optimization is of little use.

####################### #小 ********** Knot ###############################

1, here is simply a hmm model to analyze the stock data examples, although the practical value is not small, but can give other complex algorithms to provide a little thought.

2, or that sentence, away from the stock market, away from harm.

#################################################################


Note: This section of the code has been uploaded to ( my GitHub), Welcome to download.

Resources:

1, Python machine learning classic example, Prateek Joshi, Tao Junjie, Chen Xiaoli translation

"Stove-refining AI" machine learning 045-Modeling of stock data by hidden Markov model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.