Advanced sampling and rare eventsLesson 65 of 76 · 8 min

Keep weights stable on a log scale

Normalise extreme log weights without numerical underflow.

Floating-point arithmetic has a finite range. Expanding very negative log weights can underflow, rounding them all to zero before their ratios are formed. Very large exponentials can overflow to infinity. Log weights record the logarithms of positive weights so ratios can be handled before exponentiation.

logsumexp(l) means log(sum(exp(l))). It computes this stably by subtracting the largest log value m first, then returning m+log(sum(exp(l−m))). For [-1001,-1000,-999], m=-999 leaves [-2,-1,0], whose exponentials are representable. Divide those exponentials by their sum: approximately [.090,.245,.665]. A common shift cancels from normalised weights.

The code uses exp(l−logsumexp(l)), the same normalisation in one expression. This repairs arithmetic, not poor target/proposal overlap. For an ordinary importance estimate with known normalising constant, preserve its original contribution scale. A self-normalised estimate instead divides weighted function values by total weight; the next lessons examine that different ratio estimator.

ℓi\ell_i is log weight i, z is logsumexp of all log weights, and w~i\widetilde w_i is its positive weight divided by the total. In code, dot multiplies corresponding entries and adds them, giving the weighted sum.

Keep relative weights when absolute values underflow
Weight 1 · 0.09Weight 2 · 0.245Weight 3 · 0.665
Weight 1 · 0.09Weight 2 · 0.245Weight 3 · 0.665
w~i=exp⁡(ℓi−logsumexp⁡(ℓ))\widetilde w_i=\exp(\ell_i-\operatorname{logsumexp}(\ell))
z=logsumexp⁡(ℓ)w~i=eℓi−z\begin{gathered}z=\operatorname{logsumexp}(\ell)\\\widetilde w_i=e^{\ell_i-z}\end{gathered}
Reference illustration. Actual stable normalised weights from the constructed log values −1001, −1000 and −999. Subtracting log-sum-exp preserves relative weights; direct exponentials underflow. The displayed weights sum to one.

Should adding the same constant to every log weight change their normalised proportions?

Write a prediction (optional)

Constructed log-weight example

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Direct exponential sum: 0.0
Stable normalised weights: [0.090031 0.244728 0.665241]
Weight sum: 1.0
Weighted average of [1,2,3]: 2.57521

Read the first result

The stable calculation recovers meaningful relative weights even though direct exponentials all become zero.

Try an edit

Replace [-1001.0, -1000.0, -999.0] with [-1000.0, -1000.0, -1000.0]. Select Run Python again. Compare Direct exponential sum, Stable normalised weights and Weighted average of [1,2,3]. What weights should a set of three equal inputs produce?

Back to the Python editor

Scope. The demonstration concerns floating-point arithmetic on supplied weights, not validation of an underlying probability model.

Try a fresh case

Does stable log arithmetic ensure that importance sampling has explored every important region?

Choose an answer
Need a hint?
Hint 1

Separate the values stored in memory from the states visited.

Hint 2

Ask whether a stabilised weight can represent a state never sampled.

Your notes