quantlab.factor.kunquant_cs#

Custom KunQuant cross-sectional operators: each symbol against the others on one bar.

KunQuant is the library this project uses to compute most factors. A factor formula is written as a graph of operators (WindowedAvg, Rank, …), and KunQuant compiles that graph to native C++ code that runs over a whole (timestamp, symbol) array at once. This module adds operators that work on one bar at a time; quantlab.factor.kunquant_ts has the ones that look along time.

CrossSectionalZScore is a cross-sectional normalization: at each timestamp, each symbol is compared with all the other symbols on the same bar. Its time-series counterpart, kunquant_ts.WindowedZScore, compares each symbol with its own recent past; which one is right depends on the strategy that consumes the factor.

CrossSectionalWinsorize (winsorizing, 缩尾) and CrossSectionalTrim (trimming, 截尾) handle outliers on each bar: the first clips values to that bar’s lower and upper quantiles across symbols, the second replaces values outside them with NaN. They are typically applied before a cross-sectional z-score so that a few extreme symbols do not dominate its mean and spread.

CrossSectionalWeightedMean, CrossSectionalTopN, CrossSectionalSigmaClip, CapWeightedStandardize, the weighted least-squares residual CrossSectionalWLSResidual and CrossSectionalIndustrySizeFill are the estimation-universe tools of a Barra-style risk factor (quantlab.factor.predefined.barra), usable by any factor. CrossSectionalNeutralize fits the same industry and size regression as CrossSectionalIndustrySizeFill and keeps the residual: it neutralizes a factor against industry, size or both. RenormalizedCombine is elementwise, with no time or symbol window; it is here because it is a per-bar step of that same pipeline (combining descriptors).

Classes

CapWeightedStandardize

Standardize to a weighted mean of 0 and an equal-weighted std of 1 in a universe.

CrossSectionalIndustrySizeFill

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

CrossSectionalNeutralize

Neutralize y against industry, size or both: its residual from a per-bar regression.

CrossSectionalSigmaClip

Drop data errors and clip outliers at multiples of a universe's standard deviation.

CrossSectionalTopN

1 for the n symbols with the largest v on each bar, 0 elsewhere.

CrossSectionalTrim

Cross-sectional trimming (截尾): drop values outside each bar's quantile bounds.

CrossSectionalWLSResidual

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

CrossSectionalWeightedMean

Weighted mean of v across symbols per bar, broadcast to every symbol.

CrossSectionalWinsorize

Cross-sectional winsorization (缩尾): clip each bar to its quantile bounds.

CrossSectionalZScore

Cross-sectional z-score, (x - mean_t) / std_t over symbols per bar.

RenormalizedCombine

Fixed-weight sum of several values, renormalized over the ones present.