Logo

MovingCORREL

Updated 2023-11-13 20:51:24.347000

Syntax

SELECT [westclintech].[wct].[MovingCORREL](
  <@Y, float,>
 ,<@X, float,>
 ,<@Offset, int,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)

Description

Use the scalar function MovingCORREL to calculate the Pearson product moment correlation coefficient through data points in y- and x-values within a resultant table or partition, without the need for a self-join. The correlation coefficient value is calculated for each value from the first value in the window to the last value in the window. If the column values are presented to the functions out of order, an error message will be generated.

Arguments

@Id

a unique identifier for the MovingCORREL calculation. @Id allows you to specify multiple MovingCORREL calculations within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.

@Offset

specifies the window size. @Offset is an expression of type int or of a type that can be implicitly converted to int.

@RowNum

the number of the row within the group for which the sum is being calculated. If @RowNum for the current row in a set is less than or equal to the previous @RowNum and @RowNum is not equal to 1, an error message will be generated. @RowNum is an expression of type int or of a type that can be implicitly converted to int.

@X

the x-value passed into the function. @X is an expression of type float or of a type that can be implicitly converted to float.

@Y

the y-value passed into the function. @Y is an expression of type float or of a type that can be implicitly converted to float.

Return Type

float

Remarks

If @Id is NULL then @Id = 0.

@RowNum must be in ascending order.

To calculate the correlation coefficient over an entire set or partition of x- and y-values use the RunningCORREL function.

If @RowNum = 1 then MovingCORREL is NULL.

To calculate a single correlation coefficient for a set, use the CORREL function.

There may be cases where the order in which the data are returned to the function and the order in which the results are returned are different, generally due to parallelism. You can use OPTION(MAXDOP 1) or OPTION(MAXDOP 1,FORCE ORDER) to help eliminate this problem.

Examples

In this example we will calculate the correlation coefficient in the relationship between square footage and house prices. We will create a temporary table, #se, populate it with some data and then run the SELECT.

--Create the temporary table

CREATE TABLE #se

(

    rn int,

    id_lot int,

    amt_sqft int,

    amt_price int,

    PRIMARY KEY (rn)

);

--Put some date in the table

INSERT INTO #se

VALUES

(1, 21783, 1147, 393918);

INSERT INTO #se

VALUES

(2, 94729, 1313, 470479);

INSERT INTO #se

VALUES

(3, 33028, 1433, 512474);

INSERT INTO #se

VALUES

(4, 59446, 1724, 610477);

INSERT INTO #se

VALUES

(5, 97646, 1162, 388196);

INSERT INTO #se

VALUES

(6, 44823, 1813, 636916);

INSERT INTO #se

VALUES

(7, 88397, 1105, 374348);

INSERT INTO #se

VALUES

(8, 13588, 1555, 559149);

INSERT INTO #se

VALUES

(9, 13891, 1775, 623900);

INSERT INTO #se

VALUES

(10, 90957, 1585, 563947);

INSERT INTO #se

VALUES

(11, 44167, 1510, 529806);

INSERT INTO #se

VALUES

(12, 75533, 1628, 592533);

INSERT INTO #se

VALUES

(13, 56812, 1145, 408634);

INSERT INTO #se

VALUES

(14, 12897, 1632, 589522);

INSERT INTO #se

VALUES

(15, 93826, 1850, 668852);

INSERT INTO #se

VALUES

(16, 74510, 1867, 633400);

INSERT INTO #se

VALUES

(17, 17262, 1587, 552178);

INSERT INTO #se

VALUES

(18, 30929, 1809, 633141);

INSERT INTO #se

VALUES

(19, 49030, 1521, 555713);

INSERT INTO #se

VALUES

(20, 33431, 1195, 434542);

--Calculate CORREL

SELECT rn,

       id_lot,

       amt_sqft,

       amt_price,

       wct.MovingCORREL(amt_price, amt_sqft, 5, ROW_NUMBER() OVER (ORDER BY rn), 

                 NULL) as CORREL

FROM #se;

--Clean up

DROP TABLE #se;

This produces the following result.

{"columns":[{"field":"rn","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"id_lot","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"amt_sqft","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"amt_price","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"CORREL"}],"rows":[{"rn":"1","id_lot":"21783","amt_sqft":"1147","amt_price":"393918","CORREL":"NULL"},{"rn":"2","id_lot":"94729","amt_sqft":"1313","amt_price":"470479","CORREL":"1"},{"rn":"3","id_lot":"33028","amt_sqft":"1433","amt_price":"512474","CORREL":"0.997250449708387"},{"rn":"4","id_lot":"59446","amt_sqft":"1724","amt_price":"610477","CORREL":"0.996692220392964"},{"rn":"5","id_lot":"97646","amt_sqft":"1162","amt_price":"388196","CORREL":"0.994211415947064"},{"rn":"6","id_lot":"44823","amt_sqft":"1813","amt_price":"636916","CORREL":"0.995557722348421"},{"rn":"7","id_lot":"88397","amt_sqft":"1105","amt_price":"374348","CORREL":"0.995683821294555"},{"rn":"8","id_lot":"13588","amt_sqft":"1555","amt_price":"559149","CORREL":"0.996427794541849"},{"rn":"9","id_lot":"13891","amt_sqft":"1775","amt_price":"623900","CORREL":"0.997799841713257"},{"rn":"10","id_lot":"90957","amt_sqft":"1585","amt_price":"563947","CORREL":"0.997214166156517"},{"rn":"11","id_lot":"44167","amt_sqft":"1510","amt_price":"529806","CORREL":"0.996966940563728"},{"rn":"12","id_lot":"75533","amt_sqft":"1628","amt_price":"592533","CORREL":"0.994608422607885"},{"rn":"13","id_lot":"56812","amt_sqft":"1145","amt_price":"408634","CORREL":"0.994251992678361"},{"rn":"14","id_lot":"12897","amt_sqft":"1632","amt_price":"589522","CORREL":"0.993876395060519"},{"rn":"15","id_lot":"93826","amt_sqft":"1850","amt_price":"668852","CORREL":"0.996919052487866"},{"rn":"16","id_lot":"74510","amt_sqft":"1867","amt_price":"633400","CORREL":"0.984412158236675"},{"rn":"17","id_lot":"17262","amt_sqft":"1587","amt_price":"552178","CORREL":"0.982927101413885"},{"rn":"18","id_lot":"30929","amt_sqft":"1809","amt_price":"633141","CORREL":"0.987103002853674"},{"rn":"19","id_lot":"49030","amt_sqft":"1521","amt_price":"555713","CORREL":"0.944542077284074"},{"rn":"20","id_lot":"33431","amt_sqft":"1195","amt_price":"434542","CORREL":"0.983842801937084"}]}