Hidden Markov Model Approach


Background     Dynamic Programming     Hidden Markov Model    Conclusion&References    Downloads 

Hidden Markov Model (HMM)

A hidden Markov model (HMM) is a statistical model in which the system being modeled is assumed to be a Markov process with unknown parameters, and the challenge is to determine the hidden parameters from the observable parameters[1]. It has been widely applied to speech recognition and financial time series analysis[2].

 

A general form of a HMM is as follows:

1. S={1,….,N} is the set of states. The state at time t is denoted s_t
2. K={k1,…,km} is the output alphabet. In a discrete observation case, M is the number of possible movements (observation).
3. Initial state distribution Л={лi}, defined as
                           Л_i = P(s_1=i)
4. State transition probability distribution A ={aij} 
                           aij=P(sj|si), 1<=i,j<=N
5. Observation symbol probability distribution B=bj(ot). The probability for each state j is:
                           Bj(ot) = P(ot|st=j)

 

We need to train the HMM, the process is a learning process. Given an observation sequence and the set of hidden states, we need to learn the transition probability matrix A and the observation probability matrix B. there are several learning algorithms, and we use a popular algorithm called "Baum-Welch".

 

The transition matrix A is learned as:

The observation matrix B is learned as:

Experiments:

Parameters

There are several parameters which can influence the model. They are: S (the number of inner states), O (the number of emissions), Prelength (the number of training data). Different parameters leads to different results. We try to test the sensitivity of this model to these parameters.

Performance Evaluation

Methods

First we use two different methods to evaluate the model: the accuracy of predicting Raise and Fall (Accuracy), and the standard deviation (STD) between the prediction stock price and the actual stock price.

They are computed as follows:

(x is the predict stock price and y is the actual stock price)

Result

Every time we fix one parameter and test the influence of another parameter on the performance of the model.

  • We test the relation between number of states S and the accuracy on the MSFT data. When number of emissions=11, we let number of inner states vary from 3->21

        The following figure is the relation between S and STD

  • When number of states=5, we let number of emissions vary from 3->21

 The following figure is the relation between O and STD

  • When number of Emissions=11, and number of States=5, we let training window size vary from 20 to 300.

         The following figure is the relation between window size and STD

 

From the above experiments, we found that STD is a better evaluation method for this model. Apparently, when the negative accuracy goes down , the positive accuracy will goes up, so the average accuracy always remain in a certain level. But the STD shows a trend.

Test on ten different stocks

Test on ten different stocks, using window size 100, inner states number 5, emission number 11. The blue line is the predicted stock prices, and the green one is the actual stock prices.

The average standard deviation is :0.0261.

Strategy

Now we have done the prediction part. then how can we use the prediction to do market timing?

Naive Strategy

Upper threshold: U
Lower threshold: L
The strategy is:
Buy when predicted return of next day > U
Sell when Predicted Return of next day < L

The red line shows the growth of our money. We can see that in the most time the red line is above the actual stock price, so in this sense, we can say that it beats the "buy-hold" strategy.

Introducing Error control

During extreme period, for example, the stock pric falls dramaticly, the HMM model will fall behind the actual price movement. In this case, the prediction is not accurate. So we introducing error control, which means every time before we make decision, we calculate the error of the last day prediction. If the error is greater than a threshold, we do not take action until the error falls below the threshold.

The above figure shows the experiment with error control. We can see that it outperforms the previous result withou error control.