6.1.10Algorithmic & Quant Trading

Learn about overfitting and curve fitting

3,503 words16 min readdifficulty · medium

What is Overfitting?

WHY it happens:

  1. Too many parameters: A model with 50 tunable parameters can fit any dataset, even random noise
  2. Insufficient data: 100 data points can't reliably estimate 20 parameters (rule of thumb: need10-20 observations per parameter)
  3. Repeated testing: Running 100 backtests and picking the best one means you found the luckiest parameter set, not the best strategy
  4. Ignoring regime changes: A model trained on 2010-2020 may fail in 2021 if market structure changed

The Mathematics of Overfitting

Bias-Variance Tradeoff

Every model's prediction error decomposes into three components:

Total Error=Bias2+Variance+Irreducible Error\text{Total Error} = \text{Bias}^2 + \text{Variance} + \text{Irreducible Error}

Deriving from first principles:

Let's say the true relationship is y=f(x)+ϵy = f(x) + \epsilon where ϵ\epsilon is random noise with E[ϵ]=0E[\epsilon] = 0 and Var(ϵ)=σ2\text{Var}(\epsilon) = \sigma^2.

Our model predicts f^(x)\hat{f}(x). The expected prediction error at a point x0x_0 is:

E[(yf^(x0))2]E[(y - \hat{f}(x_0))^2]

Step 1: Expand using y=f(x0)+ϵy = f(x_0) + \epsilon:

E[(f(x0)+ϵf^(x0))2]E[(f(x_0) + \epsilon - \hat{f}(x_0))^2]

Step 2: Rearrange and add/subtract E[f^(x0)]E[\hat{f}(x_0)]:

E[((f(x0)E[f^(x0)])+(E[f^(x0)]f^(x0))+ϵ)2]E[((f(x_0) - E[\hat{f}(x_0)]) + (E[\hat{f}(x_0)] - \hat{f}(x_0)) + \epsilon)^2]

Step 3: Expand the square (three terms, six cross products):

E[(f(x0)E[f^(x0)])2]+E[(E[f^(x0)]f^(x0))2]+E[ϵ2]+cross termsE[(f(x_0) - E[\hat{f}(x_0)])^2] + E[(E[\hat{f}(x_0)] - \hat{f}(x_0))^2] + E[\epsilon^2] + \text{cross terms}

WHY cross terms vanish:

  • (f(x0)E[f^(x0)])(f(x_0) - E[\hat{f}(x_0)]) is a constant (no randomness)
  • E[ϵ]=0E[\epsilon] = 0 by assumption
  • E[f^(x0)]f^(x0)E[\hat{f}(x_0)] - \hat{f}(x_0) has zero mean by definition of expectation

Step 4: Recognize the components:

(f(x0)E[f^(x0)])2Bias2+E[(f^(x0)E[f^(x0)])2]Variance+σ2Irreducible\underbrace{(f(x_0) - E[\hat{f}(x_0)])^2}_{\text{Bias}^2} + \underbrace{E[(\hat{f}(x_0) - E[\hat{f}(x_0)])^2]}_{\text{Variance}} + \underbrace{\sigma^2}_{\text{Irreducible}}

Overfitting in Action: Polynomial Regression

Suppose we're predicting stock returns using a polynomial of degree dd:

y^=β0+β1x+β2x2++βdxd\hat{y} = \beta_0 + \beta_1 x + \beta_2 x^2 + \ldots + \beta_d x^d

WHY this demonstrates overfitting: As dd increases, the model can fit training data perfectly, but test error explodes.

HOW to see it mathematically:

With nn data points and degree d=n1d = n-1, the polynomial passes through every point exactly (Lagrange interpolation). Training error = 0, but between points, the polynomial oscillates wildly.

Degrees of freedom: The effective number of parameters. For polynomial degree dd, DoF = d+1d+1. Rule of thumb: need n10×DoFn \geq 10 \times \text{DoF} for stable estimates.

Examples from Algorithmic Trading

Common Mistakes and How to Avoid Them

How to Detect and Prevent Overfitting

Detection Methods

  1. Train/Test Split: Overfitting Score=Train PerformanceTest PerformanceTrain Performance\text{Overfitting Score} = \frac{\text{Train Performance} - \text{Test Performance}}{\text{Train Performance}}

    If this ratio > 0.2 (20% degradation), likely overfit.

  2. Cross-Validation:

    • K-Fold: Split data into K chunks, train on K-1, test on 1, repeat K times
    • Time-series CV: Use expanding or sliding window (preserve temporal order) WHY this works: Harder to overfit K different test sets simultaneously. High variance across folds = model unstable.
  3. Learning Curves:

    • Plot training error and validation error vs. training set size
    • Converging curves (small gap at large N): Good generalization
    • Diverging curves (widening gap): Overfitting

    HOW to interpret: If adding more data closes the gap, collect more data. If gap persists, simplify model.

  4. Permutation Test:

    • Randomly shuffle target variable (destroy any real relationship)
    • Re-run entire optimization process
    • If "optimized" strategy on shuffled data still looks good, your process is curve-fitting noise

    WHY this is powerful: Random data has no signal. Any found edge = false positive from your methodology.

