An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN...

25
DEGREE PROJECT, IN , FIRST LEVEL COMPUTER SCIENCE STOCKHOLM, SWEDEN 2015 An evolutionary approach to time series forecasting with artificial neural networks CARL SANDSTRÖM KTH ROYAL INSTITUTE OF TECHNOLOGY CSC SCHOOL

Transcript of An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN...

Page 1: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

DEGREE PROJECT, IN , FIRST LEVELCOMPUTER SCIENCE

STOCKHOLM, SWEDEN 2015

An evolutionary approach to timeseries forecasting with artificial neuralnetworks

CARL SANDSTRÖM

KTH ROYAL INSTITUTE OF TECHNOLOGY

CSC SCHOOL

Page 2: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

An evolutionary approach to time seriesforecasting with artificial neural networks

CARL SANDSTRÖM

Bachelor’s Thesis at CSCSupervisor: Pawel HermanExaminer: Örjan Ekeberg

May 2015

Page 3: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time
Page 4: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Abstract

In this paper an evolutionary approach to forecastingthe stock market is tested and compared with backpropaga-tion. An neuroevolutionary algorithm is implemented andbacktested measuring returns and the normalized-mean-square-error for each algorithm on selected stocks from NAS-DAQ. The results are not entirely conclusive and further in-vestigation would be needed to say definitely, but it seemsas a neuroevolutionary approach could outperform back-propagation for time series prediction.

Page 5: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Referat

I denna uppsats testas en evolutionär strategi för attförutsäga aktiemarknaden och jämförs sedan med backpro-pagation. En neuro-evolutionär algoritm implementeras ochtestas, där avkastning och det normaliserade-genomsnitts-kvadrat-felet mäts för varje algoritm på de utvalda akti-erna från NASDAQ. Resultaten är inte helt entydiga ochvidare utredning skulle behövas för att säga definitivt, mendet verkar som ett neuroevolutionärt tillvägagångssätt kanöverträffa backpropagation för att förutsäga tidsserier.

Page 6: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Contents

1 Introduction 11.1 Problem statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.1.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Background 32.1 Stock Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.1.1 Algorithmic trading . . . . . . . . . . . . . . . . . . . . . . . 32.2 Artificial neural network . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.2.1 Neuroevolution . . . . . . . . . . . . . . . . . . . . . . . . . . 42.2.2 Backpropagation . . . . . . . . . . . . . . . . . . . . . . . . . 4

3 Method 73.1 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3.1.1 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.1.2 Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.1.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.1.4 Trading strategy . . . . . . . . . . . . . . . . . . . . . . . . . 83.1.5 Error functions . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3.2 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.2.1 Third party libraries . . . . . . . . . . . . . . . . . . . . . . . 83.2.2 NEAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.2.3 Backpropagation . . . . . . . . . . . . . . . . . . . . . . . . . 93.2.4 Benchmark . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3.3 Measuring the Artificial neural network . . . . . . . . . . . . . . . . 9

4 Results 11

5 Discussion 155.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Bibliography 17

Page 7: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time
Page 8: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Chapter 1

Introduction

Being able to predict the stock market does not only have monetary benefits but asnowadays the stock market is a major part of a country’s economy, understandingit is becoming more and more important. Several methods exists for predicting thestock market and one of the most common is to use software that use graphical andstatistical approaches to forecast the market [2]. This approach does still need atrader to execute the trades. This is a type of algorithmic trading, an other typeis where the computer does not need a trader to execute the trades but does thisautomatically.

A method of algorithmic trading that is becoming more and more popular inthe financial world is using artificial neural network(ANN) to forecast stocks andthen trade base on the forecasted values[10], this can be used either for automatingtrade or to help a trader. ANN has on paper characteristics that both speaksfor and against the use of it in time series. Neural networks are universal functionapproximators and can map any nonlinear function[9], due to this flexibility they areexcellent methods for eg. pattern recognition, classification and forecasting. Someresearches [4] show that ANN preform better then most statistical approaches infinancial forecasting. They are however not that well suited to operate on datacontaining a lot of noise compared to other methods, and due to their complexnature are hard to gain and optimal solution without for example overfittning.

To be able to forecast a time series the neural network has to be trained, themost common way of doing this is backpropagation. This technique requires thearchitecture to be known beforehand.

1.1 Problem statement

One major problem when using ANN is to decide the architecture of the network,neuroevolution aims to solve this by not only evolving the weights but the archi-tecture as well, thus eliminating the need for a fixed network architecture that thebackpropagation algorithm needs.

In this paper an evolutionary approach to training artificial neural networks for

