HMM & Stock
Prediction
David Chiu @ ML/DM Monday
http://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 λ ?
1. The Decoding Problem - Viterbi
– What is the most likely state sequence in the given model λ that
produced the given observations O = o1 ,o2 ,...,oT ?
1. 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
• TWII_Predict <- cbind(TWII_Subset$TWII.Close, VitPath$states)
• chartSeries(TWII_Predict[,1])
• addTA(TWII_Predict[TWII_Predict[,2]==1,1],on=1,type="p",col=5,pch=25)
• addTA(TWII_Predict[TWII_Predict[,2]==2,1],on=1,type="p",col=6,pch=24)
• addTA(TWII_Predict[TWII_Predict[,2]==3,1],on=1,type="p",col=7,pch=23)
• addTA(TWII_Predict[TWII_Predict[,2]==4,1],on=1,type="p",col=8,pch=22)
• addTA(TWII_Predict[TWII_Predict[,2]==5,1],on=1,type="p",col=10,pch=21)
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.
Hidden Markov Model & Stock Prediction

Hidden Markov Model & Stock Prediction

  • 1.
    HMM & Stock Prediction DavidChiu @ ML/DM Monday http://ywchiu-tw.appspot.com/
  • 2.
    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
  • 3.
  • 4.
    Every time historyrepeats 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
  • 5.
    BENEFIT OF USINGHMM • Handle new data robustly • Computationally efficient to develop and evaluate • Able to predict similar patterns efficiently
  • 6.
    HMM ON STOCKPREDICTION • 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.
  • 7.
    CHARACTERIZE HMM • Numberof 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}
  • 8.
  • 10.
    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 λ ? 1. The Decoding Problem - Viterbi – What is the most likely state sequence in the given model λ that produced the given observations O = o1 ,o2 ,...,oT ? 1. 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?
  • 11.
    BAUM-WELCH ALGORITHM • Findthe 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.
  • 12.
  • 13.
    TOOL KIT • RPackage – HMM – RHMM • JAVA – JHMM • Python – Scikit Learn
  • 14.
  • 15.
    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)
  • 16.
    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)
  • 17.
    SCATTER PLOT • TWII_Predict<- cbind(TWII_Subset$TWII.Close, VitPath$states) • chartSeries(TWII_Predict[,1]) • addTA(TWII_Predict[TWII_Predict[,2]==1,1],on=1,type="p",col=5,pch=25) • addTA(TWII_Predict[TWII_Predict[,2]==2,1],on=1,type="p",col=6,pch=24) • addTA(TWII_Predict[TWII_Predict[,2]==3,1],on=1,type="p",col=7,pch=23) • addTA(TWII_Predict[TWII_Predict[,2]==4,1],on=1,type="p",col=8,pch=22) • addTA(TWII_Predict[TWII_Predict[,2]==5,1],on=1,type="p",col=10,pch=21)
  • 18.
  • 19.
    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)
  • 20.
  • 21.
  • 23.
    PREDICTION #State Prediction –using Scikit-learn •data_vec = [diff[last_day], volume[last_day]] •State = model.predict([data_vec])
  • 24.
    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.

Editor's Notes

  • #22 Transition matrix [[ 8.11812291e-01 2.64161406e-18 3.52349878e-02 2.81734681e-02 1.24779253e-01] [ 8.14457640e-18 9.18502509e-01 8.14974906e-02 8.07189607e-18 8.78456355e-18] [ 2.00105290e-02 2.44748522e-02 8.98883560e-01 2.65606045e-18 5.66310587e-02] [ 4.34984913e-07 3.52347211e-18 3.73302534e-18 8.62880547e-01 1.37119018e-01] [ 3.54215900e-01 6.07265466e-18 6.95345225e-02 1.44955842e-01 4.31293736e-01]] means and vars of each hidden state 0th hidden state mean = [ 6.47102406e+00 1.77594314e+06] var = [ 2.08576453e+03 2.84056347e+10] 1th hidden state mean = [ 3.58949104e+01 3.52922834e+06] var = [ 6.49509351e+03 3.01606472e+11] 2th hidden state mean = [ 8.00347774e+00 2.38896636e+06] var = [ 2.97831204e+03 1.13694938e+11] 3th hidden state mean = [ -1.56406476e+01 1.42048949e+06] var = [ 2.74139080e+03 2.90272908e+10] 4th hidden state mean = [ -7.67653562e+00 2.03930314e+06] var = [ 1.46905928e+04 2.24122434e+11]