Prevention Strategies

Practical Prevention Checklist:

  1. Simplicity first: Start with simple model (linear regression, 2-3 features). Only add complexity if justified by theory and out-of-sample improvement.

  2. Feature engineering over feature dumping: 5 carefully chosen features beat 100 raw indicators.

  3. Economic reasoning: Every parameter should have a financial explanation. "Buy when 23-day MA crosses 47-day MA" has no theoretical basis—why not 22 and 46?

  4. Walk-forward analysis:

    • Train on Year 1, test on Year 2
    • Retrain on Year 1-2, test on Year 3
    • Continue rolling forward
    • WHY: Simulates real trading where you periodically retrain with new data
  5. Robust optimization: Instead of picking the single best parameter set, choose one that performs well enough across a range of nearby parameters. If performance is sensitive to small changes (e.g., MA period 14 vs. 15 completely changes results), strategy is fragile.

  6. Information coefficient (IC): For predictive models, measure rank correlation between predictions and actual outcomes: IC=cor(predicted returns,actual returns)\text{IC} = \text{cor}(\text{predicted returns}, \text{actual returns})

    IC should be stable across time periods. Declining IC = strategy decay or overfitting.

  7. Sample size rules:

    • Minimum: n>10pn > 10p where pp = number of parameters
    • Conservative: n>30pn > 30p
    • For rare events (e.g., predicting crashes): Need hundreds of occurrences
Recall Explain to a 12-year-old

Imagine you're learning to recognize your friend's handwriting. Your friend shows you 20 words they wrote. You could memorize exactly how they wrote those 20 specific words—every little wigle and dot. But that's silly! Because next time they write "hello," it won't look exactly the same. Maybe the'e' will be slightly rounder. If you memorized too perfectly, you won't recognize it.

The smart way is to learn the general style: "Oh, they make their 'h' taller than normal and connect their letters smoothly." Now you can recognize their handwriting even in new words you've never seen them write before.

In stock trading, overfitting is like memorizing those exact20 words instead of learning the general style. Your computer strategy looks at old stock prices and learns "On June 5th 2018 when price was exactly $34.27, it went up!" But that exact situation will never happen again. The strategy fails on new data because it memorized history's details instead of learning real patterns.

The fix? Keep your strategy simple. Test it on data your computer has never seen before (like surprise quiz!). And always ask "Why would this work?" not just "Did this work in the past?"

Mathematical Rigor: Degrees of Freedom

The effective degrees of freedom of a model is the number of independent parameters being estimated. For trading strategies:

DoF=# explicit parameters+# implicit choices\text{DoF} = \text{\# explicit parameters} + \text{\# implicit choices}

Explicit: MA periods, threshold values, position sizes Implicit: Which features to include, which optimization metric to use, when to start/stop backtest

WHY this matters: Statistical tests assume degrees of freedom. If you claim "My strategy has t-statistic of 3.5, significant at p< 0.001," but you secretly tried 100 variations, the actual p-value is much higher.

Bonferroni correction: If you run mm independent tests, adjust significance level:

αcorrected=αm\alpha_{\text{corrected}} = \frac{\alpha}{m}

If you want overall5% significance and run 100 tests: αcorrected=0.05/100=0.005\alpha_{\text{corrected}} = 0.05 / 100 = 0.005. Now you need t-statistic of ~3.5 instead of 1.96.

Connections

  • 6.1.8-Backtesting-strategies-and-interpreting-results - Overfitting makes backtest results unreliable
  • 6.1.9-Walk-forward-analysis-and-out-of-sample-testing - Primary defense against overfitting
  • 6.21-What-is-quantitative-trading - Quant models are especially vulnerable to overfitting
  • 5.3.5-Risk-management-in-algorithmic-trading - Overfit models have higher real-world risk than backtest suggests
  • 6.1.3-Types-of-algorithmic-strategies - All strategy types must guard against overfitting
  • 4.4-Standard-deviation-and-variance - Understanding variance is key to bias-variance tradeoff
  • Machine-learning-in-trading - ML models have high capacity, require strong overfitting prevention

#flashcards/stock-market

What is overfitting in trading? :: When a model learns noise and random fluctuations in historical data rather than true signal, resulting in good backtest performance but poor live performance.

