A Soft Introduction to Recurrent Neural Networks and Applications in Volatile Trend Analysis

 

Introduction

There are patterns all around us. For example, the I-15 through Salt Lake will always be busiest at 7:00 AM and 5:30 PM. This situation can easily be explained by an external effect: the end of the working day. In many cases there is a simple explanation for why a pattern occurs, but often there are several complex variables at play that make it difficult to predict the direction and confidence of a trend. However, what if there was insight within the trend itself?

Patterns in the Stock Market

One example of an area that is very difficult/impossible to predict are stock market prices. Several factors go into the price of a stock. These can include company earnings, investor expectations, emotions such as greed and fear, and macro-economic variables. Some of these can be measured, but others are subjective and create a significant amount of volatility and noise. The process of fundamental analysis seeks to measure these variables and predict a stocks fair market value. The goal of this type of analysis is to give a long-term view of the health and direction of a stock.

The other common way to predict stock prices is through technical analysis. Technical analysis focuses on stock price trends to highlight short term trends that a stock may take. Some of the tools that are used in technical analysis include oscillators, momentum indicators, and moving averages. Technical analysis operates under the assumption that history will repeat itself from trends that have previously been observed. These patterns are called technical indicators and a few examples are shown below:

Figure 1 Cup and Handle trend shown by a cup shape, sharp decreasing handle shape, and then sharp rise. Credit Investopedia

Figure 2 Head and Shoulders trend. Can show either a bullish or bearish reversal. Credit Investopedia

While these patterns are interesting, it is difficult to accurately identify when these trends may be occurring. Also, could these trends possibly exhibit themselves differently in different markets? Because of its high efficiency at persisting continuous data, I believe that a recurrent neural network can help us uncover potential short-term trends. I will explain how it can do this in the next section.

Recurrent Neural Networks

A recurrent neural network is derived from the traditional neural network where nodes are linked in a continuous sequence that simultaneously process new data as information and “remember” previous information. This results in a prediction at each node/time-step. We can see this process shown below where input Xt is given at step “t” and the output Yt is given. Yt is then fed forward to the hidden node and together with new data at Xt+1, the output Yt+1 is given, and so on.

Figure 3 Recurrent Neural Network example. Credit IBM

Simple Example of Using RNN to Predict a Sine Wave

Let’s show an example of how an RNN can be used to predict future patterns by predicting the pattern of a sine wave from a segment of the data. My code for this can be accessed at this link.

We first start by creating a training dataset. This data is simply a vector of numbers from 0 to 10 by increments of 0.1 that were passed through the sine function as seen below.

We then create our test data that will be passed into our trained RNN model. Since we are trying to predict a continuous pattern, we will grab the values of 10 to 20 incrementing by 0.1.



After training the model and fitting it to our test data we get the following output:


As we can see, the results are not perfect, but they are accurate at predicting what the sine function is.

Conclusion: Extrapolating Method to Complex Patterns

As expected, areas like the stock market will not have patterns that are as simple as a sine wave. However, according to the theory that forms the foundation of technical analysis: markets flow in repeatable, quantifiable patterns. If this is true, we should be able to distinguish, in the short term, when a predictable pattern is occurring, and a level of confidence associated with that pattern.


Comments

Popular posts from this blog

Technical Analysis Stock Predictor System – Final Report

Initial attempt to predict short-term stock changes using a Gated Recurrent Unit on Bitcoin stock price Data