Transcript
Page 1: Stock Market Forecasting

Stock Market Forecasting

Stock Exchange Forecasting

Supervisor:

Mr. Manish Kumar

Group Members:

G. Aravind (R200503)

Manam Kiran (R200515)

G. Sravan Kumar (R200533)

1

Page 2: Stock Market Forecasting

Stock Market Forecasting

List of contents

o Candidates declaration 3

o Certificate 3

o Acknowledgement 4

o Topic 5

o Motivation 5

o Problem Definition 5

o Literature Survey 6

o Course of work 7

Designing of the neural network 8 Designing of the mining system 9 Training the system 10 Using the system for forecasting 12

o Snapshots of GUI 13

o Platform, tools, language 15

o Challenges 15

o Scope for future work 15

o References 16

o Comments of the Mid-Sem evaluation board 17

Figures

Delta of hidden layer node 11Delta of input layer node 11Delta of hidden layer node 12Main GUI 14Selecting an input file 14Prediction 15Actual vs predicted prices 15

2

Page 3: Stock Market Forecasting

Stock Market Forecasting

Candidate’s Declaration

We hereby declare that the work presented in this project entitled “Stock Market Forecasting using Neural Networks” submitted towards completion of mini-project in Sixth Semester of B.Tech(IT) at the IIIT-Allahabad, Amethi campus, is an authentic record of our original work carried out under the guidance of Mr. Manish Kumar, M.Tech(Computer Science), Lecturer, IIIT-Allahabad.

Place: Amethi

Date:

Certificate

This is to certify that the above declaration made by the candidates is correct to the best of my knowledge and belief.

Mr. Manish Kumar

M.Tech(Comp. Science)

Place: Amethi Lecturer, IIIT-Allahabad

Date: Devghat, Jhalwa,Allahabad

3

Page 4: Stock Market Forecasting

Stock Market Forecasting

Acknowledgement

We would like to express our sincere gratitude to our project supervisor, Mr. Manish Kumar, M.Tech(Computer Science), Lecturer, IIIT-Allahabad. We were privileged to experience a sustained enthusiasm and involved interest from his side. This fueled our interest even further and encouraged us to boldly step into what was a totally dark and unexplored expanse before us.

We would also like to thank our batch-mates and friends who were ready with their inputs at all times, whether it was an offhand comment to encourage us or a constructive piece of criticism. Last but not least, we would like to thank the IIIT-A staff members and the institute, in general, for extending a helping hand at every juncture of need.

G. Aravind (R200503)

Manam Kiran (R200515)

G. Sravan Kumar (R200533)

4

Page 5: Stock Market Forecasting

Stock Market Forecasting

Topic:

Stock exchange forecasting with the use of neural networks.

Motivation:

There are numerous motivating factors for trying to predict the future stock market trends. The basic factor of motivation for us is the interest in building a system that could consistently guide us through the dynamic and ever changing stock market and support decision making with respect to investments. Such a system would be a great support for the individuals/corporations who like to take calculated risks in investing their capital. Also before the invention of computers, the stock market trends were forecasted by a set of highly qualified individuals or by pure instinct. The purpose of this project is to automate the process of forecasting.

Neural networks have been chosen to model the stock market because of their ability to learn the non-linear mappings between inputs and outputs. The stock market is considered to be highly chaotic model, which cannot be understood by the human mind, and thus can’t be easily expressed. However, with the neural networks’ ability to learn chaotic and nonlinear systems, it is possible to solve many more problems which could not be solved by simple traditional analysis or current computer methodologies.

In addition to financial market predictions, neural networks have been already put to use by many organizations for handling different financial tasks [1]. Some banks use neural networks to scan credit and loan applications to predict bankruptcy probabilities. Though the neural networks were primarily used as a tool in the financial domain, many improvements, with respect to the design issues and the training of neural networks have been achieved during research and the implementation.

Problem Definition:

Our aim is to implement stock market forecasting using neural networks, with multi-layer back-propagation, for the stock market data as a time series. The objective of the implementation is to forecast the future trends of the stocks, on the base of the previous or past prices. The implementation is planned to be done using JAVA.

5

Page 6: Stock Market Forecasting

Stock Market Forecasting

Literature Survey:

From the beginning of mankind, man has always been trying to make his life easy. The computers were also invented for making calculations easier and possibly automate them. It is also a common notion that wealth gives luxury and makes life easier. There have been many attempts, both partly successful and unsuccessful, to forecast the future trends of the stock markets.

Y. Yoon, et al. [3], proposed a system for stock market prediction using neural networks. The inputs used were the phrases extracted out of the President’s (of the organization) report to its shareholders. The system based its predictions on the types and frequencies of the phrases occurring in the president’s report.

