A financial neural network for quants
By: Nicolus Rotich
Tags: Algorithms, Towards Data Science, Artificial Neural Network, Artificial Intelligence, Finance

ALPHA-BETA CLASSIFIER

A financial neural network for quants

Background

Artificial intelligence (AI) is continuously becoming an integral part of what we do nowadays, from the suggestions of the YouTube videos we ought to watch, to the algorithms running our high frequency trading in the Wall Street. Indeed Google algorithms know what we prefer, better than what we want. That was according to the Twitter CEO and founder — Jack Dorsey. This would not be mentioned if it were not for pattern recognition and classification, which are the main underlying principles of AI.

Fig: 1 An Image of Humanoid robot. Photo Credit: Photo by Franck V. on Unsplash

Although the two principles have been undertaken for centuries purely through conventional statistical inference and mathematics, it took us (as a civilization) a while to recognize these processes were not good enough, rather they can be incredibly difficult to solve, if not clumsy and time consuming to say the least. Artificial neural networks forms the core of AI, as they are the main entities that have been used to successfully mimic unnecessarily voluntary, but evidently the right decisions nevertheless in many instances. Here we are referring to autonomous or self-driving cars, speech and facial recognition, objects tracking, and even humanoid robots.

As a result, there are many types of neural networks that have been put to use up to date for various purposes. For example: Feed forward neural networks, radial basis function neural nets, recurrent neural networks, convolutional neural networks, modular neural networks, and Kohonen self-organizing neural nets. The latter one is particularly dear to me, as professor Teuvo Kohonen — the proponent happens to be one of my biggest inspirational figures since I got to know about neural networks in general, and how they function. My first project on neural networks concerned prediction of wind speeds and directions as part of a feasibility study for setting up wind farms in various municipalities in Finland¹. Even though the concept was advanced, the very basics such as types of neural networks and which ones suits what problem are discussed in this reference. This article is however not about the types of neural networks, but a particular one as the title suggests — financial neural network. You will notice it doesn’t appear in the types of neural nets listed above, and may not appear elsewhere at all at the moment. It is one of those patterns that we come across in the relatively new playground of AI.

Introducing alpha-beta classifier for quants

Imagine this scenario, supposing that the survivors of RMS titanic (April 1912) could be pre-determined and foretold? that means you will have to assume you already knew the accident would occur — that’s kinda creepy. Let’s make it even more interesting, what if the number of men, women, and children could be told a part, and classified according to the injuries they would sustain, just by the position they occupied in the cruise ship. This was an unfortunate incident — let’s consider asset trading.

In a similar way, supposing that an asset that you are about to buy, say a stock, future, bonds, or even cryptocurrency could already be pre-determined in such a way that you could tell which trade will be profitable and which will not, even before you get into those trades. You would be more judicious in knowing what to trade right? This is what alpha-beta financial neural network is all about. With this type of neural network, you attempt to classify the trades you are about to enter, and assign a probability of whether it would be profitable and worthy to engage in or not. These are the decisions that usually determine the the “buy” and “sell” side signal of your strategy, in particular cases that you are writing a code for algorithmic trading.

Fig: 2 Photo Credit: Chris Liverani on Unsplash

The mathematics of alpha-beta classifier

This class of neural network is a classifier, with the input being a ratio of the current highest closing rate, “High” and opening, “Open” rates ratio as a single input, and a bias of -1. These are passed through a logistic activation function that scales the input to a probability distribution function between 0 and 1. To perform an illustration on this:

Fig: 3 Author’s illustration of ABC neural network

Let High be z, Open be x, and weights be w:

Eq. (1)

The symbol β is the bias, and in this case is a negative constant -1. The activation function α is a function of the weights that are initialized in the beginning and estimated as the classification progresses. Gradient descent is the ultimate measure of the performance efficiency of the model. Minimizing the gradient is equivalent to increasing the value of the probability distribution function, and in the process we get the following:

Fig 4: The activation function for alpha-beta classifier

Eq. (2)

A practical guide

Following along always makes it tenable if you seriously want to consider using this type of technology as you code your next trading algorithm. We will begin by listing the requirements to enable you accomplish this task:

First you need to acquaint yourself with using data frames in python.

Next you will need python3.6 installed on your system. You also need to have pandas and numpy basics.

Finally, this is not a must, but installing jupyter notebook or ipython will lessen your work to a large extent.

Data acquisition

We will make use on the standard asset information, presented in a table format with columns: ‘date’, ‘open’, ‘high’, ‘low’, ‘close’, ‘adj close’, ‘volume’. You can get this from yahoo finance, downloaded to your project folder. For this case study, we will make use of the ticker symbols ‘AAPL’ and ‘GOOG’, corresponding to historical stock data for Aple inc. and Google Inc. This will effectively download the data to your project.

Importing libraries

First, navigate to your project folder. This (the whole project rather) is best done when you can work on the command prompt, or Unix terminal. I will assume Linux terminal for this. Therefore ensure you have installed all the required software, and run the following commands to get started:

$ cd /your/project/root/directory/

$ jupyter notebook

The above should bring up a text editor window within your browser, similar to this one:

Fig 5: How Jupyter notebook project folder looks

Click the red-circled area to create a new document where we will place our code. We will then proceed to import the libraries as mentioned earlier. These are: pandas, numpy, and time as follows:

# import libraries

import pandas as pd

import numpy as np

import time

# Read in the data from the downloaded csv file(s) into a data frame

df = pd.read_csv('AAPL.csv')

Examining the data and pre-processing

To check out the data imported to your workspace, type the following and run the cell — clicking the red-rectangular selection in Fig. 2:

