Skip to main content
Log in

hhsmm: an R package for hidden hybrid Markov/semi-Markov models

  • Original paper
  • Published:
Computational Statistics Aims and scope Submit manuscript

Abstract

This paper introduces the hhsmm R package, which involves functions for initializing, fitting, and predication of hidden hybrid Markov/semi-Markov models. These models are flexible models with both Markovian and semi-Markovian states, which are applied to situations where the model involves absorbing or macro-states. The left-to-right models and the models with series/parallel networks of states are two models with Markovian and semi-Markovian states. The hhsmm also includes Markov/semi-Markov switching regression model as well as the auto-regressive HHSMM, the nonparametric estimation of the emission distribution using penalized B-splines, prediction of future states and the residual useful lifetime estimation in the predict function. The commercial modular aero-propulsion system simulation (C-MAPSS) data-set is also included in the package, which is used for illustration of the application of the package features. The application of the hhsmm package to the analysis and prediction of the Spain’s energy demand is also presented.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Fig. 1
Fig. 2
Fig. 3
Fig. 4
Fig. 5
Fig. 6
Fig. 7
Fig. 8
Fig. 9
Fig. 10
Fig. 11
Fig. 12
Fig. 13
Fig. 14
Fig. 15
Fig. 16
Fig. 17
Fig. 18

Similar content being viewed by others

References

  • Adam T, Langrock R, Weiß CH (2019) Penalized estimation of flexible hidden Markov models for time series of counts. Metron 77(2):87–104

    Article  MathSciNet  MATH  Google Scholar 

  • Azimi M (2004) Data transmission schemes for a new generation of interactive digital television (Doctoral dissertation, University of British Columbia)

  • Bulla J, Bulla I, Nenadic O (2010) hsmm an R package for analyzing hidden semi-Markov models. Comput Stat Data Anal 54(3):611–619

    Article  MathSciNet  MATH  Google Scholar 

  • Cartella F, Lemeire J, Dimiccoli L, Sahli H (2015) Hidden semi-Markov models for predictive maintenance. Math Probl Eng

  • Cook AE, Russell MJ (1986) Improved duration modeling in hidden Markov models using series-parallel configurations of states. Proc Inst Acoust 8:299–306

    Google Scholar 

  • Dempster AP, Laird NM, Rubin DB (1977) Maximum likelihood from incomplete data via the EM algorithm. J R Stat Soc Ser B (Methodol) 39(1):1–22

    MathSciNet  MATH  Google Scholar 

  • Durbin R, Eddy SR, Krogh A, Mitchison G (1998) Biological sequence analysis: probabilistic models of proteins and nucleic acids. Cambridge University Press, Cambridge

    Book  MATH  Google Scholar 

  • Fontdecaba S, Muñoz MP, Sànchez JA (2009) Estimating Markovian Switching Regression Models in An application to model energy price in Spain. In The R User Conference, France

  • Guédon Yann (2005) Hidden hybrid Markov/semi-Markov chains. Comput Stat Data Anal 49(3):663–688

    Article  MathSciNet  MATH  Google Scholar 

  • Harte D (2006) Mathematical background notes for package “HiddenMarkov”. Statistics Re.

  • Jackson C (2007) Multi-state modelling with R: the msm package. Cambridge, UK, pp 1–53

  • Kim CJ, Piger J, Startz R (2008) Estimation of Markov regime-switching regression models with endogenous switching. J Econom 143(2):263–273

    Article  MathSciNet  MATH  Google Scholar 

  • Langrock R, Kneib T, Sohn A, DeRuiter SL (2015) Nonparametric inference in hidden Markov models using P-splines. Biometrics 71(2):520–528

    Article  MathSciNet  MATH  Google Scholar 

  • Langrock R, Adam T, Leos-Barajas V, Mews S, Miller DL, Papastamatiou YP (2018) Spline-based nonparametric inference in general state-switching models. Statistica Neerlandica 72(3):179–200

    Article  MathSciNet  Google Scholar 

  • Li J, Li X, He D (2019) A directed acyclic graph network combined with CNN and LSTM for remaining useful life prediction. IEEE Access 7:75464–75475

    Article  Google Scholar 

  • Lloyd S (1982) Least squares quantization in PCM. IEEE Trans Inf Theory 28(2):129–137

    Article  MathSciNet  MATH  Google Scholar 

  • ÓConnell J, Højsgaard S (2011) Hidden semi Markov models for multiple observation sequences: the mhsmm package for R. J Stat Softw 39(4):1–22

    Google Scholar 

  • R Development Core Team (2010) R: a Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria

  • Schellhase C, Kauermann G (2012) Density estimation and comparison with a penalized mixture approach. Comput Stat 27:757–777

    Article  MathSciNet  MATH  Google Scholar 

  • Van Buuren S, Groothuis-Oudshoorn K (2011) mice: multivariate imputation by chained equations in R. J Stat Softw 45:1–67

    Article  Google Scholar 

  • Visser I, Speekenbrink M (2010) depmixS4: an R package for hidden Markov models. J Stat Softw 36(7):1–21

    Article  Google Scholar 

  • Viterbi A (1967) Error bounds for convolutional codes and an asymptotically optimum decoding algorithm. IEEE Trans Inf Theory 13(2):260–269

    Article  MATH  Google Scholar 

