## Overflow

A good general-purpose formula for describing pensioner mortality rates is the logistic function:

q = exp(α) / (1 + exp(α))

where the value of α varies by age.  This particular formula arises when using logistic regression, a type of generalised linear model (GLM).  Any mathematician looking at the equation above will see that the value of q tends to 1 as α increases.  This is demonstrated in Table 1 for various increasing values of α.

Table 1. Evaluation of the logistic function for various values of α.

αLogistic function
50.993307149
100.999954602
150.999999694
200.999999998
251

So far so good: as α increases, the value of the logistic function approaches 1 as expected.  At α=25 the logistic function is around 1e13 different from 1, which is so close that the answer is just 1 when shown to nine decimal places in Table 1 above.

Now try this in Microsoft Excel or any other PC calculator.  In particular, try the value α=710.  Instead of getting the answer 1, you will probably see that the calculation has failed and the result will be something like #NUM!.  The problem lies in how computers store real numbers.  Machine arithmetic typically dedicates a fixed amount of space to hold an approximation of a real number.  In order to keep things compact and relatively fast, this involves various compromises.  As a result there is a limit to how large a number can be and still be stored.  If a number is larger than this during any intermediate calculation, overflow occurs and the whole calculation fails.  This happens even if the final result of the whole formula is well within the computer's arithmetical range.  In the case of the double-precision standard being used by Microsoft Excel, any intermediate value which exceeds 10^320 will overflow and the entire calculation will fail.

The solution lies in careful programming.  For example, the formula at the top of this post can be re-written as follows:

q = 1 / (1 + exp())

Although this formula is mathematically equivalent to the one at the top, in terms of computer arithmetic it is far more robust for large, positive values of α.  For example, if you set α=710 this new formula will yield 1 in Microsoft Excel instead of the #NUM! error previously.

Observant readers will note that this new formula will itself fail when α=-710 and below.  The complete solution is therefore to use the formula at the top of the page when α is negative, and to use the second formula when α is positive.  This allows evaluation of the logistic function over the entire real line without error.