rank¶
Defined in fynance.signal
- rank(pred, top, bottom)[source]
Cross-sectional long/short by rank.
For each time step (row of a 2-D
(T, n_assets)prediction), go long thetophighest-ranked assets and short thebottomlowest, equally weighted within each leg (dollar-neutral).- Parameters:
- predarray-like, shape (T, n_assets)
Cross-sectional predictions.
- top, bottomint
Number of assets in the long and short legs. Both must be
>= 0andtop + bottommust not exceedn_assets– otherwise the legs would overlap and the long assignment would silently overwrite the short, breaking dollar-neutrality.
- Returns:
- numpy.ndarray
Weights of shape
(T, n_assets).
- Raises:
- ValueError
If
predis not 2-D, iftoporbottomis negative, or iftop + bottomexceeds the number of assets.
Examples
>>> import numpy as np >>> w = rank(np.array([[1.0, 2.0, 3.0, 4.0]]), top=1, bottom=1) >>> w.sum() # dollar-neutral 0.0