Download references

Acknowledgements

The authors would like to thank the two anonymous referees and the associate editor for their useful comments and suggestions, which improved an earlier version of the hhsmm package and this paper.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Morteza Amini.

Additional information

Publisher's Note

Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

Appendix

Appendix

1.1 Forward-backward algorithm for the HHSMM model

Denote the sequence \(\{Y_{t},\ldots ,Y_{s}\}\) by \(Y_{t:s}\) and suppose that the observation sequence \(X_{0:\tau -1}\) with the corresponding hidden state sequence \(S_{0:\tau -1}\) is observed. The forward-backward algorithm is an algorithm to compute the probabilities

$$\begin{aligned}L_j(t)=P(S_t=j|X_{0:{\tau -1}}=x_{0:{\tau -1}})\end{aligned}$$

within the E-step of the EM algorithm. The above probabilities are computed in the backward recursion of the forward-backward algorithm.

For a semi-Markovian state j, and \(t=0,\ldots ,\tau -2\), the forward recursion computes the following probabilities (Guédon 2005),

$$\begin{aligned} F_j(t)= & P(S_{t+1}\ne j,S_t = j|X_{0:t} = x_{0:t})\nonumber \\= & \frac{f_j(x_t)}{N_t}\Biggl [\sum _{{u=1}}^{t}\Bigg \{\prod _{\nu =1}^{u-1}\frac{f_j(x_{t-\nu })}{N_{t-\nu }}\Bigg \}d_j(u)\sum _{i\ne j}p_{ij}F_i(t-u)\nonumber \\ &\qquad+\Bigg \{\prod _{\nu =1}^{t}\frac{f_j(x_{t-\nu })}{N_{t-\nu }}\Bigg \}d_j(t+1)\pi _j\Biggl ], \end{aligned}$$
(A.21)

and

$$\begin{aligned} F_j(\tau -1)= & P(S_{\tau -1} = j|X_{0:{\tau -1}} =x_{0:{\tau -1}})\nonumber \\= & \frac{f_j(x_{\tau -1})}{N_{\tau -1}}\Biggl [\sum _{u=1}^{\tau -1}\Bigg \{\prod _{\nu =1}^{u-1}\frac{f_j(x_{\tau -1-\nu })}{N_{\tau -1-\nu }}\Bigg \}D_j(u)\sum _{i\ne j}p_{ij}F_i(\tau -1-u)\nonumber \\ &\qquad+\Bigg \{\prod _{\nu =1}^{\tau -1}\frac{f_j(x_{\tau -1-\nu })}{N_{\tau -1-\nu }}\Bigg \}D_j(\tau )\pi _j\Biggl ]. \end{aligned}$$
(A.22)

where the normalizing factor \(N_t\) is computed as follows

$$\begin{aligned} N_t = P(X_t = x_t|X_{0:{t-1}} = x_{0:{t-1}}) =\sum _{j}P(S_t = j,X_t = x_t|X_{0:{t-1}} = x_{0:{t-1}}) \end{aligned}$$

For a Markovian state j, and for \(t = 0\),

$$\begin{aligned} {\tilde{F}}_j(0) = P(S_0 = j|X_0 = x_0)=\frac{f_j(x_0)}{N_0}\pi _j\end{aligned}$$

and for \(t = 1,\ldots ,\tau -1\)

$$\begin{aligned} {\tilde{F}}_j(t) = P(S_t = j|X_{0:t} = x_{0:t}) =\frac{f_j(x_t)}{N_t}\sum _{i}{\tilde{p}}_{ij}{\tilde{F}}_i(t-1) \end{aligned}$$
(A.23)

The log-likelihood of the model is then

$$\begin{aligned}\log P(X_{0:{\tau -1}} = x_{0:{\tau -1}};\theta ) = \sum _{t=0}^{\tau -1}\log N_t,\end{aligned}$$

which is used as a criteria for convergence of the EM algorithm and the evaluate the quality of the model. The backward recursion is initialized by

$$\begin{aligned}L_j(\tau -1) = P(S_{\tau -1} = j|X_{0:{\tau -1}} = x_{0:{\tau -1}}) = F_j(\tau -1) = {\tilde{F}}_j(\tau -1).\end{aligned}$$

For a semi-Markovian state j, we have

