Quant Memo
Core

Hyperparameter Tuning

Choosing the model's dials — tree depth, learning rate, regularization strength — that the data cannot learn on its own, and doing it without secretly overfitting the very scores you tune on.

Prerequisites: Cross-Validation for Time Series, Overfitting

A model has two kinds of settings. Parameters are learned from the data, the weights of a regression, the split points of a tree. Hyperparameters are the dials you set before training that control how the model learns: how deep a tree may grow, how large a step gradient boosting takes, how hard regularization penalizes complexity. The data cannot pick these for you, because they govern the very fitting process. Choosing them well is hyperparameter tuning, and getting it wrong is one of the quietest ways to overfit a strategy.

The reason tuning matters is the bias-variance tradeoff. Turn the dials toward more flexibility (deeper trees, weaker regularization) and the model fits the training data beautifully but memorizes noise, high variance. Turn them toward less flexibility and the model is too rigid to capture the signal, high bias. The best setting sits between, and the only honest way to find it is to score candidates on data the model has not trained on.

The search strategies

You never tune on the training score, that always improves with more flexibility and would just pick the most overfit model. You tune on a validation score computed by cross-validation. The question is which combinations to try.

MethodHow it worksCostWhen to use
Grid searchTry every combination on a fixed gridExplodes with dimensionsFew hyperparameters, coarse grid
Random searchSample combinations at random from rangesFixed budget you chooseThe default; finds good settings faster
Bayesian optimizationModel the score surface, probe where it looks promisingFewer trials, more overheadExpensive models, many dials

Random search usually beats grid search for a surprising reason: only a few hyperparameters actually matter, and random sampling explores more distinct values of those important dials for the same budget, while a grid wastes trials varying dials that barely move the score.

Tune on a held-out validation score, never the training score. The training error always falls as you add flexibility, so optimizing it hands you the most overfit model in the search. The validation score is the only signal that penalizes memorizing noise.

The multiple-testing trap

Here is the subtle danger. Every hyperparameter combination you try is a separate bet, and you keep the best one. With enough combinations, some setting scores well on your validation folds by luck alone, exactly the data-snooping and multiple-testing problem. The winning validation score is then an optimistic estimate of true performance, because you selected it for being lucky. The more combinations you search, the more the reported score inflates.

The validation score of the best configuration is biased upward, because you picked it out of many tries. If you also report that same score as your estimate of live performance, you are double-dipping. The clean fix is Nested Cross-Validation: an inner loop tunes, a separate outer loop grades, so the number you quote was never used to choose the model.

Worked example: a boosting grid

You tune a gradient-boosting classifier on a price-direction label with a small grid: learning rate in {0.01,0.05,0.1}\{0.01, 0.05, 0.1\} and max depth in {2,3,4}\{2, 3, 4\}. That is 3×3=93 \times 3 = 9 combinations, each scored by 5-fold cross-validation.

Suppose the true out-of-sample accuracy of the best real setting is 53%. Because you evaluate 9 candidates and keep the top one, the reported cross-validated accuracy of the winner comes back at 56%. That 3-point gap is not skill, it is the selection bias of choosing the luckiest of 9 noisy estimates. If you had searched 100 combinations instead of 9, the winner's reported score would look even better, say 58%, while its true accuracy stayed near 53%. The lesson: the size of the search inflates the winning score, so you must either shrink the search, penalize it, or grade the final model on data the search never touched.

Two more finance-specific corrections. First, on labeled market data the folds themselves leak unless you use Purged & Embargoed Cross-Validation, so every score in the table is already too high before selection bias is added. Second, prefer fewer, well-motivated hyperparameters over a giant grid, each extra dial is another chance to overfit.

Common mistakes

  • Tuning on the test set. The moment you look at test performance to pick a setting, that set is contaminated and its score is no longer honest.
  • Leaking through preprocessing. Fitting a scaler or feature selector on the full sample before cross-validation lets test information reach the model, see Data Leakage in Machine Learning. Every transform belongs inside the fold.
  • Reporting the tuned score as the final estimate. Without a nested outer loop, the number you publish is the one you optimized, and it overstates reality.
  • Over-large grids on short financial history. With few genuinely independent observations, a wide search almost guarantees a lucky winner.

Search learning rates and regularization strengths on a log scale (0.001, 0.01, 0.1), not a linear one — they act multiplicatively, so linear grids waste most of their points in a range where nothing changes. And search coarse first, then zoom into the promising region, rather than one giant fine grid.

The mature workflow: pick a small set of hyperparameters that matter, search them with random search (or Bayesian optimization if models are costly) using purged, embargoed folds, and estimate the final model's performance with a separate outer loop so the reported number was never part of the tuning.

Related concepts

Practice in interviews

Further reading

  • Bergstra & Bengio, Random Search for Hyper-Parameter Optimization
  • Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (Ch. 7)
  • López de Prado, Advances in Financial Machine Learning (Ch. 9)
ShareTwitterLinkedIn