Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Factor Mean & Standard Deviation

Hey there everyone,

I'm fairly new to Quantopian and I'm having trouble figuring out how to solve a problem with Pipelines and Alphalens.

I was analyzing the forward PE ratio in Alphalens in the notebook, and it would appear that values near the mean have high returns that get progressively lower the further away we get from the mean.

The numeric data in the analysis is garbage because Alphalens assumes that the first quartile should have relatively negative returns and that the last quartile should have relatively positive returns.

I would like to transform this data to adhere to these assumptions, and I think that the number of standard deviations from the mean of the factor would be a better representation of what I'm looking for. I'm not sure of the syntax of how to get this from the data though.

2 responses

Maybe consider using the 'demean' and the 'abs' (absolute value) methods. Something like this.

    # Get the pe ratios  
    pe = Fundamentals.forward_pe_ratio.latest  


    # Use the demean method to subtract the mean from each value  
    # PE values close to the mean will now be 'demeaned' close to zero  
    # High values will be higher positive and low values will negative  
    # While we're at it lets filter out the stupid high PE values  
    pe_demean = pe.demean(mask= pe < 100)  


    # Since either a high OR a low value is considered the same  
    # Use the absolute value method to flip the negatives to positive  
    # Use a negative to then reverse so the best (closest to zero) are highest  
    testing_factor = -pe_demean.abs()


Attached is your notebook with this modification. It does appear you are correct the further from the mean PE ratio the worse the return.

Good luck.

That's exactly what I was looking for (and computationally less expensive)! Thanks Dan!