$$\begin{aligned}L_j(\tau -1) = P(S_{\tau -1} = j|X_{0:{\tau -1}} = x_{0:{\tau -1}}) = F_j(\tau -1), \end{aligned}$$

and for \(t = \tau -2,\ldots ,0\),

$$\begin{aligned} L_j(t)= & {} L1_j(t) + L_j(t+1) - G_j(t+1)\sum _{i\ne j}{p}_{ij}{\tilde{F}}_i(t), \end{aligned}$$
(A.24)

where

$$\begin{aligned} L1_j(t)= & {} \sum _{k\ne j}G_k(t+1)p_{jk}F_j(t), \end{aligned}$$
(A.25)

and

$$\begin{aligned} G_j(t+1)= & {} \sum _{u=1}^{\tau -1-t}G_j(t+1,u) \end{aligned}$$

with

$$\begin{aligned} G_j(t+1,u)=\left\{ \prod _{\nu =0}^{\tau -2-t}\frac{f_j(x_{\tau -1-\nu })}{N_{\tau -1-\nu }}\right\} D_j(\tau -1-t). \end{aligned}$$

For a Markovian state j and for \(t = \tau -2,\ldots ,0\)

$$\begin{aligned} L_j(t)= & {} L1_j(t) \end{aligned}$$
(A.26)

Furthermore, if a mixture of multivariate normal distributions with probability density function

$$\begin{aligned} f_j(x) = \sum _{k=1}^{K_j}\lambda _{kj} {{\mathcal {N}}}_p(x; \mu _{kj}, \Sigma _{kj}),\quad j=1,\ldots ,J, \end{aligned}$$
(A.27)

is considered as the emission distribution, then the following probabilities of the mixture components are computed in the E-step of the \((s+1)\)th iteration of the EM algorithm

$$\begin{aligned} \gamma _{kj}^{(s+1)}(t) = \frac{\lambda _{kj}^{(s)}{{\mathcal {N}}}_p(x_t; \mu _{kj}^{(s)}, \Sigma _{kj}^{(s)})}{f_j(x_t) }, \end{aligned}$$
(A.28)

where \(\lambda _{kj}^{(s)}\), \(\mu _{kj}^{(s)}\) and \(\Sigma _{kj}^{(s)}\) are the sth updates of the emission parameters in the M-step of the sth iteration of the EM algorithm.

1.2 The M-step of the EM algorithm

In the M-step of the EM algorithm, the initial probabilities are updated as follows

$$\begin{aligned}\pi _j^{(s+1)} = P(S_0 = j|X_{0:{\tau -1}} = x_{0:{\tau -1}};\theta ^{(s)}) = L_j(0), \end{aligned}$$

For a semi-Markovian state i, the transition probabilities are updated as follows

$$\begin{aligned} p_{ij}^{(s+1)}= & {} \frac{\sum _{t=0}^{\tau -2}G_j(t+1)p_{ij}^{(s)}F_i(t)}{\sum _{t=0}^{\tau -2}L1_i(t)} \end{aligned}$$
(A.29)

and for a Markovian state i

$$\begin{aligned} {\tilde{p}}_{ij}^{(s+1)}= & {} \frac{\sum _{t=0}^{\tau -2}{\tilde{G}}_j(t+1){\tilde{p}}_{ij}^{(s)}{\tilde{F}}_i(t)}{\sum _{t=0}^{\tau -2}L_i(t)} \end{aligned}$$
(A.30)

If we consider the mixture of multivariate normals, with the probability density function (A.27), as the emission distribution, then its parameters are updated as follows

$$\begin{aligned}&\lambda _{kj}^{(s+1)} = \frac{\sum _{t=0}^{\tau -1}\gamma _{kj}^{(s)}(t) L_j^{(s)}(t)}{\sum _{m=1}^{K_j}\sum _{t=0}^{\tau -1}\gamma _{mj}^{(s)}(t) L_j^{(s)}(t)} = \frac{\sum _{t=0}^{\tau -1}\gamma _{kj}^{(s)}(t) L_j^{(s)}(t)}{\sum _{t=0}^{\tau -1}L_j^{(s)}(t)},\\&\mu _{kj}^{(s+1)} = \frac{\sum _{t=0}^{\tau -1}\gamma _{kj}^{(s)}(t) L_j^{(s)}(t)x_t}{\sum _{t=0}^{\tau -1}\gamma _{kj}^{(s)}(t) L_j^{(s)}(t)},\\&\Sigma _{kj}^{(s+1)} = \frac{\sum _{t=0}^{\tau -1}\gamma _{kj}^{(s)}(t) L_j^{(s)}(t)(x_t-\mu _{kj}^{(s+1)})(x_t-\mu _{kj}^{(s+1)})^T}{\sum _{t=0}^{\tau -1}\gamma _{kj}^{(s)}(t) L_j^{(s)}(t)} \end{aligned}$$

