Utilizing CNN for monetary time collection prediction


Final Up to date on November 20, 2021

Convolutional neural networks have their roots in picture processing. It was first printed in LeNet to acknowledge the MNIST handwritten digits. Nonetheless, convolutional neural networks are usually not restricted to dealing with photographs.

On this tutorial, we’re going to take a look at an instance of utilizing CNN for time collection prediction with an software from monetary markets. By the use of this instance, we’re going to discover some strategies in utilizing Keras for mannequin coaching as effectively.

After finishing this tutorial, you’ll know

  • What a typical multidimensional monetary knowledge collection appears like?
  • How can CNN utilized to time collection in a classification drawback
  • Tips on how to use turbines to feed knowledge to coach a Keras mannequin
  • Tips on how to present a customized metric for evaluating a Keras mannequin

Let’s get began

Using CNN for financial time series prediction

Utilizing CNN for monetary time collection prediction
Picture by Aron Visuals, some rights reserved.

Tutorial overview

This tutorial is split into 7 components; they’re:

  1. Background of the concept
  2. Preprocessing of knowledge
  3. Information generator
  4. The mannequin
  5. Coaching, validation, and check
  6. Extensions
  7. Does it work?

Background of the concept

On this tutorial we’re following the paper titled “CNNpred: CNN-based inventory market prediction utilizing a iverse set of variables” by Ehsan Hoseinzade and Saman Haratizadeh. The info file and pattern code from the writer can be found in github:

The aim of the paper is easy: To foretell the following day’s course of the inventory market (i.e., up or down in comparison with right this moment), therefore it’s a binary classification drawback. Nonetheless, it’s attention-grabbing to see how this drawback are formulated and solved.

We now have seen the examples on utilizing CNN for sequence prediction. If we contemplate Dow Jones Industrial Common (DJIA) for instance, we might construct a CNN with 1D convolution for prediction. This is smart as a result of a 1D convolution on a time collection is roughly computing its transferring common or utilizing digital sign processing phrases, making use of a filter to the time collection. It ought to present some clues concerning the pattern.

Nonetheless, after we take a look at monetary time collection, it’s fairly a standard sense that some derived indicators are helpful for predictions too. For instance, worth and quantity collectively can present a greater clue. Additionally another technical indicators such because the transferring common of various window dimension are helpful too. If we put all these align collectively, we could have a desk of knowledge, which every time occasion has a number of options, and the aim continues to be to foretell the course of one time collection.

Within the CNNpred paper, 82 such options are ready for the DJIA time collection:

Excerpt from the CNNpred paper exhibiting the record of options used.

Not like LSTM, which there’s an express idea of time steps utilized, we current knowledge as a matrix in CNN fashions. As proven within the desk under, the options throughout a number of time steps are introduced as a 2D array.

Preprocessing of knowledge

Within the following, we attempt to implement the concept of the CNNpred from scratch utilizing Tensorflow’s keras API. Whereas there’s a reference implementation from the writer within the github hyperlink above, we reimplement it in a different way as an example some Keras strategies.

Firstly the info are 5 CSV recordsdata, every for a distinct market index, underneath the Dataset listing from github repository above, or we are able to additionally get a duplicate right here:

The enter knowledge has a date column and a reputation column to determine the ticker image for the market index. We will depart the date column as time index and take away the identify column. The remaining are all numerical.

As we’re going to predict the market course, we first attempt to create the classification label. The market course is outlined because the closing index of tomorrow in comparison with right this moment. If we’ve learn the info right into a pandas DataFrame, we are able to use X["Close"].pct_change() to seek out the proportion change, which a constructive change for the market goes up. So we are able to shift this to 1 time step again as our label:

The road of code above is to compute the proportion change of the closing index and align the info with yesterday. Then convert the info into both 1 or 0 for whether or not the proportion change is constructive.

For 5 knowledge file within the listing, we learn every of them as a separate pandas DataFrame and hold them in a Python dictionary:

The results of the above code is a DataFrame for every index, which the classification label is the column “Goal” whereas all different columns are enter options. We additionally normalize the info with a normal scaler.

In time collection issues, it’s typically cheap to not break up the info into coaching and check units randomly, however to arrange a cutoff level during which the info earlier than the cutoff is coaching set whereas that afterwards is the check set. The scaling above are primarily based on the coaching set however utilized to the whole dataset.

Information generator

