quantlab.factor.kunquant_cs.CapWeightedStandardize#

class quantlab.factor.kunquant_cs.CapWeightedStandardize(v, w, universe)[source]#

Bases: GenericCrossSectionalOp

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

On each bar, with U the symbols where universe > 0 and v is finite:

mean = sum(w * v) / sum(w)       over U where w is finite and > 0
std  = sample std of v (ddof=1)  over U, equally weighted
out  = (v - mean) / std          for EVERY symbol with a valid v

This is USE4’s standardization with w the market cap: the cap-weighted universe then has zero exposure, and the universe’s exposures have unit spread. Symbols outside the universe are shifted and scaled by the same numbers, so they get exposures too. NaN and infinite inputs come out NaN; a bar whose mean or std is undefined, or whose std is 0, is NaN for every symbol.

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

Parameters:
vOpBase

The raw values, such as a descriptor.

wOpBase

The weights of the mean, such as the previous bar’s market cap.

universeOpBase

A mask, positive inside the universe (CrossSectionalTopN’s 1).

Examples

>>> cap = BackRef(Input("marketcap"), 1)
>>> estu = CrossSectionalTopN(cap, 3000)
>>> Output(CapWeightedStandardize(Log(Input("marketcap")), cap, estu), "size")
generate_body()[source]#

Return the C++ loops that standardize one bar.

generate_head()[source]#

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