Also, the parameters of the sojourn time distribution are updated by maximization of the following quasi-log-likelihood function

$$\begin{aligned}{\tilde{Q}}_d(\{d_j(u)\}|\theta ^{(s)}) = \sum _{u=1}^{M_j}{\tilde{\eta }}_{j,u}^{(s)}\log d_j(u), \end{aligned}$$

where

$$\begin{aligned}&{\tilde{\eta }}_{j,u}=\sum _{t=0}^{\tau -2} G_j(t+1,u)\sum _{i\ne j} p_{ij} F_i(t) + A(u) d_j(u)\pi _j \prod _{v=1}^u \frac{f_j(x_{u_{\tau } - v)}}{N_{u_{\tau }-v}},\\&A(u) = \left\{ \begin{array}{lr}\frac{L_{1j}(u-1)}{F_j(u-1)},&{} u\le \tau -1\\ 1,&{} u>\tau -1,\end{array}\right. \end{aligned}$$

and \(u_{\tau } = \min (u,\tau )\).

1.3 Viterbi algorithm and smoothing for the HHSMM model

The Viterbi algorithm (Viterbi 1967) is an algorithm to obtain the most likely state sequence, given the observations and the estimated parameters of the model.

For a semi-Markovian state j, and for \(t=0,\ldots ,\tau -2\), the probability of the most probable state sequence is obtained by the Viterbi recursion as follows

$$\begin{aligned} \alpha _j(t)= & {} \max _{s_0,\ldots ,s_{\tau -1}}P(S_{t+1}\ne j,S_t=j,S_{0:{t-1}}=s_{0:{t-1}},X_{0:t}=x_{0:t})\nonumber \\= & {} f_j(x_t)\max \Bigg [\max _{1\le u\le t}\Bigg [\Bigg \{\prod _{\nu =1}^{u-1}f_j(x_{t-\nu })\Bigg \}d_j(u)\max _{i\ne j}\{p_{ij}\alpha _i(t-u)\}\Bigg ],\nonumber \\&\times \Bigg \{\prod _{\nu =1}^{t}f_j(x_{t-\nu })\Bigg \}d_j(t+1)\pi _j\Bigg ], \end{aligned}$$
(A.31)

and

$$\begin{aligned} \alpha _j(\tau -1)= & {} \max _{s_0,\ldots ,s_{\tau -2}} P(S_{\tau -1} =j,S_{0:{\tau -2}} =s_{0:{\tau -2}},X_{0:{\tau -1}} =x_{0:{\tau -1}})\nonumber \\= & {} f_j(x_{\tau -1})\max \Bigg [\max _{1\le u\le \tau -1}\Bigg [\Bigg \{\prod _{\nu =1}^{u-1}f_j(x_{\tau -1-\nu })\Bigg \}D_j(u)\max _{i\ne j}\{p_{ij}\alpha _i(\tau -1-u)\}\Bigg ],\nonumber \\&\times \Bigg \{\prod _{\nu }^{\tau -1}f_j(x_{\tau -1-\nu })\Bigg \}D_j(\tau )\pi _j\Bigg ], \end{aligned}$$
(A.32)

For a Markovian state j, the Viterbi recursion is initialized by

$$\begin{aligned}{\tilde{\alpha }}_j(0) = P(S_0 = j,X_0 = x_0) = f_j(x_0)\pi _j\end{aligned}$$

and for \(t = 1,\ldots .\tau -1\),

$$\begin{aligned} {\tilde{\alpha }}_j(t)= & \max _{s_0,\ldots ,s_{t-1}}P(S_t = j,S_0^{t-1} = s_0^{t-1},X_{0:t} = x_{0:t})\end{aligned}$$
(A.33)
$$\begin{aligned}= & f_j(x_t)\max _i\{{\tilde{p}}_{ij}{\tilde{\alpha }}_i(t-1)\}. \end{aligned}$$
(A.34)

After obtaining the probability of the most probable state sequence, the current most likely state is obtained as \({\hat{s}}_t^* =\arg \max _{1\le j\le J} \alpha _j(t)\).

Another approach for obtaining the state sequence is the smoothing method, which uses the backward probabilities \(L_j(t)\) instead of \(\alpha _j(t)\).

Rights and permissions

Reprints and permissions

About this article

Check for updates. Verify currency and authenticity via CrossMark

Cite this article

Amini, M., Bayat, A. & Salehian, R. hhsmm: an R package for hidden hybrid Markov/semi-Markov models. Comput Stat 38, 1283–1335 (2023). https://doi.org/10.1007/s00180-022-01248-x

Download citation

  • Received:

  • Accepted:

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s00180-022-01248-x

Keywords

Navigation