INTERNATIONAL JOURNAL OF ELECTRICAL, ELECTRONICS AND COMPUTER SYSTEMS (IJEECS), volume 1, issue 1, MARCH 2011 www.ijeecs.org ISSN: 2221-7258(Print) ISSN: 2221-7266 (Online)

Using HMM for Forecasting Stock Market Index Md. Rafiul Hassan, Baikunth Nath and Rezaul Begg† Computer Science and Software Engineering The University of Melbourne, Carlton 3010, Australia. † CARES, Victoria University, Melbourne 8001, Australia.

Abstract Prediction of future stock values due to randomness and non-linearity in the data is a challenging problem for the financial investment markets. Numerous models are available to approximately predict these non-linear events. Recently, much research attention and interest has focused in this area, including models based on ANN, SVM and FIS. In this paper, we propose a stock prediction system based on HMM because of its proven suitability for modeling dynamic systems and pattern classification. We apply the HMM methodology to predict the Australian Stock Index ASX100 for the next few days using available past datasets. The results obtained using HMM are encouraging. Finally, we recommend development of hybrid models by combining HMM with other soft computing paradigms, for example ANN, ANFIS or GA, to improve prediction results further. Keywords: HMM, stock market, forecasting. I. INTRODUCTION The nature of the stock market is generally complex and volatile. They show random non-linear behaviour. The rate of fluctuation depends on many factors, e.g. equity, interest rate securities, options, warrants, merger and ownership of large financial corporations or companies etc. The major attraction to the business of stock market is to make large profit with good liquidation. The Australian Stock Exchange Limited (ASX) was formed in 1987 by legislation of the Australian Parliament [2]. The stock markets are now an integral part of the global economy. So any fluctuation in this market influences our personal and corporate financial lives, and as a consequence the economic fate of almost every country on the earth [1]. Financial prediction such as stock value forecasting has always been one of the most challenging topics for research and of commercial magnetism. With the rapid growth in Internet technology in recent years, e-Finance becomes a vital application of e-commerce. However, with so abundant information available, an intelligent stock forecasting system would be the most desirable key factor for business success. In recent years, numerous stock prediction systems based on artificial intelligence techniques, including artificial neural networks (ANN) [4], support vector machines (SVM) [3] and fuzzy inference have been proposed in the literature. In

this paper, we apply HMM for stock value prediction due to its demonstrated high success in similar pattern prediction applications [5]. In particular, we apply the proposed methodology to forecast a stock value traded at the Australian Stock Exchange. Performance of the proposed model as seen from the initial experimental results is encouraging. The remainder of the paper is organized as follows. Section 2 describes about HMM in details. In Section 3, we describe the forecasting process along with the steps involved in the process. Section 4 presents the model initialization and experimental results. Finally, conclusion and future directions are discusses in Section 5. II. BACKGROUND In this section we briefly review the HMM methodology along with the relevant processes to build, initialize and train the HMM. Hidden Markov Model A Hidden Markov Model (HMM) is a finite state machine which has some fixed number of states. It provides a probabilistic framework for modeling a time series of multivariate observations [8]. The HMM represents the overall process behavior in terms of movement between states and describes the inherent variations of the observations within a state. In fact, HMM represents stochastic sequences as Markov chains where the states are not directly observed but are associated with a probability density function [7]. HMM is characterized by the following: •

The number of states in the model,



The number of observation symbols,



The state transition probabilities. Probabilities to move from state i to state j, i.e P( q j | qi ) ,



The observation emission probability distribution that characterize each state qi , i.e. p ( x | qi ) ,



The initial state distribution.

Thus having a Markov chain with transition matrix A , observation emission matrix B and prior state probability π , we can build the Markov model λ = ( A, B, π ) [7]. By training the model and making adjustments to the values of A, B and π , we can compute P (O | λ ) , the probability of the observation se-

quence O ( = O1O2 ...OT ) given the model λ, using the equation

P(O|λ) = ∑P(O|Q, λ) P(Q|λ),

N), and the observation emission probability from state i is b i (O t ) , t = 1 ,….., M.

all Q

= ‡”πq bq ( O1 )aq q bq ( O2 )...aq q ( OT ) 1 1 1 2 2 T 1 T

If the number of states are N and the number of observations in a sequence is M, we assume that the state transition probability from state i to j is a ij (i, j = 1, …..,

(1)

The two probabilities given above must satisfy the probability law constraints

where, b q (O) is the observation emission probability T

a q1q 2 = transition probability from state a q 1 to state a q . 2

where,

j

t

aij ≥ 0 and b j ( Ot ) ≥ 0

Thus, we define the forward probability iteratively as follows [7]: 1)