Kamijo, et al. [4], developed a neural network that trained on price chart information. They used a recurrent network model for forecasting.

G. Tsibouris, et al. [5] developed a simple system that used only the past stock prices to forecast the future stock prices.

K. Bergerson, et al. [6], proposed a system that was trained on human designed chart information. This reduced the learning time as the input had knowledge about the patterns already known. This system used hybrid architecture, combining neural networks with expert systems. The neural networks were used to predict the future trends and the expert system contained knowledge base of rules that validated the output of the network. If the output violated any of the expert systems’ rules, the output was simply rejected. This hybrid architecture showed great promise as it combined the nonlinear prediction of neural networks and the rule based knowledge of expert systems.

Kimoto, et al. [7], designed a network that was to predict the Tokyo stocks. They used daily data (prices) for training the network for about 33 months. They proposed a variation of back-propagation for the system, called supplementary learning. The weights in the network were updated based on the sum of all errors for all the patterns. During training they used moving simulation, where prediction is done while moving the target learning and prediction periods [1]. They used a modular network consisting of 4 backpropagation neural networks, trained on different data sets.

Wilson [8], used a combination of technical, adaptive (based on limited support functions), and statistical indicators as inputs, for his self-organizing system. It was designed to construct a nonlinear and chaotic model of the stock market based on the volume and price data. The system itself extracted the features from the data, and classified them. He used two self organizing systems in tandem, one for feature extraction and the other for pattern classification.

6

Page 7: Stock Market Forecasting

Stock Market Forecasting

Ajith Abraham, et al. [9], used soft computing techniques and connectionist paradigms for modeling chaotic behavior of the stock markets. They experimented with the system on the data of NASDAQ stock market and NIFTY stock exchange. They used four different methods to model the markets and compared the results from each of them.

Chan Man-Chung, et al. [10], proposed a variation of learning algorithm, the conjugate gradient learning algorithm with a restart procedure.

Course of work:

The implementation is based on the algorithm suggested in the paper by Defu Zhang, et al [2]. The paper deals with financial data mining using multi-layer back propagation, a neural network technique commonly used for time series forecasting.

Though the paper does not prescribe any suggested method for back propagation but have given the type of inputs to be taken like learning rate is given to be 0,5 and weights are first adjusted to range from -0.5 to 0.5.

The implemented algorithm though deviates in three ways:

1) Paper predicts next 30days values based on last few years trend but the training takes about 3-4 days so we assumed that tomorrow’s value is depended on last 30 days data, which reduces the time taken to train the network.

2) Error function used is mean square error which is not very accurate in our case as we predict only one value, hence we used a non linear function to transfer out put by using sigmoid function and then error is calculated.

3) Since we are using sigmoid function we mapped the input data to range of 0 to 0.5 and then later the results are remapped which increases the error by the mapping function but is accurate enough if input values are in not too big.

The implementation is divided into four phases:

Designing the neural network Designing the mining system for forecasting Training the network with past data Using the network for forecasting the future trends of the stock based on the present

and past data

7

Page 8: Stock Market Forecasting

Stock Market Forecasting

Designing the neural network:

The neural network architecture has to be decided upon before going on with the designing of the mining system for forecasting. Feed-forward networks are the most widely used architecture as they provide good generalization capabilities and are readily implementable. So, a feed-forward network with three layers is chosen as the architecture to model the forecasting task.

The parameters defining the architecture of the neural network are:

Number of layers: The number of layers in the Neural network is three. They are one Input Layer, one Hidden Layer, one Output Layer. The Hidden layer is used to transform the inputs to the neural network using a transfer function, described later.

Number of nodes(neurons): The input layer consists of 30 nodes(neurons), one for each of the stock prices for the last 30 days. The hidden layer consists of 30 nodes, each taking an input from all of the previous input nodes, with links having certain weights. There is a single output node in the output layer, corresponding to the predicted stock price. The outputs of all of the nodes in the hidden layer are passed on to the output node, each with certain weight.

Weights of the links: All of the weights are initially randomly set in the range [-0.5,0.5]. During the training phase, the weights are adjusted according to the error of the nodes in the next layer.

Activation function: It is the function that is fired at each node, after receiving inputs from the previous layer. This is the transfer function that was mentioned earlier. This function transforms the input to a neuron(node) into its output. The activation function used in the implementation is the sigmoid function, i.e.,

This function maps any input x to the range [0,1]. So the outputs of all the nodes belong to the range [0,1].

Learning rate: It defines the rate of learning of the neural network. It can also be interpreted as the acceleration of the network while training. In the implementation the value of learning rate has been set to 0.5.

Input mapping: While taking the inputs to the neural network the maximum of all the inputs is calculated and stored. Then all the inputs are normalized by dividing them by the

8

Page 9: Stock Market Forecasting

