Post Reply 
(12C) Chi Square Statistic
06-12-2019, 10:18 PM
Post: #1
(12C) Chi Square Statistic
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:

01 x=0?                // If X=0 then there is not a single E value
02 g GTO 08            // skip storing common E and setting flag
03 STO 0            // Store common E
04 1
05 STO PMT            // Set common E flag to 1
06 Rv
07 g GTO 09        
08 STO PMT            // set common E flag to 0
09 0                // set accumulating sum to zero
10 ENTER
11 0                // Start of main loop
12 RCL n
13 x<=y?            // have all values been loaded? go to end if yes
14 g GTO 34
15 Rv
16 Rv
17 RCL PMT
18 x=0?
19 g GTO 23
20 Rv
21 RCL 0            // Load common E
22 g GTO 26
23 Rv
24 RCL g Nj            // Load & store individual E
25 STO 0
26 RCL g CFj        // Load Obs value
27 -
28 ENTER
29 *
30 RCL 0
31 /
32 +                // Add newest value to accumulating sum
33 g GTO 11
34 Rv
35 Rv                // Move accumulated sum to X register

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.
Find all posts by this user
Quote this message in a reply
06-13-2019, 12:56 AM
Post: #2
RE: (12C) Chi Square Statistic
(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
Find all posts by this user
Quote this message in a reply
06-13-2019, 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 e-07 etc. I think that is unnecessary if space savings can be made with a cruder estimate.

I'll come back on this.
Find all posts by this user
Quote this message in a reply
06-14-2019, 11:00 AM
Post: #4
RE: (12C) Chi Square Statistic
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:

// Chi-sq Distribution (eccentric - normal RV's which make up the chi-sq not necessarily mu=0)
// Modified for HP-12C by Joe_H 2019 
// from 'Programs on HP-25 Desk Calculator of Mathematical Functions for use in Statistics' E. Sverdrup 1976 pgs. 25-27

01 STO 7            
02 CHS                
03 e^x                
04 STO 4
05 Rv
06 STO 3
07 Rv
08 STO 5
09 CHS
10 e^x
11 STO 0
12 STO +2
13 RCL 1
14 RCL 3
15 x<=y?
16 g GTO 26
17 1
18 STO +1
19 RCL 1
20 INTG
21 STO /0
22 RCL 5
23 STO *0
24 RCL 0
25 g GTO 12
26 RCL 2
27 STO -2
28 RCL 4
29 *
30 STO +1
31 g PSE                // replace this line with g GTO 45 for the automated version up to m terms (below)
32 g PSE
33 RCL 3
34 STO -1
35 1
36 STO +3
37 RCL 6
38 +
39 STO 6
40 STO /4
41 RCL 7
42 STO *4
43 RCL 5
44 g GTO 09
45 RCL 6
46 3                    // replace these with the number you want: here it is m=30 but can be any number
47 0
48 x<=y?
49 g GTO 00
50 g GTO 33
Find all posts by this user
Quote this message in a reply
06-14-2019, 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).
Find all posts by this user
Quote this message in a reply
Post Reply 




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