To construct an HMM for a given problem, we must build an initial HMM and then train the HMM suitably to solve the problem. The following subsections describe in brief the operations to initialize and train the HMM.

M

∑ aij = 1 , ‡”b j (O t ) = 1 ,

at state qT ,

Q = state sequence q1 , q 2 , ... , qT in the Markov model,

N

Initialization

α 1 (i ) = π i bi (O i ) 2)

(2)

Induction 



N

α t +1 ( j ) =  ∑ α t (i ) a ij  b j (O t +1 )  i =1

A. Initialization To construct an HMM we assume initial values of A, B and π . In this study, we generate the initial values of A and π randomly. During implementation of HMM, however, the choice of the model (ergodic or left-right or spherical form), choice of the number of states and observation symbol (continuous or discrete or multi-mixture) become a tedious task [7]. For the present application of stock market value prediction, we constructed HMM by varying the parameters, and observed a better performance resulting from the leftright HMM. Since the choice of good initial estimates of the parameters of b q (O) densities is essential for rapid and T

proper convergence of the re-estimation formulae, we used the segmental k-means training procedure for estimating the parameter values [7].

3)

(3)



Termination: P (O | λ ) =

N

∑α i =1

T

(4)

(i )

Similarly we can define the backward probabilities as: i) Initialization

β T (i ) = 1 ii)

(5)

Induction N

β t (i ) = ∑ a ij b j (O t +1 ) β t +1 ( j )

(6)

j =1

We define two more probabilities γ and ξ which we shall use in the learning of parameters of HMM. The equations used to calculate these two probabilities are α (i ) β t (i ) α (i ) β t (i ) (7) γ t (i ) = t = N t P (O | λ ) ∑ α t (i ) β t (i ) i =1

B. Forward-Backward Procedure To calculate the value of P (O | λ ) , using equation (1), it requires 2TNT calculations, since at every time slot t = 1,2, …. T, there are N possible states which can be reached. So there are NT possible state sequences and for each such state sequence about 2T calculations are required for each term in equation (1). This indeed is a complex task to do. Fortunately, there is an efficient process available, called Forward-Backward procedure, which can be used to compute the value of P (O | λ ) .

α (i ) a ij b j (O t +1 ) β t +1 ( j ) ξ t (i , j ) = t P (O | λ )

(8)

C. Baum-Welch Learning Algorithms A number of algorithms are available to adjust the parameters of the HMM with observation sequences. Among them the Baum-Welch (BW) algorithm [9] is widely used. In BW algorithm, the parameters of an HMM are trained to maximize the sum of the log-likelihood of each training sequences.

Baum-Welch algorithm re-estimates the probabilities π i , aˆ ij and bˆij using the values of γ and ξ. These re-estimates are πˆ i = γ 1 (i )

(9)

T −1

aˆ ij =

∑ ξ ( i, j ) t =1 T −1

t

(10)

∑ γ (i) t =1

t

T

∑γ

bˆi ( c ) =

t =1 Ot = c T

∑γ t =1

t

( j)

(11) t

( j)

III. FORECASTING PROCESS To forecast the stock market index using HMM, the continuous signal formed by the number of predictors and the variable of interest (for example, for a d-dimensional data, if the predictor variables are: x1, x2, ….., xd and the variable of interest is y then a signal will be formed using values of x1, x2 , x3 , ….., xd and y) is input into an HMM for training the HMM. After the HMM is trained, this becomes an expert HMM that is well suited to the training data signals. Then, for any continuous signal of the size of training data signal, the HMM produces the probability which depicts how well the data signal matches with the trained HMM. In other word, it can be explained that this probability gives us a measurement of closeness of the data signal (in terms of fluctuations with respect to time) with the set of training data signals. This value being a small number we take the log of probability, which is called the likelihood value. After having a trained HMM we produce likelihood values for each of the training data signals. The average of the produced likelihood values is calculated. We refer to this average likelihood value as η.

Generating η Presenting dataset to the model

Training the model Forecasting Stock Index

Fig. 1. Steps in the Forecasting Process For any unknown data signal wherein the values of predictors are known but the value of variable of interest is unknown, it is assumed that if the value of unknown parameter would have been known then the signal formed could produce the same likelihood value as that of the average likelihood valueη . So by taking into the

