betaFeedback

WorksComputer Science

Weighted Ensemble Sampling for Rare-Event Estimation in Language Models

Abstract

Rare but severe failures of large language models can become practically significant at deployment scale. Estimating their probabilities with direct Monte Carlo sampling is highly inefficient, while proposal-based methods typically require auxiliary learning of an additional proposal distribution. We introduce Weighted Ensemble (WE), a training-free framework for rare-event estimation in language models. WE samples exclusively from the original language model and improves upon direct Monte Carlo by periodically reallocating the sampling budget toward prefixes that appear more likely to reach the target rare event. To guide this reallocation without learning a separate proposal distribution, we exploit internal LLM representations as prefix-level signals and empirically validate their effectiveness. We provide a theoretical analysis of the proposed algorithm. Experiments on safety-related tasks spanning both small-scale and modern large-scale LLMs demonstrate that WE improves the efficiency and reliability of rare-event probability estimation.

Introduction

Large Language Models (LLMs) ([2] (Team et al. 2023; Liu et al. 2024a)) have become deeply integrated into everyday life and are now deployed at an unprecedented scale. For example, ChatGPT has been reported to receive over 2.5 billion messages per day 1. At this scale, even extremely low-probability undesirable behaviors, such as rare unsafe responses, can become practically significant, as a behavior occurring with probability as small as 10−610^{-6} may still be encountered thousands of times per day. Consequently, while modern alignment methods have substantially reduced undesirable model behaviors ((Ziegler et al., 2019; Ouyang et al., 2022; Ji et al., 2025; Liu et al., 2024b)), they cannot guarantee that such failures are eliminated entirely. This makes it important to quantify not only typical model behavior, but also extremely rare undesirable events that lie far in the tail of the model’s output distribution.

Despite this practical importance, rare-event analysis for LLMs remains at an early stage ((Wu and Hilton, 2025; Jones et al., 2025; Dorman et al., 2026)). Standard evaluation typically relies on direct Monte Carlo (MC) sampling from the deployed model (Figure 1(a)) ((Wadi and Fredette, 2025)). Given a fixed prompt, direct MC independently generates responses from the original language model and estimates the rare-event probability by the fraction of sampled responses that satisfy the target event. However, this approach becomes highly sample-inefficient when the event of interest has extremely small probability. For example, reliably estimating an event with probability on the order of 10−510^{-5} may require on the order of 10610^{6} samples to observe a sufficient number of rare-event realizations, with the overwhelming majority of samples are drawn from non-rare regions of the model’s output distribution and therefore provide little information about the event of interest. Recent work addresses this inefficiency by deliberately concentrating computation on rare-event regions ([5] (Dorman et al. 2026; Zhao et al. 2024; Liu et al. 2026; Kim et al. 2025)). These methods construct a proposal distribution that assigns higher probability to outputs associated with the target rare event. Rather than sampling directly from the original language model, they generate responses from this biased proposal distribution, thereby increasing the frequency with which rare events are observed. While such proposal-based approaches can substantially improve sample efficiency, they typically require an additional procedure to learn an effective proposal distribution. A more detailed discussion of related work is provided in Appendix § 6.

Comparison of direct Monte Carlo and Weighted Ensemble sampling

Figure 1. Comparison of direct Monte Carlo (MC) and Weighted Ensemble (WE) sampling for the toxic-story generation task in § 3. (a) Direct MC independently samples complete responses from the base language model, making rare events difficult to observe when their probability is extremely small. (b) WE samples from the same base language model but adaptively reallocates the sampling budget during generation. Benign prefixes are merged (dashed), reducing the number of continuations allocated to them, while prefixes that appear more promising for reaching the target rare event are split (solid red), allowing more continuations to be generated from those prefixes.

