Machine Learning Algorithms

Chapter 8
Model Evaluation, Selection, and Hyperparameter Optimization

Post Graduate Program in Business Analytics
Machine Learning Algorithms   •   Post Graduate

Model Evaluation, Selection, and Hyperparameter Optimization

Introduction

A machine-learning model is useful only when its performance is measured on data that represent the situations in which it will be deployed. A model may fit historical observations extremely well yet fail on future customers, transactions, or operational conditions. Model evaluation therefore addresses more than the question, “Which algorithm has the highest score?” It asks whether the evaluation design is valid, whether the metric reflects the business decision, whether uncertainty has been quantified, and whether the selected model remains reliable under distributional change.

This chapter develops a systematic framework for evaluating, comparing, selecting, and tuning supervised-learning models. It covers data partitioning, cross-validation, regression and classification metrics, ROC and precision–recall analysis, threshold selection, calibration, statistical comparison, and hyperparameter optimization.

Learning Objectives

By the end of this chapter, you should be able to:

  1. Design leakage-resistant train-validation-test workflows.
  2. Select evaluation metrics appropriate to prediction type, class balance, and business costs.
  3. Interpret confusion matrices, ROC curves, precision–recall curves, and calibration plots.
  4. Optimize classification thresholds rather than treating 0.5 as universally appropriate.
  5. Compare models using uncertainty-aware statistical procedures.
  6. Conduct systematic and computationally efficient hyperparameter optimization.
  7. Communicate model performance, limitations, and governance implications to technical and managerial stakeholders.

1. Generalization and the Evaluation Problem

Let a supervised-learning model be written as f_θ(x), where x is a vector of predictors and θ represents learned parameters. For a loss function L(y, f_θ(x)), the population risk is

R(θ) = E[L(Y, f_θ(X))]

Here, the expectation is over the joint population distribution of predictors X and outcomes Y. This risk is usually unknown because the complete future population is unavailable. We estimate it using observed data, but the estimate is useful only if the observations and evaluation procedure approximate the deployment setting.

A model can have low training error and high population error because it has learned noise or idiosyncrasies in the training sample. This is overfitting. Underfitting occurs when the model is too constrained to represent important structure. Model selection attempts to balance these errors.

A central principle is that every decision made using outcome information—including feature selection, preprocessing choices, algorithm selection, and hyperparameter tuning—must be confined to the training portion of the evaluation design. Otherwise, information from the validation or test data leaks into development.

2. Train–Validation–Test Designs

2.1 Holdout partitioning

A basic design divides observations into:

  • Training set: used to fit model parameters.
  • Validation set: used to select algorithms, features, thresholds, and hyperparameters.
  • Test set: used once, at the end, to estimate performance on unseen data.

For independent and identically distributed observations, a random partition can be appropriate. A common arrangement is 60–80% training, 10–20% validation, and 10–20% testing, although the appropriate proportions depend on sample size and variance.

The test set is not a second validation set. Repeatedly inspecting test results and modifying the model converts it into a development set and produces optimistic performance estimates.

2.2 Stratification, grouping, and time

For classification, stratified sampling preserves approximately the same class proportions across partitions. If several records belong to the same customer, patient, household, or account, all records from one group should generally remain in one partition. Otherwise, the model may recognize the entity rather than learn transferable patterns.

For temporal prediction, random splitting is often invalid. A time-ordered design trains on earlier observations and tests on later observations. Features must also be constructed using information available at the prediction time. A transaction timestamp, for example, must not be enriched with a future outcome or a post-event status.

2.3 Nested cross-validation

When data are limited and many hyperparameters are considered, nested cross-validation provides a less biased estimate. The outer loop estimates generalization, while the inner loop selects hyperparameters. For each outer split, tuning occurs only within its training portion. The selected model is then evaluated on the untouched outer validation fold. The outer scores are finally summarized.

Nested cross-validation is computationally expensive but particularly useful for research comparisons, small datasets, and high-flexibility tuning. In ordinary production development, a train–validation–test workflow or cross-validation on the training data followed by one final test evaluation is often sufficient.

3. Cross-Validation

In k-fold cross-validation, the training data are divided into k approximately equal folds. For fold j, the model is fitted on the other k − 1 folds and evaluated on fold j. If mj is the metric for fold j, the mean score is

\bar{m} = (1/k) ∑j=1k mj

The standard deviation of fold scores is

sm = √[(1/(k−1)) ∑j=1k(mj − \bar{m})²]

