Tutorials › Generalized Linear Models › Stable Logistic Probabilities and Log Probabilities

Initialization and Numerical Stability · Tutorial 223 of 981

Stable Logistic Probabilities and Log Probabilities

Learn how to compute logistic probabilities and log probabilities safely across both signs of the linear predictor.

Intermediate 11 min read

What You'll Learn

  • Evaluate the logistic probability using a formula matched to the predictor’s sign
  • Compute both complementary probabilities without subtracting a rounded value from one
  • Derive stable log-probability formulas using log1p
  • Understand what floating-point underflow and rounding still limit
  • Recognize why taking a logarithm after computing a probability can give the wrong result

Why Logistic Probabilities Need Stable Formulas

For a finite linear predictor \(\eta\), the logistic model assigns probability \(p\) to a Bernoulli outcome of one. In exact arithmetic, the defining expression is simple. In floating-point arithmetic, however, the direct formula can overflow for a large negative \(\eta\), while computing a small complementary probability as \(1-p\) can lose all its significant digits. Taking the logarithm of a probability after it has rounded to zero or one can then produce an infinite or inaccurate result.

The remedy is to use algebraically equivalent formulas chosen according to the sign of the predictor. This tutorial develops those formulas for both probabilities and their logarithms. The predictor \(\eta\) is assumed to be a finite real number; numerical error in forming the predictor itself is a separate issue.

Definition: The logistic function is \(\sigma(\eta)=1/(1+\exp(-\eta))\). Write \(p=\sigma(\eta)\) and \(q=1-p\), so \(p\) and \(q\) are the probabilities of outcomes one and zero, respectively. The function \(\log1p(x)\) denotes \(\log(1+x)\), evaluated by a numerical routine designed to remain accurate when \(x\) is small.

Stable Formulas for Both Predictor Signs

A direct evaluation of \(1/(1+\exp(-\eta))\) requires exponentiating \(-\eta\). If \(\eta\) is a large negative number, this exponent is large and positive, so the exponential may overflow even though the desired probability is well defined. For \(\eta<0\), multiply the numerator and denominator by \(\exp(\eta)\); this gives a formula involving only \(\exp(\eta)\), which cannot overflow. For \(\eta\geq 0\), the original exponential \(\exp(-\eta)\) is safe from overflow.

Theorem: For every finite real \(\eta\), the probabilities can be evaluated by the following sign-specific formulas:
$$ \begin{array}{c|cc} & p & q\\ \hline \eta\geq 0 & \displaystyle\frac{1}{1+\exp(-\eta)} & \displaystyle\frac{\exp(-\eta)}{1+\exp(-\eta)} \\[1.2em] \eta<0 & \displaystyle\frac{\exp(\eta)}{1+\exp(\eta)} & \displaystyle\frac{1}{1+\exp(\eta)} \end{array} $$
These formulas avoid exponentiating a large positive number. They also compute the smaller probability directly rather than obtaining it by subtracting the larger probability from one.

Proof. When \(\eta\geq 0\), the defining expression for \(p\) uses \(\exp(-\eta)\), whose exponent is nonpositive. Since \(q=1-p\), putting the terms over a common denominator gives \(q=\exp(-\eta)/(1+\exp(-\eta))\). When \(\eta<0\), multiply the numerator and denominator of \(p=1/(1+\exp(-\eta))\) by \(\exp(\eta)\). This yields \(p=\exp(\eta)/(1+\exp(\eta))\). Subtracting this expression from one gives \(q=1/(1+\exp(\eta))\). Here \(\eta<0\), so the exponent in \(\exp(\eta)\) is negative. Thus neither branch requires exponentiating a positive number. \(\square\)

Worked Example: Evaluating Both Probabilities at a Negative Predictor

Let \(\eta=-2\). Since this is negative, use \(t=\exp(-2)\approx 0.1353353\) in the negative-predictor branch. The probabilities are