In this paper, we propose Weighted Ensemble (WE), a training-free sampling framework for rare-event estimation in language models (Figure 1b). Inspired by the Weighted Ensemble method originally for physical simulations ((Huber and Kim, 1996)), our approach preserves the original language-model generation process as in direct Monte Carlo. The key difference is that, rather than independently generating every sample to completion, WE periodically groups partially generated responses and reallocates the sampling budget across them. Prefixes that appear more likely to eventually reach the target rare event receive additional continuations while other prefixes receive fewer future continuations. In this way, WE reduces the sampling budget spent repeatedly exploring common, non-rare regions of the output distribution as in the direct MC method. To identify promising partial generations, instead of learning a separate proposal distribution as in proposal-based approaches, we exploit information already available within the language model itself. Recent work has shown that internal LLM representations can contain predictive signals of the safety properties of the eventual response even at very early generation steps ((Wang et al., 2026)). Motivated by this observation, we use the white-box representations of LLMs and empirically show that such internal representations provide useful signals for reallocating the sampling budget without any additional training.

In summary, our main contributions are: 1. We introduce Weighted Ensemble for LLM rare-event estimation, a training-free sampling framework that adaptively reallocates the sampling budget toward informative partial prefixes during generation from the original language model. 2. We provide a theoretical analysis of the proposed method (§ 2.3), including unbiasedness (Proposition 1), consistency (Corollary 1), and derive the asymptotic distribution of the WE estimator (Proposition 2), which further enables an explicit characterization of how key design choices affect estimation efficiency. To the best of our knowledge, prior WE analyses have not provided these asymptotic results. 3. We further study robustness to changes in the rare-event definition, where WE can reuse the same samples for unbiased estimation while proposal-based methods may fail, highlighting an important advantage of WE in such settings (§ 2.5). 4. We evaluate the proposed method on several safety-related rare-event estimation tasks spanning both small-scale and modern large-scale LLMs, demonstrating that WE improves rare-event estimation efficiency and reliability across model scales and safety tasks.

Weighted Ensemble Framework

Problem Setup

Let p0(⋅∣c)p_0(\cdot\mid c) denote a pretrained language model conditioned on a fixed prompt cc, and let V\mathcal{V} be its vocabulary. The language model generates a response x=(x1,…,xT)x = (x_1,\ldots,x_T) sequentially. At step tt, let the previously generated prefix be x1:t−1=(x1,…,xt−1)∈Xt−1x_{1:t-1} = (x_1,\ldots,x_{t-1}) \in\mathcal{X}_{t-1}, where Xt=Vt\mathcal{X}_t = \mathcal{V}^t denotes the space of response prefixes of length tt, with X0={∅}\mathcal{X}_0 = \{\varnothing\}. The next token xt∈Vx_t \in\mathcal{V} is then sampled according to p0(xt∣c,x1:t−1)p_0(x_t \mid c,x_{1:t-1}), producing the updated prefix x1:t∈Xtx_{1:t} \in\mathcal{X}_t. Consequently, the probability of a complete response x∈XTx \in\mathcal{X}_T of length TT is p0(x∣c)=∏t=1Tp0(xt∣c,x1:t−1)p_0(x \mid c) = \prod_{t=1}^{T} p_0(x_t \mid c,x_{1:t-1}).