The mean describes typical performance across folds; the spread indicates sensitivity to the particular sample partition. A standard error approximation is sm/√k, but folds are not fully independent, so this quantity should not be interpreted as an exact uncertainty estimate in every setting.

Typical choices are k = 5 or k = 10. Larger k uses more training data per fit but increases computational cost and may produce more correlated validation estimates. Repeated cross-validation uses several independently randomized fold assignments, reducing dependence on one partition. Stratified, grouped, and blocked time-series variants should be used when the data structure requires them.

Preprocessing must be fitted inside each training fold. For example, standardization uses

zi = (xi − μtrain)/σtrain

where μtrain and σtrain are calculated only from the fold’s training observations. Applying statistics calculated from the full dataset leaks information.

4. Regression Evaluation

4.1 Mean absolute error and mean squared error

For n observations with actual values yi and predictions \hat{y}i, mean absolute error is

MAE = (1/n) ∑i=1n|yi − \hat{y}i|

MAE has the same units as the target and treats errors linearly. It is relatively robust to outliers.

Mean squared error is

MSE = (1/n) ∑i=1n(yi − \hat{y}i)²

and root mean squared error is

RMSE = √MSE

MSE and RMSE penalize large errors more strongly than MAE. RMSE has the target’s units, whereas MSE has squared units. The choice should reflect the consequences of errors, not merely convention.

4.2 Relative and explained-variance measures

When comparison across scales is useful, mean absolute percentage error is sometimes used:

MAPE = (100/n) ∑i=1n|(yi − \hat{y}i)/yi|

MAPE is undefined when yi = 0 and unstable near zero. Symmetric alternatives or scale-dependent measures are preferable in such cases.

The coefficient of determination is

R² = 1 − [∑(yi − \hat{y}i)² / ∑(yi − \bar{y})²]

where \bar{y} is the mean of observed targets in the evaluation sample. R² compares the model with a constant-mean predictor. It can be negative on test data and does not establish causal validity or acceptable forecast accuracy.

Residual analysis should accompany aggregate metrics. Plot residuals against predictions and important predictors, inspect heteroscedasticity, identify systematic bias, and report subgroup performance. A model with a good average RMSE may be unacceptable for a strategically important segment.

5. Classification Evaluation

For binary classification, predicted labels can be summarized as:

Actual positiveActual negative
Predicted positiveTrue positive (TP)False positive (FP)
Predicted negativeFalse negative (FN)True negative (TN)

Accuracy is

Accuracy = (TP + TN)/(TP + TN + FP + FN)

It is informative when class costs and frequencies are reasonably balanced. With rare events, it can be highly misleading.

Precision, recall, and specificity are

Precision = TP/(TP + FP)
Recall = Sensitivity = TP/(TP + FN)
Specificity = TN/(TN + FP)

Precision answers, “Among predicted positives, how many are correct?” Recall answers, “Among actual positives, how many were found?” F1-score is the harmonic mean:

F1 = 2 × (Precision × Recall)/(Precision + Recall)

F1 is defined when precision plus recall is nonzero. It gives equal importance to precision and recall and ignores true negatives; it is therefore not universally appropriate.

Balanced accuracy is

Balanced accuracy = (Sensitivity + Specificity)/2

It is useful under class imbalance when equal emphasis is placed on the two classes. Cost-sensitive evaluation can be more appropriate. If a false positive costs CFP and a false negative costs CFN, expected average classification cost may be expressed as

Cost = CFP × FP + CFN × FN

The chosen costs must be justified by operational or economic evidence.

6. ROC, Precision–Recall, and Thresholds

Most classifiers produce a score or estimated probability rather than a final label. A threshold τ classifies an observation as positive when \hat{p} ≥ τ. Changing τ changes the confusion matrix.

The receiver operating characteristic (ROC) curve plots true-positive rate against false-positive rate, where

FPR = FP/(FP + TN) = 1 − Specificity

The area under the ROC curve (ROC-AUC) can be interpreted, under standard conditions, as the probability that a randomly selected positive receives a higher score than a randomly selected negative. It is a ranking measure, not a probability-calibration measure.

Precision–recall curves plot precision against recall over thresholds. They are often more informative than ROC curves when the positive class is rare because precision directly reflects false discoveries among predicted positives. The baseline precision of a no-skill classifier is approximately the positive prevalence, but average precision and PR-AUC definitions vary across implementations; the reporting method should therefore be stated.

