Abstract
Hidden Markov Models (HMMs) are foundational tools for modeling sequential data with latent Markovian structure, yet fitting them to real-world data remains computationally challenging. In this work, we show that pre-trained large language models (LLMs) can effectively model data generated by HMMs via in-context learning (ICL)—their ability to infer patterns from examples within a prompt. On a diverse set of synthetic HMMs, LLMs achieve predictive accuracy approaching the theoretical optimum. We uncover novel scaling trends influenced by HMM properties and offer theoretical conjectures for these empirical observations. We also provide practical guidelines for scientists on using ICL as a diagnostic tool for complex data. On real-world animal decision-making tasks, ICL achieves competitive performance with models designed by human experts. To our knowledge, this is the first demonstration that ICL can learn and predict HMM-generated sequences—an advance that deepens our understanding of in-context learning in LLMs and establishes its potential as a powerful tool for uncovering hidden structure in complex scientific data.
Introduction
Hidden Markov Models are the bread and butter of sequential data analysis, used to model everything from climate patterns to animal behavior. However, they come with a major headache: inference is hard. Traditional methods like the Baum-Welch algorithm are computationally intensive, prone to getting stuck in local optima (non-convexity), and require significant domain expertise to tune. For scientists in fields like biology or ecology, these "accessibility bottlenecks" make HMMs difficult to apply to messy, real-world data.
This research investigates whether the sequence-prediction power of LLMs can be repurposed as a "zero-shot" statistical tool to bypass these hurdles. The study addresses three primary questions: Can ICL learn HMMs? Do HMM properties affect performance? And can it work on real-world scientific data?
Methods
The researchers treat the LLM as a "Bayesian learner" that performs inference directly within its context window. Unlike traditional machine learning, no weights are updated during this process. Instead, the model is fed a sequence of observations ($o_1, o_2, ... o_t$) and tasked with predicting the next observation $o_{t+1}$.
To rigorously test this, the authors employed several methodologies:
- Systematic Synthetic Probing: They generated 234 distinct HMM configurations, varying state/observation space sizes, entropy (randomness), and mixing rates (how fast the system reaches a steady state).
- Benchmarking: LLMs (specifically the Qwen and Llama families) were compared against the Viterbi Algorithm (the theoretical ceiling), Baum-Welch (the standard EM-based approach), and baseline sequence models like LSTMs and n-grams.
- Theoretical Linking: The authors provide a conjecture linking LLM performance to Spectral Learning algorithms, suggesting that LLMs might be performing a form of "improper learning"—predicting the next step without explicitly reconstructing the hidden transition matrices.
Key Insights and Results
Convergence to the Optimum
The most striking finding is that LLM accuracy consistently converges to the Viterbi optimum as the context window grows. This suggests that LLMs are not merely finding surface-level patterns but are performing near-optimal inference on latent structures. Unlike Baum-Welch and LSTMs, which often show erratic performance or accuracy "dips" as context increases, LLMs exhibit monotonic improvement.
The Impact of HMM Properties
The speed at which an LLM "cracks" an HMM in-context is governed by the HMM's intrinsic properties. High-entropy (very random) or slow-mixing (long-term dependency) systems require significantly longer context windows for the LLM to achieve high accuracy. The researchers uncovered that these performance trends mirror the theoretical error bounds of spectral learning algorithms.
Real-World Performance
The study tested ICL on real-world behavioral data from animal decision-making tasks:
- IBL Mouse Dataset: Mice make decisions based on visual stimuli. LLM ICL achieved an average accuracy of 86.2%, surpassing the 82.2% achieved by the GLM-HMM, a model specifically designed by human experts for this task.
- Miller et al. Rat Dataset: A more challenging reward-learning task where rats adapt to changing reward probabilities. While LLMs were competitive, they lagged behind specialized symbolic models on this highly non-stationary data, indicating a complexity ceiling for current models.
Discussion and Future Work
This work establishes LLMs as a potential diagnostic tool for scientists. If an LLM's accuracy improves steadily with context length on a dataset, it serves as a strong signal that the data has a learnable, non-random underlying structure.
However, several challenges remain. The current study focuses on discrete token sequences, whereas much scientific data is continuous. Furthermore, LLMs remain "black boxes"; while they can predict the next observation, they do not yet provide the interpretable transition and emission matrices that scientists typically require for model building. Future research will need to address these interpretability gaps and adapt ICL for continuous-state HMMs.
Summary updated