1

Page 9: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

CHAPTER 1. INTRODUCTION

forecasting will be used and then be compared with an artificial neural networktrained with backpropagation to assess the success of the evolutionary approach.

Can an evolutionary approach to training achieved better results than back-propagation?

1.1.1 ScopeThis report will compare two different approaches for training ANN and use the re-sulting ANN:s to attempt to predict three different stocks on the NASDAQ exchangefor a time period of two years. It will explore the use of evolutionary algorithms fortraining ANN on the stock exchange.

2

Page 10: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Chapter 2

Background

2.1 Stock Market

A stock is essentially a part of a company and gives the owner of the stock the rightto a part of the company’s current and future net assets. These stocks is bought andsold by investors on various stock markets around the world. The stock market is asupply and demand market and has a lot of participants dedicating time and moneyto develop models and techniques to assess the trend of the market for monetarygain.

2.1.1 Algorithmic trading

All kind of trading that is partly or completely automated by algorithms is referredto as algorithmic trading. By letting computers do the calculations and decisions themarket has changed completely in a relatively short time, through the use of theircomputational and storage powers the trading opportunities has extended beyondwhat we thought was possible.

Algorithmic trading can mainly be divided into two categories, high and lowspeed trading. The most used high speed trading is called High-Frequency tradingand utilizes very low latency and trades on the tiny fluctuations that exists withinthe market all the time, a position is not held for very long. Low speed holds theposition much longer and it is very important to be able to assess the market trendand then make a decision based on that.

Both of these uses signals as input to algorithms which then decides if the tradershould for example open or close a position.

2.2 Artificial neural network

Artificial neural networks are designed to replicate how the neural receptors work inour brains. A network can as mentioned before approximate any nonlinear function,but needs to be trained on data to be able to do that.

3

Page 11: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

CHAPTER 2. BACKGROUND

A neural network is built out of three types of layers, one input layer, one outputlayer and a number of hidden layers, normally one. Every layer is made up of anumber of nodes. Between these nodes are connections with weights, it is theseweights that decide the output of the network. How they are connected and howmany nodes a layer consists of is its architecture. One of the biggest challenges is todecide the architecture of the neural network and will be something the programmerwill tweak for weeks if not months to get perfect as it is a very big part of the successor failure of the ANN.

An ANN need to be trained to be able to be able to make accurate predictions.When training a training data set is given to the training function containing theinput data for the network and the target value one wish the network to output. Theaccuracy of the network is usually calculated using one or several error functions.

For more on ANN see [5].The types of training that will be used in this paper is backpropagation and

neuroevolution.

2.2.1 Neuroevolution

Neuroevolution is an evolutionary approach to training neural network and it triesto replicate how natural selection works in nature. It can either be used only tochange the weights of the network or change both the weights and the architecture.

The first step when using an evolutionary approach while programming is tocreate a population of possible solutions, then evaluate them to assess each indi-vidual’s fitness. After the population’s fitness has been assess a new population iscreated using the former population. This is done by using the individuals withthe best fitness and mating them with each other. The offspring is then added tothe new population but does also have the possibility to be mutated before beingadded. By doing this over and over again for the specified number of generationsthe best solution can be found.

An advantage of an evolutionary approach is that the target output does nothave to be known as long as there is a good way of evaluating the fitness of anindividual without it.

For this paper Neuroevolution of Augmented Topologies (NEAT) where used,which is among the most successful neuroevolutionary algorithms and has beensuccessfully applied in a number of areas, including finance. NEAT uses the evo-lutionary approach described above to train ANN by changing both weights andarchitecture. For more about NEAT see[8].

2.2.2 Backpropagation

Backpropagation is among the most common algorithms for training ANN:s, it re-quires the user to provide the outputs that the ANN is going to try to predict duringtraining, it also requires the network architecture to be fixed. The backpropagationalgorithm has two steps.

4

Page 12: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

2.2. ARTIFICIAL NEURAL NETWORK

First propagation, this involves giving the network the training data and com-paring the networks output with the target output the network was going to try topredict, to generate the error of each node in the network. Second step is to updatethe ANN weights by first getting the gradient of the weight and then subtracting apercentage of the gradient from the weight. This percentage is called the learningrate, the accuracy and speed of the training is decided by the learning rate, lowlearning rate means more accurate training while high learning rate means fastertraining. For more on backpropagation see[5].

5

Page 13: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time
Page 14: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Chapter 3

Method

3.1 Setup

3.1.1 Data

