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
5 0.993307149
10 0.999954602
15 0.999999694
20 0.999999998
25 1

 

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.

 

Written by: Stephen Richards
Publication Date:
Last Updated:

Previous posts

(Not) Falling for the fallacy

An important concept is demography is the ecological fallacy.  This is where aggregate data for a group are used to draw erroneous inferences about individuals belonging to the group. 
Tags: Filter information matrix by tag: ecological fallacy, Filter information matrix by tag: smoking, Filter information matrix by tag: geodemographics, Filter information matrix by tag: asbestos

Where there's smoke...

Amongst its other claims to fame, Scotland produced one of the earliest prominent anti-smoking campaigners — our very own King James VI was an early opponent of tobacco consumption and smoking
Tags: Filter information matrix by tag: smoking, Filter information matrix by tag: lung cancer, Filter information matrix by tag: Scotland

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.