quantlab.factor.kunquant_ts.EWBeta#

class quantlab.factor.kunquant_ts.EWBeta(y, x, window, half_life)[source]#

Bases: _EWWindowOp

Slope of the exponentially weighted least-squares fit of y on x.

The fit y = alpha + beta * x minimizes sum(w * residual**2) over the bars where both are valid, with an intercept. In closed form beta = cov_w(x, y) / var_w(x) (see EWCov and EWVar). NaN for the first window - 1 bars, and NaN or infinite when x does not vary over the valid bars.

Parameters:
yOpBase

The regressand, such as a stock’s excess return.

xOpBase

The regressor, such as the market’s excess return.

windowint

Number of trailing bars, the current one included.

half_lifefloat

Age in bars at which the weight is one half.

Raises:
ValueError

If window < 1 or half_life <= 0.

Examples

>>> Output(EWBeta(Input("excess"), Input("market_excess"), 252, 63), "beta")
decompose(options)[source]#

Expand into two window loops; KunQuant calls this while compiling.