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:
_IndustrySizeRegressionFill a missing value from a per-bar weighted regression on industry and size.
On each bar the fit uses the symbols where
universe > 0,yis finite,wis finite and positive, and the regressors used are finite (industryan integer code from 0 to 4096; any other code counts as missing). An infiniteycounts as missing. The model is one intercept per industry plus a common slope onsize, fitted by weighted least squares without a matrix solve (Frisch-Waugh-Lovell):yandsizeare demeaned within each industry with the weights, the slope isb = sum(w dy ds) / sum(w ds**2), and an industry’s intercept ismean_y - b mean_sizeover 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=Falsefits one common intercept; withuse_size=Falseit fits the industry means alone (the slope is 0, and also whensizedoes 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;isinstancestill holds.CrossSectionalNeutralizefits the same model and keeps the residual instead. The batch-start and SIMD-width notes ofCrossSectionalZScoreapply.- 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")