Overfitting (Good Backtest Results May Be an Illusion)
The backtest has a 40% annualized return, a Sharpe ratio of 2.5, and a 12% maximum drawdown. Tweak the parameters a little and the annualized return becomes 45%. Tweak them again, and it's 50%. It gets better with every adjustment. Something is wrong. A strategy may keep improving in backtesting but fail in live trading. The most likely explanation is overfitting. What Is Overfitting Overfitting occurs when a model treats noise in historical data as a pattern. The strategy performs well in backtests because the model has fitted past random fluctuations too precisely. Think of memorizing every answer from years of past exam questions before an exam. You know the answer to every question. But when the exam changes the questions, you're helpless. The more thoroughly you memorize, the less useful it is for new questions. The more finely you tune a strategy's parameters, the more "familiar" it becomes with this stretch of history—and the worse its ability to generalize to the future. The more parameters there are, the greater the degrees of freedom and the higher the risk of overfitting. For a moving-average crossover strategy, tuning a single period parameter is fine. But if you tune five parameters—the fast moving average, slow moving average, stop-loss level, take-profit level, and position size—and run an exhaustive grid search for the optimal combination, you'll almost certainly find a set that performs exceptionally well historically. But those parameters only happen to match the noise pattern in that particular stretch of history. In-Sample and Out-of-Sample The most basic way to identify overfitting is to split the data into two segments. Use the in-sample data (In-Sample) to tune parameters and select a strategy. The out-of-sample data (Out-of-Sample) does not participate in any parameter tuning and is used only for validation. If a strategy returns 30% annualized in the in-sample period and 20% out of sample, it may not be overfit. If it returns 30% in the sample and 5% out of sample, it is overfit. The problem is that if you inspect the out-of-sample results, feel dissatisfied, go back to tune the parameters, and run the test again, the data is no longer "out of sample." Once you use it to make a decision, it becomes part of the in-sample data. This is called data snooping (Data Snooping). Every time you look at an out-of-sample result, that sample becomes contaminated. You may not realize that you ran 50 sets of parameters and selected the one with the best out-of-sample performance, believing it was a validation result. In reality, you selected the largest value among 50 random variables, and that result does not necessarily indicate good future performance. Walk-Forward Analysis Walk-forward analysis (Walk-Forward Analysis) improves on the in-sample/out-of-sample approach. Divide the timeline into multiple segments. Use the first segment to tune parameters and the second to validate them. Then slide the window forward: use the second segment for tuning and the third for validation. Continue in this manner. At every step, you "tune parameters with past data and validate them with future data," simulating how the strategy would actually be used along a real timeline. If validation performance is stable across each segment of a walk-forward test, the strategy may have good generalization ability. If the results vary widely across segments—for example, one segment returns 30% annualized while another loses 10%—the strategy may be fitting a specific market regime and may stop working when the regime changes. Signs of Overfitting Several warning signs are worth watching for. Parameter sensitivity. If you change the parameter from 20 days to 21 days, the return drops from 30% to 5%. If you change it from 20 days to 19 days, the return rises to 40%. A genuine pattern will not undergo a dramatic transformation because of a one-day difference. Extreme parameter sensitivity indicates that the strategy is fitting specific noise rather than an underlying structure. The more parameters, the greater the danger. A strategy with two parameters has a limited overfitting space. A strategy with ten parameters can almost always be assembled into a historically optimal combination. But that combination represents the shape of data noise, not a market规律. Rules that are too detailed. "Go fully invested from Monday to Wednesday and stay in cash on Thursday and Friday; reduce positions a day early when a holiday approaches; don't trade in a particular month; trade in reverse when the price change exceeds X%." Every additional rule is another historical exception being fitted. Once enough exceptions are memorized, they offer no guidance for new market conditions. The backtest period is too short. If you tune parameters using only one year of data, that year's market characteristics may happen to suit the strategy. It may stop working in a different year. Since style rotation cycles in the A-share market typically last two to three years, the sample period must span at least one full style rotation to test the strategy's robustness. Counterexample: A "Perfect" Strategy In 2023, I ran a factor strategy. From 2018 to 2022, it had a 35% annualized return, an 8% maximum drawdown, and a Sharpe ratio of 2.8. I tuned only two parameters and split the in-sample and out-of-sample data 7:3, with a 28% annualized out-of-sample return. The numbers looked great, and I nearly launched it. I did one more step: walk-forward testing, dividing the data into five segments and running them year by year. The first four segments performed well, but the fifth—from the second half of 2022 to the beginning of 2023—suddenly lost 15%. I went back to investigate. The factors selected by the strategy performed well in small-cap stocks, and the small-cap style dominated from 2018 to 2021, allowing the strategy to reap substantial gains. At the end of 2022, liquidity tightened for small-cap stocks, and the factors failed outright. The overall backtest looked good because the gains from the earlier years offset the loss in the final segment. If I had looked only at the overall 28% annualized out-of-sample return and ignored the segment-level performance, the most likely outcome after launch would have been catching the loss in that final segment. Summary Overfitting is one of the easiest traps to fall into in quantitative trading because strong backtest results are so misleading. Split the data into in-sample and out-of-sample sets, conduct walk-forward testing, and examine parameter sensitivity. Good backtest results do not mean you have a good strategy. A strategy has passed the first test only if it also performs well on data it has never seen.
blog.hancic.sitehttps://blog.hancic.site/post/f27c99a8-aec6-48ca-af2f-a4f148fcc7d0Comments
…