COV2

Info: A statistics routine, written in user-rpl, that calculates two-
dimensional weighted covariance (I hope this is the correct english 
expression. In german it would be called: Covarianz fuer 
zweidimensionale Haeufigkeitsverteilungen).
In addition the Pearson correlation coefficent r, the weighted means 
and the standard deviations (population) are given.

Stat03 from Christian Meland needs to be installed!!! Stat03 is a very 
useful stat lib (and also helpfull for calculating with arrays!) 
(found at http://www.hpcalc.org)

Installation:
Cut between the lines:__________________________

%%HP: T(3)A(R)F(.);
\<< 1 2 COL\GS
IFERR RCL\GS DROP '\GSDAT' RCL 1 ROT THEN 0 SWAP END
1 COL- 1 COL-
DROP SWAP 1 ROW- \->
X IMAT Y	
\<< IMAT Y DUP
MR DIAG\-> * X 		
DUP MR DIAG\-> SWAP *
0 MTOT IMAT 0 MTOT
SWAP OVER / 	
X IMAT TRNS MTOT 
DUP2 2 COL\-> STO\GS
WPSDEV 'XSDP' \->TAG 5
ROLLD DOT 3
PICK / DUP 'XMEAN'
\->TAG 4 ROLLD Y IMAT		
MTOT DUP2 2 COL\->
STO\GS WPSDEV
'YSDP' \->TAG 7 ROLLD
DOT 4 ROLL /
DUP 'YMEAN' \->TAG 4
ROLLD * - 'COV2'
\->TAG DUP 6 PICK 6
PICK * / 'PEARS'
\->TAG
  \>>
IF 7 ROLL
THEN 7 ROLL STO\GS END
\>>

________________________________________________________
Transfer in ASCII mode to your calc (using kermit).

Put an array on the stack that looks like this:

[[0 1 2 3 4]
 [2 3 9 2 1]
 [4 8 7 2 3]
 [6 4 9 1 1]]

where in this example the y-row is [1 2 3 4] and the x-column is 
[2 4 6].
The first 0 is just a *placeholder* which is canceld out of the array 
and therefore can be any number. The other numbers [3 9 2 1] [8 7 2 3] [4 9 1 1] (I call it the *inner* array) are the frequencies.

Run COV2

In this example the output on the stack will be:

6:	XSDP: 1.54919333848
5:	YSDP: 0.894427191
4:	XMEAN: 4
3:	YMEAN: 2
2:	COV2: -0.08
1:	PEARS: -5.77350269192E-2

where XSDP is the standard deviation (population) of x, XMEAN is 
selfexplaining ;-), COV2 is the Covariance of course and PEARS is the 
Pearson correlation coefficent r.

To see the corresponding equiations look at the attached bitmap-file 
EQ.tif.

Modification: If you want to calculate the standard deviation of 
sample just change the command WPSDEV to WSDEV (where the used 
denominator is n-1).
YSDP should then be called YSD and simular XSDP should be changed to 
XSD.

Special thanks to Christian Meland for his splendid Stat03 and for 
suggesting and showing me some improvements!

15.09.98
Peter Karp
karpfenteich@gmx.net
