mbetaTopN
Syntax
mbetaTopN(X, Y, S, window, top, [ascending=true],
[tiesMethod='oldest'])
Please see mTopN for the parameters and windowing logic.
Details
Within a sliding window of given length (measured by the number of elements), the function stably sorts X and Y by S in the order specified by ascending, then calculates the coefficient estimate ordinary-least-squares regressions of Y on X.
Returns
The result is of type DOUBLE, with the same form as the input parameters.
Examples
Using IBM stock as an example, simulate index return rates, stock return rates, and trading volumes for six consecutive trading days:
symbol = take(`IBM, 6)
tradeDate = 2024.01.02 2024.01.03 2024.01.04 2024.01.05 2024.01.08 2024.01.09
indexRet = [0.6, 1.1, -0.2, 0.9, 1.3, 0.4]
stockRet = [0.9, 1.7, -0.1, 1.4, 1.9, 0.8]
tradeVolume = [520, 860, 610, 940, 650, 880]
stockDaily = table(symbol, tradeDate, indexRet, stockRet, tradeVolume)
stockDaily;
Output:
| symbol | tradeDate | indexRet | stockRet | tradeVolume |
|---|---|---|---|---|
| IBM | 2024.01.02 | 0.6 | 0.9 | 520 |
| IBM | 2024.01.03 | 1.1 | 1.7 | 860 |
| IBM | 2024.01.04 | -0.2 | -0.1 | 610 |
| IBM | 2024.01.05 | 0.9 | 1.4 | 940 |
| IBM | 2024.01.08 | 1.3 | 1.9 | 650 |
| IBM | 2024.01.09 | 0.4 | 0.8 | 880 |
Over the most recent four trading days, select the top two trading days by trading volume and use the least squares method to estimate the regression coefficient of indexRet on stockRet:
mbetaTopN(X=stockRet, Y=indexRet, S=tradeVolume, window=4, top=2, ascending=false)
// Output: [ , 1.6, 1.3846, 1.5, 1.5, 1.2]
- stockRet is the regression independent variable, and indexRet is the regression dependent variable;
- tradeVolume is used to select the most active trading days;
- For the data on 2024.01.09, the two trading days with the highest trading volume in the most recent four-day window correspond to the samples (0.9, 1.4) and (0.4, 0.8);
- Based on these two samples, the least squares estimate of the regression coefficient of indexRet on stockRet is 1.2.
Related function: mbeta
