This post is a continued tutorial for how to build a recurrent neural network using Tensorflow to predict stock market prices. Part 2 attempts to predict prices of multiple stocks using embeddings. The full working code is available in github.com/lilianweng/stock-rnn.

In the Part 2 tutorial, I would like to continue the topic on stock price prediction and to endow the recurrent neural network that I have built in Part 1 with the capability of responding to multiple stocks. In order to distinguish the patterns associated with different price sequences, I use the stock symbol embedding vectors as part of the input.


Dataset

During the search, I found this library for querying Yahoo! Finance API. It would be very useful if Yahoo hasn’t shut down the historical data fetch API. You may find it useful for querying other information though. Here I pick the Google Finance link, among a couple of free data sources for downloading historical stock prices.

The data fetch code can be written as simple as:

1
2
3
4
5
6
7
8
import urllib2
from datetime import datetime
BASE_URL = "https://www.google.com/finance/historical?"
           "output=csv&q={0}&startdate=Jan+1%2C+1980&enddate={1}"
symbol_url = BASE_URL.format(
    urllib2.quote('GOOG'), # Replace with any stock you are interested.
    urllib2.quote(datetime.now().strftime("%b+%d,+%Y"), '+')
)

When fetching the content, remember to add try-catch wrapper in case the link fails or the provided stock symbol is not valid.

1
2
3
4
5
6
try:
    f = urllib2.urlopen(symbol_url)
    with open("GOOG.csv", 'w') as fin:
        print >> fin, f.read()
except urllib2.HTTPError:
    print "Fetching Failed: {}".format(symbol_url)

The full working data fetcher code is available here.

Model Construction

The model is expected to learn the price sequences of different stocks in time. Due to the different underlying patterns, I would like to tell the model which stock it is dealing with explicitly. Embedding is more favored than one-hot encoding, because:

  1. Given that the train set includes stocks, the one-hot encoding would introduce (or ) additional sparse feature dimensions. Once each stock symbol is mapped onto a much smaller embedding vector of length , , we end up with a much more compressed representation and smaller dataset to take care of.
  2. Since embedding vectors are variables to learn. Similar stocks could be associated with similar embeddings and help the prediction of each others, such as “GOOG” and “GOOGL” which you will see in Fig. 5. later.

In the recurrent neural network, at one time step , the input vector contains input_size (labelled as ) daily price values of -th stock, . The stock symbol is uniquely mapped to a vector of length embedding_size (labelled as ), . As illustrated in Fig. 1., the price vector is concatenated with the embedding vector and then fed into the LSTM cell.

Another alternative is to concatenate the embedding vectors with the last state of the LSTM cell and learn new weights and bias in the output layer. However, in this way, the LSTM cell cannot tell apart prices of one stock from another and its power would be largely restrained. Thus I decided to go with the former approach.

RNN with embedding

Fig. 1. The architecture of the stock price prediction RNN model with stock symbol embeddings.

Two new configuration settings are added into RNNConfig:

  • embedding_size controls the size of each embedding vector;
  • stock_symbol_size refers to the number of unique stocks in the dataset.

Together they define the size of the embedding matrix, for which the model has to learn embedding_size stock_symbol_size additional variables compared to the model in Part 1.

1
2
3
4
class RNNConfig():
   # … old ones
   embedding_size = 8
   stock_symbol_size = 100

Define the Graph

— Let’s start going through some code —

(1) As demonstrated in tutorial Part 1: Define the Graph, let us define a tf.Graph() named lstm_graph and a set of tensors to hold input data, inputs, targets, and learning_rate in the same way. One more placeholder to define is a list of stock symbols associated with the input prices. Stock symbols have been mapped to unique integers beforehand with label encoding.

1
2
    # Mapped to an integer. one label refers to one stock symbol.
    stock_labels = tf.placeholder(tf.int32, [None, 1])

(2) Then we need to set up an embedding matrix to play as a lookup table, containing the embedding vectors of all the stocks. The matrix is initialized with random numbers between and and gets updated during training.

1
2
3
4
5
    # Don’t forget: config = RNNConfig()
    # Convert the integer labels to numeric embedding vectors.
    embedding_matrix = tf.Variable(
        tf.random_uniform([config.stock_symbol_size, config.embedding_size], -1.0, 1.0)
    )

(3) Repeat the stock labels num_steps times to match the unfolded version of RNN and the shape of inputs tensor during training. The transformation operation tf.tile receives a base tensor and creates a new tensor by replicating its certain dimensions multiples times; precisely the -th dimension of the input tensor gets multiplied by multiples[i] times. For example, if the stock_labels is [[0], [0], [2], [1]] tiling it by [1, 5] produces [[0 0 0 0 0], [0 0 0 0 0], [2 2 2 2 2], [1 1 1 1 1]].

1
    stacked_stock_labels = tf.tile(stock_labels, multiples=[1, config.num_steps])

(4) Then we map the symbols to embedding vectors according to the lookup table embedding_matrix.

1
2
    # stock_label_embeds.get_shape() = (?, num_steps, embedding_size).
    stock_label_embeds = tf.nn.embedding_lookup(embedding_matrix, stacked_stock_labels)

(5) Finally, combine the price values with the embedding vectors. The operation tf.concat concatenates a list of tensors along the dimension axis. In our case, we want to keep the batch size and the number of steps unchanged, but only extend the input vector of length input_size to include embedding features.