The data used for benchmarking and training of the neural networks was down-loaded from Yahoo! finance api[11]. Daily data from 2003 to 2014 was taken andwere divided so that 75% of the data was used for training and the remaining 25%used for benchmarking the two ANN:s. These percentages comes from experimen-tation by the author.

The stocks downloaded all comes from NASDAQ[6], where selected stocks areamong the highest traded and where selected because of their characteristics.

Apple(AAPL) where selected because during the testing period it went consis-tently up. Microsoft(MSFT) because its value where constant, not going much upan down. And Yahoo(YHOO) because it is quite volatile, constantly going up anddown.

3.1.2 Inputs

To get the inputs to the neural network a sliding window approach was used. Thismeans that depending on the window size the inputs to the network is the last valuesback in time in the size of the window. For this experiment a window-size of 30 wasused. This means that to predict the next days closing price the 30 days before istaken into account. The window-size was decided based on experimentation by theauthor.

For every day close, open, high, low and volume was taken.The values was standardized using minmax-scaling modified to output values to

the range of [-1,1] using the entire window before being sent to the network.

zi = 2 xi − min(x)max(x) − min(x) − 1

7

Page 15: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

CHAPTER 3. METHOD

3.1.3 Output

The output of the network is the forecasted close for the next day normalized tothe range [0,1]. By reversing the minmax-scaling the close price can be extractedfrom the output of the neural network.

xi+1 = Output(max(x) − min(x)) + min(x)

3.1.4 Trading strategy

The implemented trading strategy is very simple and is only used as a simple wayto benchmark the two different approaches.

if Output ≥ Close buy else sell

3.1.5 Error functions

In the implementation two error functions are used, RMSE and SSE.

RMSE =

√∑ni=1(yi − yi)2

n

SSE =n∑

i=1(yi − yi)2

3.2 Implementation

3.2.1 Third party libraries

Due to limited time and knowledge some third party libraries was used for imple-menting.

For the NEAT implementation a python library called peas[3] was used. Thislibrary has a working NEAT algorithm that was very simple to implement with thebenchmarking program that was created for this paper.

For the ANN implementation with backpropagation the library neurolab[7] wasused.

3.2.2 NEAT

The NEAT algorithm has many parameters but for this experiment the default set-tings defined by the creator Stanley in his paper [8] where used. The only parameterschanges was the size of the population to 100 and the number of generations to 500.Both of because of time limitations.

For evaluating the individuals of the population the error function RMSE wasused over the entire training set and then used as fitness.

8

Page 16: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

3.3. MEASURING THE ARTIFICIAL NEURAL NETWORK

The network used is in the beginning only input and output nodes but is duringtraining evolved by adding hidden nodes and connections.

3.2.3 BackpropagationFor the backpropagation 500 epochs was used with the error function SSE. Thenetwork used is a fully connected feedforward network with 10 hidden nodes, thenumber of nodes is based on tests by the author. An learning rate of 0.01 was usedfor all test.

3.2.4 BenchmarkThe benchmark was created to be very simple and does not take into account anyslippage or commission. A CSV file containing the data is loaded into the programand the algorithm loops over the content. When an order is made by the tradingalgorithm assets is extracted out of the imaginary portfolio and is returned whenthe algorithm closes the position again.

Before the benchmarking algorithm start to trade 75% of the data is used totrain the neural network. Depending on which training approach is going to betested either the NEAT algorithm or the backpropagation algorithm is used. Thetrained neural network is then sent to and used in the main benchmarking.

3.3 Measuring the Artificial neural networkThe formula for calculating the return for an ANN For calculating the return thisformula where used.

Return = Obtained money − Seed money

Seed money

Where obtained money is the amount of money after the benchmark has runand seed money the amount of money before.

To measure how good the ANN fit to the target data normalized mean squarederror (NMSE) is used, which where also used to evaluate entries into the Santa FeTime Series Competition[1].

NMSE =∑n

i=1(xi − xi)2∑ni=1(xi − xi)2

Where x is the actual values, x is the mean and x is the predicted value.

9

Page 17: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time
Page 18: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Chapter 4

Results

The table and graphs presented in this chapter is the results from the benchmarksrun on the historical data from the selected stocks. For every stock five benchmarkswere run using the evolutionary approach and then using backpropagation, the bestresult from for each type where then taken. During a benchmark only one stockcould be traded for the entirety of the benchmark, this was to see if the differentapproaches performed differently on different stocks.

Security Network NMSE Return

AAPL Backp 0,02415 22,10%NEAT 0,07678 56,81%

MSFT Backp 0,01218 00,50%NEAT 0,00469 13,19%

