6.1.10 · HinglishAlgorithmic & Quant Trading

Learn about overfitting and curve fitting

3,659 words17 min readRead in English

6.1.10 · Stock-Market › Algorithmic & Quant Trading

Overfitting Kya Hai?

Yeh KYUN hoti hai:

  1. Bahut zyada parameters: 50 tunable parameters wala model kisi bhi dataset ko fit kar sakta hai, chahe random noise ho
  2. Insufficient data: 100 data points se 20 parameters reliably estimate nahi ho sakte (rule of thumb: har parameter ke liye 10-20 observations chahiye)
  3. Repeated testing: 100 backtests chalana aur best ek chunna matlab tumne best strategy nahi, balki sabse lucky parameter set dhundha
  4. Regime changes ko ignore karna: 2010-2020 pe trained model 2021 mein fail ho sakta hai agar market structure change ho gayi

Overfitting ki Mathematics

Bias-Variance Tradeoff

Har model ka prediction error teen components mein decompose hota hai:

First principles se derive karte hain:

Maan lo ki true relationship hai jahan random noise hai jiske liye aur .

Hamara model predict karta hai . Point pe expected prediction error hai:

Step 1: use karke expand karo:

Step 2: Rearrange karo aur add/subtract karo:

Step 3: Square expand karo (teen terms, chhe cross products):

Cross terms KYUN vanish hote hain:

  • ek constant hai (koi randomness nahi)
  • assumption ke by
  • ka mean expectation ki definition ke by zero hota hai

Step 4: Components ko pehchano:

Overfitting in Action: Polynomial Regression

Maan lo hum degree ke polynomial se stock returns predict kar rahe hain:

Yeh overfitting KYUN demonstrate karta hai: Jaise jaise badhta hai, model training data ko perfectly fit kar sakta hai, lekin test error explode ho jaata hai.

Mathematically HOW dekhen:

data points aur degree ke saath, polynomial har point se exactly pass hota hai (Lagrange interpolation). Training error = 0, lekin points ke beech mein polynomial wildly oscillate karta hai.

Degrees of freedom: Effective number of parameters. Polynomial degree ke liye, DoF = . Rule of thumb: stable estimates ke liye chahiye.

Algorithmic Trading ke Examples

Common Mistakes aur Unhe Kaise Avoid Karein

Overfitting Detect aur Prevent Kaise Karein

Detection Methods

  1. Train/Test Split:

    Agar yeh ratio > 0.2 (20% degradation) ho, toh likely overfit hai.

  2. Cross-Validation:

    • K-Fold: Data ko K chunks mein split karo, K-1 pe train karo, 1 pe test karo, K baar repeat karo
    • Time-series CV: Expanding ya sliding window use karo (temporal order preserve karo) Yeh KYUN kaam karta hai: K alag test sets pe simultaneously overfit karna mushkil hai. Folds mein high variance = unstable model.
  3. Learning Curves:

    • Training error aur validation error ko training set size ke against plot karo
    • Converging curves (large N pe chhota gap): Achha generalization
    • Diverging curves (widening gap): Overfitting

    HOW interpret karein: Agar zyada data add karne se gap close hota hai, toh zyada data collect karo. Agar gap persist kare, toh model simplify karo.

  4. Permutation Test:

    • Target variable ko randomly shuffle karo (koi bhi real relationship destroy karo)
    • Poora optimization process dobara chalao
    • Agar shuffled data pe "optimized" strategy abhi bhi achhi lagti hai, tumhara process noise ko curve-fit kar raha hai

    Yeh KYUN powerful hai: Random data mein koi signal nahi hota. Koi bhi found edge = tumhari methodology se false positive.

Prevention Strategies

