Uncertainty Quantification › Sampling-Based Uncertainty
Language AI HandbookAll chapters Uncertainty Quantification › Sampling-Based UncertaintyOne section · Sections in this chapter4 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Instead of trying to extract a confidence score from a single model run, sampling-based methods estimate uncertainty by running the model multiple times on the same query and measuring how much the outputs vary. High variance across samples suggests the model is uncertain; low variance suggests confidence.
This approach sidesteps several problems with token-level probability as a confidence measure. Token probabilities are affected by generation artifacts (temperature, sampling parameters), training biases, and RLHF shifts that don't necessarily reflect factual uncertainty. Behavioral variance across samples is a more direct measure of whether the model would give consistent answers in deployment: a model that gives the same answer ten times out of ten when sampled is behaving consistently, regardless of what probabilities it assigns internally.
Monte Carlo Sampling
The basic idea is Monte Carlo estimation: draw samples from the model's distribution and compute a statistic over them. The variance of that statistic estimates uncertainty.
For a yes/no question, this is straightforward:
- Generate responses with temperature (to introduce sampling randomness)
- Count how many responses say "yes" versus "no"
- Let be the fraction of "yes" responses:
- Compute binary entropy as the uncertainty measure:
where:
- is the proportion of responses in the majority class
- reaches its maximum of nats (or 1 bit) when , indicating maximum uncertainty
- when , indicating complete agreement and minimum uncertainty
Higher entropy (answers split near 50/50) means higher uncertainty. Near-unanimous answers indicate confidence. The beauty of this approach is that it requires no access to model internals: you only need to observe the model's outputs.
The computational cost is the main drawback. Running samples means forward passes through the model. For a model with a single generation costing $0.01 and samples per query, the uncertainty estimation costs $0.10 per query, ten times more than a single generation. For high-throughput production systems, this cost is often prohibitive.
Semantic Consistency
For open-ended responses, simple string comparison is too strict. "Paris is the capital of France" and "France's capital city is Paris" express the same fact but would be counted as different answers under exact match. Semantic consistency measures whether multiple responses convey the same meaning, independent of surface form.
One approach: use an NLI (natural language inference) model to check whether pairs of sampled responses entail each other. If response entails and entails , they are semantically equivalent. The fraction of response pairs that are semantically equivalent is the consistency score. Low consistency means high uncertainty.
where means "response entails response ". The bidirectional entailment check (both entails and entails ) is a proxy for semantic equivalence. NLI models are fast compared to generation, so the consistency computation adds minimal overhead once samples are available.
The limitation is that NLI models are themselves imperfect and can make errors on semantically subtle cases. Responses that are paraphrases of the same claim may fail the entailment check if they use different vocabulary. This is an active area of improvement in the field.
Self-Consistency Prompting
Self-consistency (Wang et al., 2022) was originally proposed to improve reasoning accuracy but also provides a natural uncertainty estimate. The method generates multiple chain-of-thought reasoning paths and uses majority voting to select the final answer.
The process works as follows. The model is prompted to think step by step, and this prompt is sent times with non-zero temperature to produce different reasoning paths. Each path arrives at a final answer. Majority voting selects the most common final answer as the output. The fraction of samples agreeing with the majority answer is a confidence score. If 9 out of 10 sampled paths arrive at the same answer, confidence is 0.9. If only 6 out of 10 agree, confidence is 0.6.
Self-consistency works well for structured tasks (math problems, multiple-choice QA) where you can extract a clean final answer from each reasoning path. For open-ended generation, you need semantic clustering instead of exact match voting: run all sampled responses through an NLI model to cluster them by meaning, then take the fraction belonging to the largest cluster as the confidence estimate.
The insight behind self-consistency is that different reasoning paths through a problem serve as approximate independent samples from the model's belief distribution. When the model reliably knows the answer, most paths converge on it. When the model is uncertain, paths diverge into different regions of the answer space. The variance across paths encodes the model's behavioral uncertainty.
Semantic Entropy
Semantic entropy (Kuhn et al., 2023) formalizes sampling-based uncertainty using information theory. Rather than computing entropy over raw text samples (which conflates surface variation with semantic variation), it computes entropy over semantic clusters.
The procedure has four steps:
- Sample responses from the model with non-zero temperature
- Group responses into semantic equivalence classes using bidirectional NLI entailment
- Estimate the probability of each class:
- Compute semantic entropy:
where is the number of distinct semantic clusters, is the -th cluster, is the number of responses assigned to it, and is the total number of sampled responses. The here is the natural logarithm, so SE is in units of nats.
Semantic entropy is zero when all responses are semantically identical (certain) and increases as responses become more diverse in meaning. It is invariant to surface-form variation: if all responses say the same thing in different words, they form one cluster and SE is zero. It correlates more strongly with factual accuracy than token-level probability entropy, which is noisier because it treats surface variation as signal.
Token-level entropy over responses is noisy because different surface forms of the same meaning contribute independently. "The capital is Paris" and "Paris is the capital" are different character sequences but identical semantic content. Computing entropy over them as distinct strings artificially inflates the uncertainty estimate. Semantic entropy groups by meaning before computing entropy, removing this noise and producing a cleaner, more accurate uncertainty signal. The Kuhn et al. paper showed that semantic entropy substantially outperforms token-level probability measures at predicting factual accuracy on open-domain QA benchmarks.
Dropout-Based Uncertainty
In neural networks, Monte Carlo Dropout approximates Bayesian inference by keeping dropout active at test time. Multiple forward passes through the network, each with a different random dropout mask, produce a distribution over predictions. The variance of this distribution estimates epistemic uncertainty.
The theoretical foundation is that a neural network with dropout at every layer is mathematically equivalent to a deep Gaussian process under certain conditions (Gal and Ghahramani, 2016). Each forward pass with a different dropout mask can be interpreted as sampling from an approximate posterior distribution over the network's weights. The variance across these samples therefore estimates the posterior uncertainty about the model's predictions.
For language models, this has limited practical use because large language models typically do not use dropout during inference, and enabling it changes the model's behavior unpredictably. The sheer scale of modern LLMs makes the Gaussian process approximation less precise, and activating dropout at inference time often degrades output quality substantially. This makes it difficult to separate "uncertainty due to knowledge limits" from "degradation due to random dropped activations."
But the conceptual framework carries over: any source of stochasticity (temperature sampling, different random seeds, different prompt phrasings) can be used to generate a distribution from which uncertainty is estimated. Temperature sampling is the most direct analogue. It introduces randomness proportional to the model's own uncertainty in its token predictions, and sampling multiple times with non-zero temperature produces a distribution over outputs that encodes the model's behavioral uncertainty about what to say.
Consistency-Based Uncertainty via Prompt Perturbation
A related technique uses paraphrased versions of the same question to probe consistency. If the model gives different answers to "What year was Einstein born?" and "When was Einstein born?", the inconsistency suggests uncertainty or knowledge gaps. Formally:
- Generate semantically equivalent paraphrases of the question
- Obtain the model's answer to each paraphrase
- Measure semantic consistency across answers
- Treat inconsistency as a proxy for uncertainty
This approach is robust to the specific sampling temperature and doesn't require running the model multiple times on the exact same input (which can be dominated by sampling artifacts from a single phrasing).
The intuition is that a model with stable knowledge should retrieve consistent answers regardless of how a question is phrased. When knowledge is solid, the retrieval process is robust to surface-form variation. When it is shaky, small changes in phrasing tip the model toward different answers, because the model is pattern-matching against surface forms rather than retrieving a stable fact. Sensitivity to question phrasing is a symptom of shallow or inconsistent encoding.
Prompt perturbation is especially useful for detecting factual inconsistency: the model might answer "Einstein was born in 1879" when asked directly, but answer "Einstein was born in Ulm in 1878" when a slightly different phrasing activates a different context window. Neither answer carries explicit uncertainty; they contradict each other. Standard log-probability methods might rate both answers as high-confidence because the model generates each fluently. Only consistency checking reveals the problem.
The practical challenge is generating high-quality paraphrases automatically. Large models can be prompted to paraphrase, but this adds latency and cost. Rule-based perturbations (changing word order, substituting synonyms, adding neutral context) are faster but less semantically faithful. Research on this technique has shown that consistency correlates with factual correctness in the 0.6-0.7 range, which is meaningful but not sufficient as a standalone reliability signal.