MCOV
Updated 2023-10-17 01:40:16.517000
Syntax
SELECT [westclintech].[wct].[MCOV](
<@Matrix, nvarchar(max),>)
Description
Use the scalar function MCOV to calculate a sample covariance matrix. MCOV expects a string representation of the matrix, with columns separated by commas and rows separated by semi-colons. Please refer to the COVM documentation for an explanation as to how the sample covariance matrix is calculated.
Arguments
@Matrix
a string representation of the matrix.
Return Type
nvarchar(max)
Remarks
The string representations of @Matrix must only contain numbers, commas (to separate the columns), and semi-colons to separate the rows.
Consecutive commas will generate an error.
Consecutive semi-colons will generate an error.
Non-numeric data between commas will generate an error.
Non-numeric data between semi-colons will generate an error.
To convert non-normalized data to a string format, use the MATRIX2STRING or the MATRIX2STRING_q function.
To convert normalized data to a string format, us the NMATRIX2STRING or the NMATRIX2STRING_q function.
To convert the string result to a table, use the table-valued function MATRIX.
Examples
In this example we use the MATRIX2STRING_q function to create a properly formatted matrix for a varchar variable which becomes the input to MCOV .
DECLARE @A as varchar(max)
= wct.MATRIX2STRING_q('SELECT
x1,x2,x3,x4,x5,x6,x7,x8
FROM (VALUES
(1,1,1,1,1,1,1,1)
,(2,3,4,5,6,7,8,9)
,(4,9,16,25,36,49,64,81)
)n(x1,x2,x3,x4,x5,x6,x7,x8)');
SELECT wct.MCOV(@A);
This produces the following result.
{"columns":[{"field":"output"}],"rows":[{"output":"2.33333333333333,6.33333333333333,12,19.3333333333333,28.3333333333333,39,51.3333333333333,65.3333333333333;6.33333333333333,17.3333333333333,33,53.3333333333333,78.3333333333333,108,142.333333333333,181.333333333333;12,33,63,102,150,207,273,348;19.3333333333333,53.3333333333333,102,165.333333333333,243.333333333333,336,443.333333333333,565.333333333333;28.3333333333333,78.3333333333333,150,243.333333333333,358.333333333333,495,653.333333333333,833.333333333333;39,108,207,336,495,684,903,1152;51.3333333333333,142.333333333333,273,443.333333333333,653.333333333333,903,1192.33333333333,1521.33333333333;65.3333333333333,181.333333333333,348,565.333333333333,833.333333333333,1152,1521.33333333333,1941.33333333333"}]}
We also could have passed the input to the function as:
SELECT wct.MCOV('1,1,1,1,1,1,1,1;2,3,4,5,6,7,8,9;4,9,16,25,36,49,64,81');
as well as:
SELECT wct.MCOV(wct.MATRIX2STRING_q('SELECT
x1,x2,x3,x4,x5,x6,x7,x8
FROM (VALUES
(1,1,1,1,1,1,1,1)
,(2,3,4,5,6,7,8,9)
,(4,9,16,25,36,49,64,81)
)n(x1,x2,x3,x4,x5,x6,x7,x8)'));
We can use the table-valued function MATRIX to convert the string results to a table and the PIVOT function to provide the traditional representation of a matrix.
SELECT ROUND([0], 2) as [0],
ROUND([1], 2) as [1],
ROUND([2], 2) as [2],
ROUND([3], 2) as [3],
ROUND([4], 2) as [4],
ROUND([5], 2) as [5],
ROUND([6], 2) as [6],
ROUND([7], 2) as [7]
FROM
(
SELECT *
FROM wct.MATRIX(wct.MCOV('1,1,1,1,1,1,1,1;2,3,4,5,6,7,8,9;4,9,16,25,36,49,64,
81'))
) d
PIVOT
(
SUM(ItemValue)
FOR ColNum in ([0], [1], [2], [3], [4], [5], [6], [7])
) pvt
ORDER BY RowNum;
This produces the following result.
{"columns":[{"field":"0","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"3","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"4","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"5","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"6","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"7","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"0":"2.33","1":"6.33","2":"12","3":"19.33","4":"28.33","5":"39","6":"51.33","7":"65.33"},{"0":"6.33","1":"17.33","2":"33","3":"53.33","4":"78.33","5":"108","6":"142.33","7":"181.33"},{"0":"12","1":"33","2":"63","3":"102","4":"150","5":"207","6":"273","7":"348"},{"0":"19.33","1":"53.33","2":"102","3":"165.33","4":"243.33","5":"336","6":"443.33","7":"565.33"},{"0":"28.33","1":"78.33","2":"150","3":"243.33","4":"358.33","5":"495","6":"653.33","7":"833.33"},{"0":"39","1":"108","2":"207","3":"336","4":"495","5":"684","6":"903","7":"1152"},{"0":"51.33","1":"142.33","2":"273","3":"443.33","4":"653.33","5":"903","6":"1192.33","7":"1521.33"},{"0":"65.33","1":"181.33","2":"348","3":"565.33","4":"833.33","5":"1152","6":"1521.33","7":"1941.33"}]}
See Also
COVARIANCE_P - Aggregate function to calculate the population covariance
COVARIANCE_S - Aggregate function to calculate the sample covariance
COVM - Table-valued function to calculate the covariance matrix
MCORR - Correlation matrix using a formatted matrix as input