DECLARE EXTERNAL FUNCTION covariance,correlation
DATA 6,4
DATA 7,6
DATA 8,3
DATA 9,5
DATA 10,7
DIM x(5,2)
MAT READ x
PRINT "covariance";covariance(x,1,2),"correlation"; correlation(x,1,2)
END
EXTERNAL FUNCTION mean(x(,),n)
LET a=LBOUND(x,1)
LET b=UBOUND(x,1)
LET s=0
FOR i=a TO b
   LET s=s+x(i,n)
NEXT i
LET mean=s/SIZE(x,1)
END FUNCTION 
EXTERNAL FUNCTION covariance(x(,),i,j)
DECLARE EXTERNAL FUNCTION mean
LET a=LBOUND(x,1)
LET b=UBOUND(x,1)
LET mi=mean(x,i)
LET mj=mean(x,j)
LET s=0
FOR k=a TO b
   LET s=s+(x(k,i)-mi)*(x(k,j)-mj)
NEXT k
LET covariance=s/SIZE(x,1)
END FUNCTION 
EXTERNAL FUNCTION variance(x(,),n)
DECLARE EXTERNAL FUNCTION covariance
LET variance=covariance(x,n,n)
END FUNCTION
EXTERNAL FUNCTION sd(x(,),n)
DECLARE EXTERNAL FUNCTION variance
LET sd=SQR(variance(x,n))
END FUNCTION
EXTERNAL FUNCTION correlation(x(,),i,j)
DECLARE EXTERNAL FUNCTION covariance,sd
LET correlation=covariance(x,i,j)/(sd(x,i)*sd(x,j))
END FUNCTION