consideration the aforementioned assumption we estimate the exact value of the unknown parameter via an iterative process, such that the signal formed produces a likelihood value equal to the average likelihood value η . The value of unknown parameter thus found is then the forecast value for the given values of the predictor variables. Figure 1 shows the steps of the forecasting process and the details of the steps involved are described below. A. Presenting Dataset to the Model In this section, we discuss how the dataset is preprocessed before feeding into an HMM. If there are d number of independent variables or predictors and one dependent variable then the total size of each data signal will be d+1. Thus for a total m number of training data items there are m data signals. To achieve a better performance, data are initially standardized using the mean and standard deviation of each of the data attributes. Then the m data signals are formed using the above procedure. B. Training the Model Now we have a set of data signals. In this section we form an initial HMM and then train the HMM so that it best-suits with the training data signals. To do this we adopt the processes described in section . C.

Generating η

The trained HMM obtained in III-B above is then employed to calculate the m number of likelihood values for the m training data signals. Let us refer to these likelihood (LL) values as l1, l2,,l3……..lm. The average LL value, η, is obtained using the equation m

η=

∑l i =1

m

i

(12)

D. Forecasting Stock Index To obtain a forecast of a variable of interest where the predictor value is known, we use the previously trained HMM and the average LL value η . Suppose we wish to forecast the value of variable “y”. Initially we assume that the value of “y” is zero. The set of predictors and y forms a continuous signal as is done in III-A. By feeding this signal to the trained HMM we get an LL value lp. If this lp does not exactly or closely match the value η, the value of “y” is increased and again LL is reestimated using this new value of “y”. The produced LL value lp is again checked with η. This iterative process continues until the lp becomes close enough or exactly the same as η. The last value of “y” found is the forecast value of the unknown variable of interest.

The data for our study was acquired from the Australian Stock Exchange Index ASX100 for the period 2 January 2001 to 23 January 2002. We used the daily opening price index, closing price index, high price index, low price index and the next day’s high price index (target index) as five inputs for training HMM. We randomly selected 80% of the data for training and reserved the remaining 20% data for testing. As mentioned in the Section II-A a nine state left to right HMM structure was observed to perform better for the ASX 100 index forecasting. The following section provides some further details on the choice of parameters. To evaluate the forecast efficiency of the model, we used the first four features from the test dataset as inputs variables for the trained HMM and the last remaining feature (next day’s highest price index) to compare with the obtained forecast. A. Choosing the Model Parameters To build the architecture of the HMM, we choose empirically 9 states HMM and multi-mixture observation probability, and the left-right HMM process for the stock market index forecasting.

B. Test Results Figure 2 displays the forecast performance for the ASX100 high price index. The performance of the model is evaluated by calculating the coefficient of determination, R2 = 0.981018 and the mean relative error 0.027043 [8]. R2 close to 1 indicates that the predicted price index closely follows the fluctuations in the observed price index. The scatter plot in Figure.3 shows a strong correlation between the predicted and the actual data. A positive mean relative error indicates that the model tends to underestimate the index (perhaps a bit cautionary) as we can see in the Figure 2. 3 2 Standardized Index

IV. EXPERIMENTATION AND PERFORMANCE EVALUATION

1 0 -1 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 P

-2 -3 -4

∑π i =1

i

= 1 and

N

∑a

ij

=1

j

where, N = number of states

where,

∑c

∑c j =1

µ jm

[

ℵ O , µ jm , U jm

]

(13)

c jm = mixture coefficient for the m-th mixture in N

state j (

jm

jm

= 1 ),

= mean vector, U jm = Covariance matrix, and

ℵ = Gaussian density

2 1 0 -4

-3

-2

-1

-1

0

1

2

-2 -3

A three-dimensional Gaussian distribution was used to determine the observation probability density function [7]. b j (O ) =

Day

3

Actual Value

N

Real High Value

Fig. 2. Actual vs Forecast values

Since the observations in our case are continuous rather than discrete, we use as many as 3 mixtures for each state for the model density. The covariance matrices are constrained to diagonal for each individual mixture. The initial value of A and π were chosen randomly and then normalized so that, the following conditions are satisfied:

Predicted High Value

-5

-4 -5 Predicted Value

