Why Numerical Stability Matters
Fine-mapping calculations can involve likelihood ratios that are extremely small or large. A computer may represent such quantities as floating-point numbers with a limited range and finite precision. Directly exponentiating a large log Bayes factor can overflow; exponentiating a very negative one can underflow to zero. Either problem can corrupt normalized posterior probabilities even when the mathematical calculation is well defined.
The goal here is to preserve the intended calculation while changing how it is implemented. Numerical stabilization does not change the model, the priors, or the evidence. It reduces avoidable computational error in converting those inputs into posterior probabilities and cumulative credible-set mass.
Log Weights and Posterior Normalization
Suppose a finite set of mutually exclusive and exhaustive configurations \(c=1,\ldots,K\) is under consideration. Let \(\pi_c\) be the prior probability of configuration \(c\), let \(BF_c\) be its Bayes factor relative to a common reference model, and assume \(\pi_c\geq 0\), \(BF_c>0\), and \(\sum_{c=1}^K\pi_c=1\). Bayes' rule gives the posterior probability of configuration \(c\) conditional on the data \(D\). The common reference factor cancels in the normalization.
The direct expression can be unstable when \(a_c\) is far outside the range where floating-point exponentiation is reliable. Instead, let \(m=\max_c a_c\), subtract this common value, and exponentiate only the resulting nonpositive differences. Define the log-sum-exp operation as follows.
It follows that the log posterior probability and posterior probability can be computed as
This procedure is stable against overflow in the final normalization because \(a_c-m\leq 0\), so no exponential exceeds 1. Very small terms can still underflow to zero, but only when their contribution is smaller than the computer can represent; they cannot displace the largest term or make the denominator overflow.
Why Subtracting the Maximum Works
The justification is the invariance of a normalized ratio to a common multiplicative factor. Since \(w_c=e^{a_c}\), multiplying every weight by \(e^{-m}\) leaves every ratio unchanged:
Because \(m\) is the largest log weight, \(a_j-m\leq 0\) for all \(j\). Thus each term in the stabilized sum lies between 0 and 1, and at least one term equals 1. The denominator is consequently at least 1, avoiding the particularly dangerous case in which all exponentiated weights vanish. This derivation requires no approximation: only finite-precision arithmetic in the actual implementation introduces numerical error.
Worked Example: Normalize Moderate Log Weights
Suppose four candidate configurations have log weights \(a=(0,-1,-2.5,-4)\). The largest value is \(m=0\). The stabilized unnormalized weights are
\(e^0=1\), \(e^{-1}=0.3678794\), \(e^{-2.5}=0.0820850\), and \(e^{-4}=0.0183156\). Their sum is \(1.4682801\).
The normalized posterior probabilities are therefore
\(1/1.4682801=0.6811\), \(0.3678794/1.4682801=0.2506\), \(0.0820850/1.4682801=0.05591\), and \(0.0183156/1.4682801=0.01247\), to four significant figures.
The probabilities sum to approximately 1, with any small discrepancy due to rounding. The second probability is \(e^{-1}/(1+e^{-1}+e^{-2.5}+e^{-4})\), approximately \(0.2505513\), which rounds to \(0.2506\) at four decimal places.
Extreme Values: Avoiding Overflow
The same method is especially useful when all log weights are large. A large log weight need not indicate a problem with the posterior: posterior probabilities depend on differences between log weights, not on their common level. Exponentiating the original values may be impossible even though the normalized probabilities are ordinary numbers.
Worked Example: Log Weights Too Large to Exponentiate Directly
Suppose three configurations have log weights \(1000\), \(999\), and \(995\). Directly evaluating \(e^{1000}\), \(e^{999}\), or \(e^{995}\) exceeds the range of common floating-point formats. Set \(m=1000\). The stabilized weights are \(1\), \(e^{-1}=0.3678794\), and \(e^{-5}=0.006737947\), with sum \(1.3746174\).
The posterior probabilities are \(1/1.3746174=0.7275\), \(0.3678794/1.3746174=0.2676\), and \(0.006737947/1.3746174=0.004901\), to four significant figures. The probabilities sum to approximately 1. The common level of 1000 has disappeared; only the relative log weights, \(0,-1,-5\), determine the result.
If a log weight is \(-\infty\), its corresponding weight is exactly zero. It is safe to assign that configuration posterior probability zero, provided at least one other configuration has a finite log weight. If every log weight is \(-\infty\), the normalization is undefined and signals invalid or incompatible inputs; it must not be reported as a posterior distribution.
Credible-Set Mass and Accumulation
A credible set is formed from posterior probability assigned to candidate variants or other relevant events. For a single-causal-variant posterior over candidates \(1,\ldots,J\), let \(p_j\) be the posterior probability for candidate \(j\), with \(p_j\geq0\) and \(\sum_j p_j=1\). Sort candidates so that \(p_{(1)}\geq p_{(2)}\geq\cdots\geq p_{(J)}\). For a target level \(q\), such as 0.95, include the fewest highest-probability candidates whose cumulative mass reaches or exceeds \(q\). In settings where candidate probabilities are obtained by summing configuration probabilities, perform that aggregation before ranking candidates.
Use unrounded probabilities for ranking and accumulation. Rounding each term first can change the order of near-ties or move a cumulative sum across the target. For a long list, compensated summation or extended precision can reduce accumulated floating-point error. When probabilities are extremely small, an alternative is to accumulate their log probabilities with the two-term log-sum-exp operation, rather than exponentiating them first.
Worked Example: Find a Credible Set Without Premature Rounding
Use the four posterior probabilities from the first example, in descending order: approximately \(0.681069\), \(0.250551\), \(0.055906\), and \(0.012474\). For a 95% target, the first candidate alone has mass \(0.681069\), and the first two have cumulative mass \(0.931620\), both below 0.95. Adding the third gives \(0.987526\), which reaches the target. The 95% credible set therefore contains the first three candidates.
The cumulative mass is the sum of the posterior probabilities of the included candidates; it is not necessarily exactly 0.95 because candidates are added as whole units. The result is the smallest set in this ranking whose mass is at least the chosen target. Reporting the rounded probabilities as \(0.6811\), \(0.2506\), and \(0.05591\) is appropriate after the calculation, but using those rounded values as inputs to the calculation is not.
Practical Checks and Failure Modes
A stable formula cannot rescue incorrect inputs. Before interpreting the output, check that the candidate configurations are the intended mutually exclusive and exhaustive set, the priors are nonnegative, and prior-weighted Bayes factors correspond to the same data and reference model. The log-sum-exp step addresses the numerical normalization; it does not verify those statistical assumptions.
- Overflow in direct exponentiation: intermediate weights become infinite even though their normalized ratios are finite. Subtract the maximum log weight before exponentiating.
- Underflow of small terms: a tiny weight may become zero. If its true contribution could affect a reported result, retain log probabilities and use log-sum-exp, or use higher precision.
- Unstable cancellation in a log Bayes factor: obtaining a log Bayes factor by subtracting two very large, nearly equal log marginal likelihoods can lose significant digits. Use a stable analytic expression or a numerical method designed for that difference, and compare precision when results are consequential.
- Rounding before ranking or summing: rounded values can alter ties and threshold decisions. Keep full precision internally and round only for display.
- Invalid normalization: posterior probabilities should be finite, nonnegative, and sum to 1 within numerical tolerance. A zero or non-finite denominator requires investigation, not an arbitrary rescaling.
For modest log weights, direct exponentiation and normalization may be adequate and easy to inspect. For a wide range of log weights, log-sum-exp is the preferred default. Higher-precision arithmetic can help when many terms or very small probability differences matter, but it is slower and does not correct model misspecification or an inaccurately computed Bayes factor. These approaches differ in numerical behavior, not in the posterior distribution they are meant to represent.
Check Your Understanding
Try each question, then compare your work with the solutions below.
- For log weights \(7,5,2\), what stabilized weights should be summed?
- Why does subtracting the largest log weight preserve the normalized posterior probabilities?
- A ranked list has probabilities \(0.52,0.28,0.15,0.05\). How many candidates are needed for a 90% credible set, and what is its mass?
- What should be investigated if every log weight is \(-\infty\)?
Solutions. (1) The maximum is 7, so subtracting it gives log weights \(0,-2,-5\); the stabilized weights are \(1,e^{-2},e^{-5}\), approximately \(1,0.1353,0.006738\). (2) Subtracting the same value \(m\) multiplies every original weight by \(e^{-m}\), and this common factor cancels between numerator and denominator. (3) The first two sum to \(0.52+0.28=0.80\), below 0.90; adding the third gives \(0.95\). Thus three candidates are needed, and their credible-set mass is 0.95. (4) The denominator has no positive finite weight, so normalization is undefined. Check the inputs, prior support, and evidence calculations rather than reporting a posterior distribution.