quantlab.factor.predefined.benchmark_beta.rolling_beta#
- quantlab.factor.predefined.benchmark_beta.rolling_beta(returns, market, lookback, min_bars)[source]#
Return the rolling OLS slope of each column of
returnsonmarket.Each window is the
lookbackrows ending at a row; only the rows where both the column’s return andmarketare finite count, and a window with fewer thanmin_barsof them, or with a constantmarket, is NaN.- Parameters:
- returnsarray-like
Returns on
[T, S].- marketarray-like
The benchmark’s returns on
[T].- lookbackint
Rows per window.
- min_barsint
Fewest rows a window needs.
- Returns:
- np.ndarray
The slopes on
[T, S].
Examples
>>> market = np.array([0.01, -0.02, 0.03, 0.0]) >>> rolling_beta(np.column_stack([2 * market, -market]), market, lookback=3, min_bars=2).round(6) array([[nan, nan], [ 2., -1.], [ 2., -1.], [ 2., -1.]])