Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pipeline Feature Request: Percentage rank

Actually in Quantopian it's only possible to rank with the absolute order.

Why not add a percentage rank like in pandas?

pandas.DataFrame.rank ->  
    pct : boolean, default False  
    Computes percentage rank of data  

This is especially useful with combined scores, where the factors have a different size.

1 response

I've just finished my own implementation:

import numpy as np  
from scipy.stats import rankdata

def pct_rank(data, ascending=True):  
    not_finite = ~np.isfinite(data)  
    ranks = rankdata(data, method='dense')  
    ranks[not_finite] = np.nan  
    max = np.nanmax(ranks)  
    if not ascending:  
        ranks = (max + 1) - ranks  
    return ranks / max


class PS_pct_rank(CustomFactor):  
    inputs = [morningstar.valuation_ratios.ps_ratio]  
    window_length = 1  
    def compute(self, today, assets, out, ps_ratio):  
        data = ps_ratio[-1]  
        out[:] = pct_rank(data)  

Anyway it would be great, if you could extend the method rank to provide this feature!