Fig 6: Executing commands in Jupyter notebook

This will result in something like this:

Fig 7: Checking the contents of a pandas data frame with command df.head()

We wish to change the cases for the column names for simplicity in proceeding with the code. We therefore replace the existing by running the following code:

df.columns = ['date', 'open', 'high', 'low', 'close', 'adjclose', 'volume']

You can do a whole lot of exercises here, for instance you can filter out only positive trades (pos) that you would have desired to enter — i.e. the trades that would have yielded profits had you entered into back then. To do this, simply key in the following code:

pos = df[df['change'] > 0 ]

You can do the same for the trades that ended up yielding negative results, or loses (neg). You might have desired either not to get involved in these trades, or only get in to buy — use them to create a buy signal. You would then key in:

neg = df[df['change']< 0 ]

Populating indicators

This classifier uses the simplest indicators ever. These are:

The ratio of closing to opening rates, which will be the input to the neural network, and

We will also compute the change that we will use as a guide, more like control experiment. The corresponding code is therefore placed as follows:

df['change'] = df['close'] - df['open']

df['ind'] = (df['high']/df['open']) -1

df.head()

Above code will create two extra columns, one with the ratio as discussed — let’s call this ‘ind’, and the other simply call it ‘change’ as we have always known. Notice that the variable ‘ind’ is already mirroring the LHS of Eq. (1). We will now proceed to compute the RHS in the following sections.

Weights initialization

We will start by assigning a set of linear weights using numpy library as follows:

w = np.linspace(-6,6,len(df))

The above code will define a linear set of parameters that will be used to compute the probability distribution function — or the activation function in Eq. (2). Alpha is then calculated as follows:

alpha = 1/(1+np.exp(-w))

Evaluating the algorithm

We are done. Now, we need to implement the actual equation in the data frame and check out the results. In the simplest terms, you will key in the following for every value of alpha that you compute from above.

test = df[df['ind'] >= alpha]

eval = test[test['change'] > 0 ]

Summing it all up

We have seen that it asset trading is not different from other phenomena where theoretical approaches for predictions exist. As such, asset trading can be classified based on their potential—rather, the trades themselves can be classified with the sole aim of deciding whether to enter or not. The key parameter is the projected state of profitability. The following code sums up all the steps outlined above:

# import libraries

import pandas as pd

import numpy as np

import time # used to imitate sleep mode during data streaming

from termcolor import colored # used to print stdr output in color

# Read in the data from the csv file

from contextlib import suppress

df = pd.read_csv('AAPL.csv')

# Reduce the column names to lower case

df.columns = ['date', 'open', 'high', 'low', 'close', 'adjclose', 'volume']

# compute the change to use as a guide in backtesting

df['change'] = df['close'] - df['open']

# populate the classifier

df['ind'] = (df['high']/df['open']) -1

# Initialize the weights

weights = np.linspace(-6,6,len(df))

# Fire the neural network def genFunc():

eta = 0

while eta <= 100:

for i in weights:

alpha = 1/(1+np.exp(-i))

test = df[df['ind'] >= alpha]

pos = test[test['change'] > 0]

eta = len(pos)*100/len(test)

yield(alpha) # Use generator function to extract the values of alpha

with suppress(ZeroDivisionError):

for i in genFunc():

print(i, end=" ")

Once you have determined the minimum value of probability or alpha of trades to get into, the buy and sell signals can simply be written as:

dataframe[dataframe['ind'] < min_alpha ] ----> buy

dataframe[dataframe['ind'] >= min_alpha ] ---> sell

Key Results

The resulting table is a standard output of the (longer) code above printed to your screen. Check out this scenario:

Let’s suppose that the final weight, w you settle on is ‘-4.23’, the activation function, α would yield ‘0.0143’, and the probability is thus 0.90566 — marked in the figure 8. As seen on the table there are three columns first: the weights, second column: alpha value, third the efficiency of the model, and finally: the number of trades entered onto.

This means that had you entered that trade, there is a 90.6% chance that you would have emerged with a profit, and conversely, a 9.4% chance that you would come out with a loss. As of how this percentage is calculated, we simply compute the length of the data frame with the variable ‘change’ greater than zero in the ‘eval’ data frame, and compare with the total length of the data frame ‘test’.

Fig 8: Standard output of the guiding case study code

A note on Pros, Cons and where to get help

Pros

Alpha-Beta classifier is quite accurate — at least from back testing results.

Theoretically, it can guarantee as high as 100% classification efficiency.

The best feature however, is that the higher the model efficiency, meaning fewer trades, the higher not only the the reward, but also fewer chances to lose. Hence the feature acts like a stop loss of some kind. This implies that you can choose to get in to just a few trades but with high potential for profitability. On the other hand, you may choose to enter into many trades, each with little benefits. This will help you in diversifying the risk, rather than concentrating all in one place.

Finally, alpha-beta classifier supports Do Not Repeat Yourself (DRY). One model may fit more than one situations. For instance, download Google Inc. historical data (saved as GOOG.csv), with everything else remaining the same, you should find that the model still works — sometimes even better.

Cons

However, one major setback is that the higher the model efficiency increases, the fewer the number of trades you get into.

F1#Help

This is a new concept that could prove helpful especially for quants who likes to experiment and build up on it. If you are such a person, using this as a starting point and need help, let me know and I will give as much support as possible.

References

[1] Rotich, N.K., Backman, J., Linnanen, L. and Daniil, P., 2014. Wind resource assessment and forecast planning with neural networks. Journal of Sustainable Development of Energy, Water and Environment Systems, 2(2), pp.174–190.