quantlab.factor.kunquant_cs.CrossSectionalWLSResidual#

class quantlab.factor.kunquant_cs.CrossSectionalWLSResidual(y, x, w, universe)[source]#

Bases: GenericCrossSectionalOp

Residual of a per-bar weighted least-squares fit of y on x, with an intercept.

On each bar the fit uses the symbols where universe > 0, y and x are finite and w is finite and positive:

b   = sum(w (x - mx)(y - my)) / sum(w (x - mx)**2)
out = y - my - b (x - mx)          for EVERY symbol with a finite y

mx and my being the weighted means of the fit sample. The residual has zero weighted covariance with x over the fit sample. This orthogonalizes one style against another, as USE4 orthogonalizes Residual Volatility against Beta and Non-linear Size against Size. A symbol whose x is missing is taken at mx and so keeps y - my. A bar with no fit sample, or where x does not vary over it (its centred spread below 1e-10 of its uncentred one), is NaN for every symbol.

The batch-start and SIMD-width notes of CrossSectionalZScore apply.

Parameters:
yOpBase

The values to orthogonalize.

xOpBase

The regressor.

wOpBase

The regression weights, such as the square root of market cap.

universeOpBase

A mask, positive for the symbols the fit uses.

Examples

>>> Output(CrossSectionalWLSResidual(size_cubed, size, Sqrt(cap), estu), "nlsize")
generate_body()[source]#

Return the C++ that fits one bar and writes its residuals.

tolerance is the relative size below which x’s centred spread is taken as zero, so a constant regressor gives NaN, not a slope made of rounding.

generate_head()[source]#

Return the C++ preamble for the generated function, which is empty.