We’re not going to make use of all time steps directly, however as a substitute, we use a set size of N time steps to foretell the market course at step N+1. On this design, the window of N time steps can begin from wherever. We will simply create numerous DataFrames with great amount of overlaps with each other. To save lots of reminiscence, we’re going to construct a knowledge generator for coaching and validation, as follows:

Generator is a particular operate in Python that doesn’t return a worth however to yield in iterations, such {that a} sequence of knowledge are produced from it. For a generator for use in Keras coaching, it’s anticipated to yield a batch of enter knowledge and goal. This generator alleged to run indefinitely. Therefore the generator operate above is created with an infinite loop begins with whereas True.

In every iteration, it randomly choose one DataFrame from the Python dictionary, then inside the vary of time steps of the coaching set (i.e., the start portion), we begin from a random level and take N time steps utilizing the pandas iloc[start:end] syntax to create a enter underneath the variable body. This DataFrame might be a 2D array. The goal label is that of the final time step. The enter knowledge and the label are then appended to the record batch. Till we gathered for one batch’s dimension, we dispatch it from the generator.

The final 4 traces on the code snippet above is to dispatch a batch for coaching or validation. We accumulate the record of enter knowledge (every a 2D array) in addition to an inventory of goal label into variables X and y, then convert them into numpy array so it could actually work with our Keras mannequin. We have to add yet another dimension to the numpy array X utilizing np.expand_dims() due to the design of the community mannequin, as defined under.

The Mannequin

The 2D CNN mannequin introduced within the authentic paper accepts an enter tensor of form $Ntimes m occasions 1$ for N the variety of time steps and m the variety of options in every time step. The paper assumes $N=60$ and $m=82$.

The mannequin contains of three convolutional layers, as described as follows:

and the mannequin is introduced by the next:

The primary convolutional layer has 8 items, and is utilized throughout all options in every time step. It’s adopted by a second convolutional layer to think about three consecutive days directly, for it’s a frequent perception that three days could make a pattern within the inventory market. It’s then utilized to a max pooling layer and one other convolutional layer earlier than it’s flattened right into a one-dimensional array and utilized to a fully-connected layer with sigmoid activation for binary classification.

Coaching, validation, and check

