Logo

MovingTEST

Updated 2023-11-13 21:52:23.700000

Syntax

SELECT [westclintech].[wct].[MovingTTEST](
  <@X, float,>
 ,<@Y, float,>
 ,<@TAILS, int,>
 ,<@TTYPE, int,>
 ,<@Offset, int,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>
 ,<@Exact, bit,>)

Description

Use the scalar function MovingTTEST to calculate the Student’s t-Test of column values in an ordered resultant table, without the need for a self-join. The t-Test 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 MovingTTEST calculation. @Id allows you to specify multiple moving t-Test 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.

@Exact

a bit value which tells the function whether or not to return a NULL value if the number of rows in the window is smaller the @Offset value. If @Exact is 'True' and the number of rows in the window is less the @Offset then a NULL is returned. @Exact is an expression of type bit or of a type that can be implicitly converted to bit.

@TAILS

specifies the number of distribution tails. If @Tails = 1 the one-tailed distribution is returned. If @Tails = 2 the two-tailed distribution is returned. @TAILS is an expression of type int or of a type that can be implicitly converted to int.

@TTYPE

is the kind of t-Test to perform. If @TTYPE =1, perform the paired test. If @TTYPE = 2, perform the two-sample equal variance test. If @TTYPE = 3, perform the two-sample unequal variance test. @TTYPE 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 t-Testis 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.

To calculate the t-Test from the beginning of a dataset or a partition, use the RunningTTEST function.

To calculate the t-Test for an entire data set or for an entire group within a data set use the TTEST function.

If @RowNum is equal to 1, MovingTTEST is equal to zero

@RowNum must be in ascending order.

If @Exact IS NULL then @Exact = 'True'

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 have 20 rows of data and we want to calculate the t-Test for x and y over a window of 10 rows.

SELECT rn,

       x,

       y,

       wct.MovingTTEST(   x,                               --@X

                          y,                               --@Y

                          1,                               --@TAILS

                          1,                               --@TTYPE

                          10,                              --@Offset

                          ROW_NUMBER() OVER (ORDER by rn), --@RowNum

                          NULL,                            --@Id

                          'True'                           --@Exact

                      ) as [PAIRED T TEST]

FROM

(

    VALUES

        (1, 102, 118),

        (2, 142, 89),

        (3, 110, 72),

        (4, 110, 101),

        (5, 96, 133),

        (6, 101, 97),

        (7, 99, 110),

        (8, 96, 112),

        (9, 96, 101),

        (10, 126, 97),

        (11, 98, 102),

        (12, 105, 107),

        (13, 108, 52),

        (14, 126, 152),

        (15, 72, 148),

        (16, 114, 60),

        (17, 111, 70),

        (18, 100, 69),

        (19, 118, 100),

        (20, 84, 66)

) n (rn, x, y);

This produces the following result.