Threshold selection should follow the deployment objective. Possible rules include:

  • maximize F1 or another chosen metric;
  • achieve recall of at least a required level;
  • keep false-positive rate below a capacity limit;
  • minimize expected cost;
  • select the top q% of cases when resources are fixed.

The threshold must be selected on validation data or within cross-validation, then frozen before test evaluation. Optimizing a threshold on the test set creates optimistic bias.

7. Calibration and Decision Quality

A classifier is calibrated if, among cases assigned probability p, approximately p proportion are positive. For example, among observations given probability 0.8, approximately 80% should be positive. Discrimination and calibration are distinct: a model can rank cases well while producing probabilities that are systematically too high.

Calibration can be examined with reliability diagrams, calibration intercept and slope, and proper scoring rules. The Brier score for binary outcomes is

Brier = (1/n) ∑i=1n(\hat{p}i − yi)²

where yi ∈ {0,1} and \hat{p}i is the predicted probability. Lower values are better. The Brier score combines aspects of calibration and discrimination and can be affected by prevalence.

Post-hoc calibration methods include Platt scaling, which fits a logistic transformation to validation scores, and isotonic regression, a flexible monotonic mapping. Calibration must be learned on data separate from the data used to fit the original model, or through an appropriate cross-validation procedure. Recalibration may be necessary when prevalence or operating conditions change.

8. Statistical Comparison and Model Selection

A difference in average scores does not automatically establish that one model is superior. For paired cross-validation results, compare models on the same folds and examine fold-wise differences. Confidence intervals can be obtained through repeated resampling, although naïve tests treating folds as independent can be misleading.

For paired classification predictions, McNemar’s test focuses on cases where two classifiers disagree. It is appropriate for comparing paired error decisions on the same test cases, not for comparing independent samples. For probabilistic predictions, paired bootstrap procedures can estimate uncertainty in metric differences. In time-series forecasting, rolling-origin evaluation and tests designed for forecast errors are more appropriate than random resampling.

Model selection should consider statistical and practical significance. A tiny improvement may not justify additional latency, memory, interpretability loss, maintenance burden, or compliance risk. Report the metric difference, uncertainty interval, data split, preprocessing, and decision threshold—not only a single leaderboard number.

9. Hyperparameter Optimization

Parameters are learned from data; hyperparameters control the learning procedure. Examples include tree depth, regularization strength, learning rate, number of estimators, kernel parameters, and minimum leaf size. Hyperparameter optimization seeks

λ* = argminλ ∈ Λ CVL(λ)

where λ is a hyperparameter configuration, Λ is the search space, and CVL(λ) is cross-validated loss.

9.1 Search strategies

Manual and grid search. Grid search evaluates every combination in a predefined finite grid. It is transparent but inefficient when only a few dimensions strongly affect performance.

Random search. Randomly sampled configurations often explore important dimensions more effectively than a small regular grid, especially when the search space contains irrelevant hyperparameters.

Bayesian optimization. A surrogate model estimates the relationship between configurations and validation performance. An acquisition function selects promising or informative next experiments. This is useful when each model fit is expensive, although results depend on the surrogate and search-space design.

Successive halving and Hyperband. These methods allocate small resources to many configurations and progressively devote more resources to promising candidates. The resource may be training epochs, observations, or trees. Early stopping can substantially reduce computation but may disadvantage models that improve slowly.

9.2 Search-space design and reproducibility

Use domain-informed ranges. Regularization parameters are often searched on a logarithmic scale because useful values can span orders of magnitude. Specify conditional parameters—for example, a tree-specific parameter should not be evaluated for a linear model. Record random seeds, software versions, data snapshots, search budget, rejected runs, and the exact final configuration.

Avoid tuning on the test set. If many configurations are tried, the best validation score itself may be optimistically biased because the selection process has adapted to validation noise. Nested cross-validation, a final untouched test set, or a time-based prospective evaluation can reduce this risk.

10. An Integrated Evaluation Workflow

A defensible workflow is:

  1. Define the prediction time, target, population, and business decision.
  2. Identify leakage risks, groups, temporal structure, and class prevalence.
  3. Create a locked test set using an appropriate temporal, grouped, or stratified design.
  4. Establish simple baselines, such as a mean predictor or prevalence predictor.
  5. Build preprocessing and models as one pipeline so transformations are learned within training folds.
  6. Select a primary metric and supporting metrics linked to consequences.
  7. Tune hyperparameters using cross-validation within the development data.
  8. Select the operating threshold and, if needed, calibrate probabilities without using the test set.
  9. Refit on permitted development data and evaluate once on the locked test set.
  10. Quantify uncertainty, inspect subgroup and error patterns, and document limitations.
  11. Monitor performance, calibration, prevalence, and data drift after deployment.

