libhmm : A Modern C++20 Library for Hidden Markov Models with Correct MLE Emission M-Steps

Gary Wolfman, P.Eng · 2026

Read libhmm : A Modern C++20 Library for Hidden Markov Models with Correct MLE Emission M-Steps by Gary Wolfman, P.Eng (2026) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2605-29208.txt.

Introduction

Gary Wolfman describes libhmm, a C++20 library for hidden Markov model estimation. It fills two gaps: no zero-dependency C++ HMM library, and moment approximations in the Baum-Welch emission M-step. The library gives correct maximum likelihood estimators for sixteen scalar emissions. These include ECME for Student-t, Newton-Raphson for four more, and von Mises for circular data. Forward-backward and Viterbi calculations run in log-space, with SIMD acceleration. Version 4 adds multivariate observations with three emission families, plus Python bindings, and benchmarks on seven datasets.

Catalog record

CREATORGary Wolfman, P.Eng
YEAR2026
COLLECTIONPAPERS
FORMATTXT
FILE SIZE37166
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2605.29208

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

libhmm : A Modern C++20 Library for Hidden Markov Models with Correct MLE Emission M-Steps Gary Wolfman, P.Eng Abstract We describe libhmm , a C++20 library for Hidden Markov Model parameter estimation, sequence decoding, and model selection. libhmm addresses two gaps in existing software: the absence of a well-maintained, zero-dependency C++ HMM library suitable for embedding in production systems, and the widespread use of method-of-moments (MOM) approximations in the emission distribution M-step of the Baum-Welch algorithm. The library implements correct maximum likelihood estimators for sixteen continuous and discrete emission distributions, including an ECME algorithm for the location-scale Student- t t distribution, Newton-Raphson maximization for Gamma, Beta, Weibull, and Negative Binomial distributions, and the von Mises distribution for circular data. All forward-backward and Viterbi calculations operate in full log-space. SIMD acceleration is provided for AVX-512, AVX2, SSE2, and ARM NEON via compile-time dispatch with scalar fallback. Python bindings are available via the companion package pylibhmm . We compare libhmm against established C and C++ HMM libraries and against published R reference packages on five real-data benchmarks, and discuss the architectural tradeoffs made in the design. 1 Introduction Hidden Markov Models ( 2 ; 13 ) are a foundational tool for sequential data analysis. A system transitions among a finite set of latent states, emitting observations drawn from a state-dependent probability distribution. The Baum-Welch algorithm ( 2 ; 3 ) learns the model parameters by expectation-maximisation (EM); the Viterbi algorithm ( 15 ) recovers the most likely latent state sequence. HMMs are used in ecology ( 10 ; 11 ) , finance ( 12 ) , bioinformatics ( 4 ) , meteorology ( 18 ) , and signal processing ( 13 ) . Most HMM software targets interactive analysis in R or Python. The dominant packages — moveHMM / momentuHMM ( 10 ; 9 ) in ecology, fHMM ( 12 ) in finance, HiddenMarkov ( 6 ) in R, and hmmlearn in Python — require their respective language runtimes and cannot be embedded in production C++ systems without significant infrastructure overhead. Researchers integrating HMM inference into real-time pipelines, high-throughput parameter sweeps, or existing C++ codebases have no well-maintained, dependency-free option. A second gap concerns the emission M-step. Many packages use method-of-moments (MOM) approximations for distributions lacking closed-form MLE updates. For the Gamma, Beta, Weibull, and Negative Binomial distributions this is a computational convenience rather than a principled choice: Newton-Raphson converges to the exact MLE in two to four iterations per EM step. For the Student- t distribution the discrepancy is more serious: MOM kurtosis estimation can converge to a substantially different fixed point than the ECME algorithm ( 8 ) , particularly for states with near-Gaussian tails or sparse posterior support. libhmm was originally developed during the author’s graduate research on HMM-based characterisation of HTTP reverse tunnels, beginning as a C++ port of the JAHMM library ( 5 ) . The codebase has since been redesigned from first principles with no remaining code lineage to the original port. The need for an embeddable, performant, and correct C++ implementation motivated the design decisions described here. This paper makes the following contributions: 1. A description of libhmm ’s log-space inference algorithms and their numerical stability properties (Section 3 ). 2. Derivations of correct MLE M-steps for all sixteen supported emission distributions (Section 4 ). 3. An honest discussion of the architectural tradeoffs in the design relative to existing C and C++ HMM libraries (Sections 2 and 5 ). 4. Benchmarks against established R reference packages on five published datasets and against C/C++ libraries on synthetic data (Section 6 ). 2 Related Work and Design Motivation 2.1 R and Python ecosystem The R and Python HMM ecosystems are mature and domain-validated. moveHMM ( 10 ) and momentuHMM ( 9 ) provide Gamma and von Mises joint emission models for animal movement, using numerical optimisation ( nlm ) for parameter estimation. fHMM ( 12 ) fits hierarchical Student- t HMMs to financial time series via nlm . HiddenMarkov ( 6 ) implements EM for Poisson, Normal, and discrete emissions. hmmlearn (Python) provides Gaussian and multinomial emissions with a scikit-learn-compatible API. All require their respective language runtimes, have non-trivial installation dependencies, and are not straightforward to embed in a C++ application that must run without a managed runtime. Several also rely on MOM approximations or gradient-based numerical optimisation where direct EM M-steps are available, which can affect parameter recovery for sparse states. 2.2 C and C++ libraries Table 1 summarises the C and C++ HMM libraries most relevant to libhmm . Table 1: C/C++ HMM library comparison. Library Lang SIMD Distributions Dependencies Maintained Standard GHMM C No Discrete, Gaussian/GMM libxml2, GSL No (2012) C89/C99 HMMLib C++ SSE Discrete only Boost No C++11 StochHMM C++ No Discrete only None No C++11 libhmm C++ AVX-512/AVX2/ SSE2/NEON 16 continuous+discrete None Yes C++20 GHMM. GHMM ( 14 ) is the most historically significant C HMM library. It supports Gaussian mixture and discrete emissions, has been cited in bioinformatics work since 2003, and has a reasonably complete feature set for its target applications. Its limitations are architectural: C-style API with manual memory management, XML-heavy model specification, autotools build system, and dependencies on libxml2 and GSL. Active development ceased around 2012. For forward-backward on discrete models, GHMM’s C implementation with its flat memory layout can outperform libhmm on raw throughput; this tradeoff is discussed in Section 5.1 . GHMM does not support the continuous distribution families required by libhmm ’s target applications. HMMLib. HMMLib is the closest architectural ancestor to libhmm ’s performance goals: C++, SSE SIMD intrinsics, template-based design. It targets discrete HMMs specifically and requires Boost. Active development has ceased. Its SSE implementation is faster than scalar C++ for forward-backward on discrete models; libhmm matches or exceeds it using AVX2/AVX-512 while adding broader distribution support. Section 6.1 presents a direct comparison. StochHMM. StochHMM is designed for bioinformatics workflows, providing flexible text-file model specification for gene finding and sequence annotation. It is not a general-purpose library: emissions are discrete or categorical, and the API is tightly coupled to the bioinformatics use case. 2.3 Design motivation These libraries failed in different ways for the original application context: GHMM required a C-style integration with unacceptable dependency overhead; HMMLib and StochHMM were discrete-only; none supported modern C++20 idioms or the continuous distributions required. The design goal was not primarily to outperform GHMM or HMMLib on their own terms (raw discrete forward-backward throughput) but to provide a library that: • compiles as a zero-dependency C++20 library into any CMake project with a single add_subdirectory or FetchContent ; • supports the continuous emission distributions used in ecology, finance, and signal processing with correct MLE M-steps; • operates correctly on sequences of arbitrary length without numerical underflow; • remains maintainable and extensible under modern C++ idioms. 3 Algorithms 3.1 Notation We write an HMM as \lambda=(\boldsymbol{\pi},\mathbf{A},\mathbf{B}) where \boldsymbol{\pi}\in\Delta^{K-1} is the initial state distribution, \mathbf{A}\in[0,1]^{K\times K} is the row-stochastic transition matrix, and \mathbf{B}=\{b_{j}(\cdot)\}_{j=1}^{K} is the set of emission densities. We observe a sequence \mathbf{y}=(y_{1},\ldots,y_{T}) . The forward variable \alpha_{t}(j)=P(y_{1},\ldots,y_{t},q_{t}=j\mid\lambda) and backward variable \beta_{t}(j)=P(y_{t+1},\ldots,y_{T}\mid q_{t}=j,\lambda) are the standard quantities ( 13 ) . We write \gamma_{t}(j)=P(q_{t}=j\mid\mathbf{y},\lambda) for the smoothed state occupancy probabilities and \xi_{t}(i,j)=P(q_{t}=i,q_{t+1}=j\mid\mathbf{y},\lambda) for the pairwise occupancy probabilities. 3.2

Read the complete text