Skip to content
mikerabat edited this page Aug 23, 2019 · 1 revision

Correlation

There is a small class that covers calculation of correlation between vectors. Matrices are actually converted to a vector in beforehand!

In addition the class provides functions to calc a covariance matrix.

Class description

The public class description is:

   type
     TCorrelation = class(TMatrixClass)
     public
       function Correlate(x, y : IMatrix) : double; overload; // correlation coefficient between t, r (must have the same length)
       function Correlate(x, y : IMatrix; var prob, z : double) : double; overload; // pearson correlation with Fishers's z and probobality 

       // spearman correlation. X, Y need to be vectors
       class function CorrelateSpearman( x, y : IMatrix; var zd, probd, rs, probrs : double ) : double;
       class function Covariance(x, y : IMatrix; Unbiased : boolean = True) : IMatrix; overload; // covariance matrix
       class function Covariance(A : IMatrix; Unbiased : boolean = True) : IMatrix; overload;   // covariance matrix of matrix A
     end;

Correlate returns the pearson corrleation following the formula:

1/(n-1)/(var_w1var_w2) sum_i=0_n (w1_i - mean_w1)(w2_i - mean_w2)

The extended version returns in addition the probability and Fisher's z value.

The CorrleationSpearman function is based on the Spearman correlation which is a rank based correlation function. Again the matrices are transformed into vectors to calculate this correlation factor.

The rank is returned in rs, zd holds the number of standard deviations by which D (sum of squared rank differences) deviates from the nullhypothesis.

The covariance matrix functions follow the procedure:

  • mean normalize the input matrices (or matrix).
  • cov = 1/(m - 1)*A'*A
Clone this wiki locally