$$ p=\frac{t}{1+t} =\frac{0.1353353}{1.1353353} \approx 0.1192029, \qquad q=\frac{1}{1+t} =\frac{1}{1.1353353} \approx 0.8807971. $$

As a check, the values sum to approximately \(0.1192029+0.8807971=1.0000000\). The calculation never requires \(\exp(2)\), the potentially unsafe exponential in the direct expression for this negative predictor. It also computes the small probability \(p\) directly.

Stable Log Probabilities

For a Bernoulli observation, the relevant log probabilities are \(\log(p)\) and \(\log(q)\). Computing either as the logarithm of a rounded probability can fail. For example, if a computed \(p\) rounds to exactly one, then evaluating \(\log(1-p)\) gives \(\log(0)=-\infty\), even though the exact \(q\) is positive for every finite \(\eta\).

The same sign-specific strategy gives formulas for the log probabilities. The use of \(\log1p\) matters: when \(x\) is very small, a routine that first rounds \(1+x\) may return exactly one and lose the small, nonzero value of \(\log(1+x)\).

Result: For every finite real \(\eta\), the log probabilities are
$$ \begin{array}{c|cc} & \log(p) & \log(q)\\ \hline \eta\geq 0 & -\log1p\!\left(\exp(-\eta)\right) & -\eta-\log1p\!\left(\exp(-\eta)\right) \\[0.6em] \eta<0 & \eta-\log1p\!\left(\exp(\eta)\right) & -\log1p\!\left(\exp(\eta)\right) \end{array} $$

Derivation. For \(\eta\geq 0\), the probability formulas have common denominator \(1+\exp(-\eta)\). Taking logarithms gives \(\log(p)=-\log(1+\exp(-\eta))\) and \(\log(q)=-\eta-\log(1+\exp(-\eta))\). For \(\eta<0\), the denominator is \(1+\exp(\eta)\). Taking logarithms gives \(\log(p)=\eta-\log(1+\exp(\eta))\) and \(\log(q)=-\log(1+\exp(\eta))\). In both cases the exponential argument is nonpositive, and replacing \(\log(1+x)\) by \(\log1p(x)\) preserves accuracy when \(x\) is small. \(\square\)

Worked Example: Log Probabilities at a Large Positive Predictor

Let \(\eta=40\). The quantity \(t=\exp(-40)\) is approximately \(4.248354\times 10^{-18}\). The stable log formulas give

$$ \log(p)=-\log1p(t)\approx -4.248354\times10^{-18}, \qquad \log(q)=-40-\log1p(t)\approx -40. $$

In double-precision arithmetic, \(p=1/(1+t)\) can round to exactly \(1\), because \(t\) is too small to change the stored value of one. Then calculating \(\log(1-p)\) gives negative infinity. The direct formula for \(q\), however, gives a value near \(4.248354\times10^{-18}\), and the stable log formula gives a finite log probability near \(-40\). The tiny correction \(-\log1p(t)\) to \(\log(q)\) is too small to affect its displayed value at this precision, but the leading term is retained.

One Formula for the Logarithms

The two branches can also be summarized with the softplus function, defined by \(\operatorname{softplus}(x)=\log(1+\exp(x))\). Directly evaluating this expression can overflow for a large positive \(x\). A stable identity is

$$ \operatorname{softplus}(x) = \max(x,0)+\log1p\!\left(\exp(-|x|)\right). $$

To verify the identity, first suppose \(x\geq 0\). Factoring \(\exp(x)\) out of \(1+\exp(x)\) gives \(\log(1+\exp(x))=x+\log(1+\exp(-x))\). If \(x<0\), then \(\max(x,0)=0\) and \(|x|=-x\), so the right-hand side is \(\log(1+\exp(x))\) directly. In both cases the exponent being evaluated is nonpositive. It follows that

$$ \log(p)=-\operatorname{softplus}(-\eta), \qquad \log(q)=-\operatorname{softplus}(\eta). $$