Practical Prevention Checklist:

  1. Pehle simplicity: Simple model se shuru karo (linear regression, 2-3 features). Complexity sirf tabhi add karo jab theory aur out-of-sample improvement se justified ho.

  2. Feature engineering over feature dumping: 5 carefully chosen features 100 raw indicators se behtar hain.

  3. Economic reasoning: Har parameter ka financial explanation hona chahiye. "23-day MA cross kare 47-day MA ko jab buy karo" ka koi theoretical basis nahi—kyun 22 aur 46 nahi?

  4. Walk-forward analysis:

    • Year 1 pe train karo, Year 2 pe test karo
    • Year 1-2 pe retrain karo, Year 3 pe test karo
    • Rolling forward continue karo
    • KYUN: Real trading simulate karta hai jahan tum periodically naye data ke saath retrain karte ho
  5. Robust optimization: Single best parameter set chunne ki jagah, woh chunno jo nearby parameters ki ek range mein kaafi achha perform kare. Agar performance chhote changes ke liye sensitive hai (jaise MA period 14 vs. 15 completely results change kar de), strategy fragile hai.

  6. Information coefficient (IC): Predictive models ke liye, predictions aur actual outcomes ke beech rank correlation measure karo:

    IC time periods mein stable rehna chahiye. Declining IC = strategy decay ya overfitting.

  7. Sample size rules:

    • Minimum: jahan = number of parameters
    • Conservative:
    • Rare events ke liye (jaise crashes predict karna): Hundreds of occurrences chahiye
Recall 12 saal ke bachche ko explain karo

Imagine karo tum apne dost ki handwriting pehchanna seekh rahe ho. Tumhara dost tumhe 20 words dikhata hai jo usne likhe hain. Tum un 20 specific words ko exactly memorize kar sakte ho—har choti wiggle aur dot. Lekin yeh silly hai! Kyunki agli baar jab woh "hello" likhega, woh exactly same nahi lagega. Shayad 'e' thoda gol ho. Agar tumne zyada perfectly memorize kiya, toh tum use pehchan nahi paoge.

Smart tarika yeh hai ki general style seekho: "Oh, woh apna 'h' normal se zyada lamba banata hai aur letters smoothly connect karta hai." Ab tum unki handwriting un naye words mein bhi pehchan sakte ho jo tumne unhe pehle likhte nahi dekha.

Stock trading mein, overfitting waise hai jaise un exact 20 words ko memorize karna general style seekhne ki jagah. Tumhara computer strategy purane stock prices dekhta hai aur seekhta hai "June 5th 2018 ko jab price exactly $34.27 tha, woh upar gaya!" Lekin woh exact situation kabhi dobara nahi hogi. Strategy naye data pe fail hoti hai kyunki usne history ki details memorize ki real patterns seekhne ki jagah.

Fix? Apni strategy simple rakho. Ise us data pe test karo jo tumhare computer ne kabhi nahi dekha (jaise surprise quiz!). Aur hamesha poocho "Yeh KYUN kaam karega?" na ki sirf "Kya yeh past mein kaam kiya?"

Mathematical Rigor: Degrees of Freedom

Ek model ke effective degrees of freedom woh independent parameters ki sankhya hai jo estimate ki ja rahi hain. Trading strategies ke liye:

Explicit: MA periods, threshold values, position sizes Implicit: Kaun se features include karein, kaun sa optimization metric use karein, backtest kab start/stop karein

Yeh KYUN matter karta hai: Statistical tests degrees of freedom assume karte hain. Agar tum claim karo "Meri strategy ka t-statistic 3.5 hai, p < 0.001 pe significant hai," lekin tumne secretly 100 variations try kiye, toh actual p-value bahut zyada hai.

Bonferroni correction: Agar tum independent tests chalate ho, significance level adjust karo:

Agar tum overall 5% significance chahte ho aur 100 tests chalate ho: . Ab tumhe 1.96 ki jagah ~3.5 ka t-statistic chahiye.

Connections

  • 6.1.8-Backtesting-strategies-and-interpreting-results - Overfitting backtest results ko unreliable banata hai
  • 6.1.9-Walk-forward-analysis-and-out-of-sample-testing - Overfitting ke khilaf primary defense
  • 6.21-What-is-quantitative-trading - Quant models especially overfitting ke liye vulnerable hain
  • 5.3.5-Risk-management-in-algorithmic-trading - Overfit models ka real-world risk backtest se zyada hota hai
  • 6.1.3-Types-of-algorithmic-strategies - Har strategy type ko overfitting se bachna chahiye
  • 4.4-Standard-deviation-and-variance - Variance ko samajhna bias-variance tradeoff ki key hai
  • Machine-learning-in-trading - ML models ki high capacity hoti hai, isliye strong overfitting prevention zaroori hai

