- Published on
Hyper-Parameter Tuning with Cross-Validation
Hyper-Parameter Tuning with Cross-Validation
This chapter explains how to perform hyper-parameter tuning for financial machine learning, emphasizing that standard cross-validation (CV) methods will fail and lead to overfitting. The key is to use the Purged k-fold CV (from Chapter 7) as the core mechanism for any tuning method.
Tuning Methods
1. Grid Search Cross-Validation
- Concept: An exhaustive search that tries every possible combination of hyper-parameters from a predefined grid.
- Implementation: The
clfHyperFitfunction is introduced, which integratesGridSearchCV(from scikit-learn) with the customPurgedKFoldclass. This ensures that the search for the best parameters is not contaminated by information leakage between the train and test folds.
2. Randomized Search Cross-Validation
- Concept: A more efficient method for models with many parameters. Instead of trying every combination, it samples a fixed number of parameter combinations from a specified distribution (e.g., "sample 100 combinations").
- Benefits:
- Provides control over the computational budget.
- Is not slowed down by irrelevant parameters, unlike grid search.
- Log-Uniform Distribution: For parameters that are non-negative and non-linear (like
Corgammain an SVM), sampling from a standard uniform distribution (e.g.,U[0, 100]) is inefficient. The author proposes a "log-uniform distribution" where the logarithm of the value is sampled uniformly. This ensures that the search is balanced between small (e.g., 0.01-1) and large (e.g., 1-100) scales.- PDF:
- PDF:
Scoring Metrics: Why 'Accuracy' is Wrong
Choosing the right scoring function for tuning is critical. The author argues that 'accuracy' is a poor metric for financial models.
Preferred Score: 'neg_log_loss' (Negative Log Loss)
- Why: Log loss (or cross-entropy) heavily penalizes confident, wrong predictions. Accuracy, by contrast, treats a 90% confident "miss" the same as a 51% confident "miss."
- Equation:
- Relevance: An investment strategy's PnL is highly sensitive to confident, wrong bets. Since
neg_log_lossaccounts for the probability of the prediction (position size) and can be weighted by the return of the observation (sample weight), it is a much better proxy for PnL than simple accuracy.
Score for Meta-Labeling: 'f1'
- Why: In meta-labeling, the model predicts a binary outcome (bet / no-bet), which is often highly imbalanced (mostly '0's). A model that just predicts "no-bet" all the time would have high accuracy but is useless.
- Solution: The 'f1' score, which is the harmonic mean of precision and recall, correctly measures the model's ability to find the rare, positive 'bet' cases.
API reference
RiskLabAI implements these in Python and Julia (signatures auto-generated from the package source):
| Python | Julia |
|---|---|
| |
| |