What is the bias-variance tradeoff?
Total error = Bias² + Variance + Irreducible error. Complex models have low bias but high variance; simple models have high bias but low variance. Must balance.
Why does multiple testing cause overfitting?
Testing many parameter combinations means some will look good by pure luck. With 100 tests at 5% significance, probability of at least one false positive approaches 100%.
What is walk-forward analysis?
Train on Period 1, test on Period 2, retrain on Period 1-2, test on Period 3, rolling forward. Simulates real trading with periodic retraining and prevents using future information.
What is the rule of thumb for sample size vs parameters?
Need minimum n > 10p observations, where p is number of parameters. Conservative standard is n > 30p. For rare events need hundreds of occurrences.
What is curve fitting?
Repeatedly adjusting model parameters while looking at test results until performance looks good. Contaminates the test set because you're using information from it to guide development.
What is regularization?
Adding penalty term to loss function to discourage model complexity. L1 (Lasso) forces some coefficients to zero for feature selection. L2 (Ridge) shrinks all coefficients for smoother fit.

How do you detect overfitting? :: Compare train vs test performance (>20% degradation is warning), use k-fold cross-validation (high variance across folds), plot learning curves (diverging curves), run permutation tests on shuffled data.

What is the permutation test for overfitting?
Randomly shuffle the target variable to destroy real relationships, then re-run entire optimization. If results still look good on random data, your process curve-fits noise.

Why doesn't more data always prevent overfitting? :: Markets have regime changes and non-stationarity. Old data from different regimes may hurt more than help. 1 year of current regime data beats 10 years spanning multiple different regimes.

What is the Bonferroni correction?
When running m independent tests, adjust significance level to α/m. If testing 100 parameter combinations at 5% overall significance, each individual test needs0.05/100 = 0.0005 significance.
What are effective degrees of freedom?
Total count of both explicit parameters (MA periods, thresholds) and implicit choices (which features to test, which metric to optimize, date range selection). All count toward multiple testing problem.
What is robust optimization?
Choosing parameter sets that perform well enough across a range of nearby values rather than the single best. If small parameter changes dramatically alter results, strategy is fragile and overfit.
What is information coefficient?
Rank correlation between predicted returns and actual returns: IC = corr(predictions, outcomes). Should remain stable across time periods. Declining IC indicates strategy decay or overfitting.
What makes a model parsimonious?
Using the simplest model that adequately explains the data. Each parameter must have economic justification. Prefer3 theoretically-motivated features over 50 data-mined indicators.

Concept Map

learns

ignores

causes

causes

causes

contaminates validation

shows

shows

explained by

decomposes

Bias squared plus Variance plus Irreducible

Overfitting

Curve Fitting

Historical Noise

Underlying Signal

Too Many Parameters

Insufficient Data

Repeated Testing

High In-Sample

Low Out-of-Sample

Bias-Variance Tradeoff

Total Error

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, is note ka core idea simple hai lekin bahut important hai. Overfitting ka matlab hai ki tumhara trading model historical data ki har chhoti-chhoti cheez, yahan tak ki random noise aur coincidences ko bhi "yaad" kar leta hai — bilkul waise jaise koi student practice questions ko word-for-word ratta maar leta hai, typos ke saath. Aisa model backtest mein 95% accuracy dikhayega, but jab real market mein live trading karoge toh paisa gawaoge, kyunki usne asli repeatable pattern nahi seekha, sirf past ke luck ko fit kiya hai. Market kabhi exactly repeat nahi hoti — woh sirf "rhyme" karti hai.

Ab why-it-matters wali baat: iske peeche jo maths hai woh hai bias-variance tradeoff. Total error ke teen parts hote hain — bias square, variance, aur irreducible noise. Simple model ka bias zyada hota hai (galat assumptions), par variance kam. Complex model (jaise high-degree polynomial ya 50 parameters wala model) ka bias kam hota hai kyunki woh kisi bhi shape ko fit kar sakta hai, lekin variance bahut high — matlab thoda sa data change karo aur model ka behaviour poori tarah badal jaata hai. Yehi instability live market mein loss ka reason banti hai. Agar tumhare paas sirf 100 data points hain aur tum 20 parameters estimate kar rahe ho, toh model noise ko hi signal samajh lega.

Practical takeaway ye hai ki jab bhi strategy banao, dhyan rakho: zyada parameters mat use karo, enough data rakho (rule of thumb 10-20 observations per parameter), aur baar-baar backtest chala kar "best" strategy mat chuno — kyunki 100 backtests mein se best pick karna matlab tumne sabse lucky parameter set dhoondha, sabse achhi strategy nahi. Isliye out-of-sample testing bahut zaroori hai — model ko naye, unseen data par check karo, tabhi pata chalega ki usme real edge hai ya sirf curve fitting.

Test yourself — Algorithmic & Quant Trading

Connections