Abstract
In-context learning (ICL) allows Large Language Models (LLMs) to adapt to new tasks without parameter updates, yet the underlying mechanism remains a subject of debate: is it sophisticated pattern matching or true probabilistic reasoning? This paper investigates this by evaluating LLMs in a controlled, stochastic environment using biased coin flips. By comparing model outputs to a mathematical Bayesian baseline, we demonstrate that LLMs approximate Bayesian filtering. While models exhibit an initial "heads-biased" prior and a notable recency bias, they consistently update their internal probabilities in response to new evidence, effectively acting as "Local Bayesians."
Introduction
In-context learning (ICL) is a cornerstone of modern AI, enabling models to perform tasks based solely on examples provided in a prompt. Despite its utility, the mechanics of ICL are poorly understood. Traditional tasks like question-answering are often too semantically complex to evaluate against a mathematical ground truth. To bridge this gap, this research utilizes a simplified, synthetic framework: the Beta-Bernoulli process of biased coin flips. This approach allows for a precise measurement of how closely an LLM’s internal state tracks with optimal Bayesian inference.
Methodology
The researchers evaluated a diverse suite of open-source models, including Llama-3.1 (8B), Gemma-2 (2B), and the Pythia Scaling Suite. The methodology centered on three primary techniques:
- Logit Extraction: Rather than relying on text generation, the researchers extracted raw logits for the tokens "heads" and "tails." These were renormalized to represent the model's internal probability distribution.
- Bayesian Comparison: Model probabilities were compared against a standard Bayesian posterior and a "gamma-modified" filter. The latter incorporates an exponential decay factor ($\gamma$) to account for potential recency bias, where models prioritize recent tokens over earlier evidence.
- Prompt Robustness: To ensure results were not artifacts of specific phrasing, 50 different prompt variations were used to present the coin flip sequences.
Experimental Framework
The study utilized a synthetic dataset generated from Bernoulli distributions with varying biases ($\theta$). Beyond binary coin flips, the researchers also conducted experiments with rolling dice to test the models' ability to handle multiple categorical outcomes. The evaluation spanned models of various sizes and training stages, from the 70M parameter Pythia model to the 12B parameter variant, as well as instruction-tuned models like Llama-3.1-Instruct.
Key Insights
The Persistence of the "Heads" Prior
Across all tested models, a strong initial bias toward "heads" was observed, often as high as 70%. This is likely a reflection of the training data; in natural language, the phrase "heads or tails" is far more common than the reverse, and "heads" is culturally associated with the start of a sequence. This prior must be overcome by evidence provided in the prompt.
Evidence Overrides Instructions
A significant finding was the models' inability to follow explicit instructions regarding bias (e.g., "This coin is 90% tails"). However, when shown a sequence of 10 tails via ICL, the models successfully updated their internal probabilities. This suggests that for LLMs, empirical evidence provided in the context window consistently outweighs explicit natural language instructions.
LLMs as "Local Bayesians"
LLMs do not treat all evidence in a prompt equally. The study found that models fit a Bayesian profile most accurately when a discount factor is applied to older evidence. This "recency bias" suggests that LLMs act as Local Bayesians, focusing on the most immediate context to inform their predictions. Interestingly, instruction-tuned models exhibited even shorter memory horizons than their base model counterparts.
Disconnect Between Attention and Inference
One of the more surprising results was the lack of correlation between attention weights and Bayesian updates. The magnitude of attention a model directed at a specific example did not reliably predict how much that example influenced the model's internal probability shift, suggesting that the "reasoning" happens through more complex interactions than simple attention allocation.
Results and Discussion
As the number of examples in the prompt increases, the Total Variation Distance (TVD)—the gap between the model's prediction and the true Bayesian posterior—decreases significantly. This convergence indicates that with sufficient data, LLMs effectively approximate optimal statistical reasoning.
However, model scale does not strictly correlate with Bayesian performance. In the Pythia suite, larger models sometimes performed worse at incorporating updates than smaller ones. This suggests that Bayesian behavior is an emergent property of specific architectural or training choices rather than a simple byproduct of parameter count. Instruction tuning generally improved a model's ability to handle extreme biases (0% or 100%) but increased the tendency to "forget" early evidence.
Limitations and Future Work
The current study is limited by its reliance on logit access, making it difficult to apply to "black box" proprietary models. Furthermore, the experiments focused on discrete categorical outcomes. Real-world reasoning often involves continuous variables and complex hierarchical structures. Future research should explore whether these Bayesian behaviors persist in multimodal models and whether prompt engineering can be used to better calibrate the initial priors of these systems.
Summary updated