Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
HMM & StockPredictionDavid Chiu @ ML/DM Mondayhttp://ywchiu-tw.appspot.com/
HIDDEN MARKOV MODEL• Finite state machine which has some fixed number ofstates• Provides a probabilistic framework for mod...
STOCK PRICE PREDICTION
Every time history repeats itself• Stock behavior of past is similar to behavior of current day• The Next day’s stock pric...
BENEFIT OF USING HMM• Handle new data robustly• Computationally efficient to develop and evaluate• Able to predict similar...
HMM ON STOCK PREDICTION• Using the trained HMM, likelihood value P for currentday’s dataset is calculated• From the past d...
CHARACTERIZE HMM• Number of states in the model: N• Number of observation symbols: M• Transition matrix A = {aij} , where ...
MODELING HMM
PROBLEM OF HMM1. The Evaluation Problem - Forward– What is the probability that the given observations O = o1 ,o2,...,oT a...
BAUM-WELCH ALGORITHM• Find the unknown parameters of a hidden Markov model(HMM).• Generalized expectation-maximization (GE...
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-0...
BUILD HMM MODEL# Include RHMM Library•library(RHmm)# Baum-Welch Algorithm•hm_model <- HMMFit(obs =TWII_Train , nStates = 5...
SCATTER PLOT• TWII_Predict <- cbind(TWII_Subset$TWII.Close, VitPath$states)• chartSeries(TWII_Predict[,1])• addTA(TWII_Pre...
DATA VISUALIZATION
SCIKIT LEARN#Baum-Welch Algorithm•n_components = 5•model = GaussianHMM(n_components, "diag")•model.fit([X], n_iter=1000)# ...
MODELING SAMPLE
MODELING SAMPLE
PREDICTION#State Prediction – using Scikit-learn•data_vec = [diff[last_day], volume[last_day]]•State = model.predict([data...
REFERENCE• Hassan, M. (2009). A combination of hidden Markov model andfuzzy model for stock market forecasting. Neurocompu...
Hidden Markov Model & Stock Prediction
Hidden Markov Model & Stock Prediction
Hidden Markov Model & Stock Prediction
Upcoming SlideShare
Loading in …5
×

Hidden Markov Model & Stock Prediction

16,724 views

Published on

Introducing how to apply HMM on stock prediction

  • Dating for everyone is here: ❶❶❶ http://bit.ly/2ZDZFYj ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ❤❤❤ http://bit.ly/2ZDZFYj ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • How to use "The Scrambler" ot get a girl obsessed with BANGING you...  http://t.cn/AijLRbnO
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Imagine if you had a magic wealth magnet that attracts success, happiness and limitless money into your life� with no "hard work" required? ★★★ https://tinyurl.com/y44vwbuh
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • FREE TRAINING: "How to Earn a 6-Figure Side-Income Online" ...  https://bit.ly/2kS5a5J
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Hidden Markov Model & Stock Prediction

  1. 1. HMM & StockPredictionDavid Chiu @ ML/DM Mondayhttp://ywchiu-tw.appspot.com/
  2. 2. HIDDEN MARKOV MODEL• Finite state machine which has some fixed number ofstates• Provides a probabilistic framework for modeling a timeseries of multivariate observations
  3. 3. STOCK PRICE PREDICTION
  4. 4. 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 datapattern
  5. 5. BENEFIT OF USING HMM• Handle new data robustly• Computationally efficient to develop and evaluate• Able to predict similar patterns efficiently
  6. 6. HMM ON STOCK PREDICTION• Using the trained HMM, likelihood value P for currentday’s dataset is calculated• From the past dataset using the HMM we locate thoseinstances that would produce the nearest P likelihoodvalue.
  7. 7. CHARACTERIZE HMM• Number of states in the model: N• Number of observation symbols: M• Transition matrix A = {aij} , where aij represents the transitionprobability from state i to state j• Observation emission matrix B = {bj(Ot)} , where bj(Ot) represent theprobability of observing Ot at state j• Initial state distribution π = {πi}
  8. 8. MODELING HMM
  9. 9. PROBLEM OF HMM1. 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 λ thatproduced the given observations O = o1 ,o2 ,...,oT ?1. The Learning Problem - Baum-Welch– How should we adjust the model parameters {A,B,π } in order tomaximize p{O|λ} , whereat a model λ and a sequence ofobservations O = o1 ,o2 ,...,oT are given?
  10. 10. BAUM-WELCH ALGORITHM• Find the unknown parameters of a hidden Markov model(HMM).• Generalized expectation-maximization (GEM) algorithm• Compute maximum likelihood estimates and posteriormode estimates for the parameters (transition and emissionprobabilities) of an HMM, when given only emissions as trainingdata.
  11. 11. FIREARM
  12. 12. TOOL KIT• R Package– HMM– RHMM• JAVA– JHMM• Python– Scikit Learn
  13. 13. DEMO
  14. 14. 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)
  15. 15. 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)
  16. 16. 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)
  17. 17. DATA VISUALIZATION
  18. 18. 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)
  19. 19. MODELING SAMPLE
  20. 20. MODELING SAMPLE
  21. 21. PREDICTION#State Prediction – using Scikit-learn•data_vec = [diff[last_day], volume[last_day]]•State = model.predict([data_vec])
  22. 22. REFERENCE• Hassan, M. (2009). A combination of hidden Markov model andfuzzy model for stock market forecasting. Neurocomputing, 72(16),3439-3446.• Gupta, A., & Dhingra, B. (2012, March). Stock Market PredictionUsing Hidden Markov Models. In Engineering and Systems (SCES),2012 Students Conference on (pp. 1-4). IEEE.

×