• No results found

An evolutionary approach to time series forecasting with artificial neural networks

N/A
N/A
Protected

Academic year: 2021

Share "An evolutionary approach to time series forecasting with artificial neural networks"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

DEGREE PROJECT, IN COMPUTER SCIENCE , FIRST LEVEL STOCKHOLM, SWEDEN 2015

An evolutionary approach to time

series forecasting with artificial neural networks

CARL SANDSTRÖM

KTH ROYAL INSTITUTE OF TECHNOLOGY CSC SCHOOL

(2)

An evolutionary approach to time series forecasting with artificial neural networks

CARL SANDSTRÖM

Bachelor’s Thesis at CSC Supervisor: Pawel Herman

Examiner: Örjan Ekeberg

May 2015

(3)
(4)

Abstract

In this paper an evolutionary approach to forecasting the stock market is tested and compared with backpropaga- tion. An neuroevolutionary algorithm is implemented and backtested 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 seems as a neuroevolutionary approach could outperform back- propagation for time series prediction.

(5)

Referat

I denna uppsats testas en evolutionär strategi för att förutsäga aktiemarknaden och jämförs sedan med backpro- pagation. En neuro-evolutionär algoritm implementeras och testas, 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 och vidare utredning skulle behövas för att säga definitivt, men det verkar som ett neuroevolutionärt tillvägagångssätt kan överträffa backpropagation för att förutsäga tidsserier.

(6)

Contents

1 Introduction 1

1.1 Problem statement . . . 1

1.1.1 Scope . . . 2

2 Background 3 2.1 Stock Market . . . 3

2.1.1 Algorithmic trading . . . 3

2.2 Artificial neural network . . . 3

2.2.1 Neuroevolution . . . 4

2.2.2 Backpropagation . . . 4

3 Method 7 3.1 Setup . . . 7

3.1.1 Data . . . 7

3.1.2 Inputs . . . 7

3.1.3 Output . . . 8

3.1.4 Trading strategy . . . 8

3.1.5 Error functions . . . 8

3.2 Implementation . . . 8

3.2.1 Third party libraries . . . 8

3.2.2 NEAT . . . 8

3.2.3 Backpropagation . . . 9

3.2.4 Benchmark . . . 9

3.3 Measuring the Artificial neural network . . . 9

4 Results 11 5 Discussion 15 5.1 Conclusion . . . 16

Bibliography 17

(7)
(8)

Chapter 1

Introduction

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

A method of algorithmic trading that is becoming more and more popular in the financial world is using artificial neural network(ANN) to forecast stocks and then trade base on the forecasted values[10], this can be used either for automating trade or to help a trader. ANN has on paper characteristics that both speaks for and against the use of it in time series. Neural networks are universal function approximators and can map any nonlinear function[9], due to this flexibility they are excellent methods for eg. pattern recognition, classification and forecasting. Some researches [4] show that ANN preform better then most statistical approaches in financial forecasting. They are however not that well suited to operate on data containing a lot of noise compared to other methods, and due to their complex nature 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, the most common way of doing this is backpropagation. This technique requires the architecture 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 the backpropagation algorithm needs.

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

(9)

CHAPTER 1. INTRODUCTION

forecasting will be used and then be compared with an artificial neural network trained 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 Scope

This 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 exchange for a time period of two years. It will explore the use of evolutionary algorithms for training ANN on the stock exchange.

2

(10)

Chapter 2

Background

2.1 Stock Market

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

2.1.1 Algorithmic trading

All kind of trading that is partly or completely automated by algorithms is referred to as algorithmic trading. By letting computers do the calculations and decisions the market has changed completely in a relatively short time, through the use of their computational and storage powers the trading opportunities has extended beyond what we thought was possible.

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

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

2.2 Artificial neural network

Artificial neural networks are designed to replicate how the neural receptors work in our 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

(11)

CHAPTER 2. BACKGROUND

A neural network is built out of three types of layers, one input layer, one output layer and a number of hidden layers, normally one. Every layer is made up of a number of nodes. Between these nodes are connections with weights, it is these weights that decide the output of the network. How they are connected and how many nodes a layer consists of is its architecture. One of the biggest challenges is to decide the architecture of the neural network and will be something the programmer will tweak for weeks if not months to get perfect as it is a very big part of the success or 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 the input data for the network and the target value one wish the network to output. The accuracy 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 tries to replicate how natural selection works in nature. It can either be used only to change the weights of the network or change both the weights and the architecture.

The first step when using an evolutionary approach while programming is to create 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 is created using the former population. This is done by using the individuals with the best fitness and mating them with each other. The offspring is then added to the new population but does also have the possibility to be mutated before being added. By doing this over and over again for the specified number of generations the best solution can be found.

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

For this paper Neuroevolution of Augmented Topologies (NEAT) where used, which is among the most successful neuroevolutionary algorithms and has been successfully applied in a number of areas, including finance. NEAT uses the evo- lutionary approach described above to train ANN by changing both weights and architecture. 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 during training, it also requires the network architecture to be fixed. The backpropagation algorithm has two steps.

4

(12)

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 to predict, to generate the error of each node in the network. Second step is to update the ANN weights by first getting the gradient of the weight and then subtracting a percentage of the gradient from the weight. This percentage is called the learning rate, the accuracy and speed of the training is decided by the learning rate, low learning rate means more accurate training while high learning rate means faster training. For more on backpropagation see[5].

5