That’s it for the mannequin. The paper used MAE because the loss metric and in addition monitor for accuracy and F1 rating to find out the standard of the mannequin. We should always level out that F1 rating depends upon precision and recall ratios, that are each contemplating the constructive classification. The paper, nevertheless, contemplate the common of the F1 from constructive and destructive classification. Explicitly, it’s the F1-macro metric:
F_1 = frac{1}{2}left(
frac{2cdot frac{TP}{TP+FP} cdot frac{TP}{TP+FN}}{frac{TP}{TP+FP} + frac{TP}{TP+FN}}
frac{2cdot frac{TN}{TN+FN} cdot frac{TN}{TN+FP}}{frac{TN}{TN+FN} + frac{TN}{TN+FP}}
The fraction $frac{TP}{TP+FP}$ is the precision with TP and FP the variety of true constructive and false constructive. Equally $frac{TP}{TP+FN}$ is the recall. The primary time period within the massive parenthesis above is the traditional F1 metric that thought of constructive classifications. And the second time period is the reverse, which thought of the destructive classifications.

Whereas this metric is out there in scikit-learn as sklearn.metrics.f1_score() there is no such thing as a equal in Keras. Therefore we might create our personal by borrowing code from this stackexchange query:

The coaching course of can take hours to finish. Therefore we wish to save the mannequin in the midst of the coaching in order that we might interrupt and resume it. We will make use of checkpoint options in Keras:

We arrange a filename template checkpoint_path and ask Keras to fill within the epoch quantity in addition to validation F1 rating into the filename. We put it aside by monitoring the validation’s F1 metric, and this metric is meant to extend when the mannequin will get higher. Therefore we cross within the mode="max" to it.

It ought to now be trivial to coach our mannequin, as follows:

Two factors to notice within the above snippets. We provided "acc" because the accuracy in addition to the operate f1macro outlined above because the metrics parameter to the compile() operate. Therefore these two metrics might be monitored throughout coaching. As a result of the operate is known as f1macro, we check with this metric within the checkpoint’s monitor parameter as val_f1macro.

Individually, within the match() operate, we supplied the enter knowledge by means of the datagen() generator as outlined above. Calling this operate will produce a generator, which in the course of the coaching loop, batches are fetched from it one after one other. Equally, validation knowledge are additionally supplied by the generator.

As a result of the character of a generator is to dispatch knowledge indefinitely. We have to inform the coaching course of on how you can outline a epoch. Recall that in Keras phrases, a batch is one iteration of doing gradient descent replace. An epoch is meant to be one cycle by means of all knowledge within the dataset. On the finish of an epoch is the time to run validation. Additionally it is the chance for operating the checkpoint we outlined above. As Keras has no approach to infer the scale of the dataset from a generator, we have to inform what number of batch it ought to course of in a single epoch utilizing the steps_per_epoch parameter. Equally, it’s the validation_steps parameter to inform what number of batch are utilized in every validation step. The validation doesn’t have an effect on the coaching, however it’ll report back to us the metrics we have an interest. Beneath is a screenshot of what we’ll see in the midst of coaching, which we’ll see that the metric for coaching set are up to date on every batch however that for validation set is supplied solely on the finish of epoch:

After the mannequin completed coaching, we are able to check it with unseen knowledge, i.e., the check set. As a substitute of producing the check set randomly, we create it from the dataset in a deterministic approach:

The construction of the operate testgen() is resembling that of datagen() we outlined above. Besides in datagen() the output knowledge’s first dimension is the variety of samples in a batch however in testgen() is the the whole check samples.

Utilizing the mannequin for prediction will produce a floating level between 0 and 1 as we’re utilizing the sigmoid activation operate. We’ll convert this into 0 or 1 through the use of the brink at 0.5. Then we use the capabilities from scikit-learn to compute the accuracy, imply absolute error and F1 rating (which accuracy is only one minus the MAE).

Tying all these collectively, the entire code is as follows:


The unique paper referred to as the above mannequin “2D-CNNpred” and there’s a model referred to as “3D-CNNpred”. The thought isn’t solely contemplate the various options of 1 inventory market index however cross evaluate with many market indices to assist prediction on one index. Discuss with the desk of options and time steps above, the info for one market index is introduced as 2D array. If we stack up a number of such knowledge from completely different indices, we constructed a 3D array. Whereas the goal label is identical, however permitting us to have a look at a distinct market might present some extra info to assist prediction.

As a result of the form of the info modified, the convolutional community additionally outlined barely completely different, and the info turbines want some modification accordingly as effectively. Beneath is the entire code of the 3D model, which the change from the earlier second model needs to be self-explanatory:

Whereas the mannequin above is for next-step prediction, it doesn’t cease you from making prediction for ok steps forward in case you exchange the goal label to a distinct calculation. This can be an train for you.

Does it work?

As in all prediction tasks within the monetary market, it’s at all times unrealistic to anticipate a excessive accuracy. The coaching parameter within the code above can produce barely greater than 50% accuracy within the testing set. Whereas the variety of epochs and batch dimension are intentionally set smaller to save lots of time, there shouldn’t be a lot room for enchancment.

Within the authentic paper, it’s reported that the 3D-CNNpred carried out higher than 2D-CNNpred however solely attaining the F1 rating of lower than 0.6. That is already doing higher than three baseline fashions talked about within the paper. It might be of some use, however not a magic that may allow you to generate profits fast.

From machine studying method perspective, right here we classify a panel of knowledge into whether or not the market course is up or down the following day. Therefore whereas the info isn’t a picture, it resembles one since each are introduced within the type of a 2D array. The strategy of convolutional layers can subsequently utilized, however we might use a distinct filter dimension to match the instinct we often have for monetary time collection.

Additional readings

The unique paper is out there at:

In case you are new to finance software and wish to construct the connection between machine studying strategies and finance, chances are you’ll discover this ebook helpful:

On the same subject, we’ve a earlier submit on utilizing CNN for time collection, however utilizing 1D convolutional layers;

You may additionally discover the next documentation useful to clarify some syntax we used above:


On this tutorial, you found how a CNN mannequin may be constructed for prediction in monetary time collection.

Particularly, you realized:

  • Tips on how to create 2D convolutional layers to course of the time collection
  • Tips on how to current the time collection knowledge in a multidimensional array in order that the convolutional layers may be utilized
  • What’s a knowledge generator for Keras mannequin coaching and how you can use it
  • Tips on how to monitor the efficiency of mannequin coaching with a customized metric
  • What to anticipate in predicting monetary market


Leave a Reply

Your email address will not be published. Required fields are marked *