11. Illustrative Case: Fraud Detection for a Digital Bank

Background and problem

This fictional case concerns a digital bank processing 2 million card transactions per month. Fraud represents 0.25% of transactions. A fraud model produces a probability for each transaction. Investigators can manually review only 3,000 transactions daily. False negatives create direct losses, while false positives inconvenience customers and consume review capacity.

Analysis

Accuracy is inappropriate: a classifier that labels every transaction legitimate would achieve approximately 99.75% accuracy while detecting no fraud. The bank therefore chooses recall, precision, review capacity, and expected cost as primary considerations. Because transactions are time dependent, training uses January–September data, validation uses October, and the untouched test period is November. Customer-level leakage checks ensure that post-transaction chargeback information is not used as a predictor.

Three candidate models are compared using rolling temporal validation. The gradient-boosting model has the strongest ROC-AUC, but its probabilities are overconfident. Logistic regression has slightly weaker ranking but better initial calibration. The bank calibrates the boosting model using validation predictions and evaluates reliability on later validation windows.

Approach and solution

The threshold is not set at 0.5, because only a very small fraction of transactions are fraudulent and review capacity is fixed. Instead, transactions are ranked by predicted probability and the threshold is selected so that approximately 3,000 daily cases are reviewed. A secondary rule imposes a minimum precision target. The final threshold is frozen before the November test evaluation.

The test report includes recall, precision, number of alerts, false-positive rate, Brier score, calibration plots, subgroup results by geography and transaction channel, and confidence intervals obtained by resampling transaction days. The model is approved only after investigators confirm that its alerts are actionable and that customers in protected or vulnerable groups are not disproportionately burdened.

Outcome and learning points

The case demonstrates that model choice is inseparable from operational constraints. The highest ROC-AUC model is not automatically the best system. Ranking, probability quality, threshold policy, investigator capacity, fairness, and monitoring all influence deployment value.

12. Limitations, Governance, and Contemporary Practice

Evaluation metrics can conceal unequal error rates. Organizations should examine false-positive and false-negative rates across relevant groups, while recognizing that some fairness criteria cannot generally be satisfied simultaneously when base rates differ. Group definitions, privacy protections, and legal requirements must be considered carefully.

Data leakage, sampling bias, label delay, concept drift, and feedback loops threaten validity. In fraud detection, intensified review changes which fraud cases receive labels, creating selective-label bias. In lending, a model’s decisions may affect future repayment outcomes, complicating evaluation.

Modern automated machine-learning systems make search easier but do not remove the need for sound evaluation. Large search budgets, repeated experimentation, and hidden test-set use can amplify overfitting. Reproducible pipelines, model cards, dataset documentation, audit logs, and post-deployment monitoring are essential governance practices.

Monitoring should track input drift, target prevalence, discrimination, calibration, latency, missingness, and subgroup outcomes. A threshold that was optimal at deployment may become inappropriate when costs, capacity, prevalence, or policy changes. Model evaluation is therefore a continuing management process rather than a one-time score.

Chapter Summary

Valid evaluation estimates how a model will perform in its intended deployment environment. Train, validation, and test data have distinct roles, and leakage must be prevented through appropriate splitting and pipeline design. Cross-validation improves the efficient use of development data, but its structure must respect stratification, grouping, and time.

Regression should be assessed using metrics such as MAE, RMSE, and R² with careful attention to scale and outliers. Classification requires confusion-matrix measures, ROC analysis, precision–recall analysis, cost-sensitive metrics, and threshold selection. ROC-AUC measures ranking, whereas calibration measures the reliability of predicted probabilities. Statistical comparison should quantify uncertainty and practical importance. Hyperparameter optimization can use grid, random, Bayesian, or resource-aware searches, provided that tuning remains separate from final testing. Sound model selection integrates predictive performance, uncertainty, operational constraints, fairness, interpretability, and monitoring.

Key Terms

Generalization: Performance on unseen data from the intended population.

Data leakage: Use of information unavailable at prediction time or improperly transferred across evaluation splits.

Training set: Data used to estimate model parameters.

Validation set: Data used for model, threshold, or hyperparameter selection.

Test set: Held-out data used for final performance estimation.

Cross-validation: Repeated partitioning of development data into fitting and evaluation folds.

Nested cross-validation: An outer evaluation loop containing an inner model-selection loop.

