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 the top highest-ranked assets and short the bottom lowest, 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 >= 0 and top + bottom must not exceed n_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 pred is not 2-D, if top or bottom is negative, or if top + bottom exceeds 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