YHOO Backp 0,08072 16,27%NEAT 0,00201 16,69%

Table 4.1. Results from the benchmarks

11

Page 19: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

CHAPTER 4. RESULTS

Figure 4.1. Return over time from the benchmark on AAPL

Figure 4.2. Return over time from the benchmark on MSFT

12

Page 20: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Figure 4.3. Return over time from the benchmark on YHOO

13

Page 21: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time
Page 22: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Chapter 5

Discussion

Overall a patter can be seen in the results, the neuroevlutionary approach achievedsignificantly better returns in most stocks except for the YHOO stocks, where bothalgorithms preformed basically the same. The success or lack thereof for the twoalgorithms can be because a number of reasons, as in most complex programs orsystems there are numerous parameters to take into account. The trading strategyused where not the most sophisticated and did not take into account any trends andcould therefore sell or buy to early or late. Neither of the approaches to traininggot to run for a very long time, this due to time limitations, which could result ina not completely trained ANN was used during the benchmarking phase, which inturn could have lead to stock being bought or sold when it shouldn’t have. Thedata feed to the neural networks was not smoothed which due to ANN:s problemswith noisy data could have slowed down the training. The window-size used couldhave been too small, leading to the networks not being given enough informationto make a good decision, or too big giving the ANN to much information.

In general better NMSE score did not seam to correlate to higher returns. NEATpreformed basically the same as backpropagation only on YHOO which could bebecause during the time of testing and training the stock where much more volatilethen AAPL, which has been increased in value constantly, and MSFT who’s valuehas been pretty constant. This could be either the result of the neuroevolutionaryapproach having problem to adapt to a time series that is constantly changing orbecause the algorithm did not have sufficient time to gain an optimal solution andshould have been trained longer, but that can be said about both approaches.

To achieve better results in the future one should drastically increase the timeeach algorithms trains as this is the most important and computationally heavyprocess when using ANN. During testing a fixed number of epochs or generationwhere set, this should if one has the computational power be set so that instead ofa fixed number the training should continue until a satisfactory solution has beenfound. The parameters should also be tweaked further, but as that is a majortask this was outside the scope of this paper. The benchmarking algorithm couldbe improved as well to take into account commission, which would mean that the

15

Page 23: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

CHAPTER 5. DISCUSSION

number of trades by each algorithm would have been taken into account as well,rewarding the algorithm that could get the best return in the least amount of trades.

5.1 ConclusionTo answer this paper’s problem statement "Can an evolutionary approach to trainingachieved better results than backpropagation?", the results here are not entirelyconclusive and further investigation would be needed to say definitely, but it seemsas a neuroevolutionary approach could outperform backpropagation for time seriesprediction and therefor stock prediction.

16

Page 24: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

Bibliography

[1] N.A. Gershenfeld A.S. Weigend. “Time Series Prediction: Forecasting the Fu-ture and Understanding the Past”. In: Proceedings of the NATO AdvancedResearch Workshop on Comparative Time Series Analysis (1994).

[2] Andrew Beattie. The Basics Of Business Forecasting. Accessed: 2015-05-11.2011. url: http://www.investopedia.com/articles/financial-theory/11/basics-business-forcasting.asp.

[3] T. van den Berg. Python evolutionary algorithms (PEAS). 2013. url: https://github.com/noio/peas/tags.

[4] E.Schoneburg. “Stock prediction using neural network: A project report”. In:Neurocomputing 12.1 (1990), pp. 12–27.

[5] Martin T Hagan, Howard B Demuth, Mark H Beale, et al. Neural networkdesign. Pws Pub. Boston, 1996.

[6] Nasdaq. Accessed: 2015-05-11. url: http://www.nasdaq.com/.[7] Neurolab. url: https://code.google.com/p/neurolab/.[8] Kenneth O. Stanley and Risto Miikkulainen. “Evolving Neural Networks Through

Augmenting Topologies”. In: Evolutionary Computation 10.2 (2002), pp. 99–127. url: http://nn.cs.utexas.edu/?stanley:ec02.

[9] H. White. “Learning in neural networks: A statistical perspective”. In: NeuralComputation 4 (1989), pp. 425–464.

[10] D.E. Widrow B. Rumelhart and M.A. Lehr. “Neural networks: applications inindustry, business and science”. In: Communications of the ACM 37.3 (1994),pp. 93–105.

[11] Yahoo Finance Api. Accessed: 2015-05-06. url: http://finance.yahoo.com/.

17

Page 25: An evolutionary approach to time series forecasting …815021/FULLTEXT01.pdfDEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015 An evolutionary approach to time

www.kth.se