[PYTHON] Stock price forecast with tensorflow

http://qiita.com/northriver/items/4f4690053e1770311335

I'm used to tensorlfow, so I'll try to predict the stock price by deep learning Create a record date and use the stock price of the past 90 days (on business days) and the stock price 30 days later to put out a stock that is likely to rise.

The correct answer data after 30 days was selected as x% higher, lower, or in the meantime with respect to the standard. I decided to solve it as a classification problem.

First, make the necessary functions with two hidden layers

import tensorflow as tf
import numpy as np
sess = tf.InteractiveSession()


def inference(x_placeholder,SCORE_SIZE,NUM_HIDDEN1,NUM_OUTPUT,keep_prob):
	with tf.name_scope('hidden1') as scope:
		hidden1_weight = tf.Variable(tf.truncated_normal([SCORE_SIZE, NUM_HIDDEN1], stddev=0.1), name="hidden1_weight")
		hidden1_bias = tf.Variable(tf.constant(0.1, shape=[NUM_HIDDEN1]), name="hidden1_bias")
		mean1, variance1 = tf.nn.moments(x_placeholder,[0])
		bn1 = tf.nn.batch_normalization(x_placeholder, mean1, variance1, None, None, 1e-5)
		hidden1_output = tf.nn.relu(tf.matmul(bn1, hidden1_weight) + hidden1_bias)

	with tf.name_scope('hidden2'):
		hidden2_weight = tf.Variable(tf.truncated_normal([NUM_HIDDEN1, NUM_HIDDEN2], stddev=0.01), name='hidden2_weight')
		hidden2_bias = tf.Variable(tf.constant(0.1, shape=[NUM_HIDDEN2]), name="hidden2_bias")
		mean2, variance2 = tf.nn.moments(hidden1_output,[0])
		bn2 = tf.nn.batch_normalization(hidden1_output, mean2, variance2, None, None, 1e-5)
		hidden2_output = tf.nn.relu(tf.matmul(bn2, hidden2_weight) + hidden2_bias)

	dropout = tf.nn.dropout(hidden1_output, keep_prob)

	with tf.name_scope('output') as scope:
		output_weight = tf.Variable(tf.truncated_normal([NUM_HIDDEN1, NUM_OUTPUT], stddev=0.1), name="output_weight")
		output_bias = tf.Variable(tf.constant(0.1, shape=[NUM_OUTPUT]), name="output_bias")
		mean_out, variance_out = tf.nn.moments(dropout,[0])
		bn_out = tf.nn.batch_normalization(dropout, mean_out, variance_out, None, None, 1e-5)
		output = tf.nn.softmax(tf.matmul(bn_out, output_weight) + output_bias)
	return output

def loss(output, y_placeholder, loss_label_placeholder):
	with tf.name_scope('loss') as scope:
		loss = tf.reduce_mean(-tf.reduce_sum(y_placeholder * tf.log(tf.clip_by_value(output,1e-10,1.0)), reduction_indices=[1]))
	return loss

def training(loss,rate):
	with tf.name_scope('training') as scope:
		train_step = tf.train.GradientDescentOptimizer(rate).minimize(loss)
	return train_step

Here, I was careful that it is better to do batch normalization, so I tried to dig into all layers

The number of hidden layers and the learning rate are appropriate, and will be adjusted in the future. Run!

rate=0.08
SCORE_SIZE=90
NUM_OUTPUT=3
NUM_HIDDEN1=90
NUM_HIDDEN2=45


y_placeholder = tf.placeholder("float", [None, NUM_OUTPUT], name="y_placeholder")
x_placeholder = tf.placeholder("float", [None, SCORE_SIZE], name="x_placeholder")
loss_label_placeholder = tf.placeholder("string", name="loss_label_placeholder")
keep_prob = tf.placeholder("float")


output = inference(x_placeholder,SCORE_SIZE,NUM_HIDDEN1,NUM_OUTPUT,keep_prob)
loss1 = loss(output, y_placeholder, loss_label_placeholder)
training_op = training(loss1,rate)