(13)
(14)

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 and were 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 are among 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 up an down. And Yahoo(YHOO) because it is quite volatile, constantly going up and down.

3.1.2 Inputs

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

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

(15)

CHAPTER 3. METHOD

3.1.3 Output

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

xˆi+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 way to 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 = s

Pn

i=1(yi− ˆyi)2 n SSE =

n

X

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. This library has a working NEAT algorithm that was very simple to implement with the benchmarking program that was created for this paper.

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

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 parameters changes 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 was used over the entire training set and then used as fitness.

8

(16)

3.3. MEASURING THE ARTIFICIAL NEURAL NETWORK

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

3.2.3 Backpropagation

For the backpropagation 500 epochs was used with the error function SSE. The network used is a fully connected feedforward network with 10 hidden nodes, the number of nodes is based on tests by the author. An learning rate of 0.01 was used for all test.

3.2.4 Benchmark

The benchmark was created to be very simple and does not take into account any slippage or commission. A CSV file containing the data is loaded into the program and the algorithm loops over the content. When an order is made by the trading algorithm assets is extracted out of the imaginary portfolio and is returned when the algorithm closes the position again.

Before the benchmarking algorithm start to trade 75% of the data is used to train the neural network. Depending on which training approach is going to be tested either the NEAT algorithm or the backpropagation algorithm is used. The trained neural network is then sent to and used in the main benchmarking.

3.3 Measuring the Artificial neural network

The formula for calculating the return for an ANN For calculating the return this formula where used.

Return = Obtained money − Seed money Seed money

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

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

NMSE = Pn

i=1(xi− ˆxi)2 Pn

i=1(xi− ¯xi)2

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

9

(17)
(18)

Chapter 4

Results

The table and graphs presented in this chapter is the results from the benchmarks run on the historical data from the selected stocks. For every stock five benchmarks were run using the evolutionary approach and then using backpropagation, the best result from for each type where then taken. During a benchmark only one stock could be traded for the entirety of the benchmark, this was to see if the different approaches 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

(19)

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

(20)

Figure 4.3. Return over time from the benchmark on YHOO

13

(21)
(22)

Chapter 5

Discussion

Overall a patter can be seen in the results, the neuroevlutionary approach achieved significantly better returns in most stocks except for the YHOO stocks, where both algorithms preformed basically the same. The success or lack thereof for the two algorithms can be because a number of reasons, as in most complex programs or systems there are numerous parameters to take into account. The trading strategy used where not the most sophisticated and did not take into account any trends and could therefore sell or buy to early or late. Neither of the approaches to training got to run for a very long time, this due to time limitations, which could result in a not completely trained ANN was used during the benchmarking phase, which in turn could have lead to stock being bought or sold when it shouldn’t have. The data feed to the neural networks was not smoothed which due to ANN:s problems with noisy data could have slowed down the training. The window-size used could have been too small, leading to the networks not being given enough information to 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. NEAT preformed basically the same as backpropagation only on YHOO which could be because during the time of testing and training the stock where much more volatile then AAPL, which has been increased in value constantly, and MSFT who’s value has been pretty constant. This could be either the result of the neuroevolutionary approach having problem to adapt to a time series that is constantly changing or because the algorithm did not have sufficient time to gain an optimal solution and should have been trained longer, but that can be said about both approaches.

To achieve better results in the future one should drastically increase the time each algorithms trains as this is the most important and computationally heavy process when using ANN. During testing a fixed number of epochs or generation where set, this should if one has the computational power be set so that instead of a fixed number the training should continue until a satisfactory solution has been found. The parameters should also be tweaked further, but as that is a major task this was outside the scope of this paper. The benchmarking algorithm could be improved as well to take into account commission, which would mean that the

15

(23)

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 Conclusion

To answer this paper’s problem statement "Can an evolutionary approach to training achieved better results than backpropagation?", the results here are not entirely conclusive and further investigation would be needed to say definitely, but it seems as a neuroevolutionary approach could outperform backpropagation for time series prediction and therefor stock prediction.

16

(24)

Bibliography

[1] N.A. Gershenfeld A.S. Weigend. “Time Series Prediction: Forecasting the Fu- ture and Understanding the Past”. In: Proceedings of the NATO Advanced Research 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 network design. 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: Neural Computation 4 (1989), pp. 425–464.

[10] D.E. Widrow B. Rumelhart and M.A. Lehr. “Neural networks: applications in industry, 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

(25)

www.kth.se

References

Related documents

The table shows the average effect of living in a visited household (being treated), the share of the treated who talked to the canvassers, the difference in turnout

In our case study we investigate the first arms race on Internet triggered by the Napster introduction of an easy to use service for sharing files with music content among users..

The only frame of reference references one should make is when describing how a movement in north‐south direction is being deflected, only to explain that the

Skall medarbetare på kärnkraftverket bara kunna det som är nödvändigt för att utföra de arbetsuppgifter som ingår i en viss befattning eller vill verken ha personal med

This paper shows that in the organisation studied, there is a general approach towards managing conflict situations, regardless of the conflict type that exists, in this

The streamline weighting factors obtained from the ground truth and modified bundle for each of the different curving and crossing fiber models (Fig. 16) were estimated with

With a reactive path-finding algorithm in place, the responsible ROS node searches for the next recognizable landmark, using a vector field obstacle avoid- ance algorithm to stay

In the case of time-varying uncertainties, including nonlinear elements with bounded L 2 gain and parametric varying parameters, D and G are generally restricted to constants