Home > Technology > Hidden Markov Model & Stock Prediction

# Hidden Markov Model & Stock Prediction

Date post: 10-May-2015
Category:
View: 6,046 times
Description:
Introducing how to apply HMM on stock prediction
Popular Tags:
25
HMM & Stock Prediction David Chiu @ ML/DM Monday http://ywchiu-tw.appspot.com/
Transcript

HMM & Stock PredictionDavid Chiu @ ML/DM Mondayhttp://ywchiu-tw.appspot.com/

HIDDEN MARKOV MODEL

• Finite state machine which has some fixed number of states

• Provides a probabilistic framework for modeling a time series of multivariate observations

STOCK PRICE PREDICTION

Every time history repeats itself

• Stock behavior of past is similar to behavior of current day• The Next day’s stock price should follow about the same past data

pattern

BENEFIT OF USING HMM

• Handle new data robustly

• Computationally efficient to develop and evaluate

• Able to predict similar patterns efficiently

HMM ON STOCK PREDICTION

• Using the trained HMM, likelihood value P for current day’s dataset is calculated

• From the past dataset using the HMM we locate those instances that would produce the nearest P likelihood value.

CHARACTERIZE HMM

• Number of states in the model: N

• Number of observation symbols: M

• Transition matrix A = {aij} , where aij represents the transition probability from state i to state j

• Observation emission matrix B = {bj(Ot)} , where bj(Ot) represent the probability of observing Ot at state j

• Initial state distribution π = {πi}

MODELING HMM

PROBLEM OF HMM

1. The Evaluation Problem - Forward– What is the probability that the given observations O =

o1 ,o2 ,...,oT are generated by the model p{O|λ} with a given HMM λ ?

2. The Decoding Problem - Viterbi– What is the most likely state sequence in the given model λ that

produced the given observations O = o1 ,o2 ,...,oT ?

3. The Learning Problem - Baum-Welch– How should we adjust the model parameters {A,B,π } in order to

maximize p{O|λ} , whereat a model λ and a sequence of observations O = o1 ,o2 ,...,oT are given?

BAUM-WELCH ALGORITHM

• Find the unknown parameters of a hidden Markov model(HMM).

• Generalized expectation-maximization (GEM) algorithm

• Compute maximum likelihood estimates and posterior mode estimates for the parameters (transition and emission probabilities) of an HMM, when given only emissions as training data.

FIREARM

TOOL KIT

• R Package– HMM– RHMM

• JAVA– JHMM

• Python– Scikit Learn

DEMO

GET DATASET

• library(quantmod)

• getSymbols("^TWII")

• chartSeries(TWII)

• TWII_Subset<- window(TWII, start = as.Date("2012-01-01"))

• TWII_Train <- cbind(TWIISubset\$TWII.Close - TWII_Subset\$TWII.Open, TWII_Subset\$TWII.Volume)

BUILD HMM MODEL

# Include RHMM Library

•library(RHmm)

# Baum-Welch Algorithm

•hm_model <- HMMFit(obs =TWII_Train , nStates = 5)

# Viterbi Algorithm

•VitPath <- viterbi(hm_model, TWII_Train)

SCATTER PLOT

DATA VISUALIZATION

SCIKIT LEARN

#Baum-Welch Algorithm

•n_components = 5

•model = GaussianHMM(n_components, "diag")

•model.fit([X], n_iter=1000)

# predict the optimal sequence of internal hidden state

•hidden_states = model.predict(X)

MODELING SAMPLE

MODELING SAMPLE

PREDICTION

#State Prediction – using Scikit-learn

•data_vec = [diff[last_day], volume[last_day]]

•State = model.predict([data_vec])

REFERENCE

• Hassan, M. (2009). A combination of hidden Markov model and fuzzy model for stock market forecasting. Neurocomputing, 72(16), 3439-3446.

• Gupta, A., & Dhingra, B. (2012, March). Stock Market Prediction Using Hidden Markov Models. In Engineering and Systems (SCES), 2012 Students Conference on (pp. 1-4). IEEE.

Recommended