Back to Community
Alphalens with a single security?

Hello all,

I'd like to use Alphalens to evaluate a factor on just one security. However, I always end up with an error: ValueError: Bin edges must be unique I've tried to work around it using ranking instead of raw numbers (guaranteed no duplicate values), trying bins instead of quantiles, using defined bins instead of equally-sized, etc, but I cannot seem to get it going.

This Pull Request says that this error happens when you have a "single value for factor per group (only one value over a date, or sector)" - pretty sure this is what I'm hitting.

Big picture question: Can Alphalens be used to check out signal alpha on a single equity?

More direct question: If so, what do I need to do to get it working? (Please see attached notebook)

Loading notebook preview...
5 responses

You are getting that error due to bugs in panda.qcut and pandas.cut that were fixed in version v0.20.0 (one, two and three). But don't expect to see pandas v0.20.0 on Quantopian too soon (this is my opinion but v0.20.0 hasn't been released yet). You can use a workaround described here though

Hi Peter,

I don't think Alphalens will give sensible results for one security. It is set up to consider a factor as one value per day per security, which it then 'bins' into say 3 or 5 or 10 groups of securities and shows you the returns to each of those sub-portfolios.

If you pass it a timing signal for a single stock it can only create 1 'bin' on any given day and doesn't have anything to compare amongst. Alphalens is a tool designed to evaluate a cross-sectional signal which can be used to rank many securities each day versus what sounds like a timing signal that tells you what days to be long or short a single stock.

Have you tried out using just the pyfolio tearsheet analysis or a backtest report on your signal? I'd think those should work ok.

Best, Jess


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.

While I agree with Jessica, there is still a subset of Alphalens features that can be used to quickly verify the ability of a signal to generate alpha: the returns analysis. I used Alphalens to analyse signals generated by technical patterns in here. No assumptions on the number of stocks are required.

Obviously the goal of Alphalens is not to analyse those kind of scenarios, but it works nevertheless.

Here is Peter's NB fixed.

Loading notebook preview...

Hi Jess - I intend to get going with the backtest tearsheets, etc, soon - thanks for that thought. I wanted to give this a once-over in Alphalens first though. I think the use case of "I have a signal that I want to see how it applies to a single stock" is a reasonable one for Alphalens - I'm finding it useful.

Luca - that's a ton for your suggestion! It worked like a champ. My googling around didn't find your GitHub response, so I'm glad the solution is preserved here in the community as well. It did the trick and is giving me good insight.

Tried cloning and running Luca's version of Peter's NB, and I when I run create_returns_tear_sheet(factor_data, long_short=False) , I get:

TypeError: ('rate_of_return() takes exactly 2 arguments (1 given)',
u'occurred at index 1D')

Did something change? Or am I not doing something right?