(12C) Chi Square Statistic - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (12C) Chi Square Statistic (/thread-13121.html) |
(12C) Chi Square Statistic - Joe_H - 06-12-2019 10:18 PM The Chi-sq statistic is very widely used in many disciplines not least in finance. It is used to test if there is a significant difference between the expected frequency of occurrence of an event and the actual frequency of occurrence (e.g. rolling a dice 60 times and getting 30 1's 30 2'sand nothing else would intuitively be suspicious - I'll calculate this below) It would be useful to be able to calculate the statistic quickly from a series of observations. Tony Hutchins did this in a simple interactive program of only 8 lines but I have completely changed it to a more convenient and faster form. I don't want an interactive program as it doesn't suit how I'm going to set up the calculator but also you can't check if you made a data entry error as values get rolled up as the sum is calculated. Also in his program the expected frequency has to be entered each time but I wanted a work around when it is common to avoid re-entering it. The following program calculates the Chi-sq statistic where the observation counts and expected counts are placed in the cashflow registers. CFj is used for the Obs and Nj for the Expt values in each category. Where there is a common Expt (as example below) then don't enter them and instead enter the Expt in X before calling the program. If Expt is entered in the cashflow then set X to zero before calling the program. n is used and should be correctly set after inputting the values (don't use CF0 set n to 0 - better yet clear all reg before inputting the data) and start first values at n=1 (this will be automatic if set n=0 before start data entry). PMT is used as the common/individual Expt flag. Note n will be 0 after the computation and needs to be reset. Could save and reset it for a few extra lines. Code:
Examples: Hutchins dice roll Rolling a dice 120 times produced the following numbers 1 - 25 2 - 17 3 - 15 4 - 23 5 - 24 6 - 16 A fair dice should on average produce 20 of each number for 120 throws (Expt in this example). Is this dice fair? f Clear Reg 25 g CFi (don't bother with Nj as only one Expt but enter it now if different for categories) 17 g CFi ditto for other 4; n=6 now 20 (place common Expt in X) R/S Answer = 5 and from chi-sq tables the probability with 5 degrees of freedom (1 less than n) and alpha of 5% (i.e. 95% certainty) chi-sq is 11.07. As 5 is less than this the dice is fair to 95% certainty. My example of 60 throws enter as before (I'll make 3 to 6 1 each to avoid any problems with 0 entry). Expt is 10 (60/6). Chi-sq calculates as 97.2. Clearly this is much greater than 11.07 so at 95% confidence level we know there's a problem with my dice. Individual Expt values just are different in data entry (use the Nj's) and set X to 0 every thing after that is identical. I will look into doing the probability values if I can find a simple approximation for the chi-sq probability distribution. RE: (12C) Chi Square Statistic - SlideRule - 06-13-2019 12:56 AM (06-12-2019 10:18 PM)Joe_H Wrote: I will look into doing the probability values if I can find a simple approximation for the chi-sq probability distribution. Perhaps VII THE CHI-SQUARE DISTRIBUTION (pages 25-26) in (25) Mathematical Functions for Use In Statistics, HP Forums, HP Software Libraries, General Software Library is of modest utility? If not, maybe some other stat related post is apropos. BEST! SlideRule RE: (12C) Chi Square Statistic - Joe_H - 06-13-2019 03:30 PM Thanks SlideRule that's a very interesting source of information that I will explore. As it is for business applications the accuracy of the probability value is ok to only 2 decimal places I reckon so I'll see where it can be reduced to as simple a formula that meets that requirement. What he has in that paper may work. He's talking about accuracy to e-07 etc. I think that is unnecessary if space savings can be made with a cruder estimate. I'll come back on this. RE: (12C) Chi Square Statistic - Joe_H - 06-14-2019 11:00 AM I had a go at the chi-sq distribution (the probability distribution as opposed to the statistic itself that I calculated above which follows this probability distribution if all assumptions in it are met). It is significantly more complicated (and accurate!) than I require but it was good to give it a go anyway and see could I modify the HP-25 program successfully. The program required some significant modification in registers as R5-R7 don't allow register arithmetic in HP-12C and the conditionals are x>=y the opposite of it too (no NOP instruction and a few other things) . Got it all working and versions A and B are in the code below. I haven't changed or improved the program at all using the more expanded program memory and registers of the HP-12C. It works exactly as per the HP-25 but as it was limited to 7 registers the HP-12C could use and 8th for the output rather than using R7 for counter j as well as the output (j is the integer part and the right tail probability or the result is the fractional part of the register entry). Also the variable input is cumbersome as pre-calculations have to be done on them before starting the program but that is simple enough with the RPN stack on the HP's! To run the program (use manual version with pause first to check it is working): f CLEAR REG (this is very important as it took me some time to figure out!) Z: chi-sq statistic/2 Y: degrees freedom/2 - 1 (df must be even as this has to be an integer) X: eccentricity/2 R/S For the paused version it will pause and show the result at each i (the outer variable) and this accumulates in the fractional part of R7. Use chi-sq = 18.307; df = 10 (also called nu) & eccent. = 3.71 and check the outputs against the values in his paper pg. 26. Note: this can only deal with df being even and in our example earlier it was odd (5). The paper proposes either simple or more complex interpolation for odd df's. For the standard chi-sq distribution (as in normal tables of it) I think eccent. =0. I suspect it can be done much more simply if I only want accuracy to 2 decimal places. I'll hunt around for a solution if I get time. Code:
RE: (12C) Chi Square Statistic - Joe_H - 06-14-2019 11:10 AM Sorry forgot to say the changes in registers I made. R7 in the original had the result but in my program it is R1. Also R4 and R5 are swapped around as R4 can do arithmetic but none was used in the original and a fair bit was with R5. Other registers remain as they were. So the final result is in R1 (fractional part only). |