Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
zscore factor

I'm trying to create a CustomFactor that returns the zscore of the closes for the window length. I'm was attempting to use the mstat.zscore package from scipy but I'm getting some errors. Anyone got any ideas?

class LatestZScore(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 120  
    def compute(self, today, assets, out, closes):  
        out[:] = zscore(closes)  # <= What do?  

I get the following from the above:
ValueError: could not broadcast input array from shape (120,7824) into shape (7824)

6 responses

Hi Matt,

As you can see, for each of the 7824 equities, you are getting 120 z-scores, one for each day in the window_length. Most factors only have one output per equity, which is why out is a 1-dimensional array by default. If you want the factor to output all 120 z-scores for each equity, you can look into writing custom factors with multiple outputs. There are some details here.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Nathan,

Sorry that I'm confusing. I actually just want the 7824 x 1 zscore. I just really don't understand how the closes fit into the zscore function.

So z-scoring is built into pipeline so say you wanted to get the zscore of the Return On Equity values (note this is applicable to any factor) it is just

my_factor = morningstar.operation_ratios.roe.latest.zscore()

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Okay, I got it now. I was not understanding that the return of the zscore is the same size as the input.

class LatestZScore(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 120  
    def compute(self, today, assets, out, closes):  
        out[:] = zscore(closes, axis=0)[-1]  

Thanks for the help!!

@James I believe this is different based on my understanding of the documentation. The zscore method of a Factor is a normalization mechanism and is the zscore over a different axis (axis 1?).

@james, does this calculate the zscore over the Asset axis (so score each latest ROE relatively) or the last 120 ROEs of each stock?. I assume the former?

yeah. it z-scores across the assets so you can have that cross sectional normalization. the zscore function also allows you to group by sector/industry/whatever so you can get a more relative picture.