Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Quantopian Lecture Series: Overfitting

Overfitting bias has a way of subtly creeping into your analysis, in fact, overfit backtests is one of the biggest issues in quantitative finance. In this notebook we discuss some forms of overfitting and some ways to deal with it.

The lecture will be presented at this meetup. We will be releasing a video lecture as well, watch this thread for a link.

Also in this lecture:

This is part of Quantopian’s Summer Lecture Series. We are currently developing a quant finance curriculum and will be releasing clone-able notebooks and algorithms to teach key concepts. Stay tuned for more. We are also working on a permanent home for all of our notebooks.

Credit for the notebooks goes to Evgenia 'Jenny' Nitishinskaya, and credit for the algorithms goes to David Edwards.

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.

7 responses

Great stuff, this paper is also relevant in this regard: http://www.ams.org/notices/201405/rnoti-p458.pdf

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.

It seems that you forgot to clear your position at the end of a loop in trade(stock, length) function (so money earned is calculated incorrectly).
Simple fix is this:

return money + count*stock[-1]

As a result the last plot (showing earnings for different mean reversion window lengths in 2013-2015 and 2011-2013) is completely incorrect — there are no such dramatical changes as your plot has.

I think it may have been a mistake to define a variable as "count" . If you want to save these values into a dictionary or data frame and work some arithmetic with the column "count" you will discover Python considers it a method......count() as does pandas.....
Replace it with a non-reserved word.

    money = 0  
    count = 0  
    for i in range(len(stock)):  
        # Sell short if the z-score is > 1  
        if zscores[i] > 1:  
            money += stock[i]  
            count -= 1  
        # Buy long if the z-score is < 1  
        elif zscores[i] < -1:  
            money -= stock[i]  
            count += 1  
        # Clear positions if the z-score between -.5 and .5  
        elif abs(zscores[i]) < 0.5:  
            money += count*stock[i]  
            count = 0  

Dmitry Ivanov
I only spotted that by exporting to a dataFrame and noting I had to do a manual calculation on the last row.

Very much liked the simplicity of the mean reversion system for stocks, even though its sole purpose was to demonstrate the dangers of over fitting. I amended it and posted a notebook on my website.
https://zenothestoic.com/2018/12/10/a-simple-mean-reversion-system-for-stocks/

It is of course a trivial attempt, only works on daily data and needs fixed fractional position sizing as well as provision for slippage and commission. Nonetheless I find it helpful when looking at a system to have the ability to load my own data and to produce CSV files which can be further analysed in excel.

For what it is worth here is a trend following system based on switching around the buy / sell signals from the mean reversion system.
https://gist.github.com/AnthonyFJGarner/6ee79ac658607866c42e1b0ca3ee4d2f

It would be trivially easy to adapt this to a Quantopian research notebook if preferred.

Silly stuff but these ultra simple systems are quite good for beginners learning the basics.

Unfortunately the little demo mean reversion system featured in this notebook is wrong. Incorrectly drafted and fatally flawed. Mr Ivanov pointed out one serious error but there is another, equally invidious mistake which I will probably correct on my Gist when I can be bothered.

The error in this joky little demo system does not invalidate the points being made re overfitting.

Nonetheless it is unfortunate.