Communications on Applied Nonlinear Analysis ISSN: 1074-133X Vol 31 No. 4s (2024) 1 https://internationalpubls.com Analysis and Prediction of Stock Price using HMM and Facebook’s Prophet Computational Models K. Senthamarai Kannan1, V. Parimyndhan2*, P. Kumar3 1Senior Professor, Department of Statistics, Manonmaniam Sundaranar University, Tirunelveli – 627 012. 2*Research Scholar, Department of Statistics, Manonmaniam Sundaranar University, Tirunelveli – 627 012 3Associate Professor, Department of Information Technology and Engineering, Manonmaniam Sundaranar University, Tirunelveli – 627 012 Article History: Received: 18-04-2024 Revised: 02-06-2024 Accepted: 20-06-2024 Abstract In recent times, the utilization of Statistical and Machine Learning techniques has gained prominence in the realm of financial data analysis. These methods are applied to various types of financial data, encompassing textual information, numerical data, and graphical representations. This study aims to compare the performance of two prominent forecasting methods, Hidden Markov Models and Facebook’s Prophet in the context of stock price prediction. Assessing the predictive accuracy, interpretability, and adaptability of both approaches through empirical experiments and case studies sheds light on their respective advantages and limitations. These experiments demonstrate that the predicted stock prices are in closer proximity to the actual price when compared to using a single data source. Furthermore, the achieved MAPE are 0.01, 0.025 and respectively, outperforming conventional methodologies. Our validation of effectiveness extends to real-world datasets encompassing the NIFTY50 Index. These findings offer valuable insights for researchers and practitioners seeking effective strategies for stock price prediction. Keywords: Stocks, HMM, Facebook’s Prophet, Machine Learning, Forecasting. 1. Introduction Stock price prediction has been a longstanding challenge in the field of finance, attracting the attention of researchers, investors, and analysts alike. Accurate forecasting of stock prices is a complex task due to the intricate interplay of factors, including market sentiment, economic indicators, geopolitical events, and company-specific developments. In recent times, the emergence of advanced machine learning methods has created fresh opportunities for tackling this problem, offering the potential to uncover hidden patterns and relationships within historical stock price data. Among the myriad forecasting methods available, this study focuses on comparing two prominent approaches: Facebook Prophet and Hidden Markov Models (HMMs). These methods offer distinct methodologies and capabilities for capturing the temporal dynamics of stock prices, each with its unique set of strengths and weaknesses. Facebook Prophet, introduced by Taylor and Letham in 2017, presents an intuitive and user- friendly approach to time series forecasting. Prophet is designed to handle a variety of time series data, including those with seasonality, holidays, and abrupt changes in trends. In contrast, HMM provides a probabilistic framework for modelling sequences of observations. Originally developed in the context of speech recognition, HMMs have found application in a wide range of domains, Communications on Applied Nonlinear Analysis ISSN: 1074-133X Vol 31 No. 4s (2024) 2 https://internationalpubls.com including finance. HMM are particularly adept at capturing hidden states or regimes that govern observed data [4]. The objective of this research is to evaluate the effectiveness of a Hidden Markov Model and a Prophet in capturing stock movements through training on historical data, ultimately leading to their capacity to forecast stock prices. The selection of these two models is based on their distinct underlying principles. The Hidden Markov Model primarily relies on statistical analysis and probability distributions for optimization, while a Prophet is a forecasting tool designed for time series data. This study aims to ascertain which of these two models demonstrates superior performance in analyzing stock data. By comparing the results of the two experiments, the finding is which model is most suitable for the stock prices. The models will be evaluated by MAPE. In this study, prices of NIFTY50 were utilized. The remainder of this study is organized as follows: In Section II, a review of some existing techniques for stock market prediction is conducted, with a particular focus on the thought of HMM and Facebook's Prophet [5]. In Section III, the data is described, and the approach is elaborated upon, providing detailed mathematical explanations. The presentation of the experimental results is encompassed in Section IV. Finally, Section V presents conclusions and future work. 2. Literature Review In recent times, stock price forecasting has witnessed significant advancements, with associated technologies reaching greater levels of maturity. For example, Bustos and Pomares (2020) conducted a comprehensive examination of prediction methods employed in the financial market [2]. Their systematic review incorporated 52 studies that were published between 2014 and 2018. This particular review delved into various categories of techniques, including machine learning, statistical approaches, deep learning, text mining, and ensemble methods. Su, Z., & Yi, B. (2022) discuss that owing to theoretical advancements in the financial domain, the mathematical models employed to characterize stocks have become more diverse. Currently, research in share price forecasting can be categorized into two main streams: statistics-driven and non-statistical approaches [7]. Wu et al. (2022) proposed a stock price predicting method integrating multiple data sources and investor sentiment [9]. Tanvir et al. (2023) employed the Maximum a Posteriori HMM technique to forecast share prices for the subsequent day by leveraging historical data [8]. Palupi et al. (2021) applied the HMM concept to discern trends in the financial market. They focused on using only the observed state, which is derived from the stock price data [5]. Ersoz et al. (2021) conducted a study comparing the accuracy of three different forecasting algorithms, namely Prophet, ARIMA and Holt Winters for predicting COVID-19 disease epidemiology in Europe. Their findings indicate that the Holt-Winters exponential smoothing method, with an RMSE of 0.2080 and MAE of 0.1747, outperforms the ARIMA and Prophet forecasting approaches [3]. Kumar et al. (2021) proposed a novel approach to utilize data collected from supermarkets for the creation of an FB Prophet tool designed to predict food sales. In their study, they examined various forecasting models, including the additive model, the ARIMA model, and the FB Prophet model. Their research findings indicate that, in terms of accuracy, superior predictions, and improved Communications on Applied Nonlinear Analysis ISSN: 1074-133X Vol 31 No. 4s (2024) 3 https://internationalpubls.com fitting, the FB Prophet model stands out as the more effective prediction model when compared to the others [4]. Riady, S. R. (2023) used the ARIMA algorithm and Facebook's Prophet to forecast COVID-19 spread in Indonesia. Prophet generally outperformed ARIMA, although its accuracy declined further into the future it forecasted [6]. 3. Materials and Methods This segment will provide information about the dataset utilized, Additionally, this section presents a detailed description of the two forecasting methods under consideration, highlighting their underlying principles, key features, and implementation processes. It outlines how Facebook Prophet captures seasonality, trends, and holidays, and contrasts it with the ability of HMMs to model hidden states and complex dependencies in stock price data. The evaluation metric employed to assess the effectiveness of each model's performance will be expounded upon. 3.1. Data Description The dataset used in this study consists of daily close prices of the stock index from 1 January 2018 to 23 July 2023, a total of 1222 observations. Stocks from the selection included NIFTY50. The features utilized were closing price spanning the past 1222 working days during market hours. During the testing phase, the latest 100 observations were reserved, with the rest of the data being utilized for model training. 3.2. Methods 3.2.1. Hidden Markov Model An HMM is a statistical model comprising the following components: N = the number of Hidden States T = the number of time steps/observations in an Observation Sequence Π = Initial State Probabilities for each N A = the Transition Matrix, with size N × N B = the Emission Matrix, with size N × z, where z is the number of Markets used in the model. Figure 1 illustrates the Procedure of Creating a Sequence Using a Hidden Markov Model with Continuous Observations, Thus Involving Probability Distributions. Communications on Applied Nonlinear Analysis ISSN: 1074-133X Vol 31 No. 4s (2024) 4 https://internationalpubls.com An HMM operates on sequences of observations, each comprising T time steps. At each time step, there corresponds to a specific state. The transitions between these states are governed by a transition matrix, dictating the likelihood of transitioning from one state to another in the subsequent time step. Each state has a probability of generating potential observations. These observations can take on discrete or continuous values. In this research, continuous observations will be employed, aligning with the nature of the stock data. To establish probabilities for continuous observations, probability distributions are essential. In Figure 1, an illustration demonstrates how a Hidden Markov Model generates or characterizes an observation sequence. At a specific time, t, a particular state produces a distribution, from which a sample can be derived. Subsequently, the model transitions to the subsequent state and iterates through this procedure. Additionally, four algorithms are imperative in the utilization of a Hidden Markov Model: 1. The forward algorithm. This algorithm determines the likelihood of an observation sequence. This algorithm starts at t = 1. 2. The backward algorithm. This algorithm has the same functionality as the forward algorithm, but starts at t = T, where T is the latest time step of the observation sequence, and then moves backward in time. 3. The Viterbi algorithm. This algorithm finds the state sequence that would most likely have emitted the given observation sequence. 4. The Baum-Welch algorithm. This algorithm maximizes the likelihood of the model, given a set of observation sequences. These algorithms enable the training of a Hidden Markov Model using a set of observations. They also empower the model to calculate the likelihood of a given observation sequence and identify the state sequence that is most probable to have produced the current observation sequence. 3.2.2. HMM for Stock Price Forecasting In this study, an observation sequence takes the form of z sets of parallel vectors. Each set encompasses T historical daily closing prices for z distinct markets. Instead of relying on a single normal distribution, as depicted in Figure 1, this research employs Gaussian Mixtures. This choice is motivated by the absence of a priori grounds to assume that stock prices within each state follow a unimodal Gaussian distribution. The adoption of the GMM approach enables the modeling of distributions with multiple peaks. In the Hidden Markov Model used in this study, each state incorporates z Gaussian Mixture Models to characterize the emission probabilities. Here, z denotes the total number of markets utilized. Training: The training process of the Hidden Markov Model employs a sliding window technique. This window, with a length of 100, traverses through the data in steps of t = 1, generating a sequence of observations. Each observation is labelled with the value of the stock at T+1. Consequently, the initial observation encompasses values for 1 <= t <= 100, with the label being the value at t = 101. The subsequent observation covers values for 2 <= t <= 201, with its label being the value at t = 102, and this pattern continues for the entirety of the dataset. Subsequently, the model undergoes training based on the first observation utilizing the Baum-Welch algorithm for 100 epochs, culminating in a prediction for T+1. Following this, the next observation is introduced to the Communications on Applied Nonlinear Analysis ISSN: 1074-133X Vol 31 No. 4s (2024) 5 https://internationalpubls.com model and undergoes further training. Another prediction is then made for this observation, and this process is iterated for all observations. Predicting: There exist various approaches for employing a Hidden Markov Model in stock prediction. In the literature introduced in the opening section, one technique involves providing a single observation and identifying a past observation that, according to the trained Hidden Markov Model, yields a similar likelihood. Subsequently, in this prior observation, the difference between T and T+1 is computed and added to the present value at t, resulting in the prediction. An alternative approach involves taking an observation sequence. Initially, the most probable state sequence is computed, along with the subsequent state that is projected to be reached, using the transition matrix derived from the trained HMM. Subsequently, the Gaussian Mixture of this state is employed to derive the mean, which is then utilized as the prediction. Nonetheless, it's worth noting that in the presence of a Gaussian Mixture, this mean may not always represent the most probable value within this distribution. In situations involving a mixture of Gaussians, a more logical approach would be to randomly choose one of the Gaussians within the mixture. The mean of this selected Gaussian can then be provided as the prediction. In this study, to facilitate prediction, the observation sequence will be inputted into the Viterbi algorithm. This algorithm will provide the most probable sequence of states that could have generated the given observation sequence. Specifically, only the final state, st, of this sequence is retained, representing the state at the present time, t. Utilizing the transition matrix A from the trained model, the subsequent state, st+1 for t+1, will be selected in such a way that A [st; st+1] holds the highest probability in that row. This state encompasses a Gaussian Mixture Model. For prediction purposes, the probability density function (PDF) transforms a cumulative distribution function (CDF). Subsequently, a random value, denoted as i along the y-axis, is generated. The x-coordinate of the point of intersection between the line y = i and the CDF serves as the anticipated value. 3.2.3. Facebook ‘s Prophet A prophet is an algorithm used for predicting time-series data. Prophet separates a time series into three components, seasonal, trend, and holidays, as follows: 𝑦(𝑡) = 𝑔(𝑡) + 𝑠(𝑡) + ℎ(𝑡) + 𝜖𝑡 Where, g(t) represents the trend function capturing non-seasonal variations, s(t) represents the function that accounts for seasonal changes, and h(t) denotes the function related to holidays. 𝜖𝑡 is a function that captures any additional changes that don't conform to the three primary functions. 𝑔(𝑡) incorporates both saturating growth and piecewise linear models. Specifically, g(t) defines the logistic growth model in the following manner: 𝑔(𝑡) = 𝑐 1 + exp (−(𝑘(𝑡 − (𝑚)) Communications on Applied Nonlinear Analysis ISSN: 1074-133X Vol 31 No. 4s (2024) 6 https://internationalpubls.com Where 𝑐 represents the maximum capacity, 𝑘 represents the rate of growth, and 𝑚 is a parameter that specifies an initial condition. 𝑔(𝑡) then incorporates updates to the growth model by indicating specific times 𝑠𝑗 where the growth rate can change at time 𝑡. If we assume there are "S" such change points at times 𝑠𝑗 denoted by 𝑗 = 1,2, … , 𝑠, we can define a vector of rate adjustments as "δ ∈ RS," where each "δj" represents the change in the growth rate occurring at the time 𝑠𝑗 . The growth rate at any given time "t" is determined as follows: 𝑦 = 𝑘 + 𝑎(𝑡)𝑇𝛿 Here, a(t)ᵀδ represents the cumulated growth up to the changepoints 𝑠𝑗 and 𝑎(𝑡) ∈ {0,1}𝑆 is a vector and it can be calculated as follows: 𝑎𝑗(𝑡) = { 1, 𝑖𝑓 𝑡 ≥ 𝑠𝑗 0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 4. Results and Discussion This research aimed to assess the effectiveness of HMM and Facebook’s Prophet in predicting stock prices. The open-source Python library hmmlearn and prophet was utilized to train the model and compute observation likelihood. 4.1. HMM Prediction Predictions were made for the last 100 days, commencing from the 100th day. The model was iteratively fine-tuned using true observations for preceding days in a sequential manner. Each iteration expanded the training dataset by one sample. Initially, a fixed model with four states was implemented. Figure 2 illustrates the HMM predictions for NIFTY50 stock prices using four states. The MAPE was later assessed to visually compare the predicted and actual prices. To enhance the model, optimization was carried out by selecting the configuration with the lowest BIC value, which takes into account the number of states. Figure 2: NIFTY50 Close Price Predictions made by the HMM versus the Actual data Figure 2 illustrates the comparison between real stock prices and predicted prices generated by our trained model for NIFTY50. The used methodology is versatile and can be applied to create prediction models for any company, as long as the HMM is trained on that specific company's stock Communications on Applied Nonlinear Analysis ISSN: 1074-133X Vol 31 No. 4s (2024) 7 https://internationalpubls.com dataset. Therefore, the potential to employ a similar strategy to develop distinct prediction models for each stock. 4.2. Prophet Prediction Figure 3: Nifty50 Close Price Plots using Prophet for Forecasting Figure 3 illustrates the NIFTY50 chart generated by the Prophet Model. In this figure, the actual prices are denoted by black dots, while the predicted price values are represented by the blue line. An intriguing aspect is the inclusion of upper and lower bounds for the projected prices. This chart demonstrates that, for the majority of the time, the predicted values closely align with the actual prices and there are inconsistencies like predicting minor price increases right after a drop. 4.3. Performance Evaluation Table 1: Performance Metrics Measurement for NIFTY50’s Close Price Model HMM Facebook’s Prophet ARIMA [1] MAPE 0.01 0.025 0.745 RMSE 0.31 0.33 0.45 The MAPE and RMSE is a metric commonly used to evaluate the accuracy of forecasting models. HMM has the lowest MAPE value of 0.01, indicating that it has the better predictive accuracy among the three models. Facebook's Prophet has a MAPE value of 0.025, which is higher than HMM but still quite low. Prophet is known for its ability to handle various trends, seasonality, and holidays in time series data. ARIMA has the highest MAPE value of 0.745, suggesting that it might not perform as well in terms of accuracy compared to the other two models in the context of the dataset you're evaluating. Similar to MAPE, the HMM model also demonstrates the lowest RMSE of 0.31, indicating the smallest overall error in its predictions. Prophet follows with an RMSE of 0.33, indicating slightly higher error compared to the HMM model. ARIMA, once again, exhibits the highest RMSE of 0.45, implying that its forecasts have the largest root mean square deviation from the actual data. In general, a lower MAPE and RMSE value indicates better forecasting accuracy, so based on the provided values, it seems that the HMM model is the most accurate, followed by Facebook's Prophet, and then ARIMA. Communications on Applied Nonlinear Analysis ISSN: 1074-133X Vol 31 No. 4s (2024) 8 https://internationalpubls.com 5. Conclusion Statistical and machine learning methods have been commonly employed to predict the future values of the NIFTY50 Index. For prediction, HMM and Prophet models were used. Both the HMM and Prophet models have very similar performance with the Prophet model having slightly higher MAPE value. MAPE value for HMM is 0.01 and Prophet is 0.025. The constraints observed during the application of the forecasting methods explored in this study highlight the need to assess additional models that can combine the strengths of each approach. Additionally, it would be pertinent to apply the attention mechanism, one of the most promising recent developments in machine learning, to the forecasting task, was investigated. Exploring this specific area is certainly appealing and has recently gained attention in this field. The further work involved the utilization of hybrid Machine Learning techniques for stock forecasting. Reference [1] Ashik, A. M., & Kannan, K. S. (2019). Time series model for stock price forecasting in India. Logistics, Supply Chain and Financial Predictive Analytics: Theory and Practices, 221-231. [2] Bustos, O., & Pomares-Quimbaya, A. (2020). Stock market movement forecast: A systematic review. Expert Systems with Applications, 156, 113464. [3] ERSÖZ, N. Ş., GÜNER, P., AKBAŞ, A., & BAKİR-GUNGOR, B. (2022). COMPARATIVE PERFORMANCE ANALYSIS OF ARIMA, PROPHET AND HOLT-WINTERS FORECASTING METHODS ON EUROPEAN COVID-19 DATA. International Journal of 3D Printing Technologies and Digital Industry, 6(3), 556-565. [4] Jha, B. K., & Pande, S. (2021, April). Time series forecasting model for supermarket sales using FB-prophet. In 2021 5th International Conference on Computing Methodologies and Communication (ICCMC) (pp. 547-554). IEEE. [5] Palupi, I., Wahyudi, B. A., & Putra, A. P. (2021, August). Implementation of hidden markov model (HMM) to predict financial market regime. In 2021 9th International Conference on Information and Communication Technology (ICoICT) (pp. 639-644). IEEE. [6] Riady, S. R. (2023). Stock Price Prediction using Prophet Facebook Algorithm for BBCA and TLKM. International Journal of Advances in Data and Information Systems, 4(1), 1-8. [7] Su, Z., & Yi, B. (2022). Research on HMM-Based Efficient Stock Price Prediction. Mobile Information Systems, 2022. [8] Tanvir, R., Shawon, M. T. R., & Alam, M. G. R. (2023). DSE Stock Price Prediction using Hidden Markov Model. arXiv preprint arXiv:2302.08911. [9] Wu, S., Liu, Y., Zou, Z., & Weng, T. H. (2022). S_I_LSTM: stock price prediction based on multiple data sources and sentiment analysis. Connection Science, 34(1), 44-62.