correct_prediction = tf.equal(tf.argmax(output,1), tf.argmax(y_placeholder,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

best_loss = float("inf")

sess.run(tf.initialize_all_variables())
for step in range(1001):
	x_train,y_train,c_train=train_data_make(SCORE_SIZE,200)
	feed_dict_train={y_placeholder: y_train,x_placeholder: x_train,keep_prob: 0.8}
	feed_dict_test={y_placeholder: y_test,x_placeholder: x_test, keep_prob: 1.0}

	if step%100 == 0:print "size %d,step %d,rate %g loss %g,test accuracy %g"%(num_long,step,rate,loss1.eval(feed_dict=feed_dict_train),accuracy.eval(feed_dict=feed_dict_test))
	sess.run(training_op, feed_dict=feed_dict_train)	

#test_result
print "final_result : size %d,step %d, test accuracy %g"%(num_long,step,accuracy.eval(feed_dict=feed_dict_test))

As a result, 9 are allocated ... Well, most of the stocks have the same stock price in one month, so it may be due to no change. Rather, 90% of the test data was classified without change ...

size 90,step 0,rate 0.08 loss 1.31044,test accuracy 0.257594
size 90,step 100,rate 0.08 loss 0.369047,test accuracy 0.91373
size 90,90,step 200,rate 0.08 loss 0.249306,test accuracy 0.90887
・ ・ ・

Let's take a look at what kind of numbers are given for each stock. After all, there were many expectations that it wouldn't change, but ... that? If you arrange them in descending order of likelihood of increase, some stocks are expected to do so.

image

Is it also a unique movement before the stock price goes up?

I would like to see the situation for a while and observe if it really goes up.

According to the following, it seems that the batch size should be smaller http://postd.cc/26-things-i-learned-in-the-deep-learning-summer-school/ (Invest in stocks at your own risk. Articles are not responsible at all)

Recommended Posts

Stock price forecast with tensorflow
Stock Price Forecast with TensorFlow (LSTM) ~ Stock Forecast Part 1 ~
Stock Price Forecast with TensorFlow (Multilayer Perceptron: MLP) ~ Stock Forecast Part 2 ~
Kyotei forecast with TensorFlow
Stock price forecast using deep learning (TensorFlow)
Stock Price Forecast 2 Chapter 2
Stock Price Forecast 1 Chapter 1
Stock Price Forecast Using Deep Learning (TensorFlow) -Part 2-
Python: Stock Price Forecast Part 2
Get stock price with Python
Python: Stock Price Forecast Part 1
Bitcoin Price Forecast on TensorFlow (LSTM)
[Python] My stock price forecast [HFT]
Stock price forecast using machine learning (scikit-learn)
Download Japanese stock price data with python
Stock price forecast using machine learning (regression)
Zundokokiyoshi with TensorFlow
Breakout with Tensorflow
Get stock price data with Quandl API [Python]
Stock price forecast by machine learning Numerai Signals
Automatic acquisition of stock price data with docker-compose
Let's do web scraping with Python (stock price)
Python & Machine Learning Study Memo ⑦: Stock Price Forecast
Stock price forecast using deep learning [Data acquisition]
Reading data with TensorFlow
Cryptocurrency price fluctuation forecast
Kaggle ~ House Price Forecast ② ~
Try regression with TensorFlow
Kaggle ~ Home Price Forecast ~
Data is missing when getting stock price data with Pandas-datareader
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Translate Getting Started With TensorFlow
Try deep learning with TensorFlow
Use TensorFlow with Intellij IDEA
Programming history 1 month Extract NY Dow stock price with Python!
Approximate sin function with TensorFlow
[Introduction to Systre] Stock price forecast; Monday is weak m (__) m
Easy image classification with TensorFlow
Scraping weather forecast with python
Stock Price Forecasting Using LSTM_1
[Stock price analysis] Learning pandas with fictitious data (002: Log output)
Stock price data acquisition tips
Try TensorFlow MNIST with RNN
Get Japanese stock price information from yahoo finance with pandas
Stock price forecast by machine learning Let's get started Numerai
[Stock price analysis] Learn pandas with Nikkei 225 (004: Change read data to Nikkei 225)
Stock price forecast by machine learning is so true Numerai Signals
[Stock price analysis] Learning pandas with fictitious data (001: environment preparation-file reading)
[Stock price analysis] Learning pandas with fictitious data (003: Type organization-candlestick chart)
Get US stock price from Python with Web API with Raspberry Pi
TensorFlow 2.2 can't be installed with Python 3.8!
MNIST (DCNN) with Keras (TensorFlow backend)
Predict Bitcoin price changes with Prophet
Customize Model / Layer / Metric with TensorFlow
Inference & result display with Tensorflow + matplotlib
Classify "Wine" with TensorFlow MLP code
Precautions when installing tensorflow with anaconda
[TensorFlow 2] Learn RNN with CTC Loss
Try deep learning with TensorFlow Part 2
[TensorFlow] [Keras] Neural network construction with Keras
Use Tensorflow 2.1.0 with Anaconda on Windows 10!