Lecture 7: Financial Applications
| Duration: ~45 minutes | Slides: 27 | Prerequisites: Lecture 6 |
Learning Objectives
After completing this lecture, you should be able to:
- Apply walk-forward validation to financial data
- Identify and avoid common pitfalls in financial ML
- Understand challenges specific to financial time series
- Design features for stock prediction
- Interpret model outputs in a trading context
- Critically evaluate financial ML claims
Key Concepts
1. Why Financial Data is Different
Financial data presents unique challenges not found in typical ML datasets:
| Challenge | Description |
|---|---|
| Non-stationarity | Statistics change over time (mean, variance, correlations) |
| Low signal-to-noise ratio | True predictive signal is tiny relative to noise |
| Regime changes | Market behavior shifts dramatically (bull/bear, crisis) |
| Limited data | Can’t generate more historical data |
| Survivorship bias | Only surviving companies are in historical databases |
| Look-ahead bias | Accidentally using future information |
2. Walk-Forward Validation
Standard cross-validation is WRONG for time series because it leaks future information.
The problem with k-fold CV:
Standard k-fold: [Train|Val|Train|Val|Train]
^ ^
Uses future data to predict past!
Walk-forward (rolling window) validation:
Period 1: [====Train====][Val]
Period 2: [====Train====][Val]
Period 3: [====Train====][Val]
Implementation:
- Fix training window size (e.g., 5 years)
- Fix test window size (e.g., 1 year)
- Train on window, test on next period
- Roll forward, repeat
Advantages:
- Never uses future data
- Tests across multiple market regimes
- More realistic performance estimate
3. Look-Ahead Bias
Definition: Accidentally using information that wouldn’t have been available at prediction time.
Common sources:
| Source | Example |
|---|---|
| Data snooping | Using final (adjusted) stock prices instead of real-time |
| Feature leakage | Including future earnings in current prediction |
| Survivorship bias | Only training on companies that survived |
| Point-in-time data | Using revised economic data instead of initial release |
Prevention:
- Use point-in-time databases
- Strictly separate train/test by date
- Be paranoid about feature timestamps
4. Regime Changes
Markets operate in different “regimes” with different statistical properties.
Examples:
- Bull market vs bear market
- Low volatility vs high volatility
- Pre-crisis vs post-crisis
The problem: A model trained in one regime may fail completely in another.
Solutions:
- Walk-forward validation (exposes regime failures)
- Include regime indicators as features
- Use ensemble of regime-specific models
- Apply strong regularization
5. Feature Engineering for Finance
Raw data is rarely predictive. Features must be engineered carefully.
Categories of features:
| Category | Examples |
|---|---|
| Price-based | Returns, moving averages, RSI, MACD |
| Volume-based | Volume ratio, OBV, volume MA |
| Fundamental | P/E, P/B, ROE, debt/equity |
| Technical | Bollinger bands, support/resistance |
| Sentiment | News sentiment, social media metrics |
| Macro | Interest rates, VIX, sector indices |
Feature engineering principles:
- Stationarity: Use returns, not prices
- Normalization: Z-score or rank within period
- Lag appropriately: Ensure feature was available at prediction time
- Domain knowledge: Finance experts can suggest meaningful features
6. Case Study: Stock Direction Prediction
Problem: Predict whether a stock will go up (>0% return) or down in the next month.
Architecture:
Inputs (20 features):
- 5 technical indicators
- 5 fundamental ratios
- 5 momentum features
- 5 volatility features
Hidden Layer 1: 32 neurons (ReLU)
Hidden Layer 2: 16 neurons (ReLU)
Output: 1 neuron (Sigmoid) - P(up)
Regularization: L2 (lambda=0.001), Dropout (0.3)
Training setup:
- Training: 10 years of data
- Validation: 2 years
- Test: 2 years (walk-forward)
- Batch size: 64
- Early stopping: patience=20
Expected results:
- Accuracy: 52-55% (anything above 50% is valuable!)
- Key: Small edge with many trades = significant profit
7. Transaction Costs
Real trading has costs that erode model profits:
| Cost Type | Typical Value |
|---|---|
| Commission | $0 - $0.01/share (mostly zero now) |
| Bid-ask spread | 0.01% - 0.5% |
| Market impact | 0.1% - 1% (for large orders) |
| Slippage | 0.05% - 0.2% |
Impact on strategy:
- A model predicting 55% accuracy with 2% average win/loss
- Before costs: Expected return = 0.552% - 0.452% = 0.2% per trade
- After 0.3% round-trip costs: 0.2% - 0.3% = -0.1% (LOSING!)
Solutions:
- Include transaction costs in loss function
- Reduce trading frequency
- Focus on larger predicted moves
8. The Efficient Market Hypothesis (EMH)
EMH states: All available information is already reflected in prices.
Three forms:
| Form | What’s Priced In | Implication |
|---|---|---|
| Weak | Past prices | Technical analysis doesn’t work |
| Semi-strong | Public information | Fundamental analysis doesn’t work |
| Strong | All information | Even insider trading doesn’t work |
Reality for ML:
- Markets are approximately efficient
- Any predictable patterns are quickly arbitraged away
- Useful patterns exist but are small and temporary
Practical implication: Don’t expect your model to achieve 70% accuracy. Even 52% with proper risk management can be very profitable.
9. Model Interpretation in Finance
Unlike image classification, finance requires understanding WHY the model makes predictions.
Why interpretation matters:
- Regulatory requirements (explainability)
- Risk management (understand exposures)
- Model debugging (detect look-ahead bias)
- Strategy confidence (logical reasoning)
Interpretation techniques:
- Feature importance: Which inputs drive predictions?
- Partial dependence: How does output change with one input?
- SHAP values: Contribution of each feature to each prediction
- Attention weights: (for attention-based models)
10. Realistic Expectations
What neural networks CAN do in finance:
- Find subtle patterns humans miss
- Process many features simultaneously
- Adapt to changing conditions (with retraining)
- Provide consistent, unemotional signals
What neural networks CANNOT do:
- Predict the unpredictable (true random events)
- Maintain edge indefinitely (competition erodes alpha)
- Work without proper validation (easy to overfit)
- Replace domain expertise (garbage in, garbage out)
Expected performance:
- Directional accuracy: 50-55%
- Sharpe ratio improvement: 0.1-0.3
- Alpha generation: 1-5% annually (before costs)
Key Formulas
Sharpe Ratio
Sharpe = (Return - RiskFreeRate) / Volatility
Maximum Drawdown
MDD = max(Peak - Trough) / Peak
Walk-Forward Return
Total Return = Product(1 + r_i) - 1
where r_i is return in period i
Finance Application: Complete Workflow
Step-by-step process:
- Data Collection
- Price data, fundamentals, alternative data
- Ensure point-in-time accuracy
- Feature Engineering
- Calculate technical indicators
- Normalize features
- Remove look-ahead bias
- Train/Test Split
- Walk-forward methodology
- Multiple non-overlapping test periods
- Model Training
- Architecture design
- Regularization
- Early stopping on validation
- Evaluation
- Accuracy, Sharpe, drawdown
- Transaction cost analysis
- Regime analysis
- Paper Trading
- Test on live data without real money
- Verify execution is feasible
- Live Trading (Carefully)
- Start with small position sizes
- Monitor for degradation
Practice Questions
Mathematical Understanding
Q1: A model has 54% accuracy predicting daily stock direction. The average win is 1% and average loss is 1%. What is the expected daily return (ignoring costs)?
Answer
Expected Return = P(win) * win + P(loss) * loss = 0.54 * 1% + 0.46 * (-1%) = 0.54% - 0.46% = 0.08% per day Over 252 trading days: (1.0008)^252 - 1 = 22.3% annually This shows how a small edge compounds significantly!Q2: Transaction costs are 0.1% per trade. How does this affect the strategy from Q1?
Answer
Net Expected Return = 0.08% - 0.1% = -0.02% per day The strategy is now LOSING money! This illustrates why transaction costs are critical. Solutions: - Trade less frequently (weekly instead of daily) - Only trade when predicted move > cost threshold - Focus on less liquid assets where inefficiencies are larger (but costs are higher)Conceptual Understanding
Q3: Why is standard k-fold cross-validation inappropriate for stock prediction?
Answer
K-fold CV randomly assigns data to folds, ignoring time ordering. This creates look-ahead bias because: 1. You might train on December 2020 data and validate on January 2020 data 2. The model sees "future" information during training 3. This gives unrealistically optimistic performance estimates 4. The model learns patterns that only exist because of the time leak Walk-forward validation preserves temporal order: always train on past, test on future.Q4: Why might a model work well in backtests but fail in live trading?
Answer
Common reasons: 1. **Look-ahead bias:** Model used unavailable information 2. **Overfitting:** Model memorized historical patterns 3. **Regime change:** Market behavior shifted 4. **Transaction costs:** Not properly accounted for 5. **Execution issues:** Can't trade at simulated prices 6. **Data quality:** Live data differs from historical 7. **Survivorship bias:** Only trained on surviving stocks 8. **Alpha decay:** Edge eroded as others discovered same patternQ5: A trading model has 60% accuracy but loses money. How is this possible?
Answer
Accuracy alone doesn't determine profitability. Consider: - Average win: +0.5% - Average loss: -1.5% - Accuracy: 60% Expected return = 0.6 * 0.5% + 0.4 * (-1.5%) = 0.3% - 0.6% = -0.3% The model wins often but wins small and loses big. This is why risk management (stop losses, position sizing) matters as much as prediction accuracy.Application
Q6: Design features for predicting the next month’s return of S&P 500 stocks. List 5 features and explain why each might be predictive.
Answer
1. **12-month momentum (lagged 1 month)**: Academic evidence of momentum effect; recent winners tend to continue winning 2. **Price-to-book ratio (standardized within sector)**: Value factor; low P/B stocks historically outperform 3. **30-day volatility (rolling)**: Low volatility stocks often outperform on risk-adjusted basis 4. **Earnings surprise (most recent quarter)**: Post-earnings-announcement drift; stocks continue moving after surprises 5. **Short interest ratio**: High short interest can indicate both bearish sentiment and potential short squeeze Each feature is: - Based on documented financial research - Calculable without look-ahead bias - Stationary (or can be normalized)Q7: You’ve built a model that achieves 58% accuracy on your test set. What additional analysis would you do before trusting this result?
Answer
1. **Multiple test periods:** Was 58% consistent across different time periods or driven by one lucky period? 2. **Regime analysis:** How did it perform in bull vs bear markets, high vs low volatility? 3. **Statistical significance:** Is 58% significantly different from 50%? (Sample size matters) 4. **Feature importance:** Do the important features make economic sense? 5. **Transaction cost analysis:** Is the strategy profitable after realistic costs? 6. **Drawdown analysis:** What was the maximum loss period? 7. **Correlation with known factors:** Is this just momentum or value in disguise? 8. **Out-of-sample test:** Hold out completely new data for final validation 9. **Paper trading:** Test on live data without real moneyReading List
Essential Reading
- Lopez de Prado (2018) - “Advances in Financial Machine Learning” - The definitive reference
- Aronson (2006) - “Evidence-Based Technical Analysis” - Rigorous methodology
Walk-Forward Validation
- Bailey et al. (2014) - “The Probability of Backtest Overfitting”
- Harvey et al. (2016) - “…and the Cross-Section of Expected Returns” (multiple testing)
Financial Machine Learning
- Dixon, Halperin & Bilokon (2020) - “Machine Learning in Finance”
- Heaton et al. (2016) - “Deep Learning for Finance: Deep Portfolios”
Market Efficiency
- Fama (1970) - “Efficient Capital Markets: A Review”
- Lo (2004) - “The Adaptive Markets Hypothesis”
Practical Guides
- Chan (2013) - “Algorithmic Trading: Winning Strategies and Their Rationale”
Summary
This lecture covered:
- Financial data challenges - Non-stationarity, low signal, regime changes
- Walk-forward validation - Proper time series validation methodology
- Look-ahead bias - The silent killer of financial ML
- Regime changes - Markets behave differently over time
- Feature engineering - Domain knowledge is essential
- Transaction costs - Can turn winners into losers
- Realistic expectations - Small edges are still valuable
Key Takeaway: Financial ML is harder than other ML domains. Success requires combining ML expertise with deep finance knowledge and rigorous validation.
Next Lecture: Modern Networks and Future - We’ll explore modern architectures and future directions.