[WP34S] ChiSquare Distribution

01112014, 01:37 PM
Post: #1




[WP34S] ChiSquare Distribution
Hello,
I was comparing output between HP48sx MATHLIB [Ref. 1] and WP34S (v 3.2 3375) for some of the probability distributions. For this purpose, I was replicating the examples from the HP21S user's manual (pg. 4252) I noticed that the Inverse of the ChiSquare distribution for the WP34S does not seem (to me) to produce the same result as what is listed in the 21S manual pg. 50. Degrees of freedom = 19 Upper tail probability = 0.001 Keystrokes on WP34S: 19 [STO] J 0.001 [h] [PROB] [ϰ²INV] This results in 5.4068 (in FIX4) The 21S example on pg. 50 of its user manual gives a result of 43.8202 (function name is ϰ²p). This is also consistent with HP48sx MATHLIB function IUTPC(19, 0.001). For reference, the IOP for the v3.2 manual of the WP34S indicates that ϰ²INV should be equal to ϰ²p of the 21S. Is my understanding of how to use this distribution on the WP34S incorrect? Thank you for any insights you may have! [1] HP 48SX Engineering Mathematics Library, John F. Holland  Sanjeev Visvanatha 

01112014, 03:02 PM
Post: #2




RE: [WP34S] ChiSquare Distribution
(01112014 01:37 PM)Sanjeev Visvanatha Wrote: Hello, 5.4068 is correct for 0.1%, while 43.8202 is correct for 99.9% Integrating form zero to p, the value returned must increase for growing p (see pp. 54f of the manual quoted). The HP21S calculates with the error probability instead. d:) 

01112014, 06:18 PM
Post: #3




RE: [WP34S] ChiSquare Distribution
(01112014 03:02 PM)walter b Wrote:(01112014 01:37 PM)Sanjeev Visvanatha Wrote: Degrees of freedom = 19 I tried this example on my 34s (v. 3.2 3405) and was puzzled about the execution time: about 25 seconds. So I wrote a short quickanddirty program based on my original contributions for the chisquare quantile (providing a decent initial guess, followed by a slightly modified Newton iteration), and even this simple usercode program got the result in not much more than four (!) seconds (SP), resp. 56 seconds in DP mode. The initial guess is approx. 5,6 (resp. 42,8 for p=0,999) and requires four or five iterations for SP accuracy and one or two more for DP. Another example: n = 20, p = 0,05 requires 17 s with the internal function and about 4 resp. 5 s in user code. Which leads to the question: what's going on there? Dieter 

01112014, 11:11 PM
Post: #4




RE: [WP34S] ChiSquare Distribution
The distribution code hasn't changed since we did all that work on it. Looking at the code, it boils down to doing the initial estimate and then running the Newton solver. The internal code will be running in double precision, although the convergence criteria is precision mode dependent.
Is it possible your 34S is in one of the slow clock states?  Pauli Code: XLBL"QF_CHI2" 

01122014, 02:37 PM
Post: #5




RE: [WP34S] ChiSquare Distribution
(01112014 11:11 PM)Paul Dale Wrote: The distribution code hasn't changed since we did all that work on it. Just to be sure I set it to FAST mode. But I do not think this is the problem  the point is that the iteration in user code runs so much faster than the XROM routine. Maybe there's a problem with the initial guess. Could you provide the first guesses for df=20 and p = 1E10, 0,05, 0,95 and 0,99999? Or is there a way to singlestep through the routine so that I can do this myself? The initial guess should be within approx. ±10% of the final result. Back then we also discussed a modification of the Newton iteration in that the correction term applied in each iteration should be limited to, say, ±1 or ±5%. This prevents divergence and oscillation that may otherwise occur in some cases. Dieter 

01122014, 03:13 PM
Post: #6




RE: [WP34S] ChiSquare Distribution
(01112014 03:02 PM)walter b Wrote: 5.4068 is correct for 0.1%, while 43.8202 is correct for 99.9% Integrating form zero to p, the value returned must increase for growing p (see pp. 54f of the manual quoted). The HP21S calculates with the error probability instead. Thank you for clarifying my misunderstanding. I wonder if you consider the statement on pg. 126 of the WP34S manual correct then? : "... ϰ²INV equals ϰ²p in the HP21S ..."  Sanjeev Visvanatha 

01122014, 03:16 PM
Post: #7




RE: [WP34S] ChiSquare Distribution
(01112014 06:18 PM)Dieter Wrote: Another example:In SLOW mode, this takes about 18s for me, and about 12s in FAST mode.  Sanjeev Visvanatha 

01122014, 05:57 PM
Post: #8




RE: [WP34S] ChiSquare Distribution
(01122014 03:13 PM)Sanjeev Visvanatha Wrote:(01112014 03:02 PM)walter b Wrote: 5.4068 is correct for 0.1%, while 43.8202 is correct for 99.9% Integrating form zero to p, the value returned must increase for growing p (see pp. 54f of the manual quoted). The HP21S calculates with the error probability instead. I replaced it by "... χ²INV corresponds to χ²p in the HP21S ..." and put an extra footnote on p. 55 stating: The HP21S returns Q instead of P. Thus, also the inverse works differently there. Also modified the respective IOP entries for F, t, and Φ this afternoon. Thank you for your head up. Anyway, however, those modifications will show up no earlier than with the second (revised) edition of the printed manual. Please be patient. d:) 

01122014, 07:23 PM
Post: #9




