hp41programs

Gini

Gini Coefficient for the HP-41


Overview
 

 1°)  Program#1
 2°)  Program#2
 

-The Gini coefficient measures the statistical dispersion of a distribution.
-It is always between 0 and 1.
-It may be computed by the formula:

    G = 1 + [ Sumi ni ( ni xi - 2 Sumj<=i ni xi ) ] / [ ( Sumi ni ) ( Sumi ni xi ) ]
 
 

1°)  Program#1
 

-The xi 's  must be stored in increasing order
 

Data Registers:       •  R00 = p = number of points          ( Registers R00 thru R2p  are to be initialized before executing "GINI" )

                                  •  R01 = x1     •  R03 = x2      .......    •  R2p-1 = xp      with    x1 <= x2 <= ............... <= xp
                                  •  R02 = n1     •  R04 = n2      .......    •  R2p   = np
Flags:  /
Subroutines:  /
 
 

01  LBL "GINI"   
02  CLA
03  RCL 00
04  ST+ X
05  STO M
06  CLST
07  LBL 01
08  RCL IND M
09  ST+ N
10  STO O
11  DSE M
12  RCL IND M
13  *
14  ST+ Z
15  RCL Z
16  ST+ X
17  -
18  RCL O        
19  *
20  -
21  DSE M        
22  GTO 01
23  X<>Y
24  RCL N
25  *
26  ST- Y         
27  /
28  CLA
29  END

 
   ( 53 bytes / SIZE 2p+1 )
 
 

      STACK        INPUT      OUTPUT
           X             /            G

Example:

    xi  |   2   7   9  12  15  19       Store these        R01   R03   R05   R07   R09   R11
  ----------------------------    12  numbers        ------------------------------------         respectively  and  p = 6  into  R00
    ni  |   3   5   8  14   7    2             into               R02   R04   R06   R08   R10   R12
 

   XEQ "GINI"  >>>>   G = 0.196298984   ( in 3.5 seconds )
 

Note:

-If p = 50, execution time = 29 seconds.
 

2°)  Program#2
 

-Instead of storing all the xi's, ni's in data registers, we may also store the required sums only.

-The following program also calculates the arithmetic mean, the standard deviation, the skewness and the kurtosis

   of a given set of p points        x1 , x2 , ............ ,  xp
  with respective frequencies     n1 , n2 , ............ ,  np
 

Data Registers:    R00 = Sum ni    R01 = Sum ni xi    R03 = Sum ni xi3    R05 = Sumi ni ( ni xi - 2 Sumj<=i ni xi )    R07 = G
                                                        R02 = Sum ni xi2   R04 = Sum ni xi4    R06 = m = arithmetic mean                     R08 = n m2

Flag:   F01                  CF 01  if you want to use the sample standard deviation
Subroutines: /            SF 01  if you want to use the population standard deviation
 
 

01  LBL A
02  ST+ 00
03  STO 06
04  RCL Y
05  STO T
06  *
07  STO 07      
08  *
09  ST+ 02
10  *
11  ST+ 03
12  *
13  ST+ 04
14  RCL 01
15  ST+ X
16  RCL 07
17  ST+ 01
18  +
19  RCL 06
20  *
21  ST- 05
22  R^
23  RTN
24  LBL "STAT"
25  RCL 05
26  RCL 00
27  RCL 01
28  *
29  ST+ Y
30  /
31  STO 07
32  RCL 01
33  RCL 00
34  /
35  ENTER^
36  STO 06
37  X^2
38  RCL 00
39  *
40  STO 08
41  RCL 02      
42  ENTER^
43  +
44  X<>Y
45  -
46  *
47  3
48  *
49  RCL 03
50  4
51  *
52  -
53  *
54  RCL 04
55  +
56  RCL 08
57  ST+ X
58  RCL 02      
59  3
60  *
61  -
62  RCL 06
63  *
64  RCL 03
65  +
66  RCL 00
67  ST/ Z
68  /
69  RCL 02
70  RCL 08
71  -
72  RCL 00      
73  FC? 01
74  DSE X
75  /
76  ST/ Y
77  ST/ Z
78  ST/ Z
79  SQRT
80  ST/ Y
81  RCL 07
82  SIGN
83  CLX
84  RCL 06
85  END

 
  ( 113 bytes / SIZE 009 )
 
 

      STACK      INPUTS#i    OUTPUTS#i  final OUTPUTS
           T             /             /            µ4
           Z             /             /            µ3
           Y             xi             /            s
           X             ni            xi            m
           L             /            ni            G

 Where

     m = arithmetic mean                                                     G = Gini coefficient
      s = sample standard deviation if CF 01                        µ3 = skewness
      s = population standard deviation if SF 01                   µ4 = kurtosis
 

Example:    Given the distribution:
 
 

     xi      2      7      9     12     15     19
     ni      3      5      8     14      7      2

-Clear registers R00 thru R05  ( for instance execute CLS  if your statistic registers are R00 thru R05 )
-Then,  xi  ENTER^   ni   S+  ( [A]  in user mode )  for all the points

   2  ENTER^      7  ENTER^      9  ENTER^     12  ENTER^    15  ENTER^     19  ENTER^
   3  S+               5  S+                8  S+              14  S+               7   S+               2   S+                    in user mode

-Finally, simply press  R/S  ( or XEQ "STAT" )  it yields:

     if CF 01                      m = 10.8718                                 if SF 01                         m = 10.8718
                            RDN    s  =  4.0012                                                              RDN    s  =  3.9496
                            RDN   µ3 = -0.3406 = skewness                                           RDN   µ3 = -0.3542 = skewness
                            RDN   µ4 =  3.0605 =  kurtosis                                             RDN   µ4 =  3.2238 =  kurtosis

-And in both cases:                                                     G = 0.196298984 = R07
 

Notes:

-The xi 's must also be stored in increasing order to compute G correctly.
-For a normal distribution,  µ3 = 0  &  µ4 =  3
-This program is actually a modification of "STAT" listed in "Statistics & Probability for the HP-41"