Accurate Normal Distribution for the HP67/97
|
06-26-2016, 09:34 PM
(This post was last modified: 04-22-2018 12:19 PM by Dieter.)
Post: #1
|
|||
|
|||
Accurate Normal Distribution for the HP67/97
Normal distribution for the HP67/97
This program for the HP67 and 97 evaluates various functions of the Standard Normal distribution:
Method and accuracy The Normal CDF is evaluated by two different methods. For 0 ≤ z ≤ 5 the upper-tail integral Q(z) is approximated by a (Near-)Minimax rational approximation: \(\large Q(z) \approx e^{-\frac{z^2}{2}} \cdot \frac{1+a_1z+a_2z^2+a_3z^3+a_4z^4}{2+b_1z+b_2z^2+b_3z^3+b_4z^4+b_5z^5}\) Using 10-digit coefficients the values are \(\begin{array}{ll} a_1=0,7981006015 & b_1=3,191970353\\ a_2=0,3111510842 & b_2=2,169125520\\ a_3=0,06328636234 & b_3=0,7932255604\\ a_4=0,005716530175 & b_4=0,1587036976\\ & b_5=0,01432712100 \end{array} \) If evaluated with sufficient precision, the relative error over the given domain is less than 2,6 E–10. With a few more digits the error can be reduced to approx. 2,41 E–10. For z > 5 the well known continued fraction expansion is applied, here with 8 terms: \(\large Q(z) \approx \frac{1}{\sqrt{2 \pi}} \cdot e^{-\frac{z^2}{2}} \cdot \cfrac{1}{z+\cfrac{1}{z+\cfrac{2}{z+\cfrac{3}{z+\dotsb}}}}\) The expression is calculated from right to left, starting not with 8/z but 8/(z+1,38) which significantly improves the resulting accuracy for smaller z. This way the relative error for z > 5 stays below 1 E-10 – provided the calculation is performed with sufficient precision. Due to the HP67/97's limitation to 10 significant digits and some numeric pitfalls the actual results on the HP67/97 will be less accurate. Since it is virtually impossible to verify the results over the complete domain I can only say that according to my results usually 9 significant digits (±1 unit) are achieved. See below for two exceptions. If you find substantially larger errors, please report here. The algorithm for the inverse (quantile function) first calculates a rough estimate by means of a simple rational approximation with an error of about ±0,002. The error of this first approximation is taylored for the following correction step that provides the final result. This is a very effective third order extrapolation due to Abramovitz & Stegun (1972) p. 954. With sufficient precision this method is good for about 11 significant digits over the calculator's complete working range down to 1 E–99. Again, the actual accuracy on the 67/97 is less and may drop to about 9 digits. But there is an exception: due to digit cancellation results very close to zero carry less significant digits, e.g. the quantile for a probability of 0,50003 is calculated as 7,5199 E–5. In such cases usually the remaining digits are fine, maybe within ±1 unit tolerance. So the result in FIX DSP 9 (0,000075199) should be OK. A similar limitation applies to the two-sided CDF for arguments very close to zero. Here you should not expect more than what you see in FIX DSP 9 mode (±1 digit). Evaluating the PDF seems trivial, but accuracy may degrade significantly for large arguments of the exponential function. For example e-1000/7 = 9,076766360 E-63, but the 67/97 returns 9,076765971 E-63. The error is caused by the fact that the fractional part of the argument carries only seven decimals which leads to an accuracy of merely seven significant digits. That's why the PDF is evaluated in a different way that requires three calls of ex, but achieves better accuracy. The program Here comes the listing. Code: 001 LBL B The program expects the coefficients of the rational approximation in R1...R9. If it runs on a real (hardware) 67/97 this can be done by preparing a (single track) data card. The values for the constants have already been mentioned. Be sure to enter all ten digits: Code: R1 = 7,981006015 E-01 The coefficients of the simple rational approximation for the quantile estimate are part of the program code. Of course they can just as well be stored in, say, the secondary registers S0...S3 and recalled from there. This will shorten the program and make the quantile calculation a tiny bit faster, but it requires a double-track data card. Usage Calculate the cumulative distribution function: z [A] The lower tail CDF P(z) is returned in X, the upper tail CDF Q(z) in Y. Calculate the symmetric two-sided cumulative distribution function: z [B] The two-sided CDF A(z) is returned in X, the complement 1–A(z) in Y. Calculate the quantile for a given lower-tail probability p: p [C] Calculate the quantile for a given two-sided symmetric probability p: p [D] Calculate the probability distribution function Z(z): z [E] Some examples In a soda water factory a machine fills bottles with an average volume of 503 ml. The content of the bottles varies slightly with a standard deviation of 5 ml. Determine the probability that a random soda bottle contains less than 490 ml. First calculate the Standard Normal variable z: Code: 490 [ENTER] 503 [-] 5 [÷] -2,600000000 Now compute the lower tail CDF: Code: [A] 0,004661188 So only 0,47% of all bottles will contain less than 490 ml while 99,53% exceed this volume. How much of the production will fall within ±10 ml around the mean volume? ±10 ml equals ±2 standard deviations. Code: 2 [B] 0,954499736 In which interval around the mean will 98% of the production fall? So we are here looking for the two-sided quantile. Code: 0,98 [D] 2,326347874 The tolerance interval is ±2,326 standard deviations. In absolute milliliters this is... Code: 5 [x] 11,63173937 So 98% of the production is within 503 ± 11,63 ml. In the above example all digits displayed in FIX DSP 9 mode are exact. Here are some other results and their accuracy: Code: Q(0,01) = 0,4960106435 (-2 ULP) Cave: this does not mean that this accuracy level can be guaranteed. I have not found a case where the result was not within 1 unit in the 9th place, but please do your own tests. As usual, remarks, corrections and error reports are welcome. Finally, here are two (zipped) files for use with the Panamatic HP67 emulator. The first version implements the program listed above, the second version has the coefficients of the quantile estimate in registers S0...S3 and thus is a bit shorter. Dieter NormDist_67.zip (Size: 1.21 KB / Downloads: 28) |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)