roll_beta¶
Defined in fynance.features.roll_functions
- roll_beta(x, y, w=63)[source]
Trailing rolling OLS slope of
xregressed ony.\[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
xonyover the trailing window \([t - w + 1, t]\) (inclusive oft, seeroll_cov). Typical use is a rolling hedge ratio: how many units ofyare needed to hedge one unit ofx. As withroll_corr, the1/wnormalization cancels, so the result does not depend onddof.- Parameters:
- xnp.ndarray[float64, ndim=1]
Dependent series. Cast to
float64if needed; must not contain NaN.- ynp.ndarray[float64, ndim=1]
Independent (regressor) series, same length as
x. Cast tofloat64if 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 - 1entries arenp.nan(insufficient history). Ifyhas zero variance within a window, that entry isnp.nan(checked explicitly before dividing — noRuntimeWarningis raised).
See also
roll_cov,roll_corr
Examples
y = 2 * xhas 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])