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!!!!

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a89aa64245af641095d9e31
There was a runtime error.
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)

Clone Algorithm
1
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a89d21e245af641095da171
There was a runtime error.

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.

Clone Algorithm
4
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a89fdb08f1d2642b367851d
There was a runtime error.