[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, 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 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 Upper tail probability = 0.001 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 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"                         xIN MONADIC                         GSB chi2_param                         GSB qf_check_probability                         GSB qf_chi2_est                         _INT DIST_CHI2                         XEQ qf_newton                         xOUT xOUT_NORMAL #define R_P     .00 #define R_T     .01 qf_chi2_est::           LocR 2                         STO R_P                         _INT 15                         x= 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 sourceforge Yes, 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 qf_q_int_est::    ENTER[^]         +/-         INC X         MIN         STO R_MINP        // q = min(p, 1-p)         x[!=]? L             SF F_SMALL    // Set Flag if p < 0.5 (i.e. 1-p != q)         Num 1/2            // pre-store 0.5 in a register for later use         STO R_HALF         _INT 023         SDR 002         RCL R_MINP         x>? Y             JMP qf_q_mid         ENTER[^]        // tail estimate         LN         STO+ X         +/-         STO Z         DEC X         Num [pi]         [times]         STO+ X         [sqrt]         [times]         LN         STO+ X         +/-         [sqrt]         _INT 254         SDR 003         RCL/ Z         +         STO R_ABSZ         JMP qf_q_signfix qf_q_mid::    Num 1/2         RCL- Y         Num [pi]         STO+ X         [sqrt]         [times]         ENTER[^]         x[^3]         _INT 006         /         +         STO R_ABSZ         _INT 004         SDR 002         x>? Y             CF F_ITER        // z < 0.04 requires just one correction step         SDR 007         x>? Y             SF F_EXACT        // z < 4E-9 needs no correction at all, flag for exit qf_q_signfix::    FS? F_SMALL             +/-         RTN``` 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 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. 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