Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help - Limit a Zscore Value

Hi all,

I want to limit a Z-score to 2.5 std (I want to reduce the effect of outliers). As I am calculating the Z-score for the whole universe at each sector, I am having some trouble to limit the variables to those limits.

I know that I can use winsorize to get rid of the outliers, but I want to have a cut of based on std than on the percentiles. I want to add more factors and do some math with the Zscores.

Any ideas how can I limit the variable to -2.5 to 2.5 std?

Thank you!!!!

4 responses

Not sure if this is what you had in mind, but maybe something like this?

    bp_rank = bp.zscore(mask = base_universe, groupby=Sector())

    # Create filter for zscores between -2.5 and 2.5  
    bp_rank_limit_filter = (bp_rank > -2.5) & (bp_rank < 2.5)  


    # <----> FINAL <---->  
    combined_rank = bp_rank

    # Use a mask to eliminate outliers  
    longs = combined_rank.top(10, mask=bp_rank_limit_filter)  
    shorts = combined_rank.bottom(10, mask=bp_rank_limit_filter)

It's a bit clunky, but you could put both factors into a single custom factor, do the factor combination within the factor, and then apply your z-score clipping there, before the custom factor output. I'm sure someone can provide a more elegant approach, but if you are stuck, this should work.

Thank you very much guys.

The problem is that if I filter out the ouliers I will take them out. And my plan is too keep them (I just want to limit their std value). I am planning to add some momentum factors and combine both Zscores (Value and Momentum signals) to select the best stocks. That way I will get the best stock (in std) for both Value and Momentum for each sector of the universe.

Here's an example of combining various factors within a single custom factor. It is not pretty, but you can see that I do incorporate the clipping you want within the custom factor, after the factor combination.