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
Post Reply 


Messages In This Thread
(12C) Chi Square Statistic - Joe_H - 06-12-2019 10:18 PM
RE: (12C) Chi Square Statistic - SlideRule - 06-13-2019, 12:56 AM
RE: (12C) Chi Square Statistic - Joe_H - 06-13-2019, 03:30 PM
RE: (12C) Chi Square Statistic - Joe_H - 06-14-2019, 11:00 AM
RE: (12C) Chi Square Statistic - Joe_H - 06-14-2019, 11:10 AM



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