Stock Market Forecasting

maximum value. This is done so that during training the predicted output and the expected output both lie in the range [0,1].

Output scaling: The predicted outputs lie in the range [0,1], because of the application of the sigmoid function at all the nodes. To get the actual predicted price, the output is multiplied with the maximum of all the inputs. This scales the output from the range [0,1] to the original scale of the stock price.

Designing the mining system:

The mining system for the forecasting task has to be designed. The design of mining system consists of two phases:

o Choosing the design strategy

o Choosing the trading strategy

Design strategy in the algorithm is stated as follows:

The stock market is inherently nonlinear. So the error function is not globally convex, and may contain many local minima. The trends of up or down of a stock are also important for making profits in the stock market. So, to make the prediction more accurate, some penalizing factor [2] has to be introduced into the error function. Also, there is need to address the issue of over-fitting of the data by the network.

The trading strategy is as follows:

A trading strategy [2] is required to avoid the poor explanation capabilities of the neural networks. It means instead of giving out pure numerical data, the network can be designed to explain the results in a more understandable fashion, and give suggestions for decision making and investments.

The trading system designed is as follows:

The neural network is used to predict 3 future prices of the stock. The maximum and the minimum of the predicted prices are calculated. The system proposes to the user to Buy and/or Hold the stock, if the maximum of the predicted prices is greater than the current stock price and the stock is on an up-trend. The system hints the user to Sell a part of the stock, if the maximum of the predicted prices is less than the current stock price and the stock is on a down-trend.

9

Page 10: Stock Market Forecasting

Stock Market Forecasting

Training the network:

After the network has been designed, it has to be trained with training data so that it can be used for forecasting the future trends. The training data, the past stock data, is to be extracted from the internet. The training of a network involves presenting the input data patterns in a way that the system minimizes error and improve its performance [1]. In this implementation we use a multi-layer back-propagation algorithm for training the system. To minimize overtraining, about 10% of the training data is to be kept aside for testing purposes. This approach is called Test and Train method [1] for training a neural network. This tests the performance on the testing data after it has been trained, and modify the weights of the connections when deviations are found. This also helps reduce over-fitting.

The neural network is trained with as follows:

Feeding the inputs: Initially, the neural network is provided with the inputs. The input is a text file containing the daily closing stock prices for the past some days. The data in the file is present in the following format:

The first line of the text file contains the number of training samples, i.e., the number of days for which the stock prices are present in the file.

From the second line, each line contains the daily closing prices of the stock in chronological order, one in each line.

The inputs are given to the neural network as an array of stock prices, extracted from the input file, and set the array as the output of the input layer.

Finding the out of the network: Finding the output of neural network involves, calculating the outputs of the hidden layer and output layer. The output of any node in the hidden layer or the output layer is calculated as follows:

1) Summation unit: It calculates the net input to the node as

Where, n is the node for which output is to be calculated.K is the total number of input nodes connected to the node n.W(i, n) is the weight of the link between the node i and node n.

2) Activation unit: It applies the activation function as specified earlier to the net input that is obtained from the Summation unit. The activation function used is the sigmoid function, i.e.,

10

Page 11: Stock Market Forecasting

Stock Market Forecasting

Calculating Error or Delta: The error at the output node is calculated as

The delta of the output node is calculated as

The delta value of the neuron in the output layer is used to calculate the error or delta of all neurons in the hidden layer. This can be illustrated as follows:

Fig. 1 Delta of hidden layer node

The delta of the nodes in the input layer can also be calculated using the same strategy, which is illustrated as follows.

11

Page 12: Stock Market Forecasting

Stock Market Forecasting

Fig. 2 Delta of input layer node

The formula used is

Fig. 3 Delta of hidden layer node

Thus, we have the deltas of all the nodes in the hidden as well as the output layer.

Adjusting the weights: Based on the error or the delta of the nodes, the weights of the links connecting it to the next layer are to be adjusted, so as to reduce the error for the further iterations. The new adjusted weights are calculated using the following equation:

12

Page 13: Stock Market Forecasting

Stock Market Forecasting

After following the whole training procedure for all the training samples, the neural network is made ready to predict any future prices of the stock using the network, which has been adjusted by capturing the trends in the training data.

Using the Network for forecasting:

After training the network, it has to be tested with current data, to evaluate its performance. Some of the training data will be kept aside for testing, to avoid over-fitting by the neural network.

The network is used to predict the future stock prices, after training and testing. In the implementation, the network is created so that it predicts the next days’ closing price of the stock. The closing prices of the next three days are predicted by adding to the input, the predicted prices of the previous days to predict the price of the second and the third days. Then the trading strategy is used to provide the user with the response of the system in a much understandable manner. The ultimate decision, of buying/holding/selling the stock, is completely onto the user.

