Machine learning · honest evaluation
Leakage Lab
In 2024 I built a stock price predictor whose errors looked tiny. Re-running its exact setup gives an R² of 0.9997 to 0.9999. It was not predicting anything. This page shows why, and what an honest forecast looks like.
The trap
- The 2024 model predicted each day’s closing price from that same day’s open, high and low. The high and low are known only once the day is over, and the close always sits between them.
- It used a shuffled train/test split, so it trained on days after the ones it was tested on.
- On price levels a high R² is almost free: prices are always close to yesterday’s.
An honest test
Each model predicts tomorrow’s return from what was known at today’s close (recent returns, 20-day volatility and momentum). It is trained on the past, tested on the next month, then refitted (walk-forward validation), over about 1,994 trading days per stock. The table shows each model’s forecast error compared with “no change”.
| Stock | 2024 setup R² | Historical mean | Linear model | Gradient boosting |
|---|---|---|---|---|
| Apple AAPL | 0.9999 | -0.1% | +0.4% | +0.4% |
| Amazon AMZN | 0.9998 | same | +0.4% | +2.7% |
| Alphabet GOOGL | 0.9998 | -0.1% | +0.5% | +2.6% |
| Microsoft MSFT | 0.9999 | -0.1% | -0.1% | +1.8% |
| Tesla TSLA | 0.9997 | same | +0.3% | +1.4% |
| Reliance Industries RELIANCE.NS | 0.9999 | same | +0.5% | +1.6% |
| Tata Consultancy Services TCS.NS | 0.9998 | same | +0.6% | +2.7% |
| Infosys INFY.NS | 0.9999 | same | +0.4% | +1.2% |
| State Bank of India SBIN.NS | 0.9998 | same | +0.6% | +3.2% |
| HDFC Bank HDFCBANK.NS | 0.9998 | same | +0.4% | +3.3% |
What this says: no model beats “no change” by a meaningful margin. That is the expected, honest result for daily returns of large, heavily traded stocks. A test in the repository fails on the 2024 setup and passes on the new pipeline, so the leak cannot come back unnoticed.
streamlit run streamlit_app.py; a hosted link will appear here.
Code, tests and method