quantlab.factor.kunquant_cs.CrossSectionalIndustrySizeFill#

class quantlab.factor.kunquant_cs.CrossSectionalIndustrySizeFill(y, size, industry, w, universe, eligible, use_industry=True, use_size=True)[source]#

Bases: _IndustrySizeRegression

Fill a missing value from a per-bar weighted regression on industry and size.

On each bar the fit uses the symbols where universe > 0, y is finite, w is finite and positive, and the regressors used are finite (industry an integer code from 0 to 4096; any other code counts as missing). An infinite y counts as missing. The model is one intercept per industry plus a common slope on size, fitted by weighted least squares without a matrix solve (Frisch-Waugh-Lovell): y and size are demeaned within each industry with the weights, the slope is b = sum(w dy ds) / sum(w ds**2), and an industry’s intercept is mean_y - b mean_size over its fit members. Then:

out = y                                    where y is finite
out = intercept[industry] + b * size       where y is missing and eligible > 0

a missing value whose industry has no fit member, or whose regressors are missing, stays NaN. USE4 imputes a style a stock lacks this way, from a regression on industry and size. Constructing it with use_industry=False fits one common intercept; with use_size=False it fits the industry means alone (the slope is 0, and also when size does not vary within the industries, below 1e-10 of its spread). Each pair of flags gets a class of its own (CrossSectionalIndustrySizeFill_industry_size), whose C++ has the flags written in; isinstance still holds. CrossSectionalNeutralize fits the same model and keeps the residual instead. The batch-start and SIMD-width notes of CrossSectionalZScore apply.

Parameters:
yOpBase

The values to fill, such as a style exposure.

sizeOpBase

The size regressor, such as the Size exposure.

industryOpBase

The industry code, a non-negative integer stored as a float.

wOpBase

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

universeOpBase

A mask, positive for the symbols the fit uses.

eligibleOpBase

A mask, positive for the symbols a missing value may be filled for, such as the symbols with a market cap on the bar.

use_industry, use_sizebool, default True

Which regressors the model has; at least one.

Raises:
ValueError

If both flags are false.

Examples

>>> Output(CrossSectionalIndustrySizeFill(beta, size, Input("industry"), Sqrt(cap), estu, live), "beta")
generate_body()[source]#

Return the C++ that fits one bar and fills its missing values.