Fig. 3. Correlation between actual and forecast values V. CONCLUSION In this paper, we investigated the applicability of HMM in forecasting stock market price index. The results obtained using HMM as a predictor for unknown data are very encouraging. The quality of prediction is evident from Figs. 2 and 3. The high R2 value in our experiment indicates that HMM can be usefully applied for making forecasts for the unseen data. In our future work we propose to develop hybrid systems to further improve efficiency of our stock market predictions.

REFERENCES [1] A Abraham, “Analysis of Hybrid Computing Techniques for Forex Monitoring Systems,” Proceedings IEEE International Conference on Fuzzy Systems, Vol 2, 2002, pp. 1616-1622. [2] Australian Stock Exchange http://www.asx.com.au/statistics/HistoricalData_M S2.shtm [3] W Huang, Y Nakamori and Y-Y Wang, “Forecasting stock market movement direction with support vector machine,” Computers & Operations Research, 2005 (In press). [4] A Kanas and A Yannopoulos, “Comparing linear and nonlinear forecasts for stock returns,” International Review of Economics & Finance, Vol 10 (4), 2001, pp. 383-398. [5] Z Li, Z Wu., Y He and C Fulei, “Hidden Markov model-based fault diagnostics method in speed-up and speed-down process for rotating machinery,” Mechanical Systems and Signal Processing, Vol 19(2), 2005, pp. 329-339.

[6] B Nath and H Nath, “Using Neural Networks and Statistical Methods for Forecasting Electricity Demand in Victoria,” International Journal of Management and systems, Vol: 16(1), 2000 , pp. 105112. [7] L R Rabiner, “A tutorial on Hidden Markov Models and Selected Applications in Speech Recognition,” Proceedings of the IEEE, Vol 77(2), 1989, pp. 257-286. [8] R C Vasko Jr., A El-Jaroudi and J R Boston, “An Algorithm to determine hidden Markov model topology,” ICASSP-96 IEEE Conference Proceedings, Vol. 6, 1996 , 3577-3580. [9] L E Baum, T Petrie, G Souls and N Weiss, “A maximization technique occurring in statistical analysis of probabilistic functions of Markov chains,” Ann Math Stat., Vol 41(1), 1970, pp. 164171.

Using HMM for Forecasting Stock Market Index

The Australian Stock Exchange Limited (ASX) was formed in 1987 by legislation of the Australian Parlia- ment [2]. The stock markets are now an integral part of.

322KB Sizes 1 Downloads 168 Views

Recommend Documents

Top pick for the day: Swiss Market Index
Financial Services Commission and Financial Supervisory Service. Taiwan .... they are domiciled or to Indonesia residents except in compliance with applicable ...

An empirical index for labor market density
Oct 16, 2002 - Several authors have developed empirical models along these lines, see e.g.: Diamond (1982), Burda and ... First, it ignores the role of infrastructure. What we are ..... 5 Minnesota Minneapolis-St.Cloud 0.21236. 6 Connecticut ...

Stock Market Participation
observed non-participation, they are not able to account for all of it. Polkovnichenko (2004) finds that even .... 9 Risky financial assets include stocks, corporate bonds, managed investment accounts and mutual funds. 10 We point out that in ... Hen

3-Day-Ahead Forecasting of Regional Pollution Index ... - Springer Link
Aug 29, 2009 - Regional Pollution Index as well as the number of consecutive hours, during the day, with at least one of the pollutants above a threshold ...

Download Stock Market Investing for Beginners
use to build a more secure financial foundation for you and your family. ... Market Investing for Beginners will arm you with the information you need to understand ... A Beginner's Guide to Trading Tools and Tactics, Money Management, Discipline ...

stock market for dummies pdf free download
Click here if your download doesn't start automatically. Page 1 of 1. stock market for dummies pdf free download. stock market for dummies pdf free download.

003 The Implications Of Stock Market Reaction ( Non - re) For ...
003 The Implications Of Stock Market Reaction ( Non - re) For Financial Accounting Standard Setting.pdf. 003 The Implications Of Stock Market Reaction ( Non ...

stock market trading for dummies pdf
stock market trading for dummies pdf. stock market trading for dummies pdf. Open. Extract. Open with. Sign In. Main menu. Displaying stock market trading for ...

Momentum in stock market returns: Implications for risk ...
)82.3(. 18.0. 0.09. P3. )41.0(. 24.0. −. −. )43.5(. 19.0. 0.11. P4. )33.0(. 57.0. )33.4(. 15.0. 0.07. P5. )33.1(. 39.2. )58.3(. 17.0. 0.09. P6. )65.0(. 05.1. )13.5(. 15.0. 0.08 ...