Pre-trained Large Language Models Learn to Predict Hidden Markov Models In-context

Yijia Dai, Zhaolin Gao, Yahya Sattar, Sarah Dean, Jennifer J. SunNeurIPS 2025

Introduction

Hidden Markov Models (HMMs) are the workhorse of sequential data analysis, used to model everything from climate patterns to animal decision-making. They assume that observable data is generated by "hidden" states that evolve over time. However, fitting HMMs to real-world data is notoriously difficult. Standard techniques like the Baum-Welch algorithm are computationally intensive, prone to getting stuck in local optima, and require significant domain expertise to configure.

This paper investigates whether the massive pre-training of Large Language Models (LLMs) has equipped them with an "implicit" ability to perform complex statistical inference. Specifically, the authors ask: Can an LLM look at a string of observations in a prompt and figure out the underlying hidden rules well enough to predict the next step?

Remarkably, the study demonstrates that pre-trained LLMs can learn to predict sequences generated by HMMs entirely through in-context learning (ICL). Without any task-specific fine-tuning, LLMs achieve predictive accuracy approaching the theoretical optimum, often outperforming traditional statistical methods and matching or exceeding expert-designed models on real-world animal behavior data.

Overview of the study's three main research questions regarding LLMs and HMMs

Methods and Datasets

The researchers treated LLMs (specifically the Qwen and Llama families) as "zero-shot statistical tools." They provided the models with sequences of observations and asked them to predict the next token.

To rigorously test this, the study utilized both synthetic and real-world datasets:

  • Synthetic Benchmarking: The authors generated 234 different HMM configurations, systematically varying parameters like the number of states, the "mixing rate" (how fast the system moves between states), and "entropy" (how much randomness is in the transitions and emissions). Sequences featured up to 64 states and 64 possible observations.
  • Real-World Animal Behavior: They tested the models on the IBL Mice Dataset (tracking mouse choices in a visual decision-making task) and a Rat Reward-Learning Dataset (where rats must learn changing reward probabilities without explicit external stimuli).

LLM performance was compared against strong baselines, including the Viterbi algorithm (the theoretical optimum using ground-truth parameters), Baum-Welch (the standard Expectation-Maximization approach), Spectral Learning, and custom-trained LSTMs and Transformers.

Key Insights and Results

Convergence to the Optimum

The most striking finding is that as the context window (the number of examples in the prompt) increases, LLM accuracy consistently converges toward the theoretical maximum defined by the Viterbi algorithm. On synthetic data, LLMs consistently outperformed Baum-Welch and spectral methods, which often struggled with stability or required far more data to converge. Furthermore, LLMs showed a remarkable ability to pick up patterns from relatively short sequences (hundreds of tokens) where traditional neural networks (like LSTMs) failed to learn effectively from scratch.

Sensitivity to HMM Properties

LLM performance is governed by the same fundamental limits as classical algorithms. High entropy (randomness) and slow mixing rates (long-range dependencies) make the "learning" process much slower for the LLM, mirroring the difficulty faced by human-designed algorithms.

The authors suggest LLMs perform "improper learning"—they don't explicitly calculate the transition or emission matrices, but they learn the operators necessary to predict the next step, similar to how spectral learning algorithms function.

Beating the Experts on Real-World Data

When applied to the IBL mouse dataset, LLM in-context learning achieved an average accuracy of 86.2%, surpassing the 82.2% achieved by the GLM-HMM, a specialized model developed by neuroscience experts specifically for that task.

LLMs as a Diagnostic Tool

Based on these results, the authors propose using LLMs as a litmus test for data complexity. If an LLM's performance improves steadily with more context, the data likely has a learnable, non-random structure. If it plateaus early at low accuracy, the system is likely high-entropy or lacks a simple Markovian structure.

Limitations and Future Work

While the results are highly promising, the authors note several limitations:

  • The "Black Box" Problem: While the LLM can predict the next observation, it doesn't provide the user with the underlying model parameters. Scientists often need these parameters to interpret the mechanisms of a system, not just predict its output.
  • Discrete vs. Continuous Data: The current work focuses on discrete observations (tokens). Many scientific datasets (like neural spikes or climate sensors) are continuous, and adapting ICL to these formats remains an open challenge.
  • Tokenization Sensitivity: How data is converted into text can affect performance, particularly for highly repetitive sequences which might be "filtered" during the LLM's original pre-training.

Looking forward, the authors call for the development of "Scientific Foundation Models" specifically designed to handle high-dimensional, continuous scientific sequences, opening new avenues for scientific discovery.

Summary updated

BibTeX
@inproceedings{dai2025pre,
  author = {Yijia Dai and Zhaolin Gao and Yahya Sattar and Sarah Dean and Jennifer J. Sun},
  title = {{Pre-trained Large Language Models Learn to Predict Hidden Markov Models In-context}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=btBqWTbf6q},
}
Figure