The SDL Component Suite is an industry leading collection of components supporting scientific and engineering computing. Please visit the SDL Web site for more information....


Unit: SDL_math2
Class: none
Declaration: function CalcCovar (InData, CovarMat: TMatrix; LoC, HiC: integer; LoR, HiR: integer; Mode: integer): boolean;

The routine CalcCovar calculates the covariance matrix, or alternatively, the scatter matrix, the correlation matrix, the squared correlation matrix, or the sum of squared differences for a given data matrix. The resulting matrix is of the dimension (HiC-LoC+1)*(HiC-LoC+1), this means that the rows of the data matrix are seen as objects and the columns are seen as variables.

The parameter InData contains the input data. The parameter CovarMat holds a pointer to a matrix which will contain the matrix to be calculated.

The parameters LoC HiC LoR, and HiR specify the range of the data matrix, which should be used for the calculation. This enables the user to calculate 'local' covariance matrices which originate only from a part of the data.

The parameter Mode determines which matrix to calculate:

Mode Resulting matrix(1)
0 scatter matrix of InData
1 covariance matrix of InData
2 correlation matrix of InData (Pearson's correlation coefficient)
3 squared correlation matrix of InData
4 sum of squared differences between the columns of InData

The function CalcCovar returns the value TRUE if the calculation of the matrix has been performed successfully. A FALSE value indicates that the calculation has not been completed successfully (mostly due to lack of memory or too small a matrix of CovarMat ).

CalcCovar increments the global variable ProcStat  and calls the feedback routine MathFeedBackProc in order to allow feedback to the user during time consuming calculations. The CalcCovar function can be terminated by setting the global variable AbortMathProc to TRUE.

Hint 1: The user has to take care to declare the matrix CovarMat large enough to hold the resulting matrix. This matrix has to be quadratic and of size (HiC -LoC +1)*(HiC -LoC +1). If the matrix CovarMat is too large the covariance matrix is stored in the matrix elements with the lowest indices (starting at 1). The unused elements are not changed at all.

Hint 2: The sum of the squared differences (mode=4) can be used to calculate the PRESS (predictive residual error sum of squares)

Example: COVAR.DPR
  The example program COVAR.DPR shows the application of the function CalcCovar. The user can enter values into a matrix and calculate either the scatter matrix, the covariance matrix, or the correlation matrix.

Example: This method is used in the following example program (see for downloading the code): covar

(1) The matrix contains unbiased estimates of the corresponding entries (i.e. numeric values are scaled by the factor 1/(HiR-LoR) ).

Last Update: 2023-Feb-06