[WP-34S] Chi-Square Distribution - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: Not HP Calculators (/forum-7.html) +--- Forum: Not quite HP Calculators - but related (/forum-8.html) +--- Thread: [WP-34S] Chi-Square Distribution (/thread-401.html) |
[WP-34S] Chi-Square Distribution - Sanjeev Visvanatha - 01-11-2014 01:37 PM Hello, I was comparing output between HP48sx MATHLIB [Ref. 1] and WP-34S (v 3.2 3375) for some of the probability distributions. For this purpose, I was replicating the examples from the HP-21S user's manual (pg. 42-52) I noticed that the Inverse of the Chi-Square distribution for the WP-34S 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 WP-34S: 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 HP-48sx MATHLIB function IUTPC(19, 0.001). For reference, the IOP for the v3.2 manual of the WP-34S indicates that ϰ²INV should be equal to ϰ²p of the 21S. Is my understanding of how to use this distribution on the WP-34S incorrect? Thank you for any insights you may have! [1] HP 48SX Engineering Mathematics Library, John F. Holland RE: [WP-34S] Chi-Square Distribution - walter b - 01-11-2014 03:02 PM (01-11-2014 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 HP-21S calculates with the error probability instead. d:-) RE: [WP-34S] Chi-Square Distribution - Dieter - 01-11-2014 06:18 PM (01-11-2014 03:02 PM)walter b Wrote:(01-11-2014 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 quick-and-dirty program based on my original contributions for the chi-square quantile (providing a decent initial guess, followed by a slightly modified Newton iteration), and even this simple user-code program got the result in not much more than four (!) seconds (SP), resp. 5-6 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 RE: [WP-34S] Chi-Square Distribution - Paul Dale - 01-11-2014 11:11 PM 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" RE: [WP-34S] Chi-Square Distribution - Dieter - 01-12-2014 02:37 PM (01-11-2014 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 = 1E-10, 0,05, 0,95 and 0,99999? Or is there a way to single-step 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 RE: [WP-34S] Chi-Square Distribution - Sanjeev Visvanatha - 01-12-2014 03:13 PM (01-11-2014 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 HP-21S calculates with the error probability instead. Thank you for clarifying my misunderstanding. I wonder if you consider the statement on pg. 126 of the WP-34S manual correct then? : "... ϰ²INV equals ϰ²p in the HP-21S ..." RE: [WP-34S] Chi-Square Distribution - Sanjeev Visvanatha - 01-12-2014 03:16 PM (01-11-2014 06:18 PM)Dieter Wrote: Another example:In SLOW mode, this takes about 18s for me, and about 12s in FAST mode. RE: [WP-34S] Chi-Square Distribution - walter b - 01-12-2014 05:57 PM (01-12-2014 03:13 PM)Sanjeev Visvanatha Wrote:(01-11-2014 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 HP-21S calculates with the error probability instead. I replaced it by "... χ²INV corresponds to χ²p in the HP-21S ..." and put an extra footnote on p. 55 stating: The HP-21S 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:-) RE: [WP-34S] Chi-Square Distribution - Sanjeev Visvanatha - 01-12-2014 07:23 PM (01-12-2014 05:57 PM)walter b Wrote: I replaced it by "... χ²INV corresponds to χ²p in the HP-21S ..." 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 RE: [WP-34S] Chi-Square Distribution - Paul Dale - 01-12-2014 09:38 PM (01-12-2014 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 = 1E-10, 0,05, 0,95 and 0,99999? Or is there a way to single-step 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: 1E-10 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 RE: [WP-34S] Chi-Square Distribution - Dieter - 01-12-2014 10:21 PM (01-12-2014 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 RE: [WP-34S] Chi-Square Distribution - Paul Dale - 01-13-2014 01:08 AM 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 The sheet (was: RE: [WP-34S] Chi-Square Distribution) - walter b - 01-13-2014 07:55 PM (01-12-2014 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:-) RE: [WP-34S] Chi-Square Distribution - Sanjeev Visvanatha - 01-14-2014 03:04 AM (01-13-2014 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! RE: [WP-34S] Chi-Square Distribution - Dieter - 01-14-2014 02:33 PM (01-13-2014 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 RE: [WP-34S] Chi-Square Distribution - walter b - 01-14-2014 02:46 PM (01-14-2014 02:33 PM)Dieter Wrote: Could you post the Normal estimate code before and after your last correction?You find it at sourceforge RE: [WP-34S] Chi-Square Distribution - Dieter - 01-14-2014 03:11 PM (01-14-2014 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 RE: [WP-34S] Chi-Square Distribution - Paul Dale - 01-14-2014 10:07 PM 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 RE: [WP-34S] Chi-Square Distribution - Dieter - 01-14-2014 11:05 PM (01-14-2014 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 4E-9), 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 4E-9 (!). 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 RE: [WP-34S] Chi-Square Distribution - Paul Dale - 01-16-2014 01:41 AM 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 |