Snapshots of the GUI:

Fig. 4 Main GUI

This is the main GUI of the project. It is the first one that opens on executing the executable jar file. First, the user has to select the input file, for training. This is done by writing the path of the file in the text box or by browsing for the file by clicking the “browse” button.

13

Page 14: Stock Market Forecasting

Stock Market Forecasting

Fig. 5 Selecting an input file

After selecting the file, the network has to be trained using the samples in the selected file. This is done by clicking the “train” button. After the training is completed, the user can ask the system to predict the future trends of the stock by clicking the “predict” button.

Fig. 6 prediction

The system then hints the user about the future of the stock by proposing to buy/ hold/sell the stock, as shown above. The predictions can be checked with the actual prices by clicking the “graph” button. It gives the actual trend of the stock (in red) and the predicted trend (in blue).

14

Page 15: Stock Market Forecasting

Stock Market Forecasting

Fig. 7 actual vs predicted prices

The blue curve (predicted trend) starts later than the red curve (actual trend), because the prediction starts from the 31st day after the day corresponding to the first input to the system. Also the initial trend prediction is very erratic because the initial weights of the network are set randomly, and as they are adjusted, the predictions become much better.

Platform, tools, language, data:

The implementation has been done in Java using NetBeans IDE 5.5 and JDK 1.6. The data used for training and testing has been downloaded from the website of the BSE(Bombay Stock Exchange)[13].

Challenges:

The following are some challenges in the implementation:

This implementation of the network focuses on the prediction of a single value each time. It can be used to predict multiple values at a time, but with reduced accuracy.

Even after taking certain measures to avoid overfitting, the network may tend to overfit the data.

15

Page 16: Stock Market Forecasting

Stock Market Forecasting

The noise in the input data can be a source of error in the predictions. The network may not be able to predict some typical patterns like spikes or sudden lows

in the test/train data.

Scope for future work:

The scope for future improvements is as follows:

The network predicts the future values only on the basis of the past and current price values. In reality, the prices are affected by many unquantifiable entities, like political, economic changes, etc. They can be taken into consideration to achieve better accuracy in the prediction.

The prediction of multiple values at a time can be made more accurate by training the network with more data and better validation techniques to check the tendency of the network to overfit.

References:

[1]: Ramon Lawrence “Using Neural Networks to Forecast Stock Market Prices”.

[2]: Defu Zhang, Qingshan Jiang, Xin Li “Application of Neural Networks in Financial Data Mining”, proceedings of World Academy of Science, Engineering and Technology Volume 1 January2005

[3]: Y. Yoon and G. Swales “Predicting stock price performance: A neural network approach”. In Neural Networks in Finance and Investing, chapter 19, pages 329–342. Probus Publishing Company, 1993.

[4]: K. Kamijo and T. Tanigawa. “Stock price pattern recognition: A recurrent neural network approach”. In Neural Networks in Finance and Investing, chapter 21, pages 357–370. Probus Publishing Company

[5]: G. Tsibouris and M. Zeidenberg. “Testing the Efficient Markets Hypothesis (EMH) with gradient descent algorithms”. In Neural Networks in the Capital Markets, chapter 8, pages 127–136. John Wiley and Sons, 1995

[6]: K. Bergerson and D. Wunsch. “A commodity trading model based on a neural network-expert hybrid system”. In Neural Networks in Finance and Investing, chapter 23, pages 403–410. Probus Publishing Company, 1993.

16

Page 17: Stock Market Forecasting

Stock Market Forecasting

[7]: T. Kimoto, K. Asakawa, M. Yoda, and M. Takeoka “Stock market prediction system with modular neural networks”.

[8]: C. L. Wilson. “Self-organizing neural network system for trading common stocks”. In Proc. ICNN’94, Int. Conf. on Neural Networks, pages 3651–3654, Piscataway, NJ, 1994. IEEE Service Center.

[9]: Ajith Abraham, Ninan Sajith Phillip, Baikunth Nath, P.Saratchandran “Performance evaluation of connectionist paradigms for modeling chaotic behavior of stock indices”.

[10]: Chan Man-Chung, Won Chi-Cheong, Lam Chi-Chung “Financial Time Series Forecasting by Neural Network using Conjugate Gradient Learning Algorithm and Multiple Linear Regression Weight Initialization”

[11]: http://en.wikipedia.org/wiki/Neural_Network.html

[12]: http://en.wikipedia.org/wiki/Artificial_Neural_Network.html

[13]: http://www.bseindia.com/histdata/stockprc2.asp

Comments of the End Semester Evaluation Board:

Stock Market Forecasting

Group Members:

G. Aravind (R200503)

Manam Kiran (R200515)

G. Sravan Kumar (R200533)

17


Top Related