{"columns":[{"field":"rn","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"PAIRED T TEST"}],"rows":[{"rn":"1","x":"102","y":"118","PAIRED T TEST":"NULL"},{"rn":"2","x":"142","y":"89","PAIRED T TEST":"NULL"},{"rn":"3","x":"110","y":"72","PAIRED T TEST":"NULL"},{"rn":"4","x":"110","y":"101","PAIRED T TEST":"NULL"},{"rn":"5","x":"96","y":"133","PAIRED T TEST":"NULL"},{"rn":"6","x":"101","y":"97","PAIRED T TEST":"NULL"},{"rn":"7","x":"99","y":"110","PAIRED T TEST":"NULL"},{"rn":"8","x":"96","y":"112","PAIRED T TEST":"NULL"},{"rn":"9","x":"96","y":"101","PAIRED T TEST":"NULL"},{"rn":"10","x":"126","y":"97","PAIRED T TEST":"0.299580396651529"},{"rn":"11","x":"98","y":"102","PAIRED T TEST":"0.250900153326152"},{"rn":"12","x":"105","y":"107","PAIRED T TEST":"0.471526926437087"},{"rn":"13","x":"108","y":"52","PAIRED T TEST":"0.390598624967712"},{"rn":"14","x":"126","y":"152","PAIRED T TEST":"0.445181389200508"},{"rn":"15","x":"72","y":"148","PAIRED T TEST":"0.324842150299173"},{"rn":"16","x":"114","y":"60","PAIRED T TEST":"0.496862608496906"},{"rn":"17","x":"111","y":"70","PAIRED T TEST":"0.351317444959803"},{"rn":"18","x":"100","y":"69","PAIRED T TEST":"0.23411642890432"},{"rn":"19","x":"118","y":"100","PAIRED T TEST":"0.185528301296675"},{"rn":"20","x":"84","y":"66","PAIRED T TEST":"0.20513375110675"}]}

In this example, we have 20 rows of data and we want to calculate the t-Test for x and z as well as y and z starting from the first with a window size of 10. Notice that each t-Test columns requires a different @Id . Also note that we have set @Exact to 'False' .

SELECT rn,

       x,

       y,

       z,

       wct.MovingTTEST(x, z, 1, 1, 10, ROW_NUMBER() OVER (ORDER by rn), NULL, 

                 'False') as [PAIRED T TEST xz],

       wct.MovingTTEST(y, z, 1, 1, 10, ROW_NUMBER() OVER (ORDER by rn), 1, 'False')

                 as [PAIRED T TEST yz]

FROM

(

    VALUES

        (1, 102, 106, 118),

        (2, 142, 99, 89),

        (3, 110, 99, 72),

        (4, 110, 119, 101),

        (5, 96, 106, 133),

        (6, 101, 95, 97),

        (7, 99, 90, 110),

        (8, 96, 110, 112),

        (9, 96, 101, 101),

        (10, 126, 111, 97),

        (11, 98, 79, 102),

        (12, 105, 93, 107),

        (13, 108, 96, 52),

        (14, 126, 82, 152),

        (15, 72, 107, 148),

        (16, 114, 96, 60),

        (17, 111, 92, 70),

        (18, 100, 122, 69),

        (19, 118, 105, 100),

        (20, 84, 93, 66)

) n (rn, x, y, z);

This produces the following result.

{"columns":[{"field":"rn","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"z       P","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"IRED T TEST xz       P"},{"field":"IRED T TEST yz"}],"rows":[{"rn":"1","x":"102","y":"106","z       P":"118","IRED T TEST xz       P":"NULL","IRED T TEST yz":"NULL"},{"rn":"2","x":"142","y":"99","z       P":"89","IRED T TEST xz       P":"0.343324287673918","IRED T TEST yz":"0.471142061623696"},{"rn":"3","x":"110","y":"99","z       P":"72","IRED T TEST xz       P":"0.177573653110776","IRED T TEST yz":"0.268637501554641"},{"rn":"4","x":"110","y":"119","z       P":"101","IRED T TEST xz       P":"0.1323245043316","IRED T TEST yz":"0.143912633939745"},{"rn":"5","x":"96","y":"106","z       P":"133","IRED T TEST xz       P":"0.300831927745503","IRED T TEST yz":"0.381776828090856"},{"rn":"6","x":"101","y":"95","z       P":"97","IRED T TEST xz       P":"0.279567054776969","IRED T TEST yz":"0.39318719664846"},{"rn":"7","x":"99","y":"90","z       P":"110","IRED T TEST xz       P":"0.32295200882193","IRED T TEST yz":"0.456931716246789"},{"rn":"8","x":"96","y":"110","z       P":"112","IRED T TEST xz       P":"0.392573746764456","IRED T TEST yz":"0.441763479139861"},{"rn":"9","x":"96","y":"101","z       P":"101","IRED T TEST xz       P":"0.413799013100269","IRED T TEST yz":"0.441053495066801"},{"rn":"10","x":"126","y":"111","z       P":"97","IRED T TEST xz       P":"0.299580396651529","IRED T TEST yz":"0.456999645393713"},{"rn":"11","x":"98","y":"79","z       P":"102","IRED T TEST xz       P":"0.250900153326152","IRED T TEST yz":"0.466511297089124"},{"rn":"12","x":"105","y":"93","z       P":"107","IRED T TEST xz       P":"0.471526926437087","IRED T TEST yz":"0.314516089286348"},{"rn":"13","x":"108","y":"96","z       P":"52","IRED T TEST xz       P":"0.390598624967712","IRED T TEST yz":"0.43303571802974"},{"rn":"14","x":"126","y":"82","z       P":"152","IRED T TEST xz       P":"0.445181389200508","IRED T TEST yz":"0.156716066016777"},{"rn":"15","x":"72","y":"107","z       P":"148","IRED T TEST xz       P":"0.324842150299173","IRED T TEST yz":"0.136051578624049"},{"rn":"16","x":"114","y":"96","z       P":"60","IRED T TEST xz       P":"0.496862608496906","IRED T TEST yz":"0.250331068591402"},{"rn":"17","x":"111","y":"92","z       P":"70","IRED T TEST xz       P":"0.351317444959803","IRED T TEST yz":"0.383258436236536"},{"rn":"18","x":"100","y":"122","z       P":"69","IRED T TEST xz       P":"0.23411642890432","IRED T TEST yz":"0.434966545194386"},{"rn":"19","x":"118","y":"105","z       P":"100","IRED T TEST xz       P":"0.185528301296675","IRED T TEST yz":"0.419710117770743"},{"rn":"20","x":"84","y":"93","z       P":"66","IRED T TEST xz       P":"0.20513375110675","IRED T TEST yz":"0.382556543757788"}]}