Stock Market Prediction using Artificial Neural Networks IME611 - Financial Engineering Indian Institute of Technology, Kanpur (208016), India Name Pallav Ranka (13457) Abstract Investors in stock market want to maximize their profits, it would be beneficial for them if the market movement can be predicted beforehand. Artificial Neural Network approach has been used herein to predict the adjusted closing price of stocks. In this paper feed forward neural network is implemented for prediction purpose. It has been seen that results of neural network is more promising in predicting stock price that actually is not certainly predictable. Historical data of the some stocks has been used for building and training the models. Later, the results from the model has been compared with the real data to ascertain the accuracy of the model. Keywords - Stock market prediction; Neural networks; Back propagation algorithm Introduction In the past few decades, forecasting of stock market is gaining more attention as the profitability of investors in the stock market mainly depends on the predictability. If the direction of the market is successfully predicted the investors can yield enough profits out of market using prediction. Complex relationships between inputs and outputs may not always allow us to find patters. ANN is gaining much attention these days because of its capability of solving such problems. It has robust ability to discover relationship in the input data set without a priori assumption of the knowledge of relation between the input and the output data. It can be used to build model that identify unknown hidden patterns in data which can be further used for prediction purposes. Neural Network has already been successfully applied in the fields related to finance, econometrics, medicine and engineering. Instructor Prof. Kripa Shanker In the chaotic system, like stock market, in which many known as well as unknown factors affect the stock price, there is no significant mathematical relation between the factors and the price can be found. There is no law exists which governs the stock prices using the underlying factors. Taking this into consideration, application of neural networks would be very beneficial in predicting stock market. In this paper, Feed Forward Backpropagation Neural Network is used for the prediction. Feed forward Neural Networks is unidirectional connection between the neurons that means the information can flow only in forward direction. Input has been fed into first layer and with the with help of hidden layers connected to the last layer that produces the output. There is no connection among neurons in the same layer. Since information is constantly feeding forward from one layer to the next. Hence it is called feed forward neural network. The backpropagation algorithm falls into the general category of gradient descent algorithms. Purpose of gradient descent algorithm is to find the minima/maxima of a function by iteratively moving in the direction of negative slope of the function that we want to minimize/maximize. In our problem objective is to minimize the error function. In Back-propagation algorithm the network is trained by repeatedly processing the training data set and comparing the network output with the actual output to reduce the mean square error. Weights of the connections between various neurons has been modified and this process has been continued till the error comes within threshold value. It is Backpropagation algorithm as errors from output is back-propagating towards the input layer during training sessions with the objective of minimizing the mean square error. As errors Page 1 of 7
back-propagated model can be continuously updated to minimize error. Along with this, few disadvantages are also associated with backpropagation algorithm. It is not guaranteed that result always converges to global minima on the error function. Even it is not guaranteed that result always converge. If result converges, rate of convergence is very slow. Typically each backpropagation training session starts with different initial weights and biases, and different divisions of data into training, validation, and test sets. These different conditions can lead to different solutions for the same problem. Methodology The work flow for the general neural network design process has five primary steps: a) Data Collection and Preparation b) Network Creation c) Training the network d) Validating the network e) Using the network a) Data Collection and Preparation Data collection is the primary step and it is necessary in order to train, validate and test the neural network. General practice is to divide data into three subsets, namely, training set, validation set and testing set. Training set is foremost as it is used to compute the gradient and updating the network weights and biases. Validation set is used as during training phase error on validation is monitored and when the network begins to overfit the data, the error on validation phase starts rising. Then, the network weights and biases corresponding to minimum validation error can be used for the prediction purpose. For data collection yahoo finance has been used to fetch two years historical data for several stocks. Further, data has been pre-processed before it fed to a neural network. We have collected two years data starting from 2015. b) Network Creation It is crucial step for prediction, in this step network has been created with the decision about the inputs, number of layers, number of neurons in each layer, transfer function to be used, outputs etc. We have used date of prediction and opening price as an input. Three hidden layers and one output layer has been used, while number of neurons in each layer and transfer function has been decided by MATLAB itself to predict adjusted closing price of a stock as an output. Fig. 1 Structure of typical Neural Network c) Training the network The process of training a neural network involves tuning the values of the weights and biases of the network to optimize network performance (by minimizing the Mean Square Error). Out of two years historical data of stock, in the Case-I 90% of the data has been used to train the model while remaining is used to test the model. On the other hand, in the case-ii 95% of the data has been used to train the model while remaining is used to test the model. d) Validating the network Once the network is trained the network is validated using validation data to enhance the performance of network. In our case, it has been taken care by MATLAB. e) Using the network Once the network was optimized, it has been tested using the test data. In our case collected data of google (registered on NASDAQ as GOOG) Page 2 of 7
has been used to predict the adjusted closing price of stock. Results In MATLAB training will stop if anyone of the following criterion will meet: In the simulation, training epochs can reach maximum 1000 epochs. The performance measure reaches its goal. (MSE=10to-7) The gradient of the performance measure gets under a threshold. (10to-7) Case-I) 90% of the data used to train the neural network and prediction for 50 days Initial 90% of the historical data of Google from the last two years (01-01-2015 to 31-12-2016) has been used to train the neural network. For training 2 inputs, namely, date and opening price of stock has been used while output of the network is adjusted price. Then we predict stock values for 10% (approximately 50 days) of the data using Back-Propagation algorithm and compared the predicted stock prices with the actual stock prices. The average error of the simulation for 10% of the data was found to be 1.84% Fig. 2 Prediction of 50 days using 90% data Case-II) 95% of the data used to train the neural network and prediction for 25 days For this case, same historical data of Google has been used but this time initial 95 % historical data from the last two years (01-01-2015 to 31-12-2016) has been used to train the network. For training purpose same inputs and outputs has been used. Then we predict stock values for 5% (approximately 25 days) of the data using Back- Propagation algorithm and compared the predicted stock prices with the actual stock prices. In this case, the average error of the simulation for 5% of the data was found to be 0.71% Fig. 3 Prediction of 25 days using 95% data Figure 2 and 3 corresponding to Table 1 and 2 respectively, show the predicted and actual values for the Google stock. It can be seen that both the cases provide pretty good results for initial dates of prediction. While in the first case if we compare the predicted prices with the actual prices, It can be seen that after few days there is more variation of predicted price from actual price. Conclusion In a highly volatile market like Indian Stock Market, the performance of the neural network model proposed herein seems to be very useful. As, the prediction of the direction of the market with fairly high accuracy is utmost important for the investors and regulators. The ultimate goal of prediction is to increase the yield from the investment that can easily be achieved using the method proposed herein. It has been seen that neural network tool gives a promising direction of movement of the stocks. It has been seen that as we increased training data from 90% to 95% the prediction error has been reduced to.71% from 1.84%. Thus, method proposed in this paper seems very useful for investors and stock market analysts perspective as neural network gives promising result. Proposed model can be continuously updated Page 3 of 7
for the prediction at different time. So that more information can be used by network to predict the future stock price. This is recommended as with time the characteristic of the market changes and the network capture all the information and hence prediction will be more precise. References [1] J.E. Dayhoff, J.M. DeLeo, Artificial neural networks: Opening the black box. Cancer, 91 (8 Suppl.) (2001), pp. 1615 1635. [8] Robert J. Van Eyden. The Application of Neural Networks in the Forecasting of Share Prices. Finance and Technology Publishing, 1996. [9] Artificial Intelligence a modern approach (second edition) by Stuart Russell, Peter Norvig 2004 [2] T. Kimoto, K. Asakawa,M. Yoda,M. Takeoka. Stock market prediction system with modular neural network. Proceedings of the International Joint Conference on Neural Networks, 1990, pages 1 6. [3] H. Mizuno, M. Kosaka, H. Yajima, N. Komoda. Application of Neural Network to Technical Analysis of Stock Market Prediction. Studies in Informatics and Control, Vol.7, No.3, 1998, pages.111 120. [4] 18. Wong, C., and M. Versace. 2012. CARTMAP: A Neural Network Method for Automated Feature Selection in Financial Time Series Forecasting. Neural Computing and Applications 21(5):969-977. [5] Khan, Z. H., T. S. Alin and A. Hussain. 2011. Price Prediction of Share Market using Artificial Neural Network (ANN). International Journal of Computer Applications (0975 8887) 22(2). [6] Z.Tang and P.A.Fishwick, Backpropagation neural nets as models for time series forecasting, ORSA journal on computing, vol.5, No. 4, pp 374-384, 1993. [7] Mizuno, H., Kosaka, M., Yajima, H. and Komoda N. (1998), Application of Neural Network to Technical Analysis of Stock Market Prediction, Studies in Information and Control, vol.7, no.3, pp.111-120. Page 4 of 7
Appendix Case-I) 90% of the last 2 year historical data has been used to train the neural network and prediction of adjusted closing price of stock for 50 days has been shown in table (Data set link) Date Predicted Price (USD) Actual Price (USD) Error (%) 20-Oct-16 798.94 796.97 0.25 21-Oct-16 793.51 799.37 0.73 24-Oct-16 800.85 813.11 1.51 25-Oct-16 808.42 807.67 0.09 26-Oct-16 802.23 799.07 0.40 27-Oct-16 798.98 795.35 0.46 28-Oct-16 803.99 795.37 1.08 31-Oct-16 796.27 784.54 1.49 01-Nov-16 787.59 783.61 0.51 02-Nov-16 784.37 768.70 2.04 03-Nov-16 776.15 762.13 1.84 04-Nov-16 762.77 762.02 0.10 07-Nov-16 783.04 782.52 0.07 08-Nov-16 790.02 790.51 0.06 09-Nov-16 787.78 785.31 0.31 10-Nov-16 796.19 762.56 4.41 11-Nov-16 769.95 754.02 2.11 14-Nov-16 770.14 736.08 4.63 15-Nov-16 763.16 758.49 0.62 16-Nov-16 770.46 764.48 0.78 17-Nov-16 780.33 771.23 1.18 18-Nov-16 784.15 760.54 3.10 21-Nov-16 778.18 769.20 1.17 22-Nov-16 786.43 768.27 2.36 23-Nov-16 782.93 760.99 2.88 25-Nov-16 780.80 761.68 2.51 28-Nov-16 778.28 768.24 1.31 29-Nov-16 787.81 770.84 2.20 30-Nov-16 786.97 758.04 3.82 01-Dec-16 777.07 747.92 3.90 02-Dec-16 766.41 750.50 2.12 05-Dec-16 778.45 762.52 2.09 06-Dec-16 784.49 759.11 3.34 07-Dec-16 781.73 771.19 1.37 08-Dec-16 791.20 776.42 1.90 09-Dec-16 797.22 789.29 1.00 Page 5 of 7
12-Dec-16 801.76 789.27 1.58 13-Dec-16 808.31 796.10 1.53 14-Dec-16 810.97 797.07 1.74 15-Dec-16 811.19 797.85 1.67 16-Dec-16 813.50 790.80 2.87 19-Dec-16 807.26 794.20 1.64 20-Dec-16 812.05 796.42 1.96 21-Dec-16 811.66 794.56 2.15 22-Dec-16 809.46 791.26 2.30 23-Dec-16 808.65 789.91 2.37 27-Dec-16 809.33 791.55 2.25 28-Dec-16 811.66 785.05 3.39 29-Dec-16 804.33 782.79 2.75 30-Dec-16 804.07 771.82 4.18 Table-1 Google predicted and true adjusted closing price along with error from 20-Oct-16 to 30-Dec-16 Page 6 of 7
Case-II) 95% of the last 2 year historical data has been used to train the neural network and prediction of adjusted closing price of stock for 25 days has been shown in table (Data set link). Date Predicted Price (USD) Actual Price (USD) Error (%) 25-Nov-16 764.54 761.68 0.38 28-Nov-16 760.49 768.24 1.01 29-Nov-16 771.17 770.84 0.04 30-Nov-16 769.83 758.04 1.55 01-Dec-16 758.01 747.92 1.35 02-Dec-16 745.38 750.50 0.68 05-Dec-16 758.21 762.52 0.57 06-Dec-16 764.82 759.11 0.75 07-Dec-16 761.31 771.19 1.28 08-Dec-16 771.89 776.42 0.58 09-Dec-16 778.54 789.29 1.36 12-Dec-16 782.84 789.27 0.81 13-Dec-16 790.23 796.10 0.74 14-Dec-16 793.05 797.07 0.50 15-Dec-16 792.99 797.85 0.61 16-Dec-16 795.42 790.80 0.58 19-Dec-16 787.10 794.20 0.89 20-Dec-16 792.45 796.42 0.50 21-Dec-16 791.69 794.56 0.36 22-Dec-16 788.83 791.26 0.31 23-Dec-16 787.61 789.91 0.29 27-Dec-16 787.36 791.55 0.53 28-Dec-16 789.84 785.05 0.61 29-Dec-16 781.11 782.79 0.21 30-Dec-16 780.60 771.82 1.14 Table-2 Google predicted and true adjusted closing price along with error from 25-Nov-16 to 30-Dec-16 Page 7 of 7