Following prior work ((Zhao et al., 2024; An-) Algorithm 1 Weighted Ensemble for LLM Rare-Event Estimation

Require: base language model p0p_0, prompt cc, rare-event score RR, threshold γ⋆\gamma_\star, number of walkers NN, generation length TT, checkpoints 0=t0<t1<⋯<tK=T0=t_0<t_1<\cdots<t_K=T, binning maps {Bk+1}k=0K−1\{\mathcal{B}_{k+1}\}_{k=0}^{K-1}

  1. Initialize x1:0(i)←∅x_{1:0}^{(i)}\leftarrow\varnothing and w0(i)←1/Nw_0^{(i)}\leftarrow1/N for i=1,…,Ni=1,\ldots,N

  2. for k=0,…,K−1k=0,\ldots,K-1 do

  3. Sample x~tk+1:tk+1(i)∼p0(⋅∣c,x1:tk(i))\widetilde{x}_{t_k+1:t_{k+1}}^{(i)}\sim p_0(\cdot\mid c,x_{1:t_k}^{(i)}), ∀i\forall i

  4. Set x~1:tk+1(i)←(x1:tk(i),x~tk+1:tk+1(i))\widetilde{x}_{1:t_{k+1}}^{(i)}\leftarrow(x_{1:t_k}^{(i)},\widetilde{x}_{t_k+1:t_{k+1}}^{(i)})

  5. if k+1<Kk+1<K then

  6. b(i)←Bk+1(x~1:tk+1(i))b^{(i)}\leftarrow\mathcal{B}_{k+1}(\widetilde{x}_{1:t_{k+1}}^{(i)}), ∀i\forall i

  7. Bk+1←{b:∃i, b(i)=b}\mathcal{B}_{k+1}\leftarrow\{b:\exists i,\ b^{(i)}=b\}

  8. for b∈Bk+1b\in\mathcal{B}_{k+1} do

  9. Ik+1,b←{i:b(i)=b}I_{k+1,b}\leftarrow\{i:b^{(i)}=b\} and Wk+1,b←∑i∈Ik+1,bwk(i)W_{k+1,b}\leftarrow\sum_{i\in I_{k+1,b}}w_k^{(i)}

  10. end for

  11. Choose {nk+1,b}b∈Bk+1\{n_{k+1,b}\}_{b\in\mathcal{B}_{k+1}} such that ∑b∈Bk+1nk+1,b=N\sum_{b\in\mathcal{B}_{k+1}}n_{k+1,b}=N and nk+1,b≥1n_{k+1,b}\ge1

  12. for b∈Bk+1b\in\mathcal{B}_{k+1} do

  13. for r=1,…,nk+1,br=1,\ldots,n_{k+1,b} do

  14. Draw Jk+1,b,r∈Ik+1,bJ_{k+1,b,r}\in I_{k+1,b} with replacement with Pr⁡(Jk+1,b,r=i)=wk(i)/Wk+1,b\Pr(J_{k+1,b,r}=i)=w_k^{(i)}/W_{k+1,b}

  15. Create child prefix x1:tk+1(b,r)←x~1:tk+1(Jk+1,b,r)x_{1:t_{k+1}}^{(b,r)}\leftarrow\widetilde{x}_{1:t_{k+1}}^{(J_{k+1,b,r})} and assign wk+1(b,r)←Wk+1,b/nk+1,bw_{k+1}^{(b,r)}\leftarrow W_{k+1,b}/n_{k+1,b}

  16. end for

  17. end for

  18. Reindex all children as {(x1:tk+1(i),wk+1(i))}i=1N\{(x_{1:t_{k+1}}^{(i)},w_{k+1}^{(i)})\}_{i=1}^{N}

  19. else

  20. Set x1:T(i)←x~1:T(i)x_{1:T}^{(i)}\leftarrow\widetilde{x}_{1:T}^{(i)} and wK(i)←wK−1(i)w_K^{(i)}\leftarrow w_{K-1}^{(i)} for all ii

  21. end if

  22. end for

  23. return p^WE=∑i=1NwK(i)1{R(x1:T(i))≥γ⋆}\widehat{p}_{\mathrm{WE}}=\sum_{i=1}^{N}w_K^{(i)}\mathbf{1}\{R(x_{1:T}^{(i)})\ge\gamma_\star\}

gell et al., 2026; Dorman et al., 2026, we consider the problem of estimating rare-event probabilities under the output distribution of a language model conditioned on a fixed prompt. For brevity, we suppress the dependence on cc throughout. Let R(x)∈RR(x)\in\mathbb{R} denote a scalar rare-event score, where larger values indicate a stronger presence of the behavior of interest. In practice, RR is typically obtained from a learned evaluator. For example, in reinforcement learning from human feedback, RR may be provided by a reward model trained on human preference (Ouyang et al., 2022; Ziegler et al., 2019). Given a predefined threshold γ⋆\gamma_\star, we define the target rare event as A⋆={x∈X:R(x)≥γ⋆}A_\star=\{x\in\mathcal{X}:R(x)\ge\gamma_\star\}. Our goal is to estimate its probability under the original language-model distribution, p⋆=Pp0(A⋆)=Ep0[1{R(X)≥γ⋆}]p_\star=\mathbb{P}_{p_0}(A_\star)=\mathbb{E}_{p_0}[\mathbf{1}\{R(X)\ge\gamma_\star\}].

Weighted Ensemble Algorithm

Algorithm 1 presents our Weighted Ensemble (WE) procedure for rare-event estimation in language-model generation. Similar to direct Monte Carlo sampling, all tokens are generated from the original language model p0p_0. The difference is that, at several intermediate generation checkpoints, the current response prefixes are grouped into bins designed to separate prefixes according to their potential to eventually reach the target rare event. The available sampling budget is then redistributed across these bins: prefix regions that are already represented by many samples receive fewer future continuations, while low-probability regions containing prefixes that appear more promising for reaching the rare event can receive additional continuations. This binning-and-resampling procedure is repeated throughout generation until the complete responses are obtained.

Formally, let NN denote the fixed sampling budget, i.e., the number of response prefixes maintained by the algorithm. During the autoregressive generation process, we choose a sequence of generation checkpoints 0=t0<t1<⋯<tK=T0=t_0<t_1<\cdots<t_K=T, at which the current prefixes are grouped and the sampling budget is redistributed. The checkpoints may be chosen arbitrarily. One simple choice is to place the checkpoints at a fixed interval τ=T/K\tau=T/K, such that tk+1−tk=τt_{k+1}-t_k=\tau.

At checkpoint tkt_k, we represent the state of the WE algorithm as a weighted collection of response prefixes, Ek={(x1:tk(i),wk(i))}i=1N\mathcal{E}_k=\{(x_{1:t_k}^{(i)},w_k^{(i)})\}_{i=1}^{N}, where x1:tk(i)x_{1:t_k}^{(i)} denotes the current response prefix and wk(i)w_k^{(i)} denotes the probability weight associated with that prefix, with ∑i=1Nwk(i)=1\sum_{i=1}^{N}w_k^{(i)}=1. We refer to these maintained prefixes as walkers. Initially, all walkers correspond to the empty response prefix, x1:0(i)=∅x_{1:0}^{(i)}=\varnothing, and are assigned equal weight, w0(i)=1/Nw_0^{(i)}=1/N (Algorithm 1 Line 1). At each intermediate checkpoint, the WE algorithm proceeds through three steps: propagation (Algorithm 1, Lines 3–4), binning (Lines 5–10), and resampling (Lines 11–17). We provide an illustrative example of the actual WE process for the toxic-story generation task used in our experiments in Appendix § 9.§ 1, Figure 3.

Illustrative example of the WE generation process

Figure 3. Illustrative example of the WE generation process for the prompt “Once upon a time, there was a” using Layer 4 all → RR @ 8 as reaction coordinate. At early checkpoints, walkers are grouped and resampled using hidden-representation clusters. After the reaction-coordinate switch, walkers are grouped using rare-event-score bins. Splitting preserves multiple continuations from promising prefixes, while merging removes redundant walkers within the same region. At the final checkpoint, continuations satisfying R≥γ=7R \ge\gamma= 7 belong to the target rare-event set A⋆A_\star.

Propagation. Starting from checkpoint tkt_k, each walker is independently extended to checkpoint tk+1t_{k+1} by sampling new tokens from the original language model, x~tk+1:tk+1(i)∼p0(⋅∣c,x1:tk(i))\widetilde{x}_{t_k+1:t_{k+1}}^{(i)}\sim p_0(\cdot\mid c,x_{1:t_k}^{(i)}). This produces the updated prefix x1:tk+1(i)=(x1:tk(i),x~tk+1:tk+1(i))x_{1:t_{k+1}}^{(i)}=(x_{1:t_k}^{(i)},\widetilde{x}_{t_k+1:t_{k+1}}^{(i)}). Importantly, propagation does not modify the walker weight. After propagation, walker ii still carries weight wk(i)w_k^{(i)}.

References

  1. [1]Hervé Abdi and Lynne J Williams. 2010. Principal component analysis. Wiley interdisciplinary reviews: computational statistics, 2(4):433–459.
  2. [2]Josh Achiam, Steven Adler, Sandhini Agarwal, Lama Ahmad, Ilge Akkaya, Florencia Leoni Aleman, Diogo Almeida, Janko Altenschmidt, Sam Altman, Shyamal Anadkat, et al. 2023. Gpt-4 technical report. arXiv preprint arXiv:2303.08774.arxiv.org/abs/2303.08774
  3. [3]Upendra Adhikari, Barmak Mostofian, Jeremy Copperman, Sundar Raman Subramanian, Andrew A Petersen, and Daniel M Zuckerman. 2019. Computational estimation of microsecond to second atomistic folding times. Journal of the American Chemical Society, 141(16):6519–6526.DOI
  4. [4]Sergios Agapiou, Omiros Papaspiliopoulos, Daniel Sanz-Alonso, and Andrew M Stuart. 2017. Importance sampling: Intrinsic dimension and computational cost. Statistical Science, pages 405–431.arxiv.org/abs/1511.06196
  5. [5]Rico Angell, Raghav Singhal, Zachary Horwitz, Zhou Yu, Rajesh Ranganath, Kathleen McKeown, and He He. 2026. Estimating tail risks in language model output distributions. arXiv preprint arXiv:2604.22167.arxiv.org/abs/2604.22167
  6. [6]Anthropic. 2024. The claude 3 model family: Opus, sonnet, haiku.
  7. [7]David Aristoff. 2018. Analysis and optimization of weighted ensemble sampling. ESAIM: Mathematical Modelling and Numerical Analysis, 52(4):1219–1238.DOI
  8. [8]David Arthur, Sergei Vassilvitskii, et al. 2007. k-means++: The advantages of careful seeding. In Annual ACM-SIAM Symposium on Discrete Algorithms (SODA).DOI
  9. [9]Alexandros Beskos, Ajay Jasra, Nikolas Kantas, and Alexandre Thiery. 2016. On the convergence of adaptive sequential monte carlo methods. arXiv preprint arXiv:1306.6462.arxiv.org/abs/1306.6462
  10. [10]Christopher M Bishop and Nasser M Nasrabadi. 2006. Pattern recognition and machine learning, volume 4. Springer.
  11. [11]Sid Black, Gao Leo, Phil Wang, Connor Leahy, and Stella Biderman. 2021. Gpt-neo: Large scale autoregressive language modeling with mesh-tensorflow. Zenodo.
  12. [12]Peter G Bolhuis, David Chandler, Christoph Dellago, and Phillip L Geissler. 2002. Transition path sampling: Throwing ropes over rough mountain passes, in the dark. Annual review of physical chemistry, 53(1):291–318.pubmed.ncbi.nlm.nih.gov/11972010
  13. [13]Amanda Cao, Ivan Betancourt, Manish Rangan, and Yuqi Sun. 2026. Optimizing importance sampling methods for rare output estimation in language models.
  14. [14]Sourav Chatterjee and Persi Diaconis. 2018. The sample size required in importance sampling. The Annals of Applied Probability, 28(2):1099–1135.arxiv.org/abs/1511.01437
  15. [15]Nicolas Chopin. 2004. Central limit theorem for sequential monte carlo methods and its application to bayesian inference. The Annals of Statistics, 32(6):2385–2411.DOI
  16. [16]Nicholas Kluge Corrêa. 2023. Aira.
  17. [17]Harald Cramér and Herman Wold. 1936. Some theorems on distribution functions. Journal of the London Mathematical Society, 1(4):290–294.
  18. [18]Juntao Dai, Xuehai Pan, Ruiyang Sun, Jiaming Ji, Xinbo Xu, Mickel Liu, Yizhou Wang, and Yaodong Yang. 2024. Safe rlhf: Safe reinforcement learning from human feedback. In International Conference on Learning Representations (ICLR).arxiv.org/abs/2310.12773

Paper details

vlnc:W_m7jq324w
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents