The (Artificially) Intelligent Investor
Apr 20 2020
Last updated
Apr 20 2020
Last updated
Shakespeare plays, Stephen King novels, and even Kanye lyrics have all served as training data for recurrent neural networks (RNNs) that generate text. While these projects are intriguing, I wanted to find a more practical use for text generation and decided to explore if RNNs could form coherent investing advice. After considering a few options, I chose to train the model on a copy of The Intelligent Investor by Benjamin Graham, which has been quoted by Warren Buffet as “the best book on investing ever written”. The model’s output certainly didn’t reveal the secret to beating the market as we’ll later see, but it’s still interesting to ponder if AI will one day be capable of offering sound financial advice.
RNNs are analogous to human learning. When humans think, we don’t start our thinking from scratch each second. For example, in the sentence “Bob plays basketball”, we know that Bob is the person who plays basketball because we retain information about past words while reading sentences. Similarly, RNNs are neural networks with feedback loops, which allow them to use past information before arriving at a final output. However, RNNs can only connect recent information and cannot connect older information as the time gap grows. Gated Recurrent Units (GRUs) are an improved version of RNNs that overcome the short-term memory issue through an update gate that decides what information is relevant to pass on and a reset gate that decides what past information is irrelevant. For an in-depth explanation of GRUs, click here.
Author’s Note: Definition of RNNs reused from my previous article “Machine Learning to Predict Stock Prices”
To start off, we make the necessary imports: Tensorflow, Numpy, and os.
Next step is to download our data, which is a .txt file of the Intelligent Investor. I removed the preface, index and a few graphs from the file to help our model generate more relevant text. Once we download the file, we take a look at how many total characters are in it.
Let’s take a look at how many unique characters exist in the file.
Our model can’t understand letters so we have to vectorize the text. Each unique character is mapped to an integer for the computer to understand and integers are mapped to the characters so we can later decode the computer’s output.
We train the RNN model with the goal of teaching it to predict the most likely character after a given sequence of characters. To do this we will break input sequences from the text into an example sequence and target sequence. The target sequence is the example sequence shifted one character to the right, so the chunks of text have to be one character longer than the length of sequence. For example, if our text is “Stocks”, the example sequence would be “Stock” and the target sequence would be “tocks”.
We shuffle the data and segment it into batches before we train our model. The purpose of shuffling the data is to improve the performance of the model by avoiding overfitting, which is when the model learns the training data too closely and can’t generalize well to the test set.
With the data prepared for training, we create our model with three layers.
The Embedding layer is our input layer that maps the integer representation of each character into dense vectors of 256 dimensions.
The GRU layer is our hidden layer with 1024 RNN units.
The Softmax layer is our output layer with 109 potential outputs (one for each of our 109 unique characters).
Now, we compile our model with the Adam optimizer and sparse categorical cross entropy loss function.
Before we train our model, we make sure to save checkpoints during training. By saving checkpoints, we can quickly recreate our model with a different batch size and restore the saved weights instead of training it again.
Rebuild our model and load the weights with the batch size changed to 1, which makes the prediction simpler.
Summary of the new model
Here comes the moment of truth: our model finally reveals its investing advice! The temperature parameter affects the outputs we receive: a lower temperature results in a more conservative output while a higher temperature results in a more creative output that is prone to making more errors. We’ll see examples of this below.
Here’s the advice our model generates at a temperature of 0.1.
While the output isn’t even remotely close to any investing advice you should follow, it does a decent job of mimicking The Intelligent Investor. Due to the low temperature, our model doesn’t attempt to be creative and plays it safe by sticking to standard sentences in paragraph format. Let’s see how this changes as we increase the temperature.
At a temperature of 0.5, we can begin to see differences in the output. Our model tries to be more inventive and makes more errors as a result. An example of this is in the second to last line where parentheses are used incorrectly.
Now, the differences at a temperature of 1 are very apparent as our model attempts to generate tables. However, the tradeoff for this increased creativity is that the output becomes mostly incomprehensible. I’ve included a few more outputs at the various temperatures for reference.
As we saw, RNNs aren’t anywhere close to replacing investment advisors for now. With that being said, here are some ways that we can try to improve our model’s output.
Increase the number of epochs
Get a better training dataset (some formatting was messed up when converting The Intelligent Investor from a pdf to txt file)
Use an LSTM layer instead of GRU layer (LSTMs are another improved type of RNN)
[1] Google Team, Text generation with an RNN, Tensorflow Core
[2] Aurélien Géron, Natural Language Processing with RNNs and Attention, Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow
Don’t leave yet!
I’m Roshan, a 16 year old passionate about the intersection of artificial intelligence and finance. If you’re further interested in RNNs applied to finance, check out this article: https://towardsdatascience.com/predicting-stock-prices-using-a-keras-lstm-model-4225457f0233
Reach out to me on Linkedin: https://www.linkedin.com/in/roshan-adusumilli-96b104194/
Reference : https://towardsdatascience.com/the-artificially-intelligent-investor-379a180e199f