(12C) Chi Square Statistic

06122019, 10:18 PM
Post: #1




(12C) Chi Square Statistic
The Chisq 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 reentering it.
The following program calculates the Chisq 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 chisq tables the probability with 5 degrees of freedom (1 less than n) and alpha of 5% (i.e. 95% certainty) chisq 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). Chisq 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 chisq probability distribution. 

06132019, 12:56 AM
Post: #2




RE: (12C) Chi Square Statistic
(06122019 10:18 PM)Joe_H Wrote: I will look into doing the probability values if I can find a simple approximation for the chisq probability distribution. Perhaps VII THE CHISQUARE DISTRIBUTION (pages 2526) 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 

06132019, 03:30 PM
Post: #3




RE: (12C) Chi Square Statistic
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 e07 etc. I think that is unnecessary if space savings can be made with a cruder estimate. I'll come back on this. 

06142019, 11:00 AM
Post: #4




RE: (12C) Chi Square Statistic
I had a go at the chisq 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 HP25 program successfully. The program required some significant modification in registers as R5R7 don't allow register arithmetic in HP12C 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 HP12C. It works exactly as per the HP25 but as it was limited to 7 registers the HP12C 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 precalculations 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: chisq 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 chisq = 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 chisq 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:


06142019, 11:10 AM
Post: #5




RE: (12C) Chi Square Statistic
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). 

« Next Oldest  Next Newest »

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