#flashcards/stock-market

Overfitting trading mein kya hoti hai? :: Jab model historical data mein noise aur random fluctuations seekh leta hai na ki true signal, jisse backtest performance achhi hoti hai lekin live performance kharab.

Bias-variance tradeoff kya hai?
Total error = Bias² + Variance + Irreducible error. Complex models mein low bias lekin high variance hota hai; simple models mein high bias lekin low variance. Balance karna zaroori hai.
Multiple testing overfitting KYUN cause karta hai?
Kaafi parameter combinations test karne ka matlab hai kuch pure luck se achhe lagenge. 5% significance pe 100 tests ke saath, kam se kam ek false positive ki probability 100% ke paas pahunch jaati hai.
Walk-forward analysis kya hai?
Period 1 pe train karo, Period 2 pe test karo, Period 1-2 pe retrain karo, Period 3 pe test karo, rolling forward. Real trading simulate karta hai periodic retraining ke saath aur future information use hone se rokta hai.
Sample size vs parameters ke liye rule of thumb kya hai?
Minimum n > 10p observations chahiye, jahan p parameters ki sankhya hai. Conservative standard n > 30p hai. Rare events ke liye hundreds of occurrences chahiye.
Curve fitting kya hai?
Model parameters ko baar baar adjust karna test results dekhte hue jab tak performance achhi na lage. Test set contaminate hota hai kyunki tum development guide karne ke liye us se information use kar rahe ho.
Regularization kya hai?
Loss function mein penalty term add karna model complexity discourage karne ke liye. L1 (Lasso) feature selection ke liye kuch coefficients ko zero force karta hai. L2 (Ridge) smoother fit ke liye sab coefficients shrink karta hai.

Overfitting HOW detect karte hain? :: Train vs test performance compare karo (>20% degradation warning hai), k-fold cross-validation use karo (folds mein high variance = unstable model), learning curves plot karo (diverging curves), shuffled data pe permutation tests chalao.

Overfitting ke liye permutation test kya hai?
Target variable ko randomly shuffle karo real relationships destroy karne ke liye, phir poora optimization process dobara chalao. Agar random data pe bhi results achhe lagte hain, tumhara process noise ko curve-fit kar raha hai.

Zyada data overfitting KYUN hamesha nahi rokta? :: Markets mein regime changes aur non-stationarity hoti hai. Alag regimes ke purane data se zyada nuksan ho sakta hai. Current regime ka 1 saal data multiple alag regimes spanning 10 saal se behtar hai.

Bonferroni correction kya hai?
m independent tests chalane pe, significance level ko α/m pe adjust karo. 100 parameter combinations 5% overall significance pe test karne mein, har individual test ko 0.05/100 = 0.0005 significance chahiye.
Effective degrees of freedom kya hain?
Explicit parameters (MA periods, thresholds) aur implicit choices (kaun se features test karein, kaun sa metric optimize karein, date range selection) dono ki total count. Sab multiple testing problem mein count hote hain.
Robust optimization kya hai?
Single best ki jagah aas paas ki values ki range mein kaafi achha perform karne wala parameter set chunna. Agar chhote parameter changes se results drastically badal jaate hain, strategy fragile aur overfit hai.
Information coefficient kya hai?
Predicted returns aur actual returns ke beech rank correlation: IC = corr(predictions, outcomes). Time periods mein stable rehna chahiye. Declining IC strategy decay ya overfitting indicate karta hai.
Parsimonious model kya hota hai?
Woh simplest model jo data ko adequately explain kare. Har parameter ka economic justification hona chahiye. 100 data-mined indicators se behtar hain 3 theoretically-motivated features.

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