RE: [WP34S] ChiSquare Distribution
(01122014 05:57 PM)walter b Wrote: I replaced it by "... χ²INV corresponds to χ²p in the HP21S ..." and put an extra footnote on p. 55 stating: Perhaps you would consider an errata sheet for those of us that have recently purchased the current printed manual  Sanjeev Visvanatha 

01122014, 09:38 PM
Post: #10




RE: [WP34S] ChiSquare Distribution
(01122014 02:37 PM)Dieter Wrote: Maybe there's a problem with the initial guess. Could you provide the first guesses for df=20 and p = 1E10, 0,05, 0,95 and 0,99999? Or is there a way to singlestep through the routine so that I can do this myself? The initial guess should be within approx. ±10% of the final result. It should be possible. Turning trace mode on lets you single step through xrom. I'm not sure how to do this in any emulator except the console one (which you really don't want to use These programs could be typed in in user mode, which often is the best way to debug them. Yeah, the initial guess is bad for some input values: Code: 1E10 0.9454441852127046 0.9057457376233529524406618674391 Seems good for larger values and really small ones. Pretty bad at .05 however. Time for some digging into code.  Pauli 

01122014, 10:21 PM
Post: #11




RE: [WP34S] ChiSquare Distribution
(01122014 09:38 PM)Paul Dale Wrote: Turning trace mode on lets you single step through xrom. Seems we found the first error. Take a look at the initial estimates for 0,05 and 0,95. They are the same. In the center, the guess is based on the Normal estimate, so that I assume there might be a problem there. You may want to check if the Normal estimate returns the same absolute value with opposite signs for 0,05 and 0,95. Back then we discussed different possible ways to provide a good estimate for the Chi^2 quantile. So the version I tried yesterday may be different from the one that actually got implemented. But anyway, here are the results from a slightly simplified method and df=20: Code: p quantile estimate Dieter 

01132014, 01:08 AM
Post: #12




RE: [WP34S] ChiSquare Distribution
Think I found the problem. The normal quantile estimate wasn't dealing with small input probabilities properly. I don't know if this fix breaks anything else
 Pauli 

01132014, 07:55 PM
Post: #13




The sheet (was: RE: [WP34S] ChiSquare Distribution)
(01122014 07:23 PM)Sanjeev Visvanatha Wrote: Perhaps you would consider an errata sheet for those of us that have recently purchased the current printed manual OK, for you and all the others: Here are the errors found and reported in almost one year since the manual was printed. One sheet of paper will do. d:) 

01142014, 03:04 AM
Post: #14




RE: [WP34S] ChiSquare Distribution
(01132014 07:55 PM)walter b Wrote: OK, for you and all the others: Here are the errors found and reported in almost one year since the manual was printed. One sheet of paper will do.Thank you!  Sanjeev Visvanatha 

01142014, 02:33 PM
Post: #15




RE: [WP34S] ChiSquare Distribution
(01132014 01:08 AM)Paul Dale Wrote: Think I found the problem. The normal quantile estimate wasn't dealing with small input probabilities properly. I don't know if this fix breaks anything elseThe Normal estimate is crucial not only for the Normal quantile. A problem with small probabilities would cause inaccurate results with the Normal quantile as well  which I have not noticed yet. The Normal estimate distinguishes between two cases: 0,5 >= p > 0,23 and p < 0,23. So an error with small probabilities would affect every p < 0,23. That's why I wonder what exactly the problem was. Could you post the Normal estimate code before and after your last correction? Dieter 

01142014, 02:46 PM
(This post was last modified: 01142014 02:47 PM by walter b.)
Post: #16




RE: [WP34S] ChiSquare Distribution  
01142014, 03:11 PM
Post: #17




RE: [WP34S] ChiSquare Distribution
(01142014 02:46 PM)walter b Wrote: You find it at sourceforgeYes, you will find virtually anything "on the internet". #) Sorry, but I am simply not able to reliably find a certain piece of information on the sourceforge website. How could I, since I do not even know which piece of code is included in which file and in which directory? You seem to know better, so may I ask you for two direct links to the old and new code? Dieter 

01142014, 10:07 PM
Post: #18




RE: [WP34S] ChiSquare Distribution
The problem I fixed added a +/ at the end if p<.5
The number coming out was correct, just with the wrong sign. Code below. The change is to add FS? F_SMALL +/ right down at the bottom.  Pauli Code: qf_q_est:: LocR 003 

01142014, 11:05 PM
Post: #19




RE: [WP34S] ChiSquare Distribution
(01142014 10:07 PM)Paul Dale Wrote: The problem I fixed added a +/ at the end if p<.5 Thank you. But as far as I can tell the new code contains a substantial error: For p in the center (i.e. between 0,23 and 0,77) the estimate uses the code following label qf_q_mid. Then the estimate is compared with two threshold values (0,04 and 4E9), and finally label qf_q_signfix is reached where the sign gets adjusted. But at this point X does not hold the absolute value of the estimate (R_ABSZ), but 4E9 (!). The estimate is in Y. As far as I can tell there is a X<>Y, Roll down or RCL R_ABSZ missing. This error must be relatively new since otherwise the Normal quantile would not be able to return exact results  the quantile algorithm uses at most two iterations, and this only works if the initial estimate is sufficiently accurate. Or do I miss something here? Dieter 

01162014, 01:41 AM
Post: #20




RE: [WP34S] ChiSquare Distribution
Something I'll have to look into. Not going to get a good lump of time in the near future unfortunately, so this might take a while. Unless someone else wants to investigate.
 Pauli 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)