Stock Market Trend Analysis Using Hidden Markov Models
SStock Market Trend Analysis Using Hidden Markov Models
Kavitha G
School of Applied Sciences, Hindustan University, Chennai, India. E-mail: [email protected] *Udhayakumar A
School of Computing Sciences, Hindustan University, Chennai, India E-mail: [email protected]
Nagarajan D
Department of Information Technology, Math Section, Salalah College of Technology, Salalah, Sultanate of Oman E-mail: [email protected]
Abstract — Price movements of stock market are not totally random. In fact, what drives the financial market and what pattern financial time series follows have long been the interest that attracts economists, mathematicians and most recently computer scientists [17]. This paper gives an idea about the trend analysis of stock market behaviour using Hidden Markov Model (HMM). The trend once followed over a particular period will sure repeat in future. The one day difference in close value of stocks for a certain period is found and its corresponding steady state probability distribution values are determined. The pattern of the stock market behaviour is then decided based on these probability values for a particular time. The goal is to figure out the hidden state sequence given the observation sequence so that the trend can be analyzed using the steady state probability distribution( π ) values. Six optimal hidden state sequences are generated and compared. The one day difference in close value when considered is found to give the best optimum state sequence. Keywords - Hidden Markov Model; Stock market trend; Transition Probability Matrix; Emission Probability Matrix; Steady State Probability distribution I. INTRODUCTION “A growing economy consists of prices falling, not rising”, says Kel Kelly[9] . Stock prices change every day as a result of market forces. There is a change in share price because of supply and demand. According to the supply and demand, the stock price either moves up or undergoes a fall. Stock markets normally reflect the business cycle of the economy: when the economy grows, the stock market typically reflects this economic growth in an upward trend in prices. In contrast, when the economy slows, stock prices tend to be more mixed. Markets may take time to form bottoms or make tops, sometimes of two years or more. This makes it difficult to determine when the market hits a top or a bottom[3]. The Stock Market patterns are non-linear in nature, hence it is difficult to forecast future trends of the market behaviour. In this paper, a method has been developed to forecast the future trends of the stock market. The Latent or hidden states, which determine the behaviour of the stock value, are usually invisible to the investor. These hidden states are derived from the emitted symbols. The emission probability depends on the current state of the HMM. Probability and Hidden Markov Model give a way of dealing with uncertainty. Many intelligent tasks are sequence finding tasks, with a limited availability of information. This naturally involves hidden states or strategies for dealing with uncertainty.
II.
LITERATURE SURVEY
In Recent years, a variety of forecasting methods have been proposed and implemented for the stock market analysis. A brief study on the literature survey is presented. Markov Process is a stochastic process where the probability at one time is only conditioned on a finite history, being in a certain state at a certain time. Markov chain is “Given the present, the future is independent of the past”. HMM is a form of probabilistic finite state system where the actual states are not directly observable. They can only be estimated using observable symbols associated with the hidden states. At each time point, the HMM emits a symbol and changes a state with certain probability. HMM analyze and predict time series or time depending phenomena. There is not a one to one correspondence between the states and the observation symbols. Many states are mapped to one symbol and vice-versa.
Hidden Markov Model was first invented in speech recognition [12,13], but is widely applied to forecast stock market data. Other statistical tools are also available to make forecasts on past time series data. Box–Jenkins[2] used Time series analysis for forecasting and control. White[5,18,19] used Neural Networks for stock market forecasting of IBM daily stock returns. Following this, various studies reported on the effectiveness of alternative learning algorithms and prediction methods using ANN . To forecast the daily close and morning open price, Henry [6] used ARIMA model. But all these conventional methods had problems when non linearity exists in time series. Chiang et al.[4] have used ANN to forecast the end-of-year net asset value of mutual funds. Kim and Han [10] found that the complex dimensionality and buried *Corresponding author oise of the stock market data makes it difficult to re- estimate the ANN parameters. Romahi and Shen [14] also found that ANN occasionally suffers from over fitting problem. They developed an evolving rule based expert system and obtained a method which is used to forecast financial market behaviour. There were also hybridization models effectively used to forecast financial behaviour. The drawback was requirement of expert knowledge. To overcome all these problems Hassan and Nath [15] used HMM for a better optimization. Hassan et al. [16] proposed a fusion model of HMM, ANN and GA for stock Market forecasting. In continuation of this, Hassan [7] combined HMM and fuzzy logic rules to improve the prediction accuracy on non-stationary stock data sets. Jyoti Badge[8] used technical indicators as an input variable instead of stock prices for analysis. Aditya Gupta and Bhuwan Dhingra[1] considered the fractional change in Stock value and the intra-day high and low values of the stock to train the continuous HMM . In the earlier studies, much research work had been carried out using various techniques and algorithms for training the model for forecasting or predicting the next day close value of the stock market, for which randomly generated Transition Probability Matrix (TPM), Emission Probability Matrix (EPM) and prior probability matrix have been considered.
In this paper, the trend analysis of the stock market is found using Hidden Markov Model by considering the one day difference in close value for a particular period. For a given observation sequence, the hidden sequence of states and their corresponding probability values are found. The probability values of π gives the trend percentage of the stock prices. Decision makers make decisions in case of uncertainty. The proposed approach gives a platform for decision makers to make decisions on the basis of the percentage of probability values obtained from the steady state probability distribution. III.
RESEARCH SET UP A. Basics of HMM
HMM is a stochastic model where the system is assumed to be a Markov Process with hidden states. HMM gives better accuracy than other models. Using the given input values, the parameters of the HMM ( λ ) denoted by A, B and π are found out. Hidden Sequence Observation Sequence
Fig 1.
Trellis Diagram
HMM consists of A set of hidden or latent states (S) A set of possible output symbols (O) A state transition probability matrix (A) (cid:1) probability of making transition from one state to each of the other states Observation emission probability matrix (B) (cid:1) probability of emitting/observing a symbol at a particular state Prior probability matrix ( π ) (cid:1) probability of starting at a particular state An HMM is defined as λ =(S, O, A, B, π ) where S={s ,s ,…,s N } is a set of N possible states O={o ,o ,…,o M } is a set of M possible observation symbols A is an NxN state Transition Probability Matrix (TPM) B is an NxM observation or Emission Probability Matrix (EPM) π is an N dimensional initial state probability distribution vector and A, B and π should satisfy the following conditions: o o ..... o M s s ..... s N = Nj ij a =1 where 1 ≤ i ≤ N; ∑ = Mj ij b = 1 where 1 ≤ i ≤ N; ∑ = Ni i π = 1 where i π ≥ The main problems of HMM are: Evaluation, Decoding, and Learning.
Evaluation problem
Given the HMM },,{ πλ BA = and the observation sequence O = o o ... o M , the probability that model λ has generated sequence O is calculated. Often this problem is solved by the Forward Backward Algorithm (Rabiner, 1989) (Rabiner, 1993). Decoding problem
Given the HMM },,{ πλ BA = and the observation sequence O = o o ... o M , calculate the most likely sequence of hidden states that produced this observation sequence O . Usually this problem is handled by Viterbi Algorithm (Rabiner,1989) (Rabiner,1993). Learning problem
Given some training observation sequences O = o o ... o M and general structure of HMM (numbers of hidden and visible states), determine HMM parameters },,{ πλ BA = that best fit training data. The most common solution for this problem is Baum-Welch algorithm (Rabiner,1989) (Rabiner,1993) which is considered as the traditional method for training HMM. In this paper, IBM daily close value data for a month period is considered. Two observing symbols “ I ” and “ D ” have been used: “I indicates Increasing” , “ D indicates Decreasing ”. If Today’s close value – Yesterday’s close value > 0, then observing symbol is I If Today’s close value – Yesterday’s close value < 0 then observing symbol is D There are six hidden states assumed and are denoted by the symbol S1, S2 , S3 , S4, S5, S6 where S1 indicates “very low”; S2 indicates “low”; S3 indicates “moderate low” S4 indicates “moderate high”; S5 indicates “high”; S6 indicates “very high”. The States are not directly observable. The situations of the stock market are considered hidden. Given a sequence of observation we can find the hidden state sequence that produced those observations.
B. Database
The complete set of data for the proposed study has been taken from yahoofinance.com.
The Table 1 given below shows the daily close value of the stock market:
Table I. Daily close value for finding differences in one day, two days, three days, four days, five days, six days close value
O.SD.in.3 days CV O.S D.in4 days CV O.S D.in. 5 days CV O.S D.in. 6 days CVO.SS.NO C.V D.in. 1 day CV O.S D.in. 2 days CV
C.V – Close value ; O.S – Observing symbol D.in.1 day CV - difference in 1 day close value; D.in.2 days CV - difference in 2 days close value; D.in.3 days CV - difference in 3 days close value; D.in.4 days CV - difference in 4 days close value; D.in.5 days CV - difference in 5 days close value; D.in.6 days CV - difference in 6 days close value V. CALCULATION
The various probability values of TPM, EPM and π for difference in one day, two days, three days, four days, five days, six days close value close value are calculated as given below. A. Probability values of TPM, EPM and π for difference in one day close value: S1 S2 S3 S4 S5 S6
SSSSSS
Fig 2.
TPM
I D
SSSSSS
01 10 10 29.071.0 5.05.0 10
Fig 3.
EPM
Steady state probability distribution
Table II. Transition table with probability values for difference in one day close value
TRANSITION OF STATES WITH OBSERVING SYMBOLS
S1 S2 S3 S4 S5 S6 I D I D I D I D I D I D S1
0 0 0 0 0 1 0 0 0 0 0 0 S2
0 0 0 0 0 0.5 0.5 0 0 0 0 0 S3
0 0 0 0.1429 0 0.1429 0 0 0.5714 0 0.1429 0 S4
0 0.5 0 0 0 0.5 0 0 0 0 0 0 S5
0 0.25 0 0.25 0 0.5 0 0 0 0 0 0 S6
0 0 0 0 0 0 0 0 0 0 1 0
B. Probability values of TPM, EPM and π for difference in two days close value: S1 S2 S3 S4 S5 S6
SSSSSS
Fig 4.
TPM
I D
SSSSSS
10 10 01 5.05.0 67.033.0 4.06.0
Fig 5.
EPM
Steady state probability distribution able III. Transition table with probability values for difference in two days close values
TRANSITION OF STATES WITH OBSERVING SYMBOLS
S1 S2 S3 S4 S5 S6 I D I D I D I D I D I D S1
0 0.4 0 0 0.4 0 0.2 0 0 0 0 0 S2
0 0.33 0 0.33 0.33 0 0 0 0 0 0 0 S3
0 0.33 0 0.167 0.5 0 0 0 0 0 0 0 S4
0 0 0 0 0 0 0 0 0 0 1 0 S5
0 0 0 1 0 0 0 0 0 0 0 0 S6
0 0 0 0 0 0 0 0 0 1 0 0
C. Probability values of TPM, EPM and π for difference in three days close value: S1 S2 S3 S4 S5 S6
SSSSSS
Fig 6.
TPM
I D
SSSSSS
01 01 10 6.04.0 10 5.05.0
Fig 7.
EPM
Steady state probability distribution
Table 1V. Transition table with probability values for difference in three days close values
TRANSITION OF STATES WITH OBSERVING SYMBOLS
S1 S2 S3 S4 S5 S6 I D I D I D I D I D I D S1
0 0 0 0.5 0.5 0 0 0 0 0 0 0 S2
0 0 0 0.25 0 0.75 0 0 0 0 0 0 S3
0 0.2 0 0.2 0 0.2 0.2 0 0 0 0.2 0 S4
0 0.5 0 0.5 0 0 0 0 0 0 0 0 S5
0 0 0 0 0 0 1 0 0 0 0 0 S6
0 0 0 0 0 0 0 0 0.5 0 0.5 0
D. Probability values of TPM, EPM and π for difference in four days close value: S1 S2 S3 S4 S5 S6
SSSSSS
Fig 8.
TPM
I D
SSSSSS
Fig 9.
EPM teady state probability distribution
Table V. Transition table with probability values for difference in four days close values
TRANSITION OF STATES WITH OBSERVING SYMBOLS
S1 S2 S3 S4 S5 S6 I D I D I D I D I D I D S1
0 0.33 0 0.33 0 0.33 0 0 0 0 0 0 S2
0 0 0 0 0 0.33 0.67 0 0 0 0 0 S3
0 0.67 0 0 0 0 0 0 0.33 0 0 0 S4
0 0 0 1 0 0 0 0 0 0 0 0 S5
0 0 0 0 0 0 0 0 0 0 1 0 S6
0 0 0 0.33 0 0 0 0 0 0 0.67 0
E. Probability values of TPM, EPM and π for difference in five days close value: S1 S2 S3 S4 S5 S6
SSSSSS
Fig 10.
TPM
I D
SSSSSS
01 01 5.05.0 10 10 75.025.0
Fig 11.
EPM
Steady state probability distribution
Table VI. Transition table with probability values for difference in five days close values
TRANSITION OF STATES WITH OBSERVING SYMBOLS
S1 S2 S3 S4 S5 S6 I D I D I D I D I D I D S1 S2
0 1 0 0 0 0 0 0 0 0 0 0 S3
0 0.33 0 0 0 0.67 0 0 0 0 0 0 S4
0 0 0 0.5 0 0 0 0 0.5 0 0 0 S5
0 0 0 0 0 0 0 0 0.5 0 0.5 0 S6
0 0 0 0 0 0 1 0 0 0 0 0
F. Probability values of TPM, EPM and π for difference in six days close value: S1 S2 S3 S4 S5 S6
SSSSSS
Fig 12.
TPM
I D
SSSSSS
01 33.067.0 10 01 10 10
Fig 13.
EPM
Steady state probability distribution able VII.
Transition table with probability values for difference in six days close values
TRANSITION OF STATES WITH OBSERVING SYMBOLS
S1 S2 S3 S4 S5 S6 I D I D I D I D I D I D S1
0 0.5 0 0.5 0 0 0 0 0 0 0 0 S2
0 0.5 0 0 0 0.5 0 0 0 0 0 0 S3
0 0 0 0 0 0 1 0 0 0 0 0 S4
0 1 0 0 0 0 0 0 0 0 0 0 S5
0 0.33 0 0 0 0 0 0 0.33 0 0.33 0 S6
0 0 0 0 0 0 0 0 0.5 0 0.5 0
The MATLAB function “Hmmgenerate” is used to generate a random sequence of emission symbols and states. The length of both sequence and states to be generated is denoted by L. The HMM matlab toolbox syntax is : [Sequence, States] = Hmmgenerate ( L , TPM, EPM) , see [11] For instance, If the Input is given as,
TPM = [0 0 1 0 0 0; 0 0 0.5 0.5 0 0; 0 0.143 0.143 0 0.571 0.143; 0.5 0 0.5 0 0 0; 0.25 0.25 0.5 0 0 0; 0 0 0 0.5 0 0.5]; EPM = [0 1;0.5 0.5; 0.71 0.29;0 1;0 1;1 0]; [sequence,states] = hmmgenerate(7, TPM, EPM) 'Sequence Symbols',{'I','D'},... 'Statenames',{'very low';'low';'moderate low';'moderate high';'high';'very high'}
Then the Output of few randomly generated sequences and states is given below:
Sequence: ε → I → D → D → I → I → I → I states : S3 S2 S3 S6 S6 S6 S6 sequence : ε → D → I → D → D → I → I → I states : S3 S3 S5 S1 S3 S2 S3 where ‘ ε ‘ denotes the start symbol . The fitness function used for finding the fitness value of sequence of states is defined by
Fitness = ∑ ),(1 jicompare V. DISCUSSION
Using the Iterative procedure, for each TPM and EPM framed we get an optimum sequence of states generated. The length of the sequence generated is taken as L=7, for instance. The optimum sequence of states obtained from the one day difference TPM and EPM is ε (cid:2) D (cid:2) I (cid:2) D (cid:2) I (cid:2) D (cid:2) I S1 S3 S5 S3 S5 S3 S5
Similarly ,we get 5 more such optimum sequences of states for 2 day difference , 3 day difference, 4 day difference, 5 day difference, 6 day difference TPM and EPM respectively as follows: ε (cid:2) I (cid:2) D (cid:2) D (cid:2) I (cid:2) D (cid:2) D S1 S3 S1 S1 S3 S1 S1 3. ε (cid:2) D (cid:2) D (cid:2) I (cid:2) D (cid:2) I (cid:2) I S1 S2 S3 S4 S1 S3 S4 4. ε (cid:2) D (cid:2) I (cid:2) D (cid:2) I (cid:2) D (cid:2) D S1 S2 S4 S2 S4 S2 S3 5. ε (cid:2) D (cid:2) D (cid:2) I (cid:2) I (cid:2) D (cid:2) D S1 S2 S1 S1 S1 S2 S1 6. ε (cid:2) D (cid:2) D (cid:2) I (cid:2) D (cid:2) D (cid:2) D S1 S2 S3 S4 S1 S2 S3
Using the fitness function we compute the fitness value for each of the optimum sequence of states obtained . able VIII. Comparison of Six Optimum State Sequences S.No. Comparison of 6 optimum sequence of states Calculated value Fitness = ∑ ),(1 jicompare
1. (1,2) + (1,3) + (1,4) + (1,5) + (1,6) 1 1 2. (2,1) + (2,3) + (2,4) + (2,5) + (2,6) 1.29 0.76 3. (3,1) + (3,2) + (3,4) + (3,5) + (3,6) 1.86 0.54 4. (4,1) + (4,2) + (4,3) + (4,5) + (4,6) 1.43 0.70 5. (5,1) + (5,2) + (5,3) + (5,4) + (5,6) 2.14 0.47 6. (6,1) + (6,2) + (6,3) + (6,4) + (6,5) 2.14 0.47
VI. CONCLUSION
In this paper, results are presented using Hidden Markov Model to find the trend of the stock market behavior. The highest is the fitness value, the better is the performance of the particular sequence. One day difference in close value when considered is found to give the best optimum sequence. It is observed that at any point of time over years, if the stock market behaviour pattern is the same then we can observe the same steady state probability values as obtained in one day difference of close value, which clearly determines the behavioural pattern of the stock market.
VII. REFERENCES [1] Aditya Gupta and Bhuwan Dhingra, Non-Student members, IEEE, “Stock Market Prediction Using Hidden Markov Models,” 2012. [2] G. E. P. Box and G. M. Jenkins, Time series analysis: forecasting and control. San Fransisco, CA: Holden-Day, 1976. [3] Dr. Bryan Taylor, The Global Financial Data Guide to Bull and Bear Markets, President, Global Financial Data, Inc. [4] W.C. Chiang, T. L. Urban and G. W. Baldridge,“A neural network approach to mutual fund net asset value forecasting,” Omega International Journal of Management Science.,Vol. (2), pp. 205–215,1996. [5] Halbert White, “Economic prediction using neural networks: the case of IBM daily stock returns,” Department of Economics, University of California, San Diego. [6] Henry M. K. Mok, “Causality of interest rate, exchange rate and stock prices at stock market open and close in Hong Kong,” Asia Pacific Journal of Management,Vol. (2), pp. 123–143,1993. [7] Hassan Rafiul, Nath Baikunth and Michael Kirley, “HMM based Fuzzy Model for Time Series Prediction,” IEEE International Conference on Fuzzy Systems., pp. 2120-2126,2006. [8] Jyoti Badge, “Forecasting of Indian Stock Market by Effective Macro- Economic Factors and Stochastic Model,” Journal of Statistical and Econometric Methods, Vol. (2), pp. 267-296,1993. [14] Y. Romahi and Q. Shen, “Dynamic financial forecasting with automatically induced fuzzy associations,” In Proceedings of the 9th international conference on fuzzy systems., pp. 493–498,2000. [15] Md. Rafiul Hassan and Baikunth Nath, “Stock Market forecasting using Hidden Markov Model: A New Approach,” Proceeding of the 2005 5 th international conference on intelligent Systems Design and Application 0-7695-2286-06/05, IEEE, 2005. [16] Md. Rafiul Hassan, Baikunth Nath and Michael Kirley, “ A fusion model of HMM, ANN and GA for stock market forecasting,” Expert systems with Applications., pp. 171-180,2007. [17] A. S. Weigend A. D. Back, “What Drives Stock Returns?-An Independent Component Analysis,” In Proceedings of the IEEE/IAFE/INFORMS 1998 Conference on Computational Intelligence for Financial Engineering, IEEE, New York., pp. 141-156,1998. [18] H.White, “Economic prediction using neural networks: the case of IBM daily stock returns,” In Proceedings of the second IEEE annual conference on neural networks., II, pp. 451–458,1988. [19] H.White, Learning in artificial neural networks: a statistical perspective, Neural Computation., Vol. , pp. 425–464,1989., pp. 425–464,1989.