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

Anyone understand this error:

MaxLossExceededErrorTraceback (most recent call last)
in ()
10 groupby_labels=MORNINGSTAR_SECTOR_CODES,
11 periods=periods,
---> 12 quantiles = 5)

/usr/local/lib/python2.7/dist-packages/alphalens/utils.pyc in call_w_context(*args, **kwargs) 609 "replaced by 'binning_by_group'",
610 category=DeprecationWarning, stacklevel=3)
--> 611 return func(*args, **kwargs)
612 return call_w_context
613

/usr/local/lib/python2.7/dist-packages/alphalens/utils.pyc in get_clean_factor_and_forward_returns(factor, prices, groupby, binning_by_group, quantiles, bins, periods, filter_zscore, groupby_labels, max_loss) 768 quantiles=quantiles, bins=bins,
769 binning_by_group=binning_by_group,
--> 770 max_loss=max_loss)
771
772 return factor_data

/usr/local/lib/python2.7/dist-packages/alphalens/utils.pyc in get_clean_factor(factor, forward_returns, groupby, binning_by_group, quantiles, bins, groupby_labels, max_loss) 564 message = ("max_loss (%.1f%%) exceeded %.1f%%, consider increasing it."
565 % (max_loss * 100, tot_loss * 100))
--> 566 raise MaxLossExceededError(message)
567 else:
568 print("max_loss is %.1f%%, not exceeded: OK!" % (max_loss * 100))

MaxLossExceededError: max_loss (35.0%) exceeded 90.1%, consider increasing it.

5 responses

Found the problem.

Do you know what this error actually means? I run into it every so often but I'm not sure what it means or what triggers it

Solution?

The key to understanding the MaxLossExceededError error is to look at the error description. In the above example it was

Dropped 90.1% entries from factor data: 90.1% in forward returns computation and 0.0% in binning phase

There are two distinct issues that cause this error. First, not enough forward returns to analyze the factor over the entire time period. And second, not being able to place the factor results into the desired bins.

The error above states '90.1% in forward returns computation'. So, the cause of this error is with not enough forward returns. Basically, one needs to ensure that pricing is fetched for all assets being analyzed, and all the dates in the factor output are covered. The following will get all the assets ever referenced in the factor out.

asset_list = results.index.levels[1].unique()

To ensure there is pricing data for all the dates for those assets, just make certain the get_pricing start_date is on or before the factor start_date. Then, make certain the get_pricing end_date is at least longer than the factor end_date plus the longest 'period' one wants to analyze forward returns for. In this case we wanted to analyze 21 day returns. That means we need to get prices 21 trading days beyond the end_date of the factor. In this case the factor dates and the associated get-pricing call were

results = run_pipeline(pipe, start_date='2006-01-01', end_date='2017-07-15')

prices = get_pricing(asset_list, start_date='2016-06-15', end_date='2017-08-30', fields='open_price')  
periods = (1,3,5,10,21)

Notice anything odd? The end_date for get_pricing looks good. It's more than a month after the end_date for the pipeline. That will cover the required 21 trading days past the pipeline end_date. However, the get_pricing start_date is waaaay after the pipeline start_date. That needs to be set to 2006-01-01 the same as the pipeline start date.

So, just check those three things 1) get all the stocks? 2) start_date set equal to the pipeline start-date? and 3) end_date greater than the pipeline end_date plus the longest returns period one is analyzing? If those are all ok then one should never (maybe no such thing as never) get a MaxLossExceededError due to forward returns.

We'll save what to do if the MaxLossExceededError is due to binning for another post.

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.

Thanks Dan. I was having this issue and it was due to binning. My filters were way too narrow. Once I eased up the filters, it solved the problem.