See discussions, stats, and author profiles for this publication at: [Link]
net/publication/358770053
Predicting Bitcoin Prices Using Hidden Markov Model *
Article · February 2022
CITATIONS READS
0 1,449
4 authors:
Kagame Richard Aime Munezero
Carnegie Mellon University Carnegie Mellon University
6 PUBLICATIONS 2 CITATIONS 2 PUBLICATIONS 0 CITATIONS
SEE PROFILE SEE PROFILE
Samiratu Ntohsi Daniel Emesiani
Carnegie Mellon University Africa Carnegie Mellon University
2 PUBLICATIONS 0 CITATIONS 1 PUBLICATION 0 CITATIONS
SEE PROFILE SEE PROFILE
All content following this page was uploaded by Aime Munezero on 22 February 2022.
The user has requested enhancement of the downloaded file.
Predicting Bitcoin Prices Using Hidden Markov Model*
Kagame Richard Aime Munezero
Master of Science in Information Technology Master of Science in Information Technology
Carnegie Mellon University Carnegie Mellon University
Kigali-Rwanda Kigali-Rwanda
krichard@[Link] amunezer@[Link]
Samiratu Ntohsi Daniel Emesiani
Master of Science in Engineering Artificial Intelligence Master of Science in Information Technology
Carnegie Mellon University Carnegie Mellon University
Kigali-Rwanda Kigali-Rwanda
sntohsi@[Link] demesian@[Link]
Abstract —The accurate prediction of stock prices has been a on the other hand, studied a mixture of long-term memory and HMM.
difficult adventure in mathematical modeling over the years and The HMM is used to describe the historical transition of bitcoin and
predicting bitcoin even more so, given its high volatility. The Hidden the LSTM model is used to make future predictions [9]. Also, we
Markov Model (HMM) have proven to outperform known Artificial reviewed two papers form Nguyet Nguyem focused on the
Neural Networks models in predicting bitcoin prices as shown from application of HMM for stock prediction. On one of the studies on
performance metrics. This project work focused on the optimal the forecasting of monthly closing prices for S&P 500 stock [2] and
number of hidden states that provides the most accurate predictions. the other daily stock prices for Apple, Google, and Facebook stocks
The approach used was using BIC to select the optimal hidden states [8]. Results from the S&P 500 stock forecast showed that HMM
that could provide good prediction accuracy. The result proves that performed better than traditional methods of stock forecasting.
HMM as a prediction tool for bitcoin prices has more prospects and
can be refined further. For both studies by Nguyen Nguyet the maximum number of hidden
states were determined from the HMM using Akaike information
I. BACKGROUND AND RATIONALE criterion and Bayesian information criterion [8]. From this existing
literature reviewed, we got an understanding on how the next day’s
bitcoin price can be predicted using the previous day’s opening,
Bitcoin is a digital currency that has recently gained popularity and
lowest, highest, and closing price. For this project, the approach of A.
attracted investors who either buy or sell to make some profits. Given
Jain et al which is like that use by Nguyen Nguyet will be used in
that it’s becoming a popular form of investment, knowing the future
predicting the next day’s bitcoin price. This project is different from
price can assist investors in decision-making and lead to more profits.
theirs in the sense that we are predict a different kind of stock which
This project aims to apply the Hidden Markov Model (HMM) in the
is bitcoin prices.
Applied Stochastic Processes course to forecast future bitcoin prices.
Markov's model has a lot of popularity in financial mathematics,
precisely in predicting stock prices [1]. Hidden Markov model is
based on the concept that a given sequence of stochastic observations II. RESEARCH QUESTION ,AIM AND OBJECTIVES
is influenced by a transition of states that follow a Markov chain
process an observer cannot see. In the bitcoin stock market, the prices For this project, apart from applying the Hidden Markov Model used
(opening, lowest, highest, and closing price of the day) are the for predicting bitcoin prices, the research question which will be
observations. The hidden states are the factors that influence the addressed is as follows: How many hidden states are required to
fluctuation in these prices. forecast bitcoin prices with the lowest error rate?
Researchers have carried out several studies on the use of several To address the mentioned research question, we aim at utilizing the
methods like machine learning, Random walks, traditional time series BIC (Bayesian Information Criterion) to find the optimal states, then
models, as well as HMM for predicting the best prices daily ahead of use them in the HMM to predict bitcoin prices. We find this of
time as the results of these forecasts, could help investors maximize importance because from the review of existing literature, most of the
profits. Yike Xu carried out a study that used the HMM approach to authors did not focus on studying the optimal number of hidden states
forecast bitcoin prices. He went further to compare the performance that can give the best Hidden Markov Model that can be used for
of HMM with two Artificial recurrent neural networks models: the prediction.
long short-term memory (LSTM) and the gated recurrent units
(GRU) . From his results, HMM outperformed the artificial recurrent III. THEORY OF HMM (Hidden Markov Models)
neural networks models based on the mean percentage error (MAPE)
and root means square error (RMSE) [7]. IA Hashish et al.
Hidden Markov Models are models from which observations are
obtained from a sequence of hidden internal states [9]. From the IV. METHODOLOGY APPROACH
definition, we see that a hidden Markov model has 2 stochastic
processes one being the outcomes that we see or get and the other The methodological approach used in our research project can be
being the non-observable stochastic processes otherwise referred to partitioned into different sections: data collection, Feature
as hidden states which are responsible for producing the observable engineering, selecting of the optimal number of hidden states ,
outcomes. To better explain what hidden states are, we can consider prediction using Hidden Markov Model (HMM) and evaluating these
you and a friend are performing a coin tossing experiment where they predictions.
are in a closed room, and you are outside. The person in the room
tosses the coin and tells you what the outcome is without telling
exactly what he/she is doing (the process is hidden). Grewal, M.
Krzywinski, and N. Altman use a fair coin that is unstable with a
probability of 20% of quietly alternating between head/tail - 50% of
tails and biased after each toss with a probability of say 90% of tails
[10]. We can see that the coin seems to be biased as much as we
know it’s fair, but the issue is that we cannot say which. In summary,
such a coin is said to have two observations which are head and tail,
and two hidden states of fairness and bias.
Fig 1. The interconnection between sub-sections
The figure above is a visual representation of the hidden state
according to Grewal, J. K., Krzywinski, M., and Altman, N [10] that A. Data collection
was randomly generated to explain the concept of hidden Markov
model. From the visualization, the biased hidden state(B) has a 0.9 This entails acquiring data that was utilized in the research, we
chance of emitting tails and 0.1 of emitting tails while the fair hidden accomplished it by following the sub-steps outlined below –
state (F) has 0.5 chance each of emitting heads or tails. The existence
of these hidden states has made it necessary that when trying to 1. Searching through online datasets from different websites
predict future probabilities, they should be considered as joint like [Link] and [Link] to find the bitcoin
probabilities of the values we observe and the hidden states that datasets that fit our research. Datasets are measured for data
generate these observations. The authors went further to let us know integrity using parameters such as size and the source's
that hidden states do not have any meaning but to detect invisible reputation. The size was measured from thousands of rows,
events. and datasets with a smaller number of rows were not
considered in our research.
A property of the hidden Markov model worth noting is that, given at
observed outcome Yt at time t, this observation is produced by a 2. Verifying Manually if the columns in the dataset contain all
corresponding hidden state St [11]. It is assumed that the hidden the possible information that will make our research a
states obey the Markov property which states that St only depends on success, at least 5 columns were our threshold.
the previous state St-1 regardless of all the other states that occurred
before time t-1 [9]. Also, the resulting output of the hidden Markov Finally, we selected a bitcoin dataset from [Link] of the
model also follows the same property as shown in the formula below. historical data of bitcoin exchanges recorded per day interval that fit
our research problem well. This dataset contains 1404 rows and 7
columns date, open, high, low, closing, adjusted closing and volume
for a particular day. Also, it has data records which start from
01/01/2018 to 04/11/2021high, low, closing, adjusted closing and
volume for a particular day.
[11][9]
B. Feature engineering
Predicting the price of a stock or bitcoins is difficult due to its
unpredictability. However, in this research we will use the hidden This process entails selecting crucial features that will be used to
Markov to anticipate bitcoin prices behaviours over time, thanks to predict bitcoin prices.
the existence of the hidden and invisible states. The fact that the We selected them based on how significant they are in determining
hidden states make it easy to detect bitcoin prices behaviors is bitcoin prices, despite its volatility. The best selected features within
highlighted by IA. Hashish et al. in [9]. the process are - Close, Open High and Low prices.
C. Selection of the optimal number of hidden states
This process involved choosing the optimal number of hidden states Manipulation of the difference in how the price changed over time
that will be used as parameters to our Hidden Markov Model. To from the latest day in the range Rt-j to the following day’s price after
complete the process, we relied on the Bayesian Information which it is summed up to the present day’s price to obtain the next
Criterion (BIC) because it reduces overfitting and the model day’s forecast.
complexity even with the increase in the number of states. Therefore,
it required finding the negative log-likelihood of the observed states
O.
We used a range of N states from which we computed the negative Observations are appended to the dataset after obtaining them and the
log-likelihood of the data for the BIC model using the formula HMM model is trained after each operation for more predictions.
below-
BIC=-2 ln(L)+p×ln(T)
V. Results and discussions
Where L is the likelihood parameter
T is the number of observed positions
P is the estimated parameter in BIC
𝑤ℎ𝑒𝑟𝑒, 𝑝=𝑁2+2𝑁−1 Prior to predicting the prices of Bitcoin, we obtained 12 as the
optimal number of hidden states, then used them as one the
D. HMM Prediction parameters in predicting the prices of bitcoin for the past 100
days. Thus, we computed the MAPE, then plotted the predictions
Our HMM model predicts next day’s price (Open, Close, High, Low) and the actual prices to compare the results.
based on three algorithms namely: Forward, Viterbi and Bau-Welch Graph 1, graph 2, graph 3, and graph 4 show the predicted and actual
algorithms. The input parameters to our HMM model are the state prices of bitcoin prices.
transition probabilities, mean of the multivariate Gaussian
distribution of observation of each state, Covariance matrix of the Graph 1 - Predicted closing price
distribution of observation of each state, and initial state probability
which were initiated. The state space was estimated to be [1,25].
The forward algorithm helps in determining the likelihood of
accessing a certain range Rt of n previous observations from the
present day given sequence of prices and the model. In our project,
this algorithm was used to compute the observed states by finding the
log-likelihood of a state for a certain day given the historical
sequence of prices from today for the HMM model. The likelihood
helps to find the optimal prices for the given range Rt in n days given
the observed states for the model.
The Viterbi algorithm came into play when we compared the results
from forward algorithm with the log-likelihood of the historical sub-
sequences of the prices of the same range size Rt by shifting the Graph 2: Predicted opening price
range of prices by just one day in the in the direction of historical
prices.
The Viterbi algorithm works to identify the best sequence of the
hidden state whereby in our project we made an analysis a sub-
sequence Rt-j whose log-likelihood is the nearest to that of the sub-
sequence Rt is done
Figure 3: Predicted High price
Based on the results obtained from attempting different hidden states,
our model selection criterion performed well in identifying the
optimal hidden states that will be utilized for prediction.
Moreover, after building our HMM with the best hidden states , we
compared the model to our set baseline model, the LSTM, and found
that our model outperformed the baseline model, as shown in the
graph and table below.
Figure 4: Predicted low price
Figure 5: Predicted Closing price
The performance metric that has been used to evaluate how best the
HMM predicted the actual prices is the Mean Absolute Percentage
Error(MAPE), which is computed as
Table 4. A comparison of HMM and LSTM performance on
predicting closing prices using MAPE
Table 2. Table of the computed MAPE between the actual and the MAPE
predicted prices of bitcoin using 12 hidden states. HMM 0.06554686
LSTM 0.09915812573897081
Close Open High Low
VI. Conclusion and Future work
0.06490343 0.06698296 0.05339988 0.05852871
Bitcoin prices have been proven to be extremely volatile and
speculative, with significant price fluctuations. In this paper, we
explored the optimal number of hidden states that can be used in a
From the obtained results, it can be inferred that the HMM that has Hidden Markov Model (HMM) to predict bitcoin prices. To achieve
been used in forecasting the prices of bitcoin is good, since the mean so, we investigated the performance of HMM numbers of states in the
absolute performance errors between the predicted and actual data are range of 1 to 25 using the BIC criterion. Among the other states, the
notably small. HMM with 12 hidden states proved to be the best model. As a result,
the developed model was utilized to predict bitcoin prices, and the
To make sure that the selected hidden states are optimal, we tested performance of the model was evaluated using the Mean Absolute
our model with other random hidden states, to see how the model will Error, MAPE, then we compared our developed model to a baseline
perform. model to see whether it was any good. As a result, we discovered that
our model outperformed the baseline model, indicating that our
Table 3. Table of the computed MAPE between the actual and the developed model worked admirably in predicting bitcoin values.
predicted prices of bitcoin using different hidden states.
As this research focused on using HMM for predicting the prices of
Hidden Close Open High Low Bitcoin. However, there is a problem with financial investors who
states stake their finances in bitcoin looking for a system that can predict
the future prices of Bitcoin with a higher degree of accuracy to make
20 0.06554686 0.07314548 0.0550142 0.06 better financial decisions. Several models including ours that are used
749 to predict time series of financial instruments cannot perform
873 adequately or keep up with the volatility of Bitcoin. From [3], it is
evident that other models do not provide accurate results with bitcoin
because some of those models ignore the statistics and econometrics
5 0.07575405 0.06547811 0.05042704 0.07
approach. So, even though our model forecasts future prices, it
035
cannot rely on for making business decisions hence further research
912
is required.
VII. Division of Work [Link]/dissertations-theses/bitcoin-price-forecast-
using-lstm-gru-recurrent/docview/2476124038/se-2?accountid=9902.
The division of work has been done as follows: [8] Nguyen, N. (2017). An analysis and implementation of the hidden
Markov model to technology stock prediction. Risks, 5(4), 62. Doi:
RICHARD KAGAME [Link]
[9] I. A. Hashish, F. Forni, G. Andreotti, T. Facchinetti and S.
1. Detailed explanation of the HMM model for bitcoin Darjani, "A Hybrid Model for Bitcoin Prices Prediction using Hidden
prediction in the methodology Markov Models and Optimized LSTM Networks," 2019 24th IEEE
2. Detailed explanation for BIC for selection of optimal states International Conference on Emerging Technologies and Factory
in the methodology Automation (ETFA), 2019, pp. 721-728, doi:
10.1109/ETFA.2019.8869094.
SAMIRATU Ntohsi [9] [Link]
[10]J. Grewal, M. Krzywinski and N. Altman, "Markov models —
1. Literature review on Hidden Markov Model hidden Markov models,” Nature Methods, vol. 16, no. 9, pp. 795-
2. Detailed explanation of Introduction, Theory of HMM’ 796, 2019. Available: 10.1038/s41592-019-0532-6.
Research question, aim and objectives [11] P. Giudici and I. Abu Hashish, "A hidden Markov model to
detect regime changes in crypto asset markets", Quality and
AIME MUNEZERO Reliability Engineering International, vol. 36, no. 6, pp. 2057-2065,
2020. Available: 10.1002/qre.2673.
1. Implementation of HMM for bitcoin prediction using hmm [12] A. Jain, P. Mehta and R. Teja Darbha, "Stock Forecasting using
library in Python and discuss results Hidden Markov Models", SRIBD, 2021. Available:
2. Implementation of LSTM in python and discuss results [Link]
[Accessed 7 December 2021].
DANIEL EMESIANI
1. Providing a detailed abstract for the report
2. Providing conclusion and future work
Github link: [Link]
Group-Project
REFERENCES
[1] M. Mudassir, S. Bennbaia, D. Unal, and M. Hammoudeh,
"Time-series forecasting of Bitcoin prices using high-dimensional
features: a machine learning approach", Neural Computing and
Applications, 2020. Available: 10.1007/s00521-020-05129-6
[Accessed 18 September 2021].
[2] N. Nguyen, "Hidden Markov Model for Stock Trading",
International Journal of Financial Studies, vol. 6, no. 2, p. 36, 2018.
Available: 10.3390/ijfs6020036.
[3] C. Koki, S. Leonardos and G. Piliouras, "Do Cryptocurrency
Prices Camouflage Latent Economic Effects? A Bayesian Hidden
Markov Approach", Proceedings, vol. 28, no. 1, p. 5, 2019.
[4] "Bitcoin Historical Data", [Link], 2021. [Online].
Available: [Link]
data.
[5] D. Venugopal, S. Kannan Kaliyaperumal and S. Muthu
Niraikulathan, "Stock Market Trend Prediction Using Hidden
Markov Model", Forecasting in Mathematics - Recent Advances,
New Perspectives, and Applications, 2021. Available:
10.5772/intechopen.93988
[6] K. Ramadani and D. Devianto, "The forecasting model of
Bitcoin price with fuzzy time series Markov chain and Chen logical
method", INTERNATIONAL CONFERENCE ON SCIENCE AND
APPLIED SCIENCE (ICSAS2020), 2020. Available:
10.1063/5.0032178.
[7] Y. Xu, "Bitcoin Price Forecast Using LSTM and GRU
Recurrent Networks, and Hidden Markov Model", ProQuest
Dissertations Publishing, 2020. Available: [Link]
View publication stats