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 returns on market.

Each window is the lookback rows ending at a row; only the rows where both the column’s return and market are finite count, and a window with fewer than min_bars of them, or with a constant market, 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.]])