For some reason, I don't see people doing so much, so I will try to predict the stock price using deep learning. I'm new to both deep learning and Python. Little is known about libraries, implementation methods, theories, etc. We are waiting for Tsukkomi etc.
――Using stock price data for several days, you can predict whether the Nikkei Stock Average will “rise”, “fall” or “do not change” the next day. (Category)
――The closing price of the next day is used to judge whether it was "up", "down" or "unchanged". --For input data, use "open price", "high price", "low price", and "close price" from a few days ago to the previous day. --There are four hidden layers. ――As an input, just put in the stock prices for the past few days and train.
TensorFlow 0.7 Ubuntu 14.04 Python 2.7 AWS EC2 micro instance
Prepare as much Nikkei 225 data as possible. This time, I used the data of Yahoo Finance.
Keep the Nikkei 225 text. (Because it is troublesome to go to get it every time) This time, I will look at the data for 10 days and predict the stock price for the next day. In addition, if the closing price on the next day is 0.5% or more from the closing price on the previous day, it is "up", if it is 0.5% or less, it is "down", otherwise it is "unchanged". The reason for this number is that these three are just about 33%.
if array_base[idx][3] > (array_base[idx+1][3] * (1.0+flg_range)):
y_flg_array.append([1., 0., 0.])
up += 1
elif array_base[idx][3] < (array_base[idx+1][3] * (1.0-flg_range)):
y_flg_array.append([0., 0., 1.])
down += 1
else:
y_flg_array.append([0., 1., 0.])
keep += 1
The ratio of the data as a whole is as follows. Increased: 33.9% Decreased: 32.7% No change: 33.4%
The code looks like this. It's a little spaghetti with copy and paste, but it's easy to see. (Excuse)
def inference(x_ph, keep_prob):
with tf.name_scope('hidden1'):
weights = tf.Variable(tf.truncated_normal([data_num * 4, NUM_HIDDEN1]), name='weights')
biases = tf.Variable(tf.zeros([NUM_HIDDEN1]), name='biases')
hidden1 = tf.nn.sigmoid(tf.matmul(x_ph, weights) + biases)
with tf.name_scope('hidden2'):
weights = tf.Variable(tf.truncated_normal([NUM_HIDDEN1, NUM_HIDDEN2]), name='weights')
biases = tf.Variable(tf.zeros([NUM_HIDDEN2]), name='biases')
hidden2 = tf.nn.sigmoid(tf.matmul(hidden1, weights) + biases)
with tf.name_scope('hidden3'):
weights = tf.Variable(tf.truncated_normal([NUM_HIDDEN2, NUM_HIDDEN3]), name='weights')
biases = tf.Variable(tf.zeros([NUM_HIDDEN3]), name='biases')
hidden3 = tf.nn.sigmoid(tf.matmul(hidden2, weights) + biases)
with tf.name_scope('hidden4'):
weights = tf.Variable(tf.truncated_normal([NUM_HIDDEN3, NUM_HIDDEN4]), name='weights')
biases = tf.Variable(tf.zeros([NUM_HIDDEN4]), name='biases')
hidden4 = tf.nn.sigmoid(tf.matmul(hidden3, weights) + biases)
#DropOut
dropout = tf.nn.dropout(hidden4, keep_prob)
with tf.name_scope('softmax'):
weights = tf.Variable(tf.truncated_normal([NUM_HIDDEN4, 3]), name='weights')
biases = tf.Variable(tf.zeros([3]), name='biases')
y = tf.nn.softmax(tf.matmul(dropout, weights) + biases)
return y
A placeholder where x_ph contains stock price data up to the previous day. It contains data such as [1 day ago closing price, 1 day ago opening price, 1 day ago high price, 1 day ago low price, 2 days ago closing price, ...]. The number of hidden layer units is 100, 50, 30, 10. That is, it is defined below.
# DEFINITION
NUM_HIDDEN1 = 100
NUM_HIDDEN2 = 50
NUM_HIDDEN3 = 30
NUM_HIDDEN4 = 10
By the way, the number of units and the number of layers are appropriate. Isn't it better to have more? I decided with the intention. I would appreciate it if you could tell me any guidelines for determining these numbers m (_ _) m
The optimization uses ADAM. I don't know the details, but in my experience, it's harder to diverge than the gradient descent method (GradientDescentOptimizer).
def optimize(loss):
optimizer = tf.train.AdamOptimizer(learning_rate)
train_step = optimizer.minimize(loss)
return train_step
The training looks like the following. The batch size is 100. If it is too big, it will be moss. (Insufficient memory. It's okay if you don't get stingy)
def training(sess, train_step, loss, x_train_array, y_flg_train_array):
summary_op = tf.merge_all_summaries()
init = tf.initialize_all_variables()
sess.run(init)
summary_writer = tf.train.SummaryWriter(LOG_DIR, graph_def=sess.graph_def)
for i in range(int(len(x_train_array) / bach_size)):
batch_xs = getBachArray(x_train_array, i * bach_size, bach_size)
batch_ys = getBachArray(y_flg_train_array, i * bach_size, bach_size)
sess.run(train_step, feed_dict={x_ph: batch_xs, y_ph: batch_ys, keep_prob: 0.8})
summary_str = sess.run(summary_op, feed_dict={x_ph: batch_xs, y_ph: batch_ys, keep_prob: 1.0})
summary_writer.add_summary(summary_str, i)
After training, evaluate as follows. It's basically a copy from the tutorial.
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_ph, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x_ph: x_test_array, y_ph: y_flg_test_array, keep_prob: 1.0}))
Looking at the TensorBoard, it looks like it has converged as follows.
However, looking at the percentage of correct answers,
0.3375
A terrible defeat. : scream: As mentioned above, the probability of each category (up, down, unchanged) is about 33%, so the result is the same as guessing. (Lol)
Regarding the cause of this failure, the possible causes are as follows. --Insufficient model construction? (Please give me some advice) ――In the first place, does the stock price up to the previous day affect the stock price on the next day? ――The Nikkei average is the average of stock prices, so technical factors are not relevant in the first place?
I tried various things by changing the number of days used for input, the number of hidden layers, the number of units, the activation function, etc., but there was no particular change in the results. : sob:
I'm not very familiar with matrix calculation or deep learning, but if I can understand the outline, it seems that I can do something like that with TensorFlow! : satisfied:
Official Tutorial Play to predict race value and type from Pokemon name with TensorFlow [Deep Learning (Machine Learning Professional Series)](http://www.amazon.co.jp/%E6%B7%B1%E5%B1%A4%E5%AD%A6%E7%BF%92-%E6 % A9% 9F% E6% A2% B0% E5% AD% A6% E7% BF% 92% E3% 83% 97% E3% 83% AD% E3% 83% 95% E3% 82% A7% E3% 83 % 83% E3% 82% B7% E3% 83% A7% E3% 83% 8A% E3% 83% AB% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA -% E5% B2% A1% E8% B0% B7-% E8% B2% B4% E4% B9% 8B / dp / 4061529021)
Recommended Posts