roll_beta

Defined in fynance.features.roll_functions

roll_beta(x, y, w=63)[source]

Trailing rolling OLS slope of x regressed on y.

\[roll\_beta^w_t(x, y) = \frac{roll\_cov^w_t(x, y)}{roll\_var^w_t(y)}\]

i.e. the slope of the univariate OLS regression of x on y over the trailing window \([t - w + 1, t]\) (inclusive of t, see roll_cov). Typical use is a rolling hedge ratio: how many units of y are needed to hedge one unit of x. As with roll_corr, the 1/w normalization cancels, so the result does not depend on ddof.

Parameters:
xnp.ndarray[float64, ndim=1]

Dependent series. Cast to float64 if needed; must not contain NaN.

ynp.ndarray[float64, ndim=1]

Independent (regressor) series, same length as x. Cast to float64 if needed; must not contain NaN.

wint, optional

Size of the trailing window, must be an integer >= 2. Default is 63.

Returns:
np.ndarray[float64, ndim=1]

Trailing rolling OLS slope. The first w - 1 entries are np.nan (insufficient history). If y has zero variance within a window, that entry is np.nan (checked explicitly before dividing — no RuntimeWarning is raised).

See also

roll_cov, roll_corr

Examples

y = 2 * x has a constant beta of 0.5 (cov(x, y) / var(y) = 2 var(x) / 4 var(x)), regardless of the window content:

>>> x = np.array([1., 2., 3., 4., 5.])
>>> y = 2 * x
>>> roll_beta(x, y, w=3)
array([nan, nan, 0.5, 0.5, 0.5])