1
2
3
4
    # inputs.get_shape() = (?, num_steps, input_size)
    # stock_label_embeds.get_shape() = (?, num_steps, embedding_size)
    # inputs_with_embed.get_shape() = (?, num_steps, input_size + embedding_size)
    inputs_with_embed = tf.concat([inputs, stock_label_embeds], axis=2)

The rest of code runs the dynamic RNN, extracts the last state of the LSTM cell, and handles weights and bias in the output layer. See Part 1: Define the Graph for the details.

Training Session

Please read Part 1: Start Training Session if you haven’t for how to run a training session in Tensorflow.

Before feeding the data into the graph, the stock symbols should be transformed to unique integers with label encoding.

1
2
3
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
label_encoder.fit(list_of_symbols)

The train/test split ratio remains same, 90% for training and 10% for testing, for every individual stock.

Visualize the Graph

After the graph is defined in code, let us check the visualization in Tensorboard to make sure that components are constructed correctly. Essentially it looks very much like our architecture illustration in Fig. 1.

Visualization of RNN with embedding

Fig. 2. Tensorboard visualization of the graph defined above. Two modules, “train” and “save”, have been removed from the main graph.

Other than presenting the graph structure or tracking the variables in time, Tensorboard also supports embeddings visualization. In order to communicate the embedding values to Tensorboard, we need to add proper tracking in the training logs.

(0) In my embedding visualization, I want to color each stock with its industry sector. This metadata should stored in a csv file. The file has two columns, the stock symbol and the industry sector. It does not matter whether the csv file has header, but the order of the listed stocks must be consistent with label_encoder.classes_.

1
2
3
4
5
6
import csv
embedding_metadata_path = os.path.join(your_log_file_folder, 'metadata.csv')
with open(embedding_metadata_path, 'w') as fout:
    csv_writer = csv.writer(fout)
    # write the content into the csv file.
    # for example, csv_writer.writerows(["GOOG", "information_technology"])

(1) Set up the summary writer first within the training tf.Session.

1
2
3
4
from tensorflow.contrib.tensorboard.plugins import projector
with tf.Session(graph=lstm_graph) as sess:
    summary_writer = tf.summary.FileWriter(your_log_file_folder)
    summary_writer.add_graph(sess.graph)

(2) Add the tensor embedding_matrix defined in our graph lstm_graph into the projector config variable and attach the metadata csv file.

1
2
3
4
5
6
    projector_config = projector.ProjectorConfig()
    # You can add multiple embeddings. Here we add only one.
    added_embedding = projector_config.embeddings.add()
    added_embedding.tensor_name = embedding_matrix.name
    # Link this tensor to its metadata file.
    added_embedding.metadata_path = embedding_metadata_path

(3) This line creates a file projector_config.pbtxt in the folder your_log_file_folder. TensorBoard will read this file during startup.

1
    projector.visualize_embeddings(summary_writer, projector_config)

Results

The model is trained with top 100 stocks with largest market values in the S&P 500 index.

And the following configuration is used:

input_size=10
num_steps=30
lstm_size=256
num_layers=1,
keep_prob=0.8
batch_size = 200
init_learning_rate = 0.05
learning_rate_decay = 0.99
init_epoch = 5
max_epoch = 500
embedding_size = 8
stock_symbol_size = 100

Price Prediction

As a brief overview of the prediction quality, Fig. 3 plots the predictions for test data of “KO”, “AAPL”, “GOOG” and “NFLX”. The overall trends matched up between the true values and the predictions. Considering how the prediction task is designed, the model relies on all the historical data points to predict only next 5 (input_size) days. With a small input_size, the model does not need to worry about the long-term growth curve. Once we increase input_size, the prediction would be much harder.

Results Fig. 3. True and predicted stock prices of KO, AAPL, GOOG and NFLX in the test set. The prices are normalized across consecutive prediction sliding windows (See Part 1: Normalization.

Embedding Visualization

One common technique to visualize the clusters in embedding space is t-SNE (Maaten and Hinton, 2008), which is well supported in Tensorboard. t-SNE, short for “t-Distributed Stochastic Neighbor Embedding, is a variation of Stochastic Neighbor Embedding (Hinton and Roweis, 2002), but with a modified cost function that is easier to optimize.

  1. Similar to SNE, t-SNE first converts the high-dimensional Euclidean distances between data points into conditional probabilities that represent similarities.
  2. t-SNE defines a similar probability distribution over the data points in the low-dimensional space, and it minimizes the Kullback–Leibler divergence between the two distributions with respect to the locations of the points on the map.

Check this post for how to adjust the parameters, Perplexity and learning rate (epsilon), in t-SNE visualization.

Visualization of embeddings

Fig. 4. Visualization of the stock embeddings using t-SNE. Each label is colored based on the stock industry sector.

In the embedding space, we can measure the similarity between two stocks by examining the similarity between their embedding vectors. For example, GOOG is mostly similar to GOOGL in the learned embeddings (See Fig. 5).

Visualization of embeddings: GOOG

Fig. 5. When we search “GOOG” in the embeddings tab of Tensorboard, other similar stocks are highlighted with colors from dark to light as the similarity decreases.

The full code in this tutorial is available in github.com/lilianweng/stock-rnn.


If you notice mistakes and errors in this post, don’t hesitate to contact me at [lilian dot wengweng at gmail dot com] and I would be super happy to correct them right away!

Thanks! :)