Origins of HP41 numerical routines ot compute statistical distributions


This is more of a theoretical question.

In the Stat Pac module there is a routine, SigmaNormd, that computes a number of features relevant to the standard normal distribution--i.e., actual value of the density function for a given z, upper-tail prob of given z, z of a given upper tail probability.

On inspecting the code, it looks like the probability density function is approximated algebraically in some way--for example, it looks like there are a number of numeric constants stored in the first few registers on initialization of the routine, and these get recalled later as the relevant estimates are computed. In other words, it doesn't look as though the program works directly with the actual density function (i.e., (1/sqrt(2*Pi))*exp(-z^2/2)).

Does anyone know the origins of this routine? Is it from Numerical Recipes? Is it based on a Taylor series approximation? (As for the latter, I have found on my HP48 that one has to go to a pretty high order in order for the polynomial approximation to maintain accuracy in the upper and lower tails. In emulation this is fast enough, but on the actual calculator its as slow as molasses.)

This is more of a point of curiosity than of great practical relevance, but if anyone knows what the actual formulae used are, and their source, I would be much obliged.



Just posting to correct typo in subject line.


From what you say, it looks like a polynomial approximation of the type described by

C. Hastings, Jr. Approximations for Digital Computers, Princeton University Press, 1955.

Can you tell us the values of the constants?

Please let us know if you find more about this.


Edited: 8 May 2006, 5:07 p.m.


I won't bore you with all the digits.

From what I can see, the routine has some similarities to a nested routine in Numerical Recipes to compute the complementary error function, erfc, which of course is intimately related to the cumulative normal distribution. The coefficients used there are not the same, but that doesn't matter to me--the bottom line is that I have discerned that there is something a bit more complex going on than a simple Taylor/Maclaurin series expansion.

Ah, I wish I knew more about approximation theory--Chebyshev polynomials, Fourier series, etc.



Les Wright wrote:

Ah, I wish I knew more about approximation theory--Chebyshev polynomials, Fourier series, etc.

Les, take a look at . I notice one went on eBay just recently for only $5.


--- Les Bell



I can't help but to respond to your last posting on Jon M. Smith's book "Scientific Analysis on the Pocket Calculator". It rang a bell (no pun to you). I hadn't used it in years, but I found it in a dusty corner, 1977 2nd ed. And gee it's only worth five bucks.



Yes - I have a copy on the bookshelf, myself. Bought it in 1976, just after I got my '45. It was very useful!


--- Les Bell


Actually, the code does use the exact function for determining the ordinate of the actual density function, but seems to use an interesting approximation when dealing with the cumulative distribution, since it is related to the error function erf(x) which does not have an exact indefinite integral.

I totally forgot that I have both Numerical Recipes in Pascal (the book) and Numerical Recipes in C (the code files), both purchased some years ago. The following routine estimates complementary error function erfc(x), for nonnegative x. For negative x, compute erfc(|x|) and subtract from 2. To get erf(x) subtract erfc(x), whether x is positive or negative, from unity. The relationship between erf(x) and the cumulative normal dist. has to do with scaling factors placed upon the argument and the overall function itself.

Here is that code. For those who don't recognize the syntax, it is cut and paste from Maple:


The constants and the routine in the code I was wondering about doesn't look quite like this, BUT I can discern that something like this is going on.

I will gladly look into that classic book recommended in this thread.



Your "density function" (1/sqrt(2*Pi))*exp(-z^2/2) may be only be integrated anlytically by squaring the function, integrating and takeing the square root of the result. Integrating numerically may be converging too slow. More in here.



The equations to calculate the popular probability distribution functions and their inverse are found in the Hadbook Of Mathematical Functions.

In my book "Mathematical Algorithms in Visual Basic for Scientists & Engineers" I show you libraries that calculate these functions and their inverses. You can still find a copy of this book in Amazon. Search for "Used Book" after you select the Books category, since the book is about 10 years old. In my book I show you the pseudo-code AND the actual code in Visual Basic". I also wrote a C+++ version of the book.

Namir Shammas


Though I understand I cannot identify the source, I did take a look at the Business Stats solution book on a certain website. It uses a similar routine (at least the code looks similar) to that in the Finance Pac, and quotes the Handbook of Mathematical Functions as the source. So too does Numerical Recipes.

The Handbook is in a Dover reprint and is readily available in Canada for under $40, so maybe I will get it new or used. I will all keep a look out for the Bell book (one eBay seller offers it for $2 but doesn't ship outside of the US) and Namir's book.




Check a orevious (or archived) post. Someone on this site has posted a link to the PDF version of the Handbook of Mathematical Functions.


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP Prime numerical restrictions? Alasdair McAndrew 4 1,273 11-16-2013, 05:32 PM
Last Post: Alasdair McAndrew
  HP Prime numerical precision in CAS and HOME Javier Goizueta 5 1,577 11-16-2013, 03:51 AM
Last Post: Paul Dale
  Best statistical fit Richard Berler 8 1,935 10-30-2013, 11:25 PM
Last Post: Walter B
  [HP-Prime] AMBIGUITY between Numerical Calculation (HOME) and Numerical/Symbolic Calculation (CAS mode) CompSystems 2 1,043 08-18-2013, 07:06 PM
Last Post: CompSystems
  OT: My brain is failing me again. Help with numerical / mechanical problem required. Harald 4 1,338 07-01-2013, 10:31 AM
Last Post: Harald
  HP 50g Takes hours to compute a convergent series Matthew Richards 10 2,262 05-14-2013, 03:30 PM
Last Post: Thomas Klemm
  Statistical Question Namir 3 1,025 12-22-2012, 03:39 AM
Last Post: Bruce Larrabee
  OT - Arduino In Space from HP41 Hacker PeterP 3 1,251 06-16-2012, 10:43 PM
Last Post: PeterP
  [WP34s] Inverse discrete probability distributions Eduardo Duenez 11 2,538 05-17-2012, 12:31 PM
Last Post: fhub
  HP-35A/21 D.MS<-->Decimal degrees routines Matt Agajanian 9 2,169 03-29-2012, 11:03 AM
Last Post: Matt Agajanian

Forum Jump: