linearTimeTrend

Syntax

linearTimeTrend(X, window, [intercept=true], [minPeriods])

Please see TALib for the parameters and windowing logic.

Details

Calculate the moving linear regression for X. Perform linear regression over a forward-looking window of size window for each element in X.

When X contains null values, the handling depends on whether minPeriods is specified:

  • If minPeriods is not specified, null values before the first non-null element are excluded from calculation and the corresponding results remain null; null values after the first non-null element are treated as 0.

  • If minPeriods is specified, the window still slides by position. Null values remain in the window but are excluded from calculation and are not treated as 0. If the number of valid data points in the window is less than minPeriods, the output for that position is null.

Parameters

X is a vector/matrix/table.

window is a positive integer indicating the size of the sliding window.

intercept (optional) is a BOOL scalar, default true. It specifies whether the linear regression includes an intercept term. If set to false, only beta is calculated.

minPeriods (optional) is an INT scalar. It specifies the minimum number of valid (non-null) data points required within the window for computation.

  • When minPeriods is not specified, the function follows the legacy behavior: null values before the first non-null element are ignored and preserved in the result; null values after the first non-null element are treated as 0.

  • When minPeriods is specified, all null values are ignored. If the number of valid data points in the window is less than minPeriods, the output for that position is null.

Returns

Return a tuple with 2 elements, alpha (the Linear regression intercept LINEARREG_INTERCEPT) and beta (the linear regression slope LINEARREG_SLOPE).

Examples

Example 1: Use the intercept parameter for regression without intercept.

x = 3 3 5 7 8 9 10 11 15 13 12 11 10
// Default intercept=true, with intercept
linearTimeTrend(x, 3)
// output: ([,,2.666666666666666,3,5.166666666666667,7,8,9,9.5,12,14.833333333333333,13,12],[,,1,2,1.5,1,1,1,2.5,1,-1.5,-1,-1])
// intercept=false, without intercept. Alpha is NULL.
linearTimeTrend(x, 3, false)
// output: ([,,,,,,,,,,,,],[,,2.6,3.8,4.6,5.200000000000001,5.8,6.400000000000001,8.199999999999999,8.199999999999999,7.400000000000001,6.8,6.200000000000001])

Example 2: Use the minPeriods parameter to set the minimum number of valid data points within the window.

x = NULL NULL 3 NULL 5 7 8
linearTimeTrend(x, 3)
// output: ([,,,,1.666666666666667,0.5,5.166666666666667],[,,,,1,3.5,1.5])

linearTimeTrend(x, 3, true, 3)
// output: ([,,,,,,5.166666666666667],[,,,,,,1.5])

Example 3

n = 10
t = table(09:00:00 + 1..n as time, rand(`A`B, n) as sym, rand(100.0, n) as val1, rand(1000..2000, n) as val2)
select time, sym, linearTimeTrend(val1, 3) as `alpha`beta from t
time sym alpha beta
09:00:01 B
09:00:02 A
09:00:03 A 85.0844 -30.0688
09:00:04 B 49.3461 7.3621
09:00:05 B 30.4248 28.3589
09:00:06 A 83.106 -7.7515
09:00:07 B 78.4412 -17.7575
09:00:08 A 56.8575 4.4732
09:00:09 A 53.8492 -6.0653
09:00:10 A 61.7888 -4.5586