Overfitting: Learning sample-specific noise that harms generalization.

MAE: Mean absolute prediction error.

RMSE: Square root of mean squared error, with target-scale units.

Precision: Fraction of predicted positives that are truly positive.

Recall: Fraction of actual positives that are detected.

Specificity: Fraction of actual negatives correctly rejected.

ROC-AUC: Area under the receiver operating characteristic curve.

Precision–recall curve: Relationship between precision and recall across thresholds.

Calibration: Agreement between predicted probabilities and observed frequencies.

Brier score: Mean squared error of binary predicted probabilities.

Hyperparameter: User-specified control of the learning algorithm.

Nested evaluation: Selection and assessment performed in separate data loops.

Concept drift: Change in the relationship between predictors and outcomes over time.

Review Questions

Short Answer Questions

  1. Why must the test set be protected from repeated model-development decisions?
  2. Distinguish precision from recall.
  3. When is RMSE preferable to MAE?
  4. What does calibration mean for a probabilistic classifier?
  5. Why can ROC-AUC be misleading for a severely imbalanced problem?

Long Answer Questions

  1. Explain how a leakage-resistant train–validation–test workflow should be designed for temporal data.
  2. Compare grid search, random search, Bayesian optimization, and successive halving.
  3. Discuss how business costs should influence classification threshold selection.
  4. Explain the difference between discrimination and calibration, including suitable metrics and plots.
  5. Describe how statistical uncertainty and practical significance should influence model selection.

Analytical / Application Questions

  1. A fraud classifier has 99.8% accuracy but detects only 4% of fraud cases. Diagnose the evaluation problem and propose a metric and threshold strategy.
  2. Two models have mean five-fold F1-scores of 0.71 and 0.73, but the second has much greater fold-to-fold variation. Design a comparison procedure and explain what additional evidence is needed.
  3. Design an evaluation plan for a demand-forecasting model when products, stores, and time periods are all represented in the data. Justify the split, metrics, tuning approach, and monitoring plan.

Multiple Choice Questions with Answers

Question 1

Which dataset should be used for the final unbiased performance estimate after model development?

A. Training set

B. Validation set

C. Test set

D. The dataset with the highest accuracy

Correct Answer: C. Test set

Question 2

Which metric is generally most informative for identifying rare positive cases when false discoveries matter?

A. Precision–recall analysis

B. Raw accuracy only

C. Training loss only

D. R-squared

Correct Answer: A. Precision–recall analysis

Question 3

If false negatives are substantially more costly than false positives, which action is generally appropriate?

A. Increase the classification threshold

B. Lower the classification threshold, subject to operational constraints

C. Ignore recall

D. Use the test set to select the threshold

Correct Answer: B. Lower the classification threshold, subject to operational constraints

Question 4

What does ROC-AUC primarily measure?

A. Probability calibration

B. Ranking discrimination across thresholds

C. Expected monetary cost

D. The number of model parameters

Correct Answer: B. Ranking discrimination across thresholds

Question 5

Which formula defines mean absolute error?

A. The mean of absolute prediction errors

B. The square root of the mean target value

C. The fraction of true positives among predicted positives

D. One minus specificity

Correct Answer: A. The mean of absolute prediction errors

Question 6

Why should preprocessing be fitted separately within each cross-validation training fold?

A. To increase the number of observations

B. To prevent information leakage

C. To guarantee perfect calibration

D. To eliminate class imbalance

Correct Answer: B. To prevent information leakage

Question 7

A calibrated prediction of 0.8 means that, approximately, what should occur among comparable cases?

A. 80% should be positive

B. 80% should be negative

C. The model should have 80% accuracy in every subgroup

D. The model should select 80% of cases

Correct Answer: A. 80% should be positive

Question 8

Which method uses an outer loop to estimate generalization and an inner loop to tune hyperparameters?

A. Bootstrap aggregation

B. Nested cross-validation

C. One-step grid search

D. Principal-components analysis

Correct Answer: B. Nested cross-validation

Question 9

Which regression metric has the same units as the target and penalizes errors linearly?

A. MSE

B. RMSE

C. MAE

D. R-squared

Correct Answer: C. MAE

Question 10

Why might a time-ordered split be preferable to a random split?

A. It guarantees higher accuracy

B. It imitates forecasting future observations from past observations

C. It removes the need for validation

D. It always balances classes

Correct Answer: B. It imitates forecasting future observations from past observations

Machine Learning Algorithms
Chapter 8 of 15