These compact expressions are useful when implementing functions for log probabilities. The sign-specific formulas above show how to evaluate them without introducing an unsafe exponential.

Worked Example: A Very Negative Predictor

Let \(\eta=-1000\). The exact probability \(p\) is positive but far smaller than the smallest positive number representable in ordinary double precision. In the stable probability branch, \(\exp(-1000)\) underflows to zero, so the computed \(p\) may be zero and \(q\) may round to one. Those probability outputs cannot preserve the tiny values.

The log formulas behave differently. They give

$$ \log(p)=-1000-\log1p\!\left(\exp(-1000)\right)\approx -1000, \qquad \log(q)=-\log1p\!\left(\exp(-1000)\right)\approx 0. $$

The first value remains informative: it preserves the scale of an extremely small probability even when that probability itself cannot be represented. The second log probability is a tiny negative number in exact arithmetic and may round to zero in finite precision. Stable formulas prevent avoidable overflow and cancellation; they cannot make an unrepresentable number representable.

What Stability Does—and Does Not—Guarantee

For \(t\geq 0\), the correction term \(\log1p(\exp(-t))\) is nonnegative and at most \(\exp(-t)\). Indeed, for any \(u\geq0\),

$$ 0\leq \log(1+u)=\int_0^u \frac{1}{1+s}\,ds\leq \int_0^u 1\,ds=u. $$

Taking \(u=\exp(-t)\) proves the bound. Consequently, when \(\eta\geq0\), \(\log(p)\) lies between \(-\exp(-\eta)\) and zero, while \(\log(q)\) differs from \(-\eta\) by a nonpositive correction of magnitude at most \(\exp(-\eta)\). This explains why the log probability of the less likely outcome is approximately \(-|\eta|\) at an extreme predictor. The corresponding statements for negative \(\eta\) follow by exchanging \(p\) and \(q\).

In applications, use the log probability appropriate to the observed outcome directly: \(\log(p)\) for outcome one and \(\log(q)\) for outcome zero. This is especially important when the result contributes to a Bernoulli or binomial log-likelihood. If both probabilities are needed, calculate them with the sign-specific probability formulas rather than relying on \(q=1-p\) when \(p\) is near one, or \(p=1-q\) when \(q\) is near one.

Common Mistakes

  • Using the direct logistic formula for every \(\eta\). For a large negative predictor, \(\exp(-\eta)\) can overflow. Use the negative-predictor branch, which evaluates \(\exp(\eta)\) instead.
  • Computing the small probability by subtraction. If \(p\) rounds to one, \(1-p\) may be zero even though the exact \(q\) is positive. Evaluate \(q\) directly using its branch-specific formula.
  • Taking a logarithm after rounding the probability. A rounded zero or one can turn a finite log probability into negative infinity or zero. Compute the log probability from the stable log formulas.
  • Expecting stability to eliminate underflow. If an exact probability is smaller than the representable range, its floating-point value may still be zero. A stable log probability can retain useful information about its magnitude.
Key takeaway: Match the logistic formula to the sign of \(\eta\), compute each probability directly, and use \(\log1p\) formulas for log probabilities. These choices avoid overflow and much avoidable loss of precision, while respecting the limits of floating-point representation.

Check Your Understanding

Try these questions using the sign-specific formulas and the distinction between probability rounding and log-probability evaluation.

  1. For a large positive \(\eta\), which exponential is used to compute \(q\) directly, and why is \(1-p\) a risky alternative?
  2. Write the stable formulas for \(\log(p)\) and \(\log(q)\) when \(\eta<0\).
  3. Explain why the direct expression \(1/(1+\exp(-\eta))\) can overflow when \(\eta\) is a large negative number.
  4. If a computed probability rounds to zero, can its stable log probability still be finite? Explain with the case \(\eta=-1000\).
  5. Why is \(\log1p(x)\) preferable to first computing \(1+x\) and then taking its logarithm when \(x\) is very small?
Clinical Biostats