SlideShare a Scribd company logo
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
Hidden Markov Model & Stock Prediction
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
Hidden Markov Model & Stock Prediction
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

More Related Content

PPTX
Hidden Markov Model (HMM)
PPTX
Hidden Markov Model paper presentation
PPTX
Instruction cycle
PPTX
Computer Organization : CPU, Memory and I/O organization
PPTX
Simple overview of machine learning
PDF
Hidden Markov Models
PPTX
Memory Hierarchy
PPTX
Hidden markov model
Hidden Markov Model (HMM)
Hidden Markov Model paper presentation
Instruction cycle
Computer Organization : CPU, Memory and I/O organization
Simple overview of machine learning
Hidden Markov Models
Memory Hierarchy
Hidden markov model

What's hot (20)

PPTX
MODES OF TRANSFER.pptx
PPTX
Programming fundamentals lecture 1 0f c
PPT
Microinstruction sequencing new
PPTX
Memory hierarchy
PPT
HIDDEN MARKOV MODEL AND ITS APPLICATION
PPTX
Recurrent Neural Networks (RNNs)
PPT
Cache memory
PPTX
Genetic algorithms
PPTX
Computer architecture control unit
PPTX
Digital clock (mod counters)using DSCH (DIGITAL SCHEMATIC) by Gaurav Raikar
PPTX
Problem solving agents
PPTX
Computer system bus
PPTX
Learning rule of first order rules
PDF
Programed I/O Modul..
PPTX
Modes of transfer
PPTX
Rules of data mining
PPTX
Data Science Lifecycle
PPTX
Instruction format
PDF
Day 2 global_state_and_snapshot_algorithms
PPTX
PIPELINE INTERRUPTS
MODES OF TRANSFER.pptx
Programming fundamentals lecture 1 0f c
Microinstruction sequencing new
Memory hierarchy
HIDDEN MARKOV MODEL AND ITS APPLICATION
Recurrent Neural Networks (RNNs)
Cache memory
Genetic algorithms
Computer architecture control unit
Digital clock (mod counters)using DSCH (DIGITAL SCHEMATIC) by Gaurav Raikar
Problem solving agents
Computer system bus
Learning rule of first order rules
Programed I/O Modul..
Modes of transfer
Rules of data mining
Data Science Lifecycle
Instruction format
Day 2 global_state_and_snapshot_algorithms
PIPELINE INTERRUPTS
Ad

Similar to Hidden Markov Model & Stock Prediction (20)

PPT
Logistic Regression using Mahout
PPTX
Hidden Markov Model (HMM).pptx
PPT
tommy shelby operation on Men United.ppt
PDF
Differential Machine Learning Masterclass
PDF
Mining Big Data Streams with APACHE SAMOA
PPTX
ANN ARIMA Hybrid Models for Time Series Prediction
PDF
AutoML lectures (ACDL 2019)
PPSX
Iterative Algorithms.ppsx
PPSX
iterativealgorithms.ppsx
PDF
Mining big data streams with APACHE SAMOA by Albert Bifet
PDF
AI-ASSISTED METAMORPHIC TESTING FOR DOMAIN-SPECIFIC MODELLING AND SIMULATION
PPTX
StackNet Meta-Modelling framework
PDF
Equirs: Explicitly Query Understanding Information Retrieval System Based on Hmm
PDF
Different Models Used In Time Series - InsideAIML
PPTX
Hidden Markov Model
PPTX
Applications of Machine Learning in High Frequency Trading
PPT
HMM (Hidden Markov Model)
PDF
de Valpine NIMBLE
PDF
Predicting Stock Market Price Using Support Vector Regression
PDF
Augmenting Machine Learning with Databricks Labs AutoML Toolkit
Logistic Regression using Mahout
Hidden Markov Model (HMM).pptx
tommy shelby operation on Men United.ppt
Differential Machine Learning Masterclass
Mining Big Data Streams with APACHE SAMOA
ANN ARIMA Hybrid Models for Time Series Prediction
AutoML lectures (ACDL 2019)
Iterative Algorithms.ppsx
iterativealgorithms.ppsx
Mining big data streams with APACHE SAMOA by Albert Bifet
AI-ASSISTED METAMORPHIC TESTING FOR DOMAIN-SPECIFIC MODELLING AND SIMULATION
StackNet Meta-Modelling framework
Equirs: Explicitly Query Understanding Information Retrieval System Based on Hmm
Different Models Used In Time Series - InsideAIML
Hidden Markov Model
Applications of Machine Learning in High Frequency Trading
HMM (Hidden Markov Model)
de Valpine NIMBLE
Predicting Stock Market Price Using Support Vector Regression
Augmenting Machine Learning with Databricks Labs AutoML Toolkit
Ad

More from David Chiu (10)

PDF
無中生有 - 利用外部數據打造新商業模式
PPTX
洞見未來,用python 與 r 結合深度學習技術預測趨勢
PDF
python 實戰資料科學工作坊
PDF
新聞 X 謊言 用文字探勘挖掘財經新聞沒告訴你的真相(丘祐瑋)
PDF
Data Analysis - Making Big Data Work
PDF
PyCon APAC 2014 - Social Network Analysis Using Python (David Chiu)
PPTX
Big Data Analysis With RHadoop
PDF
Social Network Analysis With R
PDF
Machine Learning With R
PPTX
R language tutorial
無中生有 - 利用外部數據打造新商業模式
洞見未來,用python 與 r 結合深度學習技術預測趨勢
python 實戰資料科學工作坊
新聞 X 謊言 用文字探勘挖掘財經新聞沒告訴你的真相(丘祐瑋)
Data Analysis - Making Big Data Work
PyCon APAC 2014 - Social Network Analysis Using Python (David Chiu)
Big Data Analysis With RHadoop
Social Network Analysis With R
Machine Learning With R
R language tutorial

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Modernizing your data center with Dell and AMD
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Chapter 3 Spatial Domain Image Processing.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx
The AUB Centre for AI in Media Proposal.docx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
NewMind AI Monthly Chronicles - July 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Modernizing your data center with Dell and AMD

Hidden Markov Model & Stock Prediction

  • 1. HMM & Stock Prediction David Chiu @ 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
  • 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 data pattern
  • 5. BENEFIT OF USING HMM • Handle new data robustly • Computationally efficient to develop and evaluate • Able to predict similar patterns efficiently
  • 6. 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.
  • 7. 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}
  • 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 • 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.
  • 13. TOOL KIT • R Package – HMM – RHMM • JAVA – JHMM • Python – Scikit Learn
  • 14. DEMO
  • 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)
  • 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)
  • 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]