ta.regress
Regress one series on another to get intercept, slope, fit and the current residual.
ta.regress(y, x, length, min=length)Ordinary least squares of y on x over the window: [intercept, slope, r-squared, this bar's residual]. beta = cov(y, x)/var(x) and is the same number ta.beta reports. alpha = mean(y) - beta*mean(x). r2 is the square of ta.correlation over the same window. The residual is y - (alpha + beta*x) on this bar, which is the tradeable spread rather than a diagnostic. It is the two-series twin of ta.linreg, which regresses one series on bar position.
Parameters
A series parameter takes a value on every bar. A constant parameter must be known before the first bar, such as a number or an input.
| Name | Kind | Type | Default | Range | Description |
|---|---|---|---|---|---|
y | Series | number | - | - | The dependent series. |
x | Series | number | - | - | The explanatory series. |
length | Constant | number | - | 1 to 10000 | Window in observed bars. |
min | Constant | number | - | 1 to 10000 | Valid pairs the window must hold before it reports (an option, written min=40). Pairs with an na on either side are skipped. Absent = length: the window must be full and gap-free, which is the reset rule. |
Outputs
| Name | Type |
|---|---|
alpha | number |
beta | number |
r2 | number |
residual | number |
The outputs come back in this order: [alpha, beta, r2, residual] = ta.regress(…).
Warm-up and missing values
- Warm-up
lengthbars. Names in the warm-up are this operator’s parameters.- Missing values
- Returns na until the last
lengthbars hold at leastminpairs where both series are present. Pairs with an na on either side are skipped. With min absent (= length) that islengthconsecutive valid pairs, so an na in either series resets the window. All four outputs are na when x has zero variance over the window. r2 alone is na when y has zero variance, because it is then 0/0. The residual is na on a bar where either series is na.
Example
[alpha, beta, r2, residual] = ta.regress(ta.ret(close), ta.ret(open), 100)spy = series(symbol="SPY")
[alpha, beta, r2, residual] = ta.regress(ta.ret(close), ta.ret(spy.close), 100)
plot(beta, "Slope")