E-Library Logo
Machine Learning
Machine Learning
Machine Learning illustration

Machine Learning

Program: Post Graduate Program in AI and ML
Program Level: Post Graduate
Language: English
Subject Syllabus

Subject Overview

An advanced, practice-oriented study of machine learning covering mathematical foundations, data preparation, major learning paradigms, model development, evaluation, deployment, governance and strategic business adoption. The subject integrates theory, coding, analytics, case analysis and responsible decision-making across contemporary industry applications.

Learning Goal

Enable students to design, evaluate, deploy and govern end-to-end machine learning solutions; critically select appropriate models; translate predictions into managerial decisions; and assess emerging developments responsibly.

Syllabus

1. Machine Learning Foundations and Problem Formulation

Learning Objective: Explain AI, machine learning and statistical learning; distinguish supervised, unsupervised, semi-supervised, self-supervised and reinforcement learning; formulate business problems as ML tasks; and understand assumptions, terminology and the end-to-end lifecycle.

Category: foundation

2. Mathematical, Statistical and Optimization Foundations

Learning Objective: Apply linear algebra, probability, distributions, estimation, Bayesian reasoning, calculus, loss functions, regularization and gradient-based optimization to understand how machine learning algorithms learn.

Category: foundation

3. Data Understanding, Preparation and Feature Engineering

Learning Objective: Perform data acquisition, exploratory analysis, cleaning, transformation, encoding, scaling, sampling, imbalance handling and feature construction while preventing leakage and ensuring data quality and lineage.

Category: core

4. Regression and Predictive Estimation

Learning Objective: Develop and interpret linear, polynomial, regularized and generalized regression models; diagnose assumptions and errors; and evaluate continuous predictions using appropriate statistical and business metrics.

Category: core

5. Classification and Kernel Methods

Learning Objective: Build classification models using logistic regression, naïve Bayes, nearest neighbours, support vector machines and kernel methods; manage class imbalance, thresholds, calibration and cost-sensitive decisions.

Category: core

6. Decision Trees and Ensemble Learning

Learning Objective: Apply decision trees, random forests, bagging, boosting, gradient boosting and stacking; analyse bias–variance trade-offs; and tune ensembles for robust performance and interpretability.

Category: core

7. Model Evaluation, Selection and Experimentation

Learning Objective: Use train-validation-test designs, cross-validation, performance metrics, learning curves, baselines, hyperparameter optimization and statistical comparison to select models and conduct reproducible experiments.

Category: core

8. Unsupervised Learning and Representation

Learning Objective: Apply clustering, dimensionality reduction, manifold learning, association analysis and anomaly detection; evaluate unsupervised results; and use latent representations for segmentation, visualization and discovery.

Category: core

9. Neural Networks and Deep Learning

Learning Objective: Explain multilayer networks, backpropagation, activation functions, optimization and regularization; apply convolutional, recurrent, attention and transformer architectures to tabular, image, text and multimodal problems.

Category: advanced

10. Time-Series and Sequential Machine Learning

Learning Objective: Model trend, seasonality and temporal dependence; create leakage-safe temporal features; apply statistical, machine learning and deep forecasting methods; and evaluate forecasts under changing conditions.

Category: application

11. Reinforcement Learning and Sequential Decision-Making

Learning Objective: Understand Markov decision processes, policies, value functions, temporal-difference learning, Q-learning and policy methods; assess exploration, reward design, simulation and operational applications.

Category: advanced

12. Explainability, Causal Analysis and Decision Analytics

Learning Objective: Interpret global and local model behaviour using feature importance, partial dependence, SHAP and counterfactuals; distinguish prediction from causation; and connect model outputs with uncertainty, optimization and managerial decisions.

Category: strategic

13. Scalable ML Systems and MLOps

Learning Objective: Design production ML pipelines using data and model versioning, APIs, cloud and distributed computing, containers, automated testing, CI/CD, feature stores, monitoring, drift detection, retraining and lifecycle management.

Category: application

14. Responsible AI, Security and ML Governance

Learning Objective: Identify bias, fairness, privacy, robustness, adversarial threats and environmental impacts; establish documentation, human oversight, auditability, regulatory compliance, risk controls and accountable governance.

Category: strategic

15. Enterprise Applications, Strategy and Emerging Directions

Learning Objective: Evaluate ML use cases, build-versus-buy choices, value and ROI, organizational readiness and digital transformation; analyse applications such as recommendation, fraud and predictive operations; and assess generative AI, foundation models, AutoML, federated learning, edge AI and future workforce implications.

Category: contemporary

Machine Learning
Post Graduate Program in AI and ML

Machine Learning

Chapter 1
Machine Learning Foundations and Problem Formulation

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Machine Learning Foundations and Problem Formulation

Introduction

Machine learning (ML) has become a central method for building systems that make predictions, recommendations, classifications, and decisions from data. Its applications range from fraud detection and medical diagnosis to language translation, industrial maintenance, scientific discovery, and autonomous systems. Yet successful ML is not primarily a matter of selecting a sophisticated algorithm. It begins with correctly understanding the problem, defining the learning objective, identifying appropriate data, stating assumptions, and designing an evaluation process that reflects real-world use.

This chapter establishes the conceptual foundation for postgraduate study of machine learning. It distinguishes artificial intelligence, machine learning, and statistical learning; compares major learning paradigms; explains essential terminology; and presents a disciplined process for converting a business or scientific question into an ML task. It also examines the assumptions and limitations that determine whether an ML solution is valid and useful.

Learning Objectives

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

  • distinguish artificial intelligence, machine learning, deep learning, and statistical learning;
  • explain supervised, unsupervised, semi-supervised, self-supervised, and reinforcement learning;
  • identify observations, features, labels, parameters, hyperparameters, models, and predictions;
  • formulate business problems as measurable ML tasks;
  • distinguish prediction, estimation, ranking, clustering, recommendation, and decision problems;
  • describe the end-to-end ML lifecycle;
  • state important assumptions concerning data, targets, distributions, and deployment;
  • recognize leakage, bias, distribution shift, and other common threats to validity;
  • evaluate ML systems using metrics appropriate to the decision context.

1. Artificial Intelligence, Machine Learning, and Statistical Learning

Artificial intelligence

Artificial intelligence (AI) is the broad field concerned with constructing systems capable of performing tasks that ordinarily require aspects of human intelligence. These aspects may include perception, reasoning, planning, language understanding, learning, and interaction. AI includes symbolic reasoning, search, knowledge representation, robotics, probabilistic modelling, machine learning, and hybrid approaches.

AI is therefore a broad objective or field, not a single algorithm. A rule-based expert system can be considered an AI system even if it does not learn from data. Conversely, a machine-learning model is an AI component when it supports intelligent behaviour, but not every statistical model is necessarily presented as an AI system.

Machine learning

Machine learning is the study and engineering of computational methods that improve their performance on a task through experience, usually represented by data or interaction with an environment. A useful formalization is:

f̂ = A(D)

Here, D is the available dataset, A is a learning algorithm, and f̂ is the learned model or predictor. The model is then used to produce outputs for new inputs.

For a supervised prediction task, the model may be written as f̂(x), where x is an input vector. The output could be a class, probability, or numerical value. Learning is meaningful only when performance is assessed on data not used to fit the model.

Statistical learning

Statistical learning emphasizes learning relationships from data while accounting for uncertainty, variability, sampling, and generalization. It includes both predictive and inferential aims. Prediction asks, “How accurately can unseen outcomes be predicted?” Inference asks, “What can be learned about relationships, effects, or mechanisms?”

For a regression model, one possible representation is:

y = f(x) + ε

where y is the observed response, x is the input, f is an unknown systematic relationship, and ε is random error. Common assumptions may include E(ε|x) = 0, meaning that the conditional mean of the error is zero. This assumption is not automatically true and must be justified for the intended interpretation.

Machine learning and statistical learning overlap substantially. Both study generalization from data, but machine learning often emphasizes predictive performance, scalability, automation, and deployment, whereas classical statistics may place greater emphasis on sampling designs, parameter interpretation, uncertainty quantification, and causal reasoning. In modern practice, the boundaries are porous.

Deep learning

Deep learning is a family of ML methods based on multi-layer neural networks. It is especially effective when large datasets and computational resources are available, and when inputs such as images, audio, text, or sensor streams have complex structure. Deep learning is not synonymous with AI or ML: it is a subset of ML, which is itself commonly treated as a subset of AI.

2. Core Terminology

A clear vocabulary is essential because imprecise terminology often leads to poorly defined projects.

TermMeaning
Observation or instanceOne entity, event, transaction, patient, document, or time point represented in the data
Feature or predictorAn input variable used by a model
Label or targetThe outcome the model is intended to predict or explain
DatasetA structured collection of observations and variables
Training setData used to estimate model parameters
Validation setData used to select models, hyperparameters, or design choices
Test setData reserved for final performance estimation
ModelA parameterized representation mapping inputs to outputs
ParameterA quantity learned from training data, such as a regression coefficient
HyperparameterA setting chosen outside the fitting process, such as tree depth or regularization strength
Loss functionA numerical measure of disagreement between predictions and observed outcomes
MetricA performance measure used for evaluation or decision-making
GeneralizationPerformance on new data drawn from the intended deployment setting
InferenceProducing an output from a fitted model for an input
Training or fittingEstimating model parameters from data
OverfittingLearning training-specific noise or accidental patterns
UnderfittingUsing a model too inflexible to capture relevant structure
Data leakageUnintended use of information unavailable at the time of prediction
Distribution shiftA change between training conditions and deployment conditions

Let a dataset contain n observations and p features. The feature matrix can be represented as X ∈ ℝn×p, and, in a supervised problem, the target vector as y ∈ ℝn for a numerical response. Each row of X corresponds to one observation and each column to one feature. The notation assumes a numerical representation; categorical data require an appropriate encoding or model representation.

3. Learning Paradigms

3.1 Supervised learning

In supervised learning, each training example contains an input and a target: {(xi,yi)}i=1n. The algorithm learns a function that maps inputs to targets.

Classification predicts a discrete category, such as fraudulent or legitimate. Binary classification has two classes; multiclass classification has more than two. A model may output a predicted class or a probability such as P(Y=1|X=x).

Regression predicts a numerical quantity, such as demand, temperature, or remaining useful life.

A general empirical risk objective is:

f̂ = arg minf ∈ F [1/n] ∑i=1n L(yi, f(xi))

Here, F is the chosen model class, L is a loss function, n is the number of training observations, and arg min denotes the model that minimizes average training loss. This formulation is useful when training observations are sufficiently representative of the deployment population and when the loss reflects the application objective. Minimizing training loss alone can cause overfitting.

For numerical prediction, mean squared error is:

MSE = [1/n] ∑i=1n (yi − ŷi)2

where ŷi is the prediction for observation i. MSE is expressed in squared units of the target and penalizes large errors strongly. Root mean squared error is RMSE = √MSE, expressed in the original target units.

For binary classification, a confusion matrix distinguishes true positives, true negatives, false positives, and false negatives. Precision and recall are:

Precision = TP/(TP + FP)
Recall = TP/(TP + FN)

These ratios require nonzero denominators. Precision answers how many predicted positives are correct; recall answers how many actual positives were found. The appropriate trade-off depends on the costs of missed cases and false alarms.

3.2 Unsupervised learning

Unsupervised learning uses inputs without target labels. The aim may be to discover groups, reduce dimensions, identify anomalies, or model the structure of the data.

In clustering, an algorithm may partition observations into groups that are internally similar according to a chosen representation and distance measure. Clusters are not automatically “natural” categories; they depend on the features, scaling, distance, algorithm, and number of groups.

In dimensionality reduction, high-dimensional observations are represented using fewer variables while attempting to preserve selected properties, such as variance or neighbourhood structure. Such representations can support visualization or downstream modelling but may reduce interpretability.

3.3 Semi-supervised learning

Semi-supervised learning combines a small labelled dataset with a larger unlabelled dataset. It is useful when obtaining labels requires expert time or costly experiments but collecting raw observations is easy. Its success depends on assumptions about the relationship between labelled and unlabelled data. For example, unlabelled observations must generally be relevant to the same prediction population; otherwise, they may add noise or reinforce bias.

3.4 Self-supervised learning

Self-supervised learning creates training targets from the data itself. A text model might hide part of a sentence and predict the missing token; an image model might predict a transformed or withheld part of an image. The constructed task provides a pretraining signal without human labels. The learned representation can later be adapted to a supervised or other downstream task.

Self-supervision is not the same as unsupervised learning in every technical taxonomy, but both operate without externally supplied labels. Self-supervised learning specifically defines an internally generated supervisory signal.

3.5 Reinforcement learning

Reinforcement learning (RL) concerns an agent that interacts with an environment over time. At time t, the agent observes a state st, chooses an action at, receives a reward rt, and transitions to a new state. The objective is usually to maximize expected discounted return:

Gt = ∑k=0∞ γk rt+k+1

where Gt is the return from time t, rt+k+1 is a future reward, and γ satisfies 0 ≤ γ < 1 in the usual discounted formulation. A smaller γ emphasizes immediate rewards; a value closer to one gives greater weight to future rewards. Episodic problems may instead use a finite horizon.

RL differs from ordinary supervised learning because the correct action is not usually supplied for every state, rewards may be delayed, and actions influence future observations. Exploration creates additional risks in safety-critical settings.

4. From Business Question to ML Problem

A business request is rarely an ML problem in its initial form. “Use AI to reduce customer loss” is an objective, not a precise learning specification. Formulation translates the objective into a decision-supported prediction or analysis task.

A disciplined formulation answers six questions:

  1. What decision will be made? For example, whether to contact a customer, inspect a machine, or approve a transaction.
  2. Who or what is the unit of prediction? The unit might be a customer, account, order, machine, claim, or customer-month.
  3. When is the prediction made? The prediction time establishes which information is legally and operationally available.
  4. What outcome and horizon matter? The target might be churn within 30 days or failure within the next 72 hours.
  5. What action follows each prediction? Predictions have value only when connected to an intervention or decision.
  6. How will success and harm be measured? Metrics should include predictive performance, financial value, operational constraints, and fairness or safety requirements.

Target definition and observation window

Suppose a retailer wants to identify customers likely to purchase in the next 30 days. A possible target for customer i is:

yi = 1 if customer i makes at least one purchase during (t0, t0 + 30 days]; otherwise yi = 0

Features must be computed using information available at t0 or earlier. Purchases occurring after t0 cannot be used as features, even if they are present in the historical database. The distinction between feature window, prediction time, and outcome window prevents temporal leakage.

Prediction versus decision

A high-probability prediction does not automatically imply that action is worthwhile. Let p(x) be predicted probability of an event, B the benefit of a correct intervention, C the intervention cost, and H the cost of an avoidable harmful action. A simplified expected net value of acting can be represented as:

Expected value of action = p(x)B − (1 − p(x))C − p(x)H

This expression is illustrative and assumes the quantities are defined on a common monetary or utility scale. Real decisions may require treatment effects, capacity constraints, delayed outcomes, and uncertainty. A prediction model estimates risk; it does not by itself estimate whether an intervention causes improvement.

Choosing the task type

Business needPossible ML formulationTypical output
Predict whether an event occursBinary classificationProbability or class
Predict a quantityRegression or forecastingNumerical estimate and interval
Order cases for reviewRankingPrioritized list
Find similar groupsClustering or representation learningGroup assignment or embedding
Detect unusual activityAnomaly detectionAnomaly score
Recommend itemsRecommendation or rankingOrdered candidates
Select actions over timeReinforcement learning or causal decision methodPolicy or action

A project should not be labelled “classification” merely because the data contain categories. The task is classification when the intended target is categorical and the output supports that decision.

5. The End-to-End Machine-Learning Lifecycle

Problem framing

The lifecycle begins by specifying the decision, stakeholders, constraints, target population, prediction time, and success criteria. A useful project charter identifies what will happen if the model is wrong and who is accountable for the resulting decision.

Data acquisition and governance

Data may come from transactional systems, sensors, surveys, documents, applications, or third-party sources. Before modelling, teams should establish provenance, access rights, consent requirements, retention policies, and data-quality controls. Data dictionaries should define variables, units, timestamps, coding conventions, missing-value meanings, and known changes in collection.

Exploration and preparation

Exploratory analysis examines distributions, missingness, duplicates, outliers, class imbalance, temporal patterns, and relationships between variables. Preparation may include imputation, scaling, encoding, aggregation, and text or image processing. Transformations must be fitted using training data only when they estimate quantities from data; otherwise information can leak from validation or test sets.

Splitting and validation

A random split may be reasonable when observations are independent and identically distributed—or approximately so—and when future deployment resembles random sampling. Time-dependent problems generally require chronological splits. Grouped observations, such as repeated visits by the same patient, require group-aware splitting to prevent the same entity appearing in both training and evaluation data.

Cross-validation estimates performance across several training-validation partitions. It is valuable for model selection, but it does not replace a genuinely untouched final test set. In small or highly structured datasets, uncertainty in the estimate may remain substantial.

Modelling and baseline construction

A baseline is a simple reference method, such as predicting the majority class, the historical mean, or the last observed value. A complex model should demonstrate improvement over a meaningful baseline, not merely over an arbitrary weak method. Model selection should consider performance, interpretability, latency, memory, maintenance, robustness, and governance.

Evaluation

Evaluation must reflect deployment. Accuracy can be misleading under severe class imbalance. A fraud detector that labels every transaction legitimate may achieve high accuracy while finding no fraud. Precision-recall analysis may be more informative when positive cases are rare. Calibration assesses whether predicted probabilities correspond to observed frequencies; for example, among cases assigned probability near 0.7, approximately 70% should experience the event under a suitable calibration population.

Uncertainty should be reported where feasible. A single score hides variation across subgroups, time periods, sites, and random seeds. Confidence or prediction intervals require assumptions and appropriate estimation procedures; they should not be treated as guaranteed bounds.

Deployment and monitoring

Deployment may involve batch scoring, real-time APIs, embedded devices, or human-facing dashboards. Monitoring should cover input quality, missingness, prediction distributions, latency, resource use, outcome performance, calibration, subgroup behaviour, and drift. A model can degrade even when its code is unchanged because user behaviour, policies, markets, sensors, or populations change.

Maintenance and retirement

Models need documented owners, versioning, rollback procedures, retraining criteria, incident handling, and retirement rules. Retraining automatically on newly collected data can amplify errors or feedback loops if labels are biased or outcomes are affected by prior model decisions.

6. Assumptions and Threats to Validity

Representativeness and dependence

Most evaluation arguments assume that deployment examples are sufficiently related to training examples. The strongest textbook assumption is often independent and identically distributed sampling, but real systems frequently violate it. Observations may be correlated over time, clustered by location, or generated by strategic users.

Label quality

Labels may be incomplete, delayed, subjective, or influenced by institutional decisions. A hospital diagnosis label may reflect access to testing rather than true disease status. In such cases, optimizing agreement with historical labels may reproduce existing inequalities.

Missing data

Missingness can be informative. A missing laboratory test may indicate that a clinician did not suspect a condition, not merely a random technical failure. Imputation must therefore be aligned with the data-generating process and evaluated for subgroup effects.

Causality and intervention effects

Predictive association does not establish causation. A feature may predict churn because it is correlated with dissatisfaction, but changing that feature may not reduce churn. If the objective is to choose an intervention, causal inference, randomized experimentation, or treatment-effect modelling may be needed.

Leakage

Leakage occurs when information unavailable at prediction time enters the features, directly or indirectly. Examples include post-outcome billing codes, future sales totals, labels encoded in filenames, and preprocessing performed before data splitting. Leakage often produces impressive validation results and disappointing deployment performance.

Distribution shift and feedback loops

Let Ptrain(X,Y) denote the training joint distribution and Pdeploy(X,Y) the deployment distribution. Performance can change when these distributions differ. Covariate shift changes P(X), label shift changes P(Y), and concept drift changes the relationship P(Y|X). These categories are idealizations; real changes may involve all three.

A deployed model may also alter the data it later observes. For example, a fraud model that blocks selected transactions prevents those transactions from generating future outcomes, making evaluation and retraining more difficult.

7. Worked Formulation Example

A logistics company wants to reduce late deliveries.

Initial request: “Build an ML system to predict late orders.”

Unit: One order scheduled for delivery.

Prediction time: At the time the order is dispatched, because intervention must occur before dispatch or during routing.

Target: y = 1 if delivery occurs more than 24 hours after the promised time; otherwise y = 0. The threshold must be agreed with operations and may need separate treatment for different service contracts.

Features: Distance, route, carrier, weather available at dispatch, warehouse workload, historical carrier performance computed only from prior orders, and package characteristics.

Forbidden features: Actual arrival time, post-dispatch delay codes, and final route changes that were not known when the prediction was required.

Action: Orders above a chosen risk threshold are routed to an expedited carrier or reviewed by a planner. Capacity limits mean that ranking may be more useful than a simple class label.

Evaluation: Use chronological validation. Report recall among the highest-risk orders, precision at the available intervention capacity, calibration, average delay, intervention cost, and performance by region and carrier.

Baseline: Predict late delivery using historical late rates by route or use the overall late rate. The ML model is valuable only if it improves operational outcomes after intervention costs, not merely if it improves an offline score.

This formulation reveals that the project is not just a classification problem. It is a time-indexed ranking and decision-support problem with operational constraints and possible feedback effects.

8. Illustrative Case Study: Predictive Maintenance in Manufacturing

Background

A manufacturer operates industrial pumps in several plants. Unexpected failures interrupt production, while inspecting every pump frequently is expensive. The organization considers an ML system to prioritize inspections.

Problem

The original proposal is “use sensor data to predict failures.” This is incomplete because failure must be defined, the forecast horizon must be specified, and the intervention must be identified.

The team defines the prediction unit as a pump-day. At the start of each day, the system predicts whether the pump will experience a failure requiring an unplanned shutdown within the next seven days. Sensors available at prediction time include vibration, temperature, pressure, operating load, and recent maintenance history.

Analysis

Historical data reveal that some pumps have many more sensor readings than others and that maintenance records are inconsistent across plants. Failures are rare, making accuracy unsuitable as the principal metric. The team also discovers that a maintenance code entered after a failure would have leaked the target and removes it.

Because readings from the same pump are correlated, a random row-level split would overstate generalization. The evaluation uses chronological splits and reports results separately for plants. A simple baseline based on recent threshold violations is retained.

Approach and solution

The team develops a probability model and a ranking interface. Rather than sending every high-risk pump for inspection, operations can inspect only the top 5% each day. The primary evaluation therefore includes precision at 5%, recall within seven days, calibration, false-alarm burden, and avoided downtime per inspection.

A human maintenance engineer reviews explanations based on recent sensor trends and operating context. The system does not automatically shut down equipment; it supports a safety-reviewed inspection decision. Monitoring detects sensor outages, changes in pump populations, and declining calibration.

Outcome

In an illustrative deployment, the model identifies a greater proportion of failures within the inspection capacity than the baseline, but performance varies by plant because sensor quality differs. The organization therefore delays expansion until data collection is standardized and establishes plant-specific monitoring. The case demonstrates that a technically strong model may not be ready for universal deployment if the measurement process and operating context are inconsistent.

Learning points

  • Define the target and horizon in relation to an actual intervention.
  • Respect temporal availability when constructing features.
  • Use grouped and chronological validation when observations are dependent.
  • Select metrics according to capacity, cost, and safety.
  • Treat data quality and maintenance workflow as part of the ML system.
  • Use human oversight when errors can create physical or operational harm.

9. Ethical, Legal, and Governance Considerations

ML systems can distribute benefits and burdens unevenly. Fairness analysis should consider the relevant groups, decision context, base rates, measurement quality, and legally protected characteristics. There is no single fairness metric that resolves every conflict. For example, equal false-positive rates and equal calibration may be difficult to achieve simultaneously when groups have different base rates.

Privacy requires data minimization, purpose limitation, access control, secure storage, and careful handling of sensitive attributes. Removing a protected attribute does not guarantee fairness because proxy variables may remain. In regulated domains, organizations may need explainability, auditability, human review, contestability, documentation, and records of model changes.

Governance should assign responsibility across data owners, model developers, domain experts, risk teams, and decision-makers. Documentation should state intended use, excluded uses, training data, evaluation populations, limitations, known failure modes, and monitoring requirements. Generative and foundation models introduce additional concerns involving data provenance, memorization, hallucination, prompt injection, and uncertain output reliability; these concerns do not eliminate the need for basic problem formulation.

10. Contemporary Directions and Best Practices

Modern ML increasingly combines self-supervised pretraining, foundation models, multimodal data, retrieval systems, and human feedback. These developments can reduce the cost of task-specific modelling but may increase infrastructure, evaluation, privacy, and governance complexity. Transfer learning is useful only when the source representation is relevant to the target population and task.

Effective practice follows several principles:

  • begin with the decision and not the algorithm;
  • define a prediction time and prevent future information from entering features;
  • establish a simple, credible baseline;
  • use validation that matches time, groups, and deployment conditions;
  • choose metrics linked to operational value and harm;
  • inspect performance across relevant subgroups and environments;
  • distinguish prediction from causal intervention;
  • document assumptions, data provenance, and limitations;
  • monitor the complete system after deployment;
  • design for human oversight, security, privacy, and accountability.

Chapter Summary

Artificial intelligence is the broad field of intelligent computational systems, while machine learning develops methods that improve through data or interaction. Statistical learning provides a framework for modelling relationships, uncertainty, and generalization. Supervised learning uses labelled examples; unsupervised learning discovers structure without target labels; semi-supervised learning combines labelled and unlabelled data; self-supervised learning constructs targets from the data itself; and reinforcement learning learns through sequential interaction and reward.

An ML project should be formulated around a decision, unit of prediction, prediction time, target, horizon, action, and success criteria. Classification, regression, ranking, clustering, anomaly detection, recommendation, forecasting, and sequential decision-making serve different purposes. The ML lifecycle extends from problem framing and data governance through preparation, validation, modelling, evaluation, deployment, monitoring, and retirement.

Generalization depends on assumptions about representativeness, labels, dependence, missingness, and distribution stability. Leakage, bias, feedback loops, and distribution shift can invalidate apparently strong results. Ethical and regulatory responsibilities require privacy protection, fairness analysis, documentation, human oversight, and accountable governance. The central lesson is that an ML system is a socio-technical decision system, not merely a trained algorithm.

Key Terms

Artificial intelligence: The field of systems that perform tasks associated with intelligent behaviour.

Machine learning: Methods that learn patterns or policies from data or interaction.

Statistical learning: Learning relationships from data while considering uncertainty, sampling, and generalization.

Feature: An input variable supplied to a model.

Label: The observed target associated with a supervised example.

Model: A learned or specified representation mapping inputs to outputs.

Parameter: A quantity estimated during model fitting.

Hyperparameter: A modelling choice set outside ordinary parameter estimation.

Supervised learning: Learning from input-target pairs.

Unsupervised learning: Learning structure from inputs without externally supplied targets.

Semi-supervised learning: Learning from both labelled and unlabelled observations.

Self-supervised learning: Learning with targets generated from the data itself.

Reinforcement learning: Learning actions through interaction and reward.

Generalization: Performance on new data from the intended deployment setting.

Overfitting: Fitting accidental or training-specific patterns that do not generalize.

Data leakage: Use of information unavailable at the prediction time.

Distribution shift: A change between training and deployment data-generating conditions.

Calibration: Agreement between predicted probabilities and observed event frequencies.

Baseline: A simple reference method used for comparison.

Model governance: Processes for accountability, documentation, monitoring, and control of models.

Review Questions

Short Answer Questions

  1. How do artificial intelligence, machine learning, and statistical learning differ?
  2. What is the distinction between a parameter and a hyperparameter?
  3. Why is prediction time important in feature engineering?
  4. Define data leakage and give one example.
  5. What is the main difference between supervised learning and reinforcement learning?

Long Answer Questions

  1. Explain the five major learning paradigms and provide one appropriate application for each.
  2. Describe the complete machine-learning lifecycle from problem framing to model retirement.
  3. Explain why accuracy may be inappropriate for a rare-event classification problem. Discuss alternative evaluation considerations.
  4. Discuss how distribution shift, label quality, and feedback loops can affect deployment performance.
  5. Explain why a business objective must be translated into a unit of prediction, target, horizon, action, and success criterion.

Analytical / Application Questions

  1. A bank wants to identify customers likely to default within 90 days. Formulate the prediction time, target, allowable features, validation strategy, and at least three evaluation metrics.
  2. A hospital has repeated records for the same patients. Explain why a random row-level split may be misleading and design a more appropriate evaluation approach.
  3. A company wants to use a churn model to decide whom to contact with retention offers. Explain why high predicted churn risk does not necessarily identify customers who will benefit from the offer, and propose a suitable analytical approach.

Multiple Choice Questions with Answers

Question 1

Which statement best distinguishes machine learning from artificial intelligence?

A. Machine learning is a broad field that includes all forms of reasoning

B. Machine learning is a set of methods that learn from data or interaction, whereas AI is the broader field

C. Artificial intelligence only concerns neural networks

D. Machine learning cannot be used for prediction

Correct Answer: B. Machine learning is a set of methods that learn from data or interaction, whereas AI is the broader field

Question 2

A model predicts whether a transaction is fraudulent or legitimate. What type of supervised task is this?

A. Regression

B. Clustering

C. Binary classification

D. Dimensionality reduction

Correct Answer: C. Binary classification

Question 3

Which situation is an example of data leakage?

A. Scaling features using training-set statistics

B. Using a customer’s post-cancellation information to predict cancellation at an earlier time

C. Evaluating a model on a chronological test set

D. Removing duplicate records before splitting data

Correct Answer: B. Using a customer’s post-cancellation information to predict cancellation at an earlier time

Question 4

What is the principal purpose of a test set?

A. To fit model parameters

B. To choose every hyperparameter repeatedly

C. To provide a final estimate of performance on unused data

D. To create labels automatically

Correct Answer: C. To provide a final estimate of performance on unused data

Question 5

Self-supervised learning differs from ordinary supervised learning because it

A. never uses an objective function

B. creates a training signal from the data itself rather than requiring externally supplied labels

C. can only be used for numerical prediction

D. requires an agent and delayed rewards

Correct Answer: B. creates a training signal from the data itself rather than requiring externally supplied labels

Question 6

In reinforcement learning, the quantity Gt = ∑k=0∞ γk rt+k+1 represents

A. the discounted future return from time t

B. the classification accuracy at time t

C. the number of features in a state

D. the training-set mean squared error

Correct Answer: A. the discounted future return from time t

Question 7

If TP = 80 and FP = 20, what is precision?

A. 0.20

B. 0.50

C. 0.80

D. 0.90

Correct Answer: D. 0.90

Question 8

Why is chronological validation usually preferable for many forecasting problems?

A. It guarantees zero prediction error

B. It reproduces the fact that future observations should be predicted from past observations

C. It always creates balanced classes

D. It removes the need for a test set

Correct Answer: B. It reproduces the fact that future observations should be predicted from past observations

Question 9

Which statement about clustering is most accurate?

A. Clusters are always objectively real categories

B. Clusters depend partly on representation, distance measure, algorithm, and other choices

C. Clustering requires a labelled target for every observation

D. Clustering directly estimates causal effects

Correct Answer: B. Clusters depend partly on representation, distance measure, algorithm, and other choices

Question 10

A model has high accuracy because 99% of cases are negative, but it detects no positive cases. Which conclusion is most appropriate?

A. The model is necessarily excellent

B. Accuracy may be misleading, so positive-class metrics and decision costs should be examined

C. The model must be unsupervised

D. The test set should be discarded automatically

Correct Answer: B. Accuracy may be misleading, so positive-class metrics and decision costs should be examined

Machine Learning
Chapter 1 of 15

Machine Learning

Chapter 2
Mathematical, Statistical and Optimization Foundations

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Mathematical, Statistical and Optimization Foundations

Introduction

Machine learning is often described as the construction of systems that learn from data. More precisely, it is the study of algorithms that estimate an unknown relationship between inputs and outputs, or discover useful structure in observations, by optimizing an objective under uncertainty. The mathematics of machine learning provides the language for representing data, defining uncertainty, measuring error, and improving models.

This chapter develops four mutually connected foundations:

  1. Linear algebra, for representing observations, parameters, transformations, and geometric relationships.
  2. Probability and statistics, for modelling uncertainty, variation, sampling, estimation, and evidence.
  3. Calculus and loss functions, for expressing sensitivity and defining what it means for a model to perform well.
  4. Optimization, for finding parameter values that minimize loss while controlling complexity and computational cost.

These foundations are not separate topics. A typical supervised-learning algorithm represents data as vectors, defines a probabilistic or deterministic prediction function, measures error through a loss function, and uses derivatives to optimize parameters. Regularization incorporates prior preferences for simpler solutions, while validation data estimate whether the learned relationship generalizes beyond the training sample.

Learning Objectives

After studying this chapter, you should be able to:

  • represent data and model parameters using vectors, matrices, and tensors;
  • explain norms, inner products, projections, rank, eigenvectors, and positive-definite matrices;
  • distinguish probability, likelihood, and posterior probability;
  • use common distributions and summary statistics appropriately;
  • derive and interpret estimators such as the sample mean and maximum-likelihood estimate;
  • apply Bayes’ theorem to revise beliefs using evidence;
  • explain derivatives, gradients, Hessians, and the chain rule in model training;
  • compare common loss functions and identify their assumptions;
  • explain overfitting, bias–variance trade-offs, and regularization;
  • analyze gradient descent and its important practical variants;
  • connect mathematical assumptions to professional machine-learning decisions.

1. Linear Algebra as the Language of Data

1.1 Scalars, vectors, matrices, and tensors

A scalar is a single numerical value. A vector is an ordered collection of values, commonly written as x ∈ ℝᵈ, where d is the number of features. A dataset containing n observations and d features can be represented by a matrix X ∈ ℝⁿˣᵈ. Its element xᵢⱼ is the value of feature j for observation i. A target vector is often denoted by y ∈ ℝⁿ.

A parameter vector w ∈ ℝᵈ defines the coefficients of a linear model. With an intercept b, prediction for observation x is:

ŷ = wᵀx + b = ∑ⱼ₌₁ᵈ wⱼxⱼ + b

Here, wᵀ is the transpose of w, and the expression is defined when w and x have the same dimension. For all observations, predictions can be written as:

ŷ = Xw + b1

where 1 is an n-dimensional vector of ones. Alternatively, a column of ones may be appended to X, allowing the intercept to be included in an augmented parameter vector.

A tensor generalizes arrays to more than two dimensions. For example, a colour image may be represented by height × width × three colour channels. Matrix operations remain important even when a model processes tensors, because many neural-network layers perform affine transformations.

1.2 Inner products, norms, and geometry

The inner product of two vectors is:

xᵀz = ∑ⱼ₌₁ᵈ xⱼzⱼ

It measures alignment. If xᵀz = 0, the vectors are orthogonal. The Euclidean, or L², norm is:

||x||₂ = √(∑ⱼ₌₁ᵈ xⱼ²)

The L¹ norm is:

||x||₁ = ∑ⱼ₌₁ᵈ |xⱼ|

Norms measure magnitude and are used in distance calculations and regularization. The Euclidean distance between points x and z is ||x − z||₂. Distances are meaningful only relative to the feature scales: a feature measured in thousands can dominate one measured in fractions. Standardization is therefore often useful:

x′ⱼ = (xⱼ − μⱼ) / σⱼ

where μⱼ and σⱼ are the training-set mean and standard deviation of feature j. The transformation is dimensionless, and the same training-set values must be used to transform validation, test, and future data.

The cosine similarity is:

cos(θ) = (xᵀz) / (||x||₂ ||z||₂)

provided neither vector is zero. It measures direction rather than magnitude and is widely used for document and embedding comparison.

1.3 Matrices, rank, and conditioning

Matrix multiplication is defined when the inner dimensions agree. If A ∈ ℝᵐˣᵖ and B ∈ ℝᵖˣⁿ, then AB ∈ ℝᵐˣⁿ. Matrix multiplication is generally not commutative: AB ≠ BA.

The rank of a matrix is the dimension of its column space, equivalently the maximum number of linearly independent columns. Rank deficiency indicates redundant features or insufficient information to identify all parameters uniquely. In linear regression, highly correlated predictors can produce an ill-conditioned design matrix: small changes in data may cause large changes in estimated coefficients.

A square matrix A is symmetric if A = Aᵀ. It is positive semidefinite if zᵀAz ≥ 0 for every vector z, and positive definite if zᵀAz > 0 for every nonzero z. Covariance matrices are positive semidefinite. Positive-definite Hessians characterize locally strictly convex objectives and support stable optimization near a minimum.

An eigenvector v of a square matrix A satisfies:

Av = λv

where λ is the corresponding eigenvalue. Principal component analysis uses eigenvectors of a covariance matrix, or equivalently singular vectors of a suitably centered data matrix, to identify directions of high variance. Singular value decomposition expresses a matrix as X = UΣVᵀ and is useful for dimensionality reduction, least-squares computation, and numerical analysis.

2. Probability and Statistical Reasoning

2.1 Random variables and probability

A random variable maps outcomes of an uncertain process to numerical values. A discrete random variable has a probability mass function p(x), whereas a continuous random variable has a probability density function f(x). For a continuous variable, f(x) itself is not a probability; probabilities are areas under the density:

P(a ≤ X ≤ b) = ∫ₐᵇ f(x) dx

A valid probability model satisfies non-negativity and total probability one. The expectation of a discrete variable is:

E[X] = ∑ₓ x p(x)

and for a continuous variable:

E[X] = ∫₋∞^∞ x f(x) dx

The variance is:

Var(X) = E[(X − μ)²] = E[X²] − μ²

where μ = E[X]. The standard deviation is σ = √Var(X), in the same units as X. Covariance between X and Y is:

Cov(X,Y) = E[(X − μₓ)(Y − μᵧ)]

Correlation is covariance divided by σₓσᵧ, provided both standard deviations are nonzero. Correlation is unit-free but does not establish causation.

2.2 Conditional probability, independence, and Bayes’ theorem

Conditional probability is:

P(A|B) = P(A ∩ B) / P(B)

when P(B) > 0. Events A and B are independent if P(A ∩ B) = P(A)P(B), equivalently P(A|B) = P(A) when P(B) > 0. Independence is a modelling assumption and should not be inferred merely because variables appear unrelated in a small sample.

Bayes’ theorem follows from the product rule:

P(A|B) = P(B|A)P(A) / P(B)

In machine learning, A may represent a class and B observed features. P(A) is the prior, P(B|A) the class-conditional likelihood, P(A|B) the posterior, and P(B) the evidence. For multiple mutually exclusive classes:

P(y = k | x) = [p(x | y = k)P(y = k)] / [∑ⱼ p(x | y = j)P(y = j)]

The denominator normalizes the posterior probabilities.

A crucial distinction is that likelihood is a function of parameters given observed data, whereas a probability distribution describes possible data under fixed parameters. Confusing P(data|model) with P(model|data) leads to incorrect interpretations of evidence.

2.3 Important distributions

The Bernoulli distribution models one binary trial with success probability p:

P(X = x) = pˣ(1 − p)¹⁻ˣ, x ∈ {0,1}

Its mean is p and variance is p(1 − p). The binomial distribution models the number of successes in n independent Bernoulli trials:

P(X = k) = C(n,k)pᵏ(1 − p)ⁿ⁻ᵏ

The categorical distribution generalizes a single trial to K classes with probabilities p₁,…,pₖ summing to one. The multinomial distribution counts class outcomes over repeated trials.

The normal distribution with mean μ and variance σ² has density:

f(x) = [1/(σ√(2π))] exp[−(x − μ)²/(2σ²)], σ > 0

It is mathematically convenient and often a reasonable approximation for aggregated measurement noise, but not every feature is normally distributed. The central limit theorem concerns the distribution of suitably normalized sums or sample means under conditions such as independent observations with finite variance; it does not say that raw data must be normal.

The exponential distribution models waiting times under a constant-rate Poisson process. Its rate parameter λ has units of inverse time, and its mean is 1/λ. Distribution choice should follow the data-generating context, support of the variable, and diagnostic evidence.

3. Sampling, Estimation, and Generalization

A population is the complete set of entities of interest; a sample is the observed subset. A statistic is computed from a sample, while a parameter describes the population. Machine learning usually observes a finite training sample and seeks low expected risk on future observations drawn from a target distribution.

For independent observations X₁,…,Xₙ with finite mean, the sample mean is:

x̄ = (1/n)∑ᵢ₌₁ⁿ Xᵢ

If the observations have variance σ², then Var(x̄) = σ²/n. The sample variance is commonly estimated by:

s² = [1/(n − 1)]∑ᵢ₌₁ⁿ (Xᵢ − x̄)²

The n − 1 denominator gives an unbiased estimator of population variance under independent identically distributed sampling with finite variance. This correction does not make biased sampling representative.

3.1 Maximum likelihood

Given observations D and a parametric model p(D|θ), maximum likelihood selects:

θ̂ML = arg max_θ p(D|θ)

Because logarithm is monotonic, it is usually equivalent to maximize the log-likelihood:

ℓ(θ) = ∑ᵢ log p(xᵢ|θ)

for independent observations. Minimizing negative log-likelihood is therefore a natural training objective. For a normal-noise regression model with fixed variance, minimizing negative log-likelihood is equivalent, apart from constants and scaling, to minimizing squared error.

3.2 Bias, variance, and predictive risk

For an estimator θ̂ of a parameter θ, bias is E[θ̂] − θ, and variance is E[(θ̂ − E[θ̂])²]. For squared-error prediction at an input x, expected test error can be decomposed conceptually as:

E[(Y − ŷ(x))²] = Bias² + Variance + Irreducible noise

The precise decomposition depends on the data-generating setup and expectation over training samples and noise. A highly flexible model may have low training error but high variance. A rigid model may have high bias. Regularization, feature design, model capacity, and sample size influence this trade-off.

4. Calculus, Loss Functions, and Statistical Learning

4.1 Derivatives and gradients

The derivative measures the local rate of change of a scalar function. For f(x) = x², f′(x) = 2x. For a multivariable function J(w), the gradient is the vector of first partial derivatives:

∇J(w) = [∂J/∂w₁, …, ∂J/∂wd]ᵀ

The gradient points in the direction of steepest local increase under the Euclidean norm; therefore −∇J points toward steepest local decrease. The Hessian is the matrix of second partial derivatives:

H(w)ᵢⱼ = ∂²J / (∂wᵢ∂wⱼ)

When the Hessian is positive semidefinite throughout a convex domain, the function is convex. Every local minimum is then global, although optimization may still be numerically difficult.

The chain rule explains backpropagation. If z = g(u) and u = h(w), then:

dz/dw = (dz/du)(du/dw)

For vector-valued functions, the same principle is expressed through Jacobian matrix products.

4.2 Loss functions

A loss function assigns a numerical penalty to a prediction. The empirical risk is the average loss over training data:

R̂(f) = (1/n)∑ᵢ₌₁ⁿ L(yᵢ, f(xᵢ))

The population risk is the expected loss over the target data distribution:

R(f) = E[L(Y, f(X))]

Training minimizes empirical risk as a proxy for population risk.

For regression, mean squared error is:

MSE = (1/n)∑ᵢ₌₁ⁿ (yᵢ − ŷᵢ)²

It strongly penalizes large errors and corresponds to Gaussian noise assumptions when used as a negative log-likelihood objective. Mean absolute error is:

MAE = (1/n)∑ᵢ₌₁ⁿ |yᵢ − ŷᵢ|

It is less sensitive to outliers and is associated with a Laplace-noise interpretation. Neither loss is universally superior; the choice should reflect the operational cost of errors.

For binary classification, with y ∈ {0,1} and predicted probability p ∈ (0,1), binary cross-entropy is:

L = −[y log(p) + (1 − y)log(1 − p)]

For K-class classification, if yₖ is a one-hot target and pₖ predicted class probability:

L = −∑ₖ₌₁ᴷ yₖ log(pₖ)

Probabilities must be valid and nonzero for any class assigned positive target weight. In practice, numerically stable log-softmax implementations should be used rather than separately computing exponentials and logarithms.

5. Regularization and Model Complexity

Regularization modifies the training objective to discourage undesirable parameter values. Ridge regression minimizes:

J(w) = (1/n)||y − Xw||₂² + λ||w||₂²

Lasso regression minimizes:

J(w) = (1/n)||y − Xw||₂² + λ||w||₁

Here λ ≥ 0 controls penalty strength. The intercept is commonly excluded from the penalty. Ridge tends to shrink correlated coefficients together and usually does not make them exactly zero. Lasso can produce sparse coefficients because the L¹ geometry has corners. The penalty and loss should be scaled consistently; standardizing features is particularly important when coefficient penalties are applied.

From a Bayesian perspective, an L² penalty corresponds to a zero-centred Gaussian prior on coefficients, while an L¹ penalty corresponds to a Laplace prior, under a compatible likelihood and parameterization. This interpretation does not mean that regularization automatically proves a causal prior belief; it provides a useful connection between penalized optimization and probabilistic modelling.

Regularization also includes early stopping, dropout, data augmentation, architectural constraints, and explicit complexity control. Hyperparameters must be selected using validation procedures, not by repeatedly inspecting the untouched test set.

6. Gradient-Based Optimization

6.1 Batch, stochastic, and mini-batch gradient descent

For objective J(w), gradient descent updates parameters as:

wₜ₊₁ = wₜ − η∇J(wₜ)

where η > 0 is the learning rate. If η is too large, updates may overshoot or diverge; if too small, training may be unacceptably slow.

Batch gradient descent computes the gradient using all training observations. Stochastic gradient descent uses one observation at a time, producing noisy but inexpensive updates. Mini-batch methods use a subset Bₜ:

wₜ₊₁ = wₜ − η [1/|Bₜ|]∑ᵢ∈Bₜ ∇Lᵢ(wₜ)

Under random sampling, the mini-batch gradient is often an approximately unbiased estimate of the full-data gradient, although dependence, weighting, and sampling schemes can alter this property.

Momentum accumulates a moving direction, reducing oscillations in poorly conditioned problems. Adaptive methods such as Adam maintain estimates of first and second moments of gradients. They can be effective in deep learning, but their adaptive dynamics do not guarantee better generalization in every setting. Learning-rate schedules, gradient clipping, normalization, and careful initialization can materially affect results.

6.2 A worked example: one-dimensional regression

Suppose a model predicts ŷ = wx with one training observation x = 2 and y = 6. Use the half-squared loss:

L(w) = ½(wx − y)²

The factor ½ simplifies differentiation. By the chain rule:

dL/dw = (wx − y)x

Let w = 1. Then ŷ = 2, residual wx − y = −4, and gradient = (−4)(2) = −8. With learning rate η = 0.1:

wnew = 1 − 0.1(−8) = 1.8

The updated prediction is 3.6, closer to 6 than the initial prediction. This single update does not establish convergence; repeated updates and a representative dataset are required.

7. Worked Statistical Example: Bayesian Diagnosis

Consider an illustrative screening test. Suppose disease prevalence is 1%, sensitivity is 90%, and the false-positive rate is 5%. Let D denote disease and + a positive result. Bayes’ theorem gives:

P(D|+) = [P(+|D)P(D)] / [P(+|D)P(D) + P(+|not D)P(not D)]

Substitution gives:

P(D|+) = (0.90 × 0.01) / [(0.90 × 0.01) + (0.05 × 0.99)]
= 0.009 / 0.0585 ≈ 0.154

Thus, approximately 15.4% of positive results correspond to disease under these assumptions. The example illustrates why a high sensitivity does not imply a high posterior probability when the condition is rare. In real healthcare applications, prevalence may vary by population, test characteristics may depend on disease stage, and decisions require clinical validation, consent, privacy protection, and appropriate communication of uncertainty.

8. Applied Case: Predicting Loan Default

Background

This illustrative case concerns a financial institution developing a model to estimate the probability that an applicant will default within twelve months. The model will support, but not replace, credit assessment and governance processes.

Problem

The institution has historical records containing income, debt ratio, repayment history, loan amount, and a binary default outcome. It wants accurate predictions while avoiding discriminatory effects, data leakage, unstable coefficients, and unjustified certainty.

Analysis

The data team first defines the prediction time and outcome window. Features unavailable at application time are excluded to prevent leakage. Numerical variables are examined for missingness, outliers, skewness, and scale. The data are split into training, validation, and test sets using a time-aware design if economic conditions change over time. Class imbalance is assessed using prevalence and appropriate metrics rather than accuracy alone.

A logistic model represents the conditional probability of default as:

P(Y = 1|x) = 1 / [1 + exp(−(wᵀx + b))]

The coefficients are estimated by minimizing cross-entropy, possibly with an L¹ or L² penalty. Standardization is fitted only on training data. Validation data are used to select λ, learning rate, and decision threshold. Performance is evaluated using discrimination measures such as area under the ROC curve, calibration, expected cost, and subgroup analyses. Calibration is important because a predicted probability of 0.20 should correspond approximately to a 20% event frequency among comparable cases.

Approach and solution

The institution compares a regularized logistic baseline with a tree-based model. It uses cross-validation within the training period, preserves the final test period for one-time evaluation, and documents features, exclusions, assumptions, and limitations. A threshold is selected according to the relative costs of false approvals and false rejections, rather than automatically using 0.5.

Governance includes access controls, retention limits, audit logs, adverse-action explanations where legally required, monitoring for drift, and a human appeal process. Protected attributes may be restricted from direct use but remain relevant for fairness auditing, subject to lawful and ethical governance.

Outcome

The selected model improves calibrated risk ranking over the existing rule system, but its deployment is conditional on monitoring. If economic conditions or applicant populations shift, training risk may no longer represent operational risk. The organization therefore establishes periodic recalibration, drift alerts, performance review, and a procedure for suspending automated decisions.

Learning points

The case demonstrates that mathematical quality is necessary but insufficient. Data definitions, sampling, loss functions, regularization, threshold choice, calibration, fairness, explainability, security, and accountability all influence whether a model is fit for use.

9. Limitations, Assumptions, and Contemporary Practice

Mathematical models simplify reality. Independent and identically distributed sampling may fail under time dependence, clustered observations, feedback loops, or changing populations. Missing data may be informative rather than random. Measurement error can attenuate or distort relationships. Optimization can find a low training objective without producing reliable predictions under distribution shift.

Best practice includes defining the estimand and decision context before modelling; establishing reproducible preprocessing pipelines; preventing leakage; choosing metrics aligned with consequences; using uncertainty intervals or calibrated probabilities where appropriate; checking sensitivity to modelling assumptions; and reporting subgroup performance. Reproducible experiments should record data versions, code, random seeds where meaningful, hyperparameters, and evaluation protocols.

Contemporary machine learning increasingly uses automatic differentiation, differentiable programming, large-scale stochastic optimization, probabilistic programming, and uncertainty-aware methods. These tools improve expressive power but do not remove the need for statistical assumptions or critical evaluation. A larger model can amplify biased labels, privacy risks, security vulnerabilities, and environmental cost. Responsible practice requires lawful data use, minimization of personal information, protection against unauthorized inference, documentation, human oversight, and mechanisms for contesting harmful outcomes.

Chapter Summary

Machine learning rests on a connected mathematical pipeline. Linear algebra represents observations, parameters, transformations, distances, and geometric structure. Probability describes uncertainty, while statistics provides tools for sampling, estimation, likelihood, and generalization. Bayes’ theorem distinguishes prior information from evidence and posterior belief. Calculus supplies derivatives, gradients, Hessians, and the chain rule needed to understand learning updates. Loss functions formalize predictive error, and maximum likelihood explains several widely used objectives. Regularization controls complexity and can be interpreted through both optimization and Bayesian perspectives. Gradient descent and its variants search parameter spaces, but their success depends on scaling, conditioning, learning rates, data quality, and objective design. Finally, applied machine learning must address leakage, distribution shift, calibration, fairness, privacy, explainability, and governance in addition to numerical performance.

Key Terms

Vector: An ordered collection of numerical values.

Matrix: A rectangular array of values used to represent data or transformations.

Inner product: A scalar product measuring alignment between two vectors.

Norm: A function measuring vector magnitude.

Rank: The dimension of a matrix’s linearly independent column or row space.

Eigenvector: A vector whose direction is unchanged by a linear transformation apart from scaling.

Random variable: A numerical representation of an uncertain outcome.

Expectation: The probability-weighted average value of a random variable.

Variance: The expected squared deviation from the mean.

Likelihood: A function measuring how compatible observed data are with parameter values.

Prior: A probability distribution representing belief before observing current evidence.

Posterior: A probability distribution after updating with evidence.

Estimator: A rule for using sample data to infer an unknown parameter.

Bias: Systematic difference between an estimator’s expected value and the target parameter.

Loss function: A function assigning a penalty to a prediction.

Gradient: The vector of first partial derivatives of a scalar function.

Regularization: An objective modification that controls model complexity.

Learning rate: The step-size parameter in an iterative optimization update.

Calibration: Agreement between predicted probabilities and observed event frequencies.

Distribution shift: A change between the data-generating conditions used for training and those encountered in deployment.

Review Questions

Short Answer Questions

  1. Why is feature standardization often important before applying L² regularization?
  2. Distinguish probability, likelihood, and posterior probability.
  3. What is the practical meaning of the gradient of a loss function?
  4. State one important difference between ridge and lasso regularization.
  5. Why can accuracy be misleading for a highly imbalanced classification problem?

Long Answer Questions

  1. Explain how linear algebra, probability, loss functions, and optimization interact in supervised learning.
  2. Derive the gradient-descent update for the half-squared loss of a linear model.
  3. Discuss the bias–variance trade-off and explain how regularization affects it.
  4. Compare MSE, MAE, and cross-entropy in terms of assumptions and practical use.
  5. Explain the assumptions and governance requirements involved in deploying a credit-default model.

Analytical / Application Questions

  1. A dataset contains features measured in dollars, years, and percentages. Design a preprocessing and regularization strategy, explaining how scale affects optimization and coefficient penalties.
  2. A disease has prevalence 2%, test sensitivity 95%, and false-positive rate 10%. Compute the posterior probability of disease after a positive result and interpret why it differs from sensitivity.
  3. A mini-batch gradient has a very large norm and training loss oscillates. Analyze at least three possible causes and propose a technically justified diagnostic and remediation plan.

Multiple Choice Questions with Answers

Question 1

Which expression represents the Euclidean norm of vector x?

A. ∑ⱼ |xⱼ|

B. √(∑ⱼ xⱼ²)

C. ∑ⱼ xⱼ²

D. maxⱼ xⱼ

Correct Answer: B. √(∑ⱼ xⱼ²)

Question 2

If two nonzero vectors have an inner product of zero, what is their relationship?

A. They are parallel.

B. They have equal magnitude.

C. They are orthogonal.

D. They are statistically independent.

Correct Answer: C. They are orthogonal.

Question 3

A disease prevalence is 1%, sensitivity is 90%, and false-positive rate is 5%. Approximately what is the probability of disease given a positive test?

A. 1.0%

B. 5.0%

C. 15.4%

D. 90.0%

Correct Answer: C. 15.4%

Question 4

Which objective is most directly associated with maximum likelihood for independent observations?

A. Maximizing the sum of log-likelihoods

B. Minimizing the feature means

C. Maximizing the L¹ norm only

D. Minimizing the number of observations

Correct Answer: A. Maximizing the sum of log-likelihoods

Question 5

What is the principal effect of an L² penalty in linear regression?

A. It usually shrinks coefficients toward zero.

B. It guarantees every coefficient is exactly zero.

C. It removes the need for a validation set.

D. It converts every feature into a binary variable.

Correct Answer: A. It usually shrinks coefficients toward zero.

Question 6

For gradient descent, the standard update with learning rate η is:

A. wₜ₊₁ = wₜ + η∇J(wₜ)

B. wₜ₊₁ = ηwₜ − ∇J(wₜ)

C. wₜ₊₁ = wₜ − η∇J(wₜ)

D. wₜ₊₁ = wₜ/η + ∇J(wₜ)

Correct Answer: C. wₜ₊₁ = wₜ − η∇J(wₜ)

Question 7

Which loss is generally most sensitive to a single very large regression error?

A. Mean absolute error

B. Mean squared error

C. Zero-one loss

D. Cosine distance

Correct Answer: B. Mean squared error

Question 8

What does calibration mean for a probabilistic classifier?

A. All classes have equal frequency.

B. Predicted probabilities agree with observed frequencies.

C. The model has zero training error.

D. Every feature has unit variance.

Correct Answer: B. Predicted probabilities agree with observed frequencies.

Question 9

Why should the untouched test set not be repeatedly used for hyperparameter selection?

A. It cannot contain numerical values.

B. Repeated inspection can adapt decisions to the test set and bias performance estimates.

C. It always has fewer observations than the training set.

D. It prevents gradient computation.

Correct Answer: B. Repeated inspection can adapt decisions to the test set and bias performance estimates.

Question 10

Which condition is characteristic of a convex optimization problem?

A. Every local minimum is a global minimum.

B. The objective must contain an exponential function.

C. The gradient must be zero everywhere.

D. The objective cannot have constraints.

Correct Answer: A. Every local minimum is a global minimum.

Machine Learning
Chapter 2 of 15

Machine Learning

Chapter 3
Data Understanding, Preparation and Feature Engineering

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Data Understanding, Preparation and Feature Engineering

Introduction

Machine-learning performance depends at least as much on data quality and representation as on algorithm selection. A sophisticated model trained on duplicated, biased, leaked, inconsistent, or poorly represented data can produce unreliable predictions. Data preparation is therefore not a clerical preliminary step; it is a form of model design.

This chapter presents an end-to-end approach to acquiring, understanding, cleaning, transforming, sampling, and representing data. Particular attention is given to exploratory data analysis (EDA), feature construction, class imbalance, data leakage, reproducibility, and lineage. The central principle is that every transformation must be justified by the problem context, learned only from appropriate data, and recorded so that it can be reproduced and audited.

Learning Objectives

After studying this chapter, a learner should be able to:

  1. Identify suitable data sources and assess their quality, provenance, and legal status.
  2. Perform univariate, bivariate, and multivariate exploratory analysis.
  3. Detect and manage missing values, duplicates, outliers, inconsistent formats, and erroneous records.
  4. Select appropriate encoding, scaling, transformation, and sampling procedures.
  5. Construct useful features while preserving semantic validity and avoiding leakage.
  6. Address class imbalance using principled methods and suitable evaluation metrics.
  7. Design reproducible preprocessing pipelines with data lineage and governance controls.

1. Data Acquisition and Data Quality

1.1 Sources and data contracts

Data may be acquired from transactional databases, application programming interfaces, sensors, surveys, documents, experiments, public repositories, or third-party providers. Before acquisition, define a data contract: the expected fields, types, units, permissible ranges, update frequency, ownership, security classification, and meaning of the target variable.

A data dictionary should specify, for each variable, its name, business meaning, storage type, measurement unit, allowed values, missing-value convention, and source system. For example, “income” is incomplete without knowing whether it means annual gross income, monthly net income, or an imputed estimate.

Important quality dimensions include:

DimensionMeaningTypical diagnostic
CompletenessRequired values are presentMissingness rate
ValidityValues follow domain rulesRange and type checks
ConsistencyRelated fields agreeCross-field constraints
AccuracyValues represent realityComparison with trusted source
TimelinessData is current enoughTimestamp and freshness
UniquenessRecords are not unintentionally repeatedDuplicate-key analysis

Data quality is contextual. A missing postal code may be tolerable for a recommendation task but unacceptable for a location-based emergency model.

1.2 Sampling and representativeness

A dataset is a sample from a target population. If the sample differs systematically from that population, model performance may not generalize. Sampling error concerns random variation, whereas sampling bias arises from the selection process itself. A very large non-representative sample can remain seriously biased.

For a population proportion estimated by a simple random sample, an approximate standard error is:

SE( p̂ ) = √[ p(1 − p) / n ]

Here, p is the population proportion, p̂ is its estimate, and n is the sample size. The approximation assumes independent observations and is most useful when the sample is reasonably representative. In practice, data scientists should also examine coverage, non-response, temporal drift, and subgroup representation.

Stratified sampling divides the population into groups and samples within each group. It can improve representation of small but important segments. Random sampling is often suitable for independent records, whereas time series require chronological splitting to prevent future information from entering the past.

2. Exploratory Data Analysis

EDA combines numerical summaries, visualizations, and domain reasoning. It is not merely a search for correlations; it is an investigation of how the data was generated and whether its structure is compatible with the intended task.

2.1 Univariate analysis

For numerical variables, inspect count, missingness, minimum, maximum, mean, median, standard deviation, quantiles, and skewness. The arithmetic mean is:

x̄ = (1/n) ∑ᵢ₌₁ⁿ xᵢ

The sample standard deviation is:

s = √[ (1/(n − 1)) ∑ᵢ₌₁ⁿ (xᵢ − x̄)² ]

The divisor n − 1 provides the usual unbiased estimator of population variance under independent, identically distributed sampling. For highly skewed data, the median and interquartile range (IQR) may be more informative than the mean and standard deviation. The IQR is Q₃ − Q₁, where Q₁ and Q₃ are the first and third quartiles.

Categorical variables should be examined using frequency and proportion tables. Rare categories may indicate genuine minority groups, data-entry errors, or excessive granularity.

2.2 Relationships among variables

Bivariate analysis compares a predictor with the target or with another predictor. Useful tools include scatter plots, grouped box plots, cross-tabulations, conditional means, and correlation measures. Pearson correlation measures linear association:

r = [∑(xᵢ − x̄)(yᵢ − ȳ)] / √[∑(xᵢ − x̄)² ∑(yᵢ − ȳ)²]

Its value lies between −1 and 1. It is sensitive to outliers and does not establish causation. A correlation near zero can coexist with a strong nonlinear relationship. Spearman correlation, based on ranks, is often more appropriate for monotonic but non-linear relationships or ordinal data.

Multivariate analysis examines interactions, redundancy, clusters, and subgroup differences. A variable may appear harmless globally but become highly predictive within one subgroup. EDA should therefore be conducted not only overall but also across time, geography, demographic groups, and operational segments.

Data preparation pipeline with leakage control Raw datasource + lineage Splittrain / validation / test Fit transformstraining data only Model + evaluationunseen data Never learn preprocessing from validation or test data Temporal or group rules may alter the split
Data preparation pipeline with leakage control
Figure: Data preparation pipeline with leakage control

Figure: A leakage-aware data preparation pipeline.

3. Cleaning and Data Quality Management

3.1 Missing values

Missingness may be completely at random, related to observed variables, or related to the unobserved value itself. These mechanisms are commonly described as MCAR, MAR, and MNAR. The mechanism matters: replacing all missing values with a mean can conceal informative non-response.

Common strategies include:

  • deletion, when missing records are few and plausibly random;
  • constant-value or statistical imputation;
  • group-wise imputation using domain-relevant strata;
  • model-based or multiple imputation;
  • an explicit missingness indicator when absence itself may carry meaning.

For a numeric feature, training-set median imputation is robust to extreme values. The imputation value must be calculated using training data only and then applied unchanged to validation and test data.

3.2 Duplicates, inconsistency, and invalid records

Duplicates can arise from repeated exports, retries in distributed systems, or legitimate repeated events. Deduplication requires a definition of record identity; blindly dropping identical rows may remove valid repeated transactions. Standardize dates, units, spelling, and categorical labels before aggregation. Validate domain rules such as age ≥ 0, an end date not preceding a start date, and total amount equalling the permitted sum of components.

3.3 Outliers

An outlier may be an error, a rare but valid event, or evidence of a new regime. Detection methods include robust IQR rules, z-scores, isolation-based methods, and domain thresholds. A common descriptive rule flags values below Q₁ − 1.5 IQR or above Q₃ + 1.5 IQR, but it is not a universal deletion rule. Alternatives include correction, winsorization, log transformation, robust scaling, separate modelling, or retention with an outlier indicator.

4. Transformation and Encoding

4.1 Numerical transformations and scaling

Standardization transforms a feature as:

zᵢ = (xᵢ − μ) / σ

where μ and σ are the training-set mean and standard deviation. It produces unitless values with mean approximately zero and standard deviation approximately one, assuming σ > 0. It is especially important for distance-based, gradient-based, and regularized models.

Min–max scaling is:

x′ = (x − xmin) / (xmax − xmin)

It maps training values to approximately [0, 1], but is sensitive to extreme values and can produce values outside that interval for future observations. Robust scaling uses the median and IQR and is less affected by outliers. Tree-based models generally do not require scaling because their splits depend on order rather than distance.

Logarithmic transformation can reduce right skew, but ordinary log(x) requires positive values. For non-negative data containing zeros, log(1 + x) is often used; negative values require a separately justified transformation.

4.2 Categorical encoding

Nominal categories can be represented using one-hot encoding. For k categories, one-hot encoding creates binary indicators. With an intercept in an ordinary linear regression, one category is commonly omitted to avoid perfect multicollinearity (the dummy-variable trap). Regularized models may tolerate redundant columns, but the modelling objective and implementation should determine the choice.

Ordinal encoding is appropriate only when category order is meaningful. Target encoding replaces a category with a target-related statistic and can be powerful for high-cardinality variables, but it is highly vulnerable to leakage. It must use out-of-fold or strictly training-only estimates, with smoothing where appropriate.

High-cardinality identifiers such as customer IDs usually identify entities rather than explain transferable behaviour. They should not be used as ordinary predictive features without a clear generalization argument.

5. Feature Engineering

Feature engineering converts raw observations into variables that expose relevant structure to a learning algorithm. Examples include age from date of birth, tenure from start date, rolling transaction totals, ratios, interaction terms, text representations, and domain-based aggregations.

A feature should satisfy four tests:

  1. Availability: Is it known at prediction time?
  2. Validity: Does it represent the intended concept?
  3. Stability: Will its meaning and distribution persist?
  4. Value: Does it improve out-of-sample performance or interpretability?

Ratios require special care. A ratio with a near-zero denominator can be unstable, and a mathematically defined ratio may not have a meaningful business interpretation. Interaction terms such as x₁x₂ can represent conditional effects, but they increase dimensionality and may need regularization.

For time-dependent data, derive lagged or rolling features only from prior observations. If a moving average over the previous w periods is used, it may be written as:

mt = (1/w) ∑ⱼ₌₁ʷ xt−j

The exclusion of xt is essential when predicting the value at time t. Feature construction should be implemented in a pipeline so that training and production apply identical logic.

6. Data Leakage and Validation Design

Leakage occurs when information unavailable at prediction time influences model training or evaluation. It can be direct, such as including a post-outcome field, or indirect, such as calculating a global mean before splitting the data.

Typical leakage sources include:

  • imputing or scaling using the entire dataset;
  • selecting features using the test target;
  • random splitting of longitudinal records;
  • duplicate entities appearing across train and test sets;
  • target encoding without out-of-fold discipline;
  • features generated after the decision event;
  • preprocessing performed before cross-validation.

A robust workflow first defines the prediction timestamp, then splits data according to deployment conditions. Random splits may be suitable for independent observations; temporal splits are needed for forecasting; group splits are needed when the same person, household, patient, or device could otherwise occur in multiple partitions.

Cross-validation estimates generalization by repeatedly fitting on training folds and evaluating on held-out folds. Every learned operation—including imputation, scaling, feature selection, and encoding—must be fitted separately within each training fold. A pipeline formalizes this requirement and reduces operational inconsistency.

7. Imbalanced Data

In binary classification, let TP, TN, FP, and FN denote true positives, true negatives, false positives, and false negatives. Accuracy is:

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

For rare events, accuracy may be misleading. Precision and recall are:

Precision = TP / (TP + FP)
Recall = TP / (TP + FN)

The F1 score is their harmonic mean:

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

These measures assume the relevant denominators are non-zero. Specificity is TN/(TN+FP). The choice of metric should reflect the costs of errors. Precision–recall curves are often informative when the positive class is rare; ROC curves remain useful but can appear optimistic under extreme imbalance.

Methods for imbalance include class-weighted learning, random undersampling, oversampling, and synthetic methods such as SMOTE. Oversampling must occur inside training folds, never before the train–test split. Synthetic examples may be implausible in sparse or mixed-type data, and undersampling can discard important majority examples. Threshold adjustment is often preferable when the model ranks cases well but the default decision threshold is inappropriate.

8. Data Lineage, Reproducibility, and Governance

Data lineage records where data originated, how it was transformed, which code and parameters were used, and which model consumed it. A production-grade dataset should have a version, schema, timestamp, owner, quality report, and transformation history. Reproducibility additionally requires controlled random seeds where possible, environment specifications, dependency versions, and immutable evaluation data.

Governance includes privacy, access control, retention, consent, and responsible use. Personal data should be minimized and protected. Sensitive attributes may be legally restricted, yet excluding them does not guarantee fairness because proxy variables can remain. Fairness analysis should examine performance and error rates across relevant groups, while respecting privacy and statistical reliability. Data preparation must also document whether labels reflect historical institutional decisions that may encode discrimination.

9. Applied Case: Credit-Card Fraud Detection

Background

Consider an illustrative payment platform processing millions of card transactions daily. Fraud represents a small minority of transactions, but missed fraud is costly and excessive false alarms inconvenience customers.

Problem

The platform wants to score each transaction at authorization time. Available fields include amount, merchant category, device information, account history, and transaction time. Labels become available only after investigation, sometimes days later.

Analysis

Initial EDA reveals strong class imbalance, repeated authorization messages, missing device identifiers, and different transaction volumes by country. A field called “investigation outcome” has high predictive power but is unavailable at authorization and must be excluded. Randomly splitting transactions would also place transactions from the same account in both partitions, overstating generalization.

Approach

The team first removes technical duplicates using transaction and event identifiers. It defines the prediction timestamp and creates lagged account features from historical transactions only: count of transactions in the preceding 24 hours, amount relative to the account’s historical median, and time since the previous transaction. Missing device identifiers receive an indicator and a training-set-derived imputation where suitable.

The evaluation uses a temporal holdout and a group-aware diagnostic by account. Class-weighted learning is compared with training-fold oversampling. Performance is reported using precision, recall, PR-AUC, calibration, and an operational cost matrix rather than accuracy alone. The decision threshold is selected according to review capacity and the relative cost of missed fraud and false alerts.

Outcome

The final pipeline is stored with its schema, feature definitions, training period, code version, and monitoring rules. Production monitoring tracks missingness, feature drift, alert rate, subgroup error rates, and delayed-label performance. Any feature whose availability changes after deployment triggers a review.

Learning points

The case demonstrates that feature availability, temporal correctness, duplicate control, imbalance handling, threshold selection, and lineage are inseparable from model quality. A high offline score is not credible unless it reproduces the information conditions of deployment.

10. Best Practices and Limitations

A disciplined preparation process should:

  • define the prediction unit, target, and prediction time;
  • profile schema, missingness, duplicates, distributions, and subgroup coverage;
  • split data according to deployment reality;
  • fit all learned transformations only on training data;
  • preserve raw data while creating versioned derived datasets;
  • test pipelines with known edge cases;
  • monitor drift, missingness, validity, and performance after deployment;
  • document assumptions, exclusions, and ethical risks.

No preprocessing method is universally optimal. Imputation can introduce bias, outlier rules can remove legitimate rare cases, synthetic sampling can distort class boundaries, and feature selection can privilege historical patterns that later change. Preparation is therefore iterative: domain experts, data engineers, statisticians, and model developers must evaluate both statistical performance and operational consequences.

Chapter Summary

Data understanding begins with a clear data contract, provenance assessment, and definition of the target population. Exploratory analysis uses summaries and visualizations to reveal distributions, relationships, errors, subgroup differences, and temporal structure. Cleaning addresses missingness, duplicates, invalid values, inconsistent representations, and outliers while preserving valid rare events.

Transformation includes numerical scaling, skew reduction, and categorical encoding. Feature engineering creates representations aligned with the prediction task, but every feature must be available at prediction time and semantically defensible. Leakage prevention requires deployment-realistic splitting and training-only fitting of preprocessing operations. Imbalanced classification requires metrics and decision thresholds linked to error costs rather than reliance on accuracy. Finally, lineage, reproducibility, privacy, fairness, and monitoring make data preparation suitable for professional machine-learning systems.

Key Terms

Data contract: Formal specification of fields, types, units, quality rules, and ownership.

Data dictionary: Description of variables, meanings, formats, and permissible values.

Data lineage: Record of data origins and transformations.

Exploratory data analysis: Systematic investigation of data using summaries, visualizations, and domain reasoning.

Missingness: Absence of an expected value.

Imputation: Replacement of missing values using a defined method.

Outlier: Observation unusually distant from a distribution or expected domain range.

Standardization: Transformation using training mean and standard deviation.

Min–max scaling: Rescaling based on observed minimum and maximum.

One-hot encoding: Representation of categories by binary indicator variables.

Target encoding: Encoding based on target-related statistics.

Feature engineering: Construction or transformation of predictive variables.

Data leakage: Use of information unavailable at prediction time.

Cross-validation: Repeated training and evaluation over data partitions.

Class imbalance: Unequal representation of target classes.

Precision: Proportion of predicted positives that are true positives.

Recall: Proportion of actual positives that are detected.

Concept drift: Change in the relationship between inputs and the target over time.

Pipeline: Ordered, reproducible sequence of preprocessing and modelling operations.

Review Questions

Short Answer Questions

  1. Why should preprocessing parameters be learned from training data only?
  2. Distinguish data validity from data accuracy.
  3. When is ordinal encoding inappropriate?
  4. Why can accuracy be misleading for rare-event classification?
  5. What information should a data dictionary contain?

Long Answer Questions

  1. Explain a leakage-aware workflow for preparing data for supervised learning.
  2. Compare deletion, statistical imputation, and model-based imputation, including their assumptions and risks.
  3. Discuss how temporal, group, and random splitting differ and when each is appropriate.
  4. Explain the advantages and limitations of standardization, min–max scaling, and robust scaling.
  5. Evaluate oversampling, undersampling, class weighting, and threshold adjustment for imbalanced classification.

Analytical / Application Questions

  1. A hospital model uses a feature recording whether a patient received a specialist referral within 30 days after admission. Explain why this may constitute leakage and propose a valid alternative.
  2. A fraud dataset has 1% positive cases. A classifier produces TP = 80, FP = 120, FN = 20, and TN = 9,780. Calculate precision, recall, and F1, and explain which operational information is still needed before selecting a threshold.
  3. Design a feature-engineering and validation strategy for predicting monthly customer churn when customers appear repeatedly over time. Specify the prediction timestamp, split method, leakage controls, and monitoring plan.

Multiple Choice Questions with Answers

Question 1

Which operation must be fitted using training data only?

A. Computing a global mean for imputation

B. Assigning a fixed column name

C. Removing an impossible negative age

D. Reading a schema description

Correct Answer: A. Computing a global mean for imputation

Question 2

Which metric is the harmonic mean of precision and recall?

A. Accuracy

B. F1 score

C. Specificity

D. Mean squared error

Correct Answer: B. F1 score

Question 3

Which encoding is appropriate when categories have a genuine order?

A. Ordinal encoding

B. Random encoding

C. Pixel encoding

D. Hashing the target

Correct Answer: A. Ordinal encoding

Question 4

What is the principal danger of target encoding without fold discipline?

A. Underflow

B. Data leakage

C. Unit conversion

D. Duplicate column names

Correct Answer: B. Data leakage

Question 5

Which split is generally most appropriate for forecasting future outcomes?

A. Purely random split

B. Temporal split

C. Alphabetical split

D. Split by feature magnitude only

Correct Answer: B. Temporal split

Question 6

For a binary classifier with TP = 80 and FP = 20, what is precision?

A. 0.20

B. 0.50

C. 0.80

D. 0.90

Correct Answer: D. 0.90

Question 7

Which model family generally does not require feature scaling for split selection?

A. Decision trees

B. Nearest-neighbour models

C. Many gradient-based linear models

D. Support-vector models with distance-based kernels

Correct Answer: A. Decision trees

Question 8

Why is a post-outcome investigation field unsuitable for authorization-time fraud prediction?

A. It has too few categories

B. It is unavailable at prediction time

C. It is always numerical

D. It prevents one-hot encoding

Correct Answer: B. It is unavailable at prediction time

Question 9

What does data lineage primarily document?

A. Only model accuracy

B. Data origin and transformations

C. Only user passwords

D. The number of neural-network layers

Correct Answer: B. Data origin and transformations

Question 10

Which method can reduce the effect of extreme values while retaining observations?

A. Blind row deletion

B. Robust scaling

C. Random label replacement

D. Test-set oversampling

Correct Answer: B. Robust scaling

Machine Learning
Chapter 3 of 15

Machine Learning

Chapter 4
Regression and Predictive Estimation

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Regression and Predictive Estimation

Introduction and Context

Regression is a family of statistical and machine-learning methods used to estimate a continuous outcome from one or more explanatory variables. Examples include forecasting electricity demand, estimating house prices, predicting customer lifetime value, modelling manufacturing yield, and quantifying the relationship between advertising expenditure and sales.

Regression has two related purposes. Prediction focuses on accurately estimating unseen outcomes. Explanation or inference focuses on understanding how predictors are associated with the response and whether those associations are statistically credible. A model can be useful for prediction even when individual coefficients are difficult to interpret; conversely, a statistically interpretable model may have limited predictive accuracy outside the observed data.

In supervised learning, a dataset consists of observations (xi, yi), where xi is a vector of predictors and yi is a continuous response. The task is to learn a function f(x) such that predictions ŷi = f(xi) are accurate for future observations drawn from a comparable data-generating process.

Learning Objectives

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

  • formulate simple and multiple linear regression models;
  • derive and interpret ordinary least-squares estimates;
  • extend linear regression with transformations and polynomial terms;
  • explain ridge, lasso, and elastic-net regularization;
  • distinguish training, validation, and test performance;
  • diagnose nonlinearity, heteroscedasticity, dependence, influential observations, and multicollinearity;
  • select and interpret appropriate statistical and business metrics;
  • communicate regression results while recognizing uncertainty, limitations, and governance risks.

1. Regression as a Predictive Model

A regression model separates systematic structure from unexplained variation. A general representation is

yi = f(xi) + εi

where yi is the observed response, f(xi) is the systematic component, and εi is the error or irreducible variation. The conditional mean is E(Y|X=x) = f(x) when the model is correctly specified for the mean.

A fitted model produces ŷi. The residual is

ei = yi − ŷi

Residuals are observed training-data discrepancies; future prediction errors are not known at training time. This distinction is essential when evaluating generalization.

Terminology

  • Response, target, or dependent variable: the continuous quantity being estimated.
  • Predictor, feature, or explanatory variable: an input used to estimate the response.
  • Coefficient: a parameter describing the model’s response to a predictor, conditional on other predictors.
  • Intercept: the expected response when all predictors equal zero, subject to the meaningfulness of that reference point.
  • Fitted value: the prediction for an observation used during fitting.
  • Residual: observed response minus fitted response.
  • Generalization: performance on new data from the target population.
  • Bias–variance trade-off: the balance between systematic approximation error and sensitivity to sampling variation.

2. Simple and Multiple Linear Regression

2.1 Model specification

Simple linear regression uses one predictor:

yi = β0 + β1xi + εi

Here, β0 is the intercept, β1 is the slope, and εi is the random error. The conditional mean is linear in x. The slope is measured in response units per predictor unit.

Multiple linear regression is

yi = β0 + β1xi1 + β2xi2 + ⋯ + βpxip + εi

The coefficient βj represents the expected change in Y associated with a one-unit increase in Xj, holding the other included predictors constant. This conditional interpretation does not automatically establish causation.

In matrix notation, with n observations and p predictors,

y = Xβ + ε

where y is an n × 1 response vector, X is an n × (p+1) design matrix including a column of ones, β is the coefficient vector, and ε is the error vector.

2.2 Ordinary least squares

Ordinary least squares (OLS) chooses coefficients that minimize the residual sum of squares:

RSS(β) = ∑i=1n (yi − ŷi)2 = ∑i=1n (yi − xiTβ)2

The notation assumes that xi includes the intercept component. In matrix form:

RSS(β) = (y − Xβ)T(y − Xβ)

If XTX is invertible, the OLS estimator is

β̂ = (XTX)−1XTy

This closed-form expression applies to ordinary least squares with full column rank. In software, QR decomposition or singular-value decomposition is generally preferred to explicitly computing a matrix inverse because it is more numerically stable.

For simple regression, the slope and intercept can be written as

β̂1 = ∑(xi − x̄)(yi − ȳ) / ∑(xi − x̄)2
β̂0 = ȳ − β̂1x̄

where x̄ and ȳ are sample means. The denominator must be nonzero; otherwise the predictor has no variation and its slope cannot be estimated.

Worked example: advertising and sales

Suppose a fitted model estimates weekly sales, in thousands of units, from digital advertising expenditure, in thousands of currency units:

Ŷ = 18 + 2.4X

For advertising expenditure of 5 units, the estimate is

Ŷ = 18 + 2.4(5) = 30

Thus, predicted sales are 30 thousand units. The slope means that an additional thousand currency units of advertising is associated with an estimated increase of 2.4 thousand units of sales, within the observed range and conditional on the model. It does not prove that increasing advertising will cause exactly that increase.

3. Assumptions, Inference, and Interpretation

OLS coefficient estimates can remain computationally defined when assumptions fail, but the assumptions determine interpretation, efficiency, and the validity of standard errors and tests.

  1. Linearity in parameters: the conditional mean is correctly represented by the chosen linear combination of terms.
  2. Independent observations: errors are not systematically dependent, unless dependence is explicitly modelled.
  3. Exogeneity or zero conditional mean: E(ε|X)=0. Predictors must not contain systematic information about the unexplained error. Omitted variables, reverse causality, and measurement error can violate this condition.
  4. Constant error variance: Var(ε|X)=σ2. This is homoscedasticity. It is not required for unbiased coefficient estimates under stronger conditions, but it affects conventional standard errors and efficiency.
  5. Normality of errors: not essential for OLS point estimates. It is mainly useful for exact small-sample t and F procedures when errors are plausibly normal.
  6. No perfect multicollinearity: no predictor is an exact linear combination of other predictors.

Under standard Gauss–Markov conditions, OLS is the best linear unbiased estimator: among linear unbiased estimators, it has the smallest covariance matrix. This result does not state that OLS is best among all possible nonlinear or biased estimators, nor does it guarantee good predictions under misspecification.

For a coefficient estimate, a conventional confidence interval is

β̂j ± t1−α/2, n−p−1 SE(β̂j)

where SE(β̂j) is the estimated standard error, t1−α/2, n−p−1 is a t critical value, α is the significance level, n is sample size, and p is the number of predictors excluding the intercept. This interval concerns a population parameter under the model and sampling assumptions; it is not automatically a prediction interval for an individual future outcome.

A prediction interval is wider because it includes both uncertainty in the estimated mean and new-observation noise. In the classical simple-regression setting, for a new predictor value x0, the interval has the form

ŷ0 ± t1−α/2,n−2 s√(1 + 1/n + (x0−x̄)2/Sxx)

where Sxx=∑(xi−x̄)2, s is the residual standard deviation, and the term 1 represents new individual error. The formula assumes a correctly specified linear model, independent errors, constant variance, and appropriate use within the data range.

4. Model Fit and Evaluation

4.1 Sum-of-squares decomposition

When an intercept is included, total variation can be decomposed as

SST = SSR + SSE

where SST=∑(yi−ȳ)2 is total sum of squares, SSR=∑(ŷi−ȳ)2 is regression sum of squares, and SSE=∑(yi−ŷi)2 is error sum of squares.

The coefficient of determination is

R2 = 1 − SSE/SST

It is the proportion of in-sample variation around the sample mean accounted for by fitted values under the stated decomposition. A high R2 does not prove causality, guarantee low future error, or demonstrate that all important predictors have been included. Out-of-sample R2 can be negative when predictions are worse than using the training or validation mean, depending on the evaluation definition.

Adjusted R2 penalizes additional predictors:

Adjusted R2 = 1 − (1−R2)(n−1)/(n−p−1)

It is useful for descriptive comparison of nested linear models under the same response and dataset, but it is not a substitute for out-of-sample validation.

4.2 Prediction metrics

Let ei=yi−ŷi be evaluation errors for m held-out observations.

MetricFormulaInterpretation
MAE[[MATH:MAE=(1/m)∑e_i
MSEMSE=(1/m)∑ei2Penalizes large errors strongly; response units squared
RMSERMSE=√MSEIn response units; sensitive to large errors
MAPE[[MATH:MAPE=(100/m)∑e_i/y_i
Median absolute errorMedian of [[MATH:e_i

Metric choice should follow the decision problem. If a large under-forecast creates disproportionate operational harm, squared-error metrics may be appropriate, but they should be complemented by directional and business-cost analysis. Percentage metrics are unsuitable when actual values can be zero or close to zero. For asymmetric costs, a weighted loss or a quantile model may be more appropriate than ordinary least squares.

4.3 Validation design

A single training score measures fit, not generalization. A typical workflow divides data into training, validation, and test sets. The training set estimates parameters; the validation set supports model and hyperparameter selection; the untouched test set provides a final estimate of expected performance.

For time-ordered data, random splitting can leak future information. Use rolling-origin or expanding-window evaluation. For grouped observations, such as multiple transactions from the same customer, split by customer when the intended deployment involves unseen customers. All preprocessing, feature selection, and imputation learned from data must be fitted within each training fold, not on the full dataset before cross-validation.

5. Nonlinearity and Polynomial Regression

A linear model can represent nonlinear relationships by transforming predictors. A polynomial model is

yi = β0 + β1xi + β2xi2 + ⋯ + βdxid + εi

Although the curve is nonlinear in x, it remains linear in its coefficients and can be estimated using linear regression after constructing the transformed features. The marginal effect is

dE(Y|X=x)/dx = β1 + 2β2x + ⋯ + dβdxd−1

Therefore, β1 alone is not the overall effect except at a particular reference point. Centering x before creating powers can improve interpretability and numerical stability, although it does not change the fitted curve when the model space is unchanged.

High-degree polynomials can oscillate strongly near boundaries and extrapolate implausibly. Splines provide a more locally controlled alternative by joining polynomial pieces at knots, subject to continuity constraints. Generalized additive models extend this idea by estimating smooth functions for predictors while retaining additive structure.

6. Regularization and Model Complexity

When predictors are numerous, correlated, or high-dimensional, least squares can have high variance. Regularization adds a penalty to the fitting objective.

Ridge regression

β̂ridge = arg min_β {∑(yi−xiTβ)2 + λ∑j=1p βj2}

The penalty parameter λ≥0 controls shrinkage. The intercept is usually not penalized, and predictors should generally be standardized so that penalty strength is comparable across units. Ridge usually retains all predictors and is particularly useful under multicollinearity.

Lasso regression

β̂lasso = arg min_β {∑(yi−xiTβ)2 + λ∑j=1p |βj|}

The absolute-value penalty can set some coefficients exactly to zero, producing a sparse model. With strongly correlated predictors, lasso may select one variable arbitrarily or distribute selection unstably; selected variables should not automatically be treated as proven causal drivers.

Elastic net

β̂EN = arg min_β {∑(yi−xiTβ)2 + λ[α∑|βj| + (1−α)∑βj2]}

where 0≤α≤1. α=1 gives lasso and α=0 gives ridge, subject to scaling conventions. λ and α should be selected using training-only cross-validation. Regularization introduces bias intentionally to reduce variance; its success must be judged by validation performance and deployment objectives.

7. Diagnosing Regression Models

Diagnostics should combine numerical summaries, visual inspection, domain knowledge, and validation results.

Residual patterns

A residual-versus-fitted plot should show a structureless cloud around zero. Curvature suggests missing nonlinear terms. A funnel shape suggests nonconstant variance. Clusters or sequences suggest dependence or omitted group effects. A Q–Q plot assesses whether residual quantiles approximately follow a reference normal distribution; deviations matter more for small-sample inference than for large-sample prediction.

Leverage and influence

A high-leverage observation has unusual predictor values. It need not be problematic, but it can strongly affect the fitted line. Influence combines unusual predictors with a large residual. Cook’s distance is a common diagnostic, but there is no universal cutoff that mechanically identifies bad data. Investigate influential points for recording errors, legitimate rare cases, regime changes, or important subpopulations. Deleting observations solely to improve a metric is poor practice.

Heteroscedasticity and dependence

When variance changes with predictors, consider transformations, weighted least squares, robust standard errors, or models designed for the conditional variance. When observations are correlated across time, location, or entity, use appropriate time-series, hierarchical, clustered, or longitudinal methods. Treating repeated measurements as independent can produce overconfident conclusions.

Multicollinearity

Correlated predictors make individual coefficients unstable and inflate standard errors, although predictions may remain accurate. Variance inflation factors can assist diagnosis, but thresholds are context-dependent. Remedies include combining variables, removing redundant features, collecting more informative data, using ridge regression, or reframing the inferential question around groups of predictors.

8. Generalized Regression Models

Ordinary linear regression is designed for continuous responses with an approximately constant conditional variance. Generalized linear models (GLMs) extend regression to non-normal response distributions through a systematic component and a link function:

g(E[Y|X]) = β0 + β1x1 + ⋯ + βpxp

For a binary response, logistic regression uses the logit link:

log(p/(1−p)) = β0 + βTx

where p=P(Y=1|X). Exponentiating βj gives an odds ratio for a one-unit increase in xj, holding other predictors constant. Logistic regression estimates probabilities, not continuous quantities, but illustrates the broader principle of generalized regression.

For counts, Poisson regression uses a log link, log(E[Y|X])=β0+βTX, while negative binomial models can address overdispersion. For positive, right-skewed continuous outcomes, a Gamma GLM with a suitable link may be more appropriate than transforming the response and then back-transforming without correction. Model selection should reflect the response scale, sampling mechanism, and variance structure.

9. Detailed Applied Case: Predicting Hospital Length of Stay

The following is an illustrative case, not a report of a real hospital.

Background and problem

A hospital wants to predict inpatient length of stay in days at admission to support bed planning and discharge coordination. Available variables include age, admission type, prior admissions, comorbidity score, emergency-department waiting time, and initial laboratory measurements. The hospital must avoid using information unavailable at admission, such as discharge diagnosis or later treatment decisions.

Analysis

Length of stay is positive, right-skewed, and may contain a small number of very long admissions. A baseline model using the historical median provides a reference. Analysts then compare a log-transformed linear model, a Gamma GLM, and a regularized model with nonlinear terms. The split is temporal: earlier admissions are used for training and later admissions for validation and testing. This reflects deployment more realistically than a random split.

Missing laboratory values are imputed using rules fitted within the training data. Predictors are standardized for regularized models. Model selection uses validation MAE and RMSE. MAE reflects typical scheduling error, while RMSE reveals whether a model fails badly on unusually long stays. Calibration is assessed by comparing average predicted and observed stay within risk bands.

Approach and solution

Suppose the selected model has a test MAE of 1.8 days and RMSE of 4.6 days, while the median baseline has MAE of 2.4 days and RMSE of 5.1 days. The model improves average accuracy, but the gap between MAE and RMSE indicates substantial large-error cases. These cases should be investigated rather than hidden by reporting only one metric. Prediction intervals or quantile estimates may support capacity planning better than a single point estimate.

The model card documents the training period, variables, exclusions, evaluation design, subgroup performance, intended use, and limitations. Performance is monitored after deployment because admission practices, coding, patient mix, and clinical pathways may change.

Outcome and learning points

The model is used as decision support, not as an automatic discharge rule. A clinician can override a prediction and record a reason. Subgroup analysis examines whether errors are systematically larger for particular age groups, hospitals, or language communities. The case demonstrates that a technically strong regression model requires leakage control, deployment-aligned validation, uncertainty communication, fairness monitoring, and operational integration.

10. Professional, Ethical, and Governance Considerations

Regression models can reproduce historical inequities. A predictor may be statistically useful yet ethically inappropriate if it encodes unequal access to care, discriminatory practices, or proxies for protected attributes. Fairness cannot be assessed by a single universal metric; organizations must define the relevant harm, population, and decision context.

Data governance should address consent, purpose limitation, access control, retention, provenance, and auditability. Sensitive attributes may be needed for fairness evaluation even when excluded from prediction, subject to lawful and ethical safeguards. Models used in high-impact settings require human oversight, documented uncertainty, monitoring for drift, and a process for contesting decisions.

Interpretability should be matched to use. Coefficients, partial dependence, residual analysis, and local explanations can support understanding, but an explanation is not proof of causation. If an organization needs to estimate the effect of an intervention, causal design, experiments, or quasi-experimental methods may be required rather than ordinary predictive regression.

11. Best Practices and Contemporary Developments

Effective regression practice includes:

  • define the decision, prediction horizon, and cost of errors before selecting a metric;
  • establish a simple baseline;
  • inspect data quality, units, missingness, outliers, and leakage;
  • use domain-informed transformations and interaction terms;
  • standardize predictors when using penalized models;
  • use cross-validation or time-aware validation to select complexity;
  • report uncertainty, calibration, subgroup performance, and confidence in the evaluation design;
  • preserve a final test set until model selection is complete;
  • monitor drift, residual patterns, data availability, and business outcomes after deployment.

Contemporary systems increasingly combine interpretable regression with tree ensembles, neural networks, and probabilistic forecasting. Regression remains valuable as a benchmark, a calibrated component, a transparent model for governance, and a source of structured features. Modern predictive estimation also emphasizes conformal prediction, which can construct distribution-free predictive intervals under exchangeability assumptions, and automated monitoring of data and concept drift. These methods do not remove the need for careful sampling, valid targets, and responsible use.

Chapter Summary

Regression estimates continuous outcomes by relating a response to predictors through a systematic component and an error term. Linear regression uses coefficients that are linear in the parameters, and OLS minimizes squared residuals. Its useful interpretations depend on assumptions concerning linearity, independence, exogeneity, variance, and multicollinearity. Polynomial terms, splines, and generalized models allow more flexible relationships or response distributions. Ridge, lasso, and elastic net control complexity by shrinking coefficients, with hyperparameters selected through validation.

Model quality must be assessed on data representative of deployment, using metrics aligned with business consequences. MAE is interpretable and comparatively robust, RMSE penalizes large errors, and percentage metrics require care near zero. Residual, leverage, influence, heteroscedasticity, dependence, and calibration diagnostics reveal weaknesses that a single score cannot. Finally, regression deployment requires attention to leakage, uncertainty, fairness, privacy, monitoring, and the distinction between prediction and causal explanation.

Key Terms

Regression: Estimation of a response from one or more predictors.

Response variable: The outcome being predicted or explained.

Predictor: An input variable used by a model.

Intercept: Expected response when predictors equal their reference values.

Coefficient: Parameter quantifying a predictor’s conditional association with the response.

Residual: Observed response minus fitted response.

Ordinary least squares: Estimation by minimizing the sum of squared residuals.

Generalization: Performance on new data from the target population.

Homoscedasticity: Constant conditional error variance.

Heteroscedasticity: Nonconstant conditional error variance.

Multicollinearity: Strong correlation or linear dependence among predictors.

Leverage: Unusualness of an observation’s predictor values.

Influence: The extent to which an observation affects fitted results.

Ridge regression: L2-penalized regression that shrinks coefficients.

Lasso regression: L1-penalized regression that can produce zero coefficients.

Elastic net: A mixture of L1 and L2 regularization.

MAE: Mean absolute error, measured in response units.

RMSE: Square root of mean squared error, measured in response units.

Prediction interval: An interval intended to contain a future individual outcome.

Data leakage: Use of information unavailable at prediction time or improperly shared across evaluation partitions.

Review Questions

Short Answer Questions

  1. What is the difference between a residual and a future prediction error?
  2. Interpret a multiple-regression coefficient while holding other predictors constant.
  3. State two assumptions required for conventional OLS inference.
  4. Why should predictors usually be standardized before ridge or lasso regression?
  5. Why can MAPE be problematic when actual values are zero or close to zero?

Long Answer Questions

  1. Derive the OLS objective and explain the matrix estimator β̂=(XTX)−1XTy, including its applicability conditions.
  2. Compare ridge, lasso, and elastic net in terms of penalties, sparsity, multicollinearity, and model selection.
  3. Explain how residual, leverage, influence, and heteroscedasticity diagnostics contribute to model assessment.
  4. Compare training, validation, test, random, grouped, and time-aware evaluation designs.
  5. Discuss why predictive association should not automatically be interpreted as causation, using a practical example.

Analytical / Application Questions

  1. A demand model has low training error but much higher validation error. Diagnose likely causes and propose a revised modelling workflow.
  2. A hospital model has good overall RMSE but substantially higher errors for one demographic group. Design an investigation and governance response.
  3. A sales response is positive, highly skewed, and contains zeros. Recommend candidate models and metrics, explaining the assumptions behind your choices.

Multiple Choice Questions with Answers

Question 1

In ordinary least squares, the fitted coefficients are chosen to minimize which quantity?

A. The sum of absolute residuals

B. The sum of squared residuals

C. The maximum residual only

D. The number of predictors

Correct Answer: B. The sum of squared residuals

Question 2

In a multiple linear regression model, the coefficient of a predictor is interpreted as the expected change in the response for a one-unit increase in that predictor, provided that:

A. The response is binary

B. All other included predictors are held constant

C. The residuals are exactly zero

D. The predictor has zero variance

Correct Answer: B. All other included predictors are held constant

Question 3

Which metric is expressed in the same units as the response and strongly penalizes large errors?

A. RMSE

B. MAPE

C. R²

D. Adjusted R²

Correct Answer: A. RMSE

Question 4

What is a principal characteristic of lasso regression?

A. It always leaves coefficients unchanged

B. It can set some coefficients exactly to zero

C. It requires a binary response

D. It removes the intercept by definition

Correct Answer: B. It can set some coefficients exactly to zero

Question 5

A funnel-shaped pattern in a residual-versus-fitted plot most directly suggests:

A. Perfect multicollinearity

B. Constant error variance

C. Heteroscedasticity

D. An exact causal relationship

Correct Answer: C. Heteroscedasticity

Question 6

If actual values are zero, which commonly used metric is undefined for those observations?

A. MAE

B. MSE

C. RMSE

D. MAPE

Correct Answer: D. MAPE

Question 7

For a time-ordered forecasting problem, which evaluation strategy best avoids using future information to predict the past?

A. Randomly shuffle all observations before splitting

B. Use a temporal or rolling-origin split

C. Fit and test on the same observations

D. Select the model using the test set repeatedly

Correct Answer: B. Use a temporal or rolling-origin split

Question 8

If a fitted model is Ŷ = 10 + 3X, what is the prediction when X = 4?

A. 12

B. 14

C. 22

D. 40

Correct Answer: C. 22

Question 9

Which statement about ridge regression is correct?

A. It uses an L2 penalty and generally shrinks correlated coefficients

B. It uses an L1 penalty and always produces a sparse model

C. It cannot be used with continuous responses

D. It selects hyperparameters using the test set by definition

Correct Answer: A. It uses an L2 penalty and generally shrinks correlated coefficients

Question 10

A prediction interval for an individual future observation is generally wider than a confidence interval for the mean response because it includes:

A. Only coefficient estimation uncertainty

B. New-observation error as well as mean-estimation uncertainty

C. No uncertainty at all

D. Only uncertainty from the intercept

Correct Answer: B. New-observation error as well as mean-estimation uncertainty

Machine Learning
Chapter 4 of 15

Machine Learning

Chapter 5
Classification and Kernel Methods

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Classification and Kernel Methods

Introduction

Classification is the supervised-learning task of assigning an observation to one of a finite set of classes. A classifier estimates a relationship between input variables and a categorical response, then uses that relationship to support a decision. Applications include fraud screening, medical diagnosis, credit-risk assessment, document categorisation, fault detection and image recognition.

Classification has two distinct aspects. First, a model estimates class membership, such as the probability that a transaction is fraudulent. Second, a decision rule converts that estimate into an action, such as blocking or approving the transaction. Separating estimation from decision-making is essential when classes are imbalanced, errors have unequal costs, or predicted probabilities must be calibrated.

This chapter develops logistic regression, naïve Bayes, nearest-neighbour methods, support vector machines (SVMs) and kernel methods. It also examines feature scaling, regularisation, class imbalance, thresholds, calibration and cost-sensitive classification.

Learning Objectives

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

  1. Explain the distinction between discriminative and generative classification.
  2. Construct and interpret logistic-regression models.
  3. Apply Bayes’ rule and the naïve Bayes assumption.
  4. Use nearest-neighbour methods and analyse their computational behaviour.
  5. Explain maximum-margin classification and the role of support vectors.
  6. Select and interpret linear and nonlinear kernels.
  7. Evaluate classifiers using metrics appropriate to imbalance and decision costs.
  8. Calibrate probabilities and choose operational thresholds responsibly.

1. Classification as Estimation and Decision

Let an observation be represented by a feature vector [[M:x ∈ ℝ^p]], and let its class label be [[M:y ∈ {1,…,K}]]. A classifier may produce either a predicted class [[M:ŷ]], a score, or estimated posterior probabilities [[M:P(y=k|x)]].

For binary classification, write the positive class as 1 and the negative class as 0. A general threshold rule is

ŷ = 1 if s(x) ≥ τ; otherwise ŷ = 0

Here [[M:s(x)]] is a score or estimated probability and [[M:τ]] is the decision threshold. The threshold need not be 0.5. It should reflect prevalence, error costs, capacity constraints and the consequences of action.

A confusion matrix records decisions against actual outcomes:

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

Accuracy is [[M:(TP+TN)/(TP+TN+FP+FN)]]. It can be misleading when one class is rare. Precision, recall and specificity are

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

Precision is also called positive predictive value; recall is also called sensitivity. The F1 score is the harmonic mean of precision and recall:

F₁ = 2 × (Precision × Recall)/(Precision + Recall)

These metrics assume binary labels and nonnegative denominators. If a denominator is zero, the corresponding metric is undefined and the reporting convention must be stated.

The receiver operating characteristic (ROC) curve plots true-positive rate against false-positive rate, where [[M:FPR=FP/(FP+TN)]], over thresholds. The area under the ROC curve (ROC-AUC) measures ranking ability, but does not itself select an operating threshold. Precision–recall curves are often more informative when the positive class is rare.

2. Logistic Regression

2.1 Model and interpretation

Logistic regression is a discriminative model for conditional class probability. For binary classification, let [[M:x=(x₁,…,x_p)]] and let [[M:β₀]] be an intercept. The linear predictor is

z = β₀ + β₁x₁ + ⋯ + βpxp = β₀ + βᵀx

The logistic function maps any real number to the interval (0,1):

P(y=1|x) = σ(z) = 1/(1+e^(−z))

Equivalently, logistic regression assumes that the log-odds are linear in the features:

log[P(y=1|x)/(1−P(y=1|x))] = β₀ + βᵀx

The coefficients are interpreted on the log-odds scale. Holding all other features fixed, increasing [[M:x_j]] by one unit changes the log-odds by [[M:β_j]] and multiplies the odds by [[M:e^{β_j}]]. This interpretation depends on the feature’s unit and on the linearity assumption in the log-odds.

With the default threshold 0.5, a prediction is positive when [[M:z≥0]], because [[M:σ(z)≥0.5]] exactly when [[M:z≥0]]. A different threshold changes decisions without necessarily changing fitted coefficients.

2.2 Estimation and regularisation

For independent observations [[M:(x_i,y_i)]], maximum likelihood estimates parameters by maximising the Bernoulli likelihood:

L(β) = ∏ᵢ pᵢyᵢ(1−pᵢ)1−yᵢ, where pᵢ = σ(β₀+βᵀxᵢ)

It is computationally more convenient to minimise the negative log-likelihood, also called log loss or cross-entropy:

J(β) = −∑ᵢ [yᵢ log pᵢ + (1−yᵢ)log(1−pᵢ)]

A regularised objective can be written as

Jreg(β) = J(β) + λΩ(β)

where [[M:λ≥0]] controls penalty strength and [[M:Ω(β)]] is a penalty. L2 regularisation uses [[M:Ω(β)=½∑_{j=1}^p β_j²]] and shrinks coefficients smoothly. L1 regularisation uses [[M:Ω(β)=∑_{j=1}^p |β_j|]] and can produce exact zero coefficients, supporting sparse feature selection. The intercept is commonly excluded from the penalty, although implementations differ.

Regularisation reduces variance but introduces bias. [[M:λ]] should be selected using validation or cross-validation within the training data; test data must remain untouched until final evaluation. Numerical optimisation is usually performed with gradient-based methods or specialised convex solvers. Logistic loss with L1 or L2 penalties is convex, so a global optimum exists under standard conditions, although unregularised estimates can fail to be finite under complete separation.

2.3 Worked example

Suppose a fitted model has [[M:z=−1.2+0.8x₁−0.4x₂]]. For an observation with [[M:x₁=2]] and [[M:x₂=1]],

[[M:z=−1.2+0.8(2)−0.4(1)=0.0]]. Therefore,

[[M:P(y=1|x)=1/(1+e^0)=0.5]].

At threshold 0.5 the observation is classified as positive. Increasing [[M:x₁]] by one unit, holding [[M:x₂]] fixed, multiplies the odds by [[M:e^{0.8}≈2.23]], not the probability by 2.23.

3. Naïve Bayes

3.1 Bayes’ rule and conditional independence

Bayes’ rule expresses posterior class probability as

P(y=k|x) = P(x|y=k)P(y=k)/P(x)

The prior [[M:P(y=k)]] represents class prevalence. The likelihood [[M:P(x|y=k)]] describes feature values under class [[M:k]]. The evidence [[M:P(x)]] normalises the probabilities.

Naïve Bayes assumes conditional independence of features given the class:

P(x|y=k) = ∏j=1p P(xj|y=k)

The assumption is often false, but the resulting classifier can still perform well, particularly with high-dimensional sparse text data. Since [[M:P(x)]] is common to all classes, prediction can use

ŷ = argmaxk [P(y=k)∏j P(xj|y=k)]

In practice, log probabilities are used to avoid underflow:

ŷ = argmaxk [log P(y=k)+∑j log P(xj|y=k)]

3.2 Variants and smoothing

Gaussian naïve Bayes models each continuous feature conditionally as a normal distribution with class-specific mean and variance. Multinomial naïve Bayes is common for nonnegative count features such as word frequencies. Bernoulli naïve Bayes models binary feature presence.

Zero counts can make a likelihood zero. Laplace smoothing replaces a count [[M:N_{jk}]] by [[M:N_{jk}+α]], where [[M:α>0]] is a smoothing parameter. For a categorical feature with [[M:V]] possible values and class total [[M:N_k]],

P(xj=v|y=k) = (Njv,k+α)/(Nk+αV)

Smoothing reduces sensitivity to unseen categories but changes estimated probabilities. Naïve Bayes probabilities are therefore often poorly calibrated even when classification accuracy is strong.

4. Nearest-Neighbour Classification

The k-nearest-neighbour (k-NN) method stores the training data. For a query point, it identifies the [[M:k]] closest training observations and predicts the majority class. A distance-weighted version gives nearer observations greater influence.

For standardised Euclidean distance,

d(x,x′) = √[∑j=1p (xj−x′j)²]

Standardisation is important when features have different units or scales. A common transformation is [[M:x′_j=(x_j−μ_j)/σ_j]], where [[M:μ_j]] and [[M:σ_j]] are calculated using training data only. Other distances, such as Manhattan distance, may be more appropriate for particular data geometries.

Small [[M:k]] gives low bias and high variance; large [[M:k]] gives smoother, more biased predictions. The value of [[M:k]] should be selected by validation. In high dimensions, distances become less discriminative, a phenomenon associated with the curse of dimensionality. Feature selection, dimensionality reduction and domain-specific representations can help.

Prediction can be expensive because a query may require comparison with many stored observations. Approximate-neighbour structures can reduce latency, but their accuracy and maintenance requirements must be evaluated. k-NN is attractive for its simplicity and local interpretability, but it is sensitive to irrelevant features, missing values, scale and data density.

5. Support Vector Machines

5.1 Linear maximum-margin classification

For linearly separable data, a hyperplane is [[M:wᵀx+b=0]]. The signed score is [[M:f(x)=wᵀx+b]]. The geometric margin is maximised by minimising [[M:½||w||²]] subject to

yᵢ(wᵀxᵢ+b) ≥ 1, for all i

where [[M:y_i∈{−1,+1}]]. The constraint fixes a scale for [[M:w]] and [[M:b]]. The closest points satisfy equality and are called support vectors.

Real data are often not separable. The soft-margin SVM introduces slack variables [[M:ξ_i≥0]]:

minw,b,ξ ½||w||² + C∑ᵢ ξᵢ
subject to yᵢ(wᵀxᵢ+b) ≥ 1−ξᵢ, ξᵢ≥0

Here [[M:C>0]] controls the trade-off between margin size and training violations. An equivalent hinge-loss formulation is

minw,b ½||w||² + C∑ᵢ max(0,1−yᵢ(wᵀxᵢ+b))

The SVM score is not automatically a probability. It is a margin-based ranking score and requires calibration if probabilities are needed.

5.2 Class imbalance and weighted SVMs

If positive errors are more important or positives are rare, separate penalties can be used. A weighted formulation replaces [[M:C]] with [[M:C_+]] for positive examples and [[M:C_-]] for negative examples. Larger class weight penalises violations more strongly. Class weights should reflect both statistical imbalance and operational costs, not merely be applied mechanically.

6. Kernel Methods

6.1 The kernel idea

A nonlinear classifier can become linear after mapping inputs into a feature space [[M:φ(x)]]. The kernel trick avoids explicitly computing that potentially high-dimensional mapping by using

K(x,x′) = φ(x)ᵀφ(x′)

A valid positive-semidefinite kernel generates a Gram matrix whose quadratic form is nonnegative. This condition supports optimisation and corresponds to an inner product in a feature space.

Common kernels include:

KernelExpressionTypical role
Linear[[M:K(x,x′)=xᵀx′]]High-dimensional sparse data
Polynomial[[M:K(x,x′)=(γxᵀx′+r)^d]]Controlled feature interactions
Radial basis function (RBF)[[M:K(x,x′)=exp(−γ
Sigmoid[[M:K(x,x′)=tanh(γxᵀx′+r)]]Specialised use; validity depends on parameters

For the RBF kernel, [[M:γ>0]] controls locality. A large [[M:γ]] produces narrow, highly local influence and can overfit; a small value produces smoother boundaries. Parameters such as [[M:C]], [[M:γ]] and polynomial degree should be selected jointly through nested or carefully controlled cross-validation.

Kernel SVMs can be powerful on moderate-sized datasets but may have substantial memory and computational costs because the Gram matrix grows with the number of training observations. Linear SVMs, approximation methods, random Fourier features and specialised solvers are alternatives for large-scale problems.

7. Imbalance, Thresholds, Calibration and Costs

7.1 Class imbalance

Imbalance occurs when class frequencies differ substantially. It does not automatically make a problem invalid, but it changes the meaning of metrics and may cause a learner to favour the majority class. Appropriate responses include stratified splitting, class-weighted loss, careful resampling, anomaly-detection formulations and collecting more minority examples.

Oversampling must be performed inside each training fold. Otherwise, duplicated or synthetic information can leak into validation data. Synthetic methods such as SMOTE should be applied only to training partitions and should be assessed for plausibility and subgroup effects.

7.2 Threshold selection and expected cost

Let [[M:C_{FP}]] be the cost of a false positive and [[M:C_{FN}]] the cost of a false negative. If the model outputs a calibrated probability [[M:p=P(y=1|x)]], predicting positive has expected cost [[M:C_{FP}(1−p)]], while predicting negative has expected cost [[M:C_{FN}p]]. Predict positive when the former is no greater than the latter:

CFP(1−p) ≤ CFN p

Thus the cost-minimising threshold is

τ = CFP/(CFP+CFN)

This simplified threshold assumes calibrated probabilities, no action-dependent benefits, and equal consideration of other operational constraints. If class priors or sampling schemes differ between development and deployment, probabilities and costs must be aligned to the deployment population.

7.3 Calibration

A classifier is calibrated if, among cases assigned probability approximately [[M:q]], the observed event frequency is approximately [[M:q]]. Reliability diagrams, calibration curves and the Brier score are useful tools. For binary outcomes, the Brier score is

BS = (1/n)∑ᵢ(pᵢ−yᵢ)²

Lower values are better, although the score reflects both calibration and refinement. Platt scaling fits a logistic transformation to validation scores; isotonic regression fits a flexible monotonic mapping. Calibration must use data separate from model fitting, preferably through cross-validation, and must be rechecked after changes in prevalence or data generation.

8. Model Development and Evaluation Workflow

A defensible workflow is as follows:

  1. Define the target, prediction horizon, intervention and costs.
  2. Inspect label quality, missingness, prevalence and possible leakage.
  3. Split data by time, entity or group when random splitting would allow information to cross the deployment boundary.
  4. Build a simple baseline, such as a prevalence predictor or regularised logistic regression.
  5. Fit candidate models using pipelines that contain preprocessing, feature selection and resampling.
  6. Tune hyperparameters using validation or nested cross-validation.
  7. Evaluate discrimination, calibration, confusion-matrix metrics and decision utility.
  8. Select a threshold using a validation set and stated operational constraints.
  9. Test once on a protected holdout set, then monitor post-deployment drift.

Performance should be reported with uncertainty where feasible. Confidence intervals may be obtained by bootstrap resampling, but resampling must respect dependence structures such as patients, customers or time periods. A high ROC-AUC does not guarantee useful precision, calibration or fairness at the chosen operating point.

9. Illustrative Case: Fraud Screening in Digital Payments

Background

A payment platform wants to identify potentially fraudulent card transactions before settlement. Fraud represents 0.3% of transactions. Blocking a legitimate transaction costs approximately 2 monetary units in customer service and lost trust. Allowing a fraudulent transaction costs an estimated 120 monetary units.

Problem

A model that predicts “legitimate” for every transaction achieves 99.7% accuracy but detects no fraud. The platform needs a ranking model, an operational threshold and a monitoring plan.

Analysis

Candidate models include regularised logistic regression for transparent baseline analysis, naïve Bayes for text-like merchant descriptors, a linear or RBF SVM for nonlinear boundaries and k-NN for local comparisons. Features include amount, merchant history, device signals and time patterns. Preprocessing is placed inside a time-aware validation pipeline. Random splitting is avoided because the same device or account may appear in both training and validation periods.

Because false negatives are much more expensive than false positives, accuracy is unsuitable as the principal metric. Precision–recall curves, recall at a fixed review capacity, expected cost and calibration are reported. Class weights are considered, but they are not treated as a substitute for threshold selection.

Approach and solution

Suppose calibrated probabilities are used and costs are [[M:C_FP=2]] and [[M:C_FN=120]]. The cost-based threshold is

[[M:τ=2/(2+120)=2/122≈0.0164]].

This low threshold is not an assertion that 1.64% probability means fraud is common; it reflects the asymmetric costs. In practice, a manual-review capacity constraint may require a different threshold. The team therefore compares the cost threshold with thresholds selected to review, for example, the highest-risk 0.5% of transactions.

A logistic model provides a transparent baseline. An RBF SVM improves ranking on validation data but initially produces poorly calibrated scores. Isotonic calibration is fitted on temporally later validation data. The final threshold is chosen on a separate decision-validation period, and the untouched test period is used for final estimation.

Outcome

The selected system sends a small high-risk group to review and automatically approves low-risk transactions. Monitoring tracks precision, recall, review volume, calibration, subgroup error rates, changes in fraud prevalence and drift in device and merchant features. Review outcomes are fed back only after label maturity, since fraud labels may arrive weeks after a transaction.

Learning points

The case illustrates that classification is a complete decision system rather than merely a model. Imbalance makes accuracy deceptive; probability quality matters for cost decisions; threshold selection depends on capacity and costs; temporal validation is essential; and monitoring must account for delayed labels and adversarial adaptation.

10. Limitations, Governance and Contemporary Practice

No classifier is universally best. Logistic regression is interpretable and often well calibrated after appropriate processing, but its linear log-odds assumption may be restrictive. Naïve Bayes is fast and effective in suitable feature spaces, but conditional independence and probability calibration can be weak. k-NN is intuitive but suffers from scaling, storage and high-dimensionality problems. SVMs can generalise strongly with appropriate margins and kernels, but tuning, calibration and scalability require care.

Feature leakage, biased labels and selective measurement can produce apparently strong but invalid models. In healthcare, a diagnosis label may reflect access to care rather than underlying disease. In lending, historical decisions may encode discriminatory practices. Governance therefore requires documentation of data provenance, intended use, limitations, performance by relevant groups, human oversight and appeal mechanisms.

Fairness cannot be inferred from one metric. Equalised error rates, predictive parity and calibration can conflict when base rates differ. The appropriate analysis depends on law, institutional policy, causal assumptions and the consequences of errors. Sensitive attributes may be needed for auditing even when they are excluded from prediction, subject to privacy and governance requirements.

Modern practice increasingly combines interpretable baselines with nonlinear models, uncertainty estimates, drift detection and decision-curve analysis. Automated machine-learning tools can accelerate experimentation but do not remove the need for leakage control, validation design, calibration, threshold governance and domain review.

Chapter Summary

Classification estimates class membership and converts estimates into decisions through thresholds. Logistic regression models conditional probability using a linear log-odds relationship and supports L1 or L2 regularisation. Naïve Bayes applies Bayes’ rule with conditional feature independence and is especially useful for fast, high-dimensional problems. k-NN makes local predictions based on a chosen distance and requires careful scaling and selection of [[M:k]]. SVMs maximise a margin, use hinge loss in the soft-margin case and can represent nonlinear boundaries through valid kernels. Kernel choice and hyperparameters control the geometry and complexity of the decision function.

Reliable classification requires more than selecting an algorithm. Imbalance demands suitable metrics and leakage-free resampling. Thresholds should reflect costs and operational capacity. Calibration is necessary when probabilities drive decisions, and evaluation should include discrimination, calibration, utility, subgroup performance and temporal robustness. Ethical deployment requires attention to labels, privacy, fairness, documentation, human oversight and post-deployment monitoring.

Key Terms

Classification: Assignment of observations to categorical classes.

Discriminative model: Model that estimates a decision boundary or conditional distribution directly.

Generative model: Model that represents how features arise within each class and combines this with class priors.

Logistic regression: A model whose log-odds are linear in the features.

Logit: The logarithm of odds, [[M:log(p/(1−p))]].

Regularisation: Addition of a penalty that controls model complexity.

Naïve Bayes: Bayesian classifier using conditional independence of features given the class.

Laplace smoothing: Adjustment that prevents zero probability estimates for unseen categories.

Nearest neighbour: Training observation used as a local reference for prediction.

Support vector: Observation defining or influencing an SVM margin.

Margin: Distance-related separation between a classifier boundary and observations.

Hinge loss: SVM loss [[M:max(0,1−yf(x))]].

Kernel: Function representing an inner product in a feature space.

RBF kernel: Gaussian-like kernel producing local nonlinear similarity.

Class imbalance: Unequal prevalence of classes in a dataset.

Calibration: Agreement between predicted probabilities and observed frequencies.

Threshold: Score or probability value separating predicted classes.

Precision: Fraction of predicted positives that are actually positive.

Recall: Fraction of actual positives that are detected.

Cost-sensitive classification: Classification in which errors have explicitly unequal consequences.

Review Questions

Short Answer Questions

  1. Why can accuracy be misleading for a highly imbalanced classification problem?
  2. Interpret a positive coefficient in logistic regression on the odds scale.
  3. State the conditional-independence assumption made by naïve Bayes.
  4. Why should features generally be scaled before Euclidean k-NN?
  5. What is the role of support vectors in an SVM?

Long Answer Questions

  1. Explain logistic regression from its linear predictor through probability estimation, likelihood, regularisation and thresholding.
  2. Compare naïve Bayes, k-NN and logistic regression in terms of assumptions, computational behaviour, interpretability and calibration.
  3. Derive the soft-margin SVM formulation and explain the roles of slack variables and [[M:C]].
  4. Explain the kernel trick, positive-semidefinite kernels and the effects of [[M:γ]] in an RBF kernel.
  5. Design a leakage-resistant evaluation protocol for an imbalanced, time-dependent fraud-classification problem.

Analytical / Application Questions

  1. A medical screening model has [[M:C_FP=1]] and [[M:C_FN=19]]. Assuming calibrated probabilities and no capacity constraint, derive the cost-minimising probability threshold and explain its operational meaning.
  2. A classifier has excellent ROC-AUC but poor precision at the deployment prevalence. Analyse at least three possible reasons and propose an evaluation and remediation plan.
  3. For a customer-retention dataset containing age, income and account tenure in different units, design a comparison between logistic regression, k-NN and an RBF SVM. Specify preprocessing, validation, metrics, calibration and threshold selection.

Multiple Choice Questions with Answers

Question 1

Which expression gives the logistic-regression probability for a linear score z?

A. [[M:1/(1+e^(−z))]]

B. [[M:e^z]]

C. [[M:z/(1+z)]]

D. [[M:1−z]]

Correct Answer: A. [[M:1/(1+e^(−z))]]

Question 2

In logistic regression, increasing feature x_j by one unit while holding other features fixed multiplies the odds by:

A. [[M:β_j]]

B. [[M:e^{β_j}]]

C. [[M:1−β_j]]

D. [[M:β_j²]]

Correct Answer: B. [[M:e^{β_j}]]

Question 3

Which assumption is central to naïve Bayes?

A. Features are marginally independent

B. Classes have equal prevalence

C. Features are conditionally independent given the class

D. All features are normally distributed

Correct Answer: C. Features are conditionally independent given the class

Question 4

Why is feature scaling important for Euclidean k-NN?

A. It guarantees linear separability

B. It prevents large-unit features from dominating distance

C. It removes the need to choose k

D. It converts labels into probabilities

Correct Answer: B. It prevents large-unit features from dominating distance

Question 5

In a soft-margin SVM, increasing C generally:

A. Penalises margin violations more strongly

B. Forces every feature coefficient to zero

C. Always increases the margin

D. Removes all support vectors

Correct Answer: A. Penalises margin violations more strongly

Question 6

For an RBF kernel [[M:K(x,x′)=exp(−γ||x−x′||²)]], a larger γ generally produces:

A. More local similarity and potentially a more complex boundary

B. A strictly linear boundary

C. Identical similarity for every pair

D. No dependence on distance

Correct Answer: A. More local similarity and potentially a more complex boundary

Question 7

If TP=80 and FN=20, recall is:

A. 0.20

B. 0.40

C. 0.80

D. 0.90

Correct Answer: C. 0.80

Question 8

With calibrated probability p, false-positive cost [[M:C_FP]] and false-negative cost [[M:C_FN]], the simplified cost-minimising threshold is:

A. [[M:C_FN/(C_FP+C_FN)]]

B. [[M:C_FP/(C_FP+C_FN)]]

C. [[M:C_FP−C_FN]]

D. [[M:C_FP×C_FN]]

Correct Answer: B. [[M:C_FP/(C_FP+C_FN)]]

Question 9

Which statement about ROC-AUC is correct?

A. It directly specifies the deployment threshold

B. It measures ranking performance across thresholds

C. It guarantees calibrated probabilities

D. It is unaffected by all prevalence changes

Correct Answer: B. It measures ranking performance across thresholds

Question 10

When oversampling is used with cross-validation, best practice is to:

A. Oversample before splitting all data

B. Apply oversampling separately inside each training fold

C. Oversample only the test fold

D. Use oversampling instead of validation

Correct Answer: B. Apply oversampling separately inside each training fold

Machine Learning
Chapter 5 of 15

Machine Learning

Chapter 6
Decision Trees and Ensemble Learning

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Decision Trees and Ensemble Learning

Introduction and Context

Decision trees are supervised learning models that represent a prediction as a sequence of hierarchical decisions. At each internal node, the model applies a rule to one feature; each branch represents an outcome of that rule; and each terminal leaf produces a prediction. Trees are attractive because their logic is visible, they accommodate nonlinear relationships and interactions, and they can work with numerical and categorical variables. Their principal weakness is instability: small changes in training data can produce substantially different trees.

Ensemble learning addresses this weakness by combining multiple models. Bagging reduces variance by fitting models to different resampled data sets and averaging their predictions. Random forests add feature-level randomisation to bagging. Boosting builds models sequentially so that later models focus on errors made by earlier models. Stacking combines heterogeneous models through a learned meta-model. These methods form a central part of practical machine learning because they often provide strong predictive performance without requiring a highly elaborate feature transformation pipeline.

The central design problem is not simply to maximise accuracy. A suitable ensemble must also be computationally feasible, calibrated, robust to distributional change, sufficiently interpretable, and consistent with legal and ethical requirements.

Learning Objectives

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

  1. Explain how decision trees recursively partition a feature space.
  2. Calculate and interpret entropy, Gini impurity, information gain and variance reduction.
  3. Apply pruning and regularisation to control tree complexity.
  4. Distinguish bagging, random forests, boosting, gradient boosting and stacking.
  5. Analyse ensemble behaviour using the bias–variance framework.
  6. Tune ensembles using valid resampling and out-of-sample evaluation.
  7. Interpret ensemble predictions responsibly and identify their limitations.

1. Decision Trees as Recursive Partitioning Models

A decision tree partitions the input space into regions. For classification, a leaf generally predicts the majority class or a class-probability distribution. For regression, a leaf commonly predicts the mean response of the training observations reaching that leaf.

A binary numerical split may be written as:

xj ≤ t

where xj is feature j and t is a candidate threshold. Observations satisfying the condition move to one child node and the remainder move to the other. Categorical features may be split by category membership, although the exact treatment depends on the implementation.

A tree is grown by repeatedly selecting a split that improves node purity. Let a node contain N observations and let its two child nodes contain NL and NR observations. If I(·) is an impurity measure, the weighted impurity after splitting is:

Isplit = (NL/N) I(L) + (NR/N) I(R)

The impurity reduction is:

ΔI = I(parent) − Isplit

The split is useful when ΔI is large. This expression applies to classification and regression, provided an appropriate impurity function is selected.

1.1 Classification impurity measures

For a node containing class proportions p1, p2, …, pK, entropy is:

H(S) = −∑k=1K pk log₂(pk)

Here, K is the number of classes, pk is the proportion of class k, and the convention 0 log₂ 0 = 0 is used. Entropy is zero for a pure node and is largest when the classes are equally represented.

Gini impurity is:

G(S) = 1 − ∑k=1K pk²

It can also be interpreted as the probability that a randomly selected observation would be misclassified if its class were assigned according to the node’s class distribution. Both entropy and Gini impurity measure node heterogeneity. Gini impurity is often computationally simpler, while entropy has a direct information-theoretic interpretation.

Information gain using entropy is the reduction in entropy produced by a split. The same weighted-child formula applies to Gini impurity. A tree algorithm may use either criterion; results can differ slightly, but preprocessing and validation usually have a greater practical effect than this choice.

1.2 Regression impurity

For regression, a common node impurity is the within-node sum of squared deviations:

SSE(S) = ∑i∈S (yi − ȳS)²

where yi is the observed response and ȳS is the mean response in node S. The mean minimises squared error within a node. A split is selected when it substantially reduces the weighted residual variation of its child nodes.

2. Worked Example: Selecting a Classification Split

Suppose a node contains 10 observations: 6 positive and 4 negative. Its Gini impurity is:

G(parent) = 1 − (0.6² + 0.4²) = 1 − 0.52 = 0.48

A candidate split produces a left child with 4 observations, all positive, and a right child with 6 observations containing 2 positive and 4 negative cases. The left impurity is zero. The right impurity is:

G(right) = 1 − ((2/6)² + (4/6)²) = 1 − 20/36 ≈ 0.444

The weighted post-split impurity is:

G(split) = (4/10)(0) + (6/10)(0.444) ≈ 0.267

Therefore, the impurity reduction is:

ΔG = 0.48 − 0.267 ≈ 0.213

This split improves class separation. The calculation assumes that the candidate split is evaluated at the current node and that class proportions are estimated from the training observations reaching that node.

3. Tree Growth, Prediction and Regularisation

A fully grown tree can continue splitting until leaves are pure or contain very few observations. Such a tree may memorise noise. Important controls include:

  • Maximum depth: limits the number of decisions along a path.
  • Minimum samples per split or leaf: prevents decisions based on very small groups.
  • Maximum number of leaves: directly controls model size.
  • Minimum impurity decrease: permits a split only when its improvement exceeds a threshold.
  • Cost-complexity pruning: grows a large tree and then removes branches whose predictive benefit is insufficient.

A common cost-complexity objective is:

R_α(T) = R(T) + α |Tleaves|

Here, R(T) is the empirical loss of tree T, |Tleaves| is the number of terminal leaves, and α ≥ 0 penalises complexity. Larger α favours smaller trees. The loss and penalty must be selected consistently with the task and implementation. Pruning parameters should be chosen using validation data or cross-validation, not by inspecting the test set.

Trees usually require little feature scaling because comparisons such as xj ≤ t are unaffected by a monotonic rescaling of a feature. Missing-value handling, categorical encoding, high-cardinality identifiers and leakage nevertheless require careful treatment. A tree can exploit an identifier or a post-outcome variable even when the apparent predictive relationship is spurious.

4. Bias, Variance and Generalisation

Expected prediction error can be conceptualised through the bias–variance decomposition. For squared-error regression at an input x, with irreducible noise variance σ²,:

E[(Y − f̂(x))²] = σ² + Bias[f̂(x)]² + Var[f̂(x)]

Here, f̂(x) is the fitted predictor over hypothetical training samples. The decomposition assumes a squared-error setting and separates irreducible noise, systematic error from an overly restrictive model, and sensitivity to the training sample.

A shallow tree generally has higher bias and lower variance. A deep tree generally has lower training bias and higher variance. Bagging primarily reduces variance, while boosting can reduce bias and variance depending on its depth, learning rate, number of iterations and regularisation. The decomposition is a conceptual guide rather than a guarantee that every tuning change affects only one component.

5. Bagging and Random Forests

5.1 Bagging

Bootstrap aggregation, or bagging, creates B bootstrap training sets by sampling observations with replacement. A base learner is fitted to each sample. For regression, predictions are averaged:

f̂bag(x) = (1/B) ∑b=1B f̂b(x)

For classification, the ensemble may use majority voting or average estimated class probabilities. If base-model errors are not perfectly correlated, averaging can reduce variance. If each model has variance σf² and pairwise error correlation ρ, the approximate variance of an average of B equally variable models is:

Var(average) ≈ σf² [ρ + (1 − ρ)/B]

This relationship assumes comparable model variances and a simplified common-correlation structure. It shows why adding models has diminishing returns when errors are highly correlated.

A bootstrap sample of size N contains, on average, approximately 63.2% of the distinct original observations. The remaining observations are called out-of-bag (OOB) for that fitted model. OOB observations can provide an internal performance estimate, although an external validation set remains valuable, especially during extensive tuning or under distribution shift.

5.2 Random forests

A random forest combines bootstrap sampling with random feature selection. At each split, only a randomly selected subset of features is considered. This decorrelates trees, which is often more important than simply increasing their number.

Important hyperparameters include:

HyperparameterMain effect
Number of treesStabilises estimates; increases computation but usually not overfitting substantially after convergence
Features considered per splitControls tree correlation and split diversity
Maximum depthControls individual-tree complexity
Minimum leaf sizeSmooths predictions and reduces small-leaf variance
Class weights or sampling strategyAddresses unequal class costs or imbalance

Random forests are robust to nonlinearities, interactions and moderate outliers. They are less naturally extrapolative than many parametric regression models: regression predictions are generally bounded by the response values represented in terminal leaves.

6. Boosting and Gradient Boosting

Boosting constructs an additive model sequentially. A general additive predictor is:

FM(x) = F0(x) + ∑m=1M η hm(x)

where F0 is the initial model, hm is the learner added at iteration m, η is the learning rate, and M is the number of iterations. Usually, 0 < η ≤ 1. Small learning rates often require more iterations but can improve generalisation when paired with appropriate regularisation.

6.1 AdaBoost intuition

AdaBoost increases the emphasis placed on observations misclassified by earlier weak learners. The next learner therefore addresses difficult cases. A weighted combination of weak learners can form a strong classifier. AdaBoost can be sensitive to noisy labels and extreme outliers because repeatedly misclassified observations may receive very large weights.

6.2 Gradient boosting

Gradient boosting generalises the idea by fitting each new learner to the negative gradient of the loss function. Let the empirical objective be:

L(F) = ∑i=1N ℓ(yi, F(xi)) + Ω(F)

where ℓ is a differentiable or suitably approximated loss and Ω represents regularisation when included by the implementation. At iteration m, the pseudo-residual for observation i is:

rim = −[∂ℓ(yi, F(xi))/∂F(xi)]_{F=Fm−1}

A weak learner is fitted to these pseudo-residuals, and the model is updated in their direction. This is functional gradient descent: optimisation occurs over functions rather than only over a finite parameter vector.

For squared-error regression, ℓ(y,F) = 1/2(y−F)², so the negative gradient is yi − Fm−1(xi), the ordinary residual. For other losses, the target used for the next learner differs from the raw residual.

Common gradient-boosting controls are learning rate, number of estimators, tree depth, minimum leaf size, row subsampling and column subsampling. Early stopping monitors validation performance and stops when additional iterations cease to improve it. The validation data used for early stopping must be kept separate from the final test data.

Modern implementations such as histogram-based gradient boosting and highly optimised tree-boosting libraries improve speed and memory use. Their additional features may include regularised leaf weights, missing-value routing, categorical-feature handling and efficient distributed training. These capabilities do not remove the need for leakage control, calibration assessment and independent testing.

7. Stacking and Model Diversity

Stacking combines predictions from several base learners using a meta-learner. For example, a linear meta-model may receive predictions from a random forest, a gradient-boosted tree and a regularised linear model.

The critical methodological requirement is that meta-features be generated out of sample. If a base learner predicts the same observations used to fit it, its predictions are overly optimistic, and the meta-learner can learn from leakage. In cross-validated stacking, each training observation receives a prediction from a base model that did not train on that observation. The base models are then refitted on all available training data before predicting the held-out test set.

Stacking is useful when models make complementary errors. Simply combining highly correlated models offers limited benefit. A meta-learner should be regularised and evaluated within a nested or carefully separated validation design, particularly when many candidate models are tried.

8. Comparing Ensemble Families

MethodConstructionMain strengthMain risk
BaggingParallel bootstrap models and aggregationVariance reductionCorrelated learners limit gains
Random forestBagging plus random feature subsetsRobust nonlinear predictionLess transparent; poor extrapolation
AdaBoostSequential reweighting of difficult casesEffective weak-learner combinationSensitivity to noisy labels and outliers
Gradient boostingSequential loss-gradient optimisationHigh predictive accuracy and flexible lossesTuning sensitivity and overfitting
StackingMeta-model combines diverse predictorsExploits complementary inductive biasesLeakage and validation complexity

No method is universally best. Random forests often provide a strong baseline with limited tuning. Gradient boosting may achieve superior accuracy on structured tabular data, but requires more careful tuning. A simple, calibrated model can be preferable where explanation, stability or regulatory defensibility is more important than a small accuracy improvement.

9. Tuning and Evaluation for Robust Performance

A reliable workflow begins with a problem definition: target population, prediction horizon, acceptable errors, operational costs and fairness requirements. Data splitting must reflect deployment. Random splits are inappropriate for many temporal, grouped or spatial problems. For time-dependent data, training must precede validation chronologically. For repeated individuals, all records from an individual should normally remain in the same partition.

Recommended practices include:

  1. Establish a simple baseline, such as majority class, mean prediction or regularised linear model.
  2. Create training, validation and final test partitions, or use nested cross-validation.
  3. Fit preprocessing steps within each training fold.
  4. Tune a small, justified hyperparameter space rather than conducting unstructured search.
  5. Use early stopping only with an appropriate validation scheme.
  6. Report uncertainty, subgroup performance and calibration, not only a single score.
  7. Retrain the selected procedure on permitted data and evaluate once on the untouched test set.

For binary classification, accuracy may be misleading under class imbalance. Useful measures include precision, recall, F1 score, ROC-AUC, precision–recall AUC, log loss and calibration. For regression, mean absolute error is:

MAE = (1/N) ∑i=1N |yi − ŷi|

and root mean squared error is:

RMSE = √[(1/N) ∑i=1N (yi − ŷi)²]

MAE treats errors linearly, whereas RMSE gives greater influence to large errors. The choice should reflect the operational loss. Metrics must be computed on observations not used to fit the evaluated predictions.

10. Interpretability and Responsible Use

Tree structure offers local transparency, but a large ensemble is not automatically interpretable. Useful tools include:

  • Permutation importance: measures performance degradation after permuting a feature. It is model- and evaluation-set-dependent and can undervalue one of several correlated features.
  • Impurity-based importance: aggregates split improvements, but may favour continuous or high-cardinality variables.
  • Partial dependence: estimates average predictions as a feature varies, but can be misleading in regions where feature combinations are unrealistic.
  • Individual conditional expectation: displays prediction curves for individual observations and can reveal heterogeneity hidden by averages.
  • Shapley-based explanations: allocate a prediction difference among features under specified background and dependence assumptions; they are explanations of a model, not proofs of causality.

Interpretability should be tested for stability across samples and model versions. A feature with high importance is not necessarily a cause of the outcome. Sensitive attributes may be excluded from model inputs yet reconstructed through proxies such as location, occupation or purchasing behaviour.

Governance concerns include privacy, discrimination, contestability, documentation, access control and monitoring for drift. In high-impact contexts, the organisation should record the training population, target definition, feature provenance, validation design, threshold rationale, known limitations and human-review procedure. Fairness cannot be inferred from aggregate accuracy alone; subgroup error rates, calibration and the consequences of false positives and false negatives should be examined.

11. Illustrative Case: Credit-Risk Screening

Background

A lender wants to predict whether a small-business applicant will become seriously delinquent within twelve months. The data contain application attributes, repayment history, cash-flow summaries and industry information.

Problem

The historical data are imbalanced: serious delinquency is uncommon. Some records contain variables updated after approval, and applications from the same business may occur repeatedly. The lender requires both predictive performance and an auditable decision process.

Analysis

A random row-level split would leak information between repeated applications and could allow post-approval variables to enter training. The modelling team therefore defines the prediction timestamp, removes post-timestamp features, groups applications by business, and uses a time-aware validation design. It reports precision–recall performance, calibration and error rates across relevant business segments.

Approach and solution

A regularised logistic model establishes a transparent baseline. A random forest provides a variance-reduced nonlinear benchmark. A gradient-boosted tree model is tuned with shallow trees, learning-rate control, row subsampling and early stopping. The decision threshold is selected using the cost of manual review, missed delinquency and unnecessary referral rather than accuracy alone.

The model card documents data limitations, missing-value treatment, feature definitions and known proxy risks. Permutation explanations are supplemented with case-level review, and an appeal process allows applicants to request human reconsideration. Monitoring checks calibration, approval rates, subgroup performance and feature drift after deployment.

Outcome

The boosted model improves ranking performance over the baseline, but its operational value depends on threshold selection and review capacity. The lender does not deploy the model solely because it has the highest validation score. Instead, it adopts the model subject to calibration monitoring, periodic fairness review, human oversight and a rollback plan.

Learning points

The case demonstrates that ensemble selection is a socio-technical decision. Leakage prevention, temporal validity, group-aware splitting, cost-sensitive evaluation and governance are as important as algorithm choice.

12. Challenges and Contemporary Directions

Ensembles can be computationally expensive, particularly when cross-validation, hyperparameter search and large forests are combined. Distributed training and histogram methods reduce cost, but energy use and latency should be measured. Ensembles may also be poorly calibrated even when they rank observations well; post-hoc calibration using a separate validation set can help.

Current research and practice address uncertainty estimation, conformal prediction, monotonic constraints, causal and invariant prediction, fairness-aware learning, online updating and efficient inference. Monotonic constraints can encode domain knowledge such as the requirement that risk should not decrease as a verified debt burden increases. Such constraints improve plausibility but do not establish causal validity. Under distribution shift, robust monitoring and selective prediction may be more valuable than a marginal improvement on an old benchmark.

Best-Practice Checklist

  • Define the deployment population and prediction time before modelling.
  • Use a baseline and compare all complex models against it.
  • Prevent leakage through preprocessing, grouping and temporal design.
  • Tune depth, learning rate, number of estimators and sampling jointly where relevant.
  • Use out-of-bag estimates or cross-validation appropriately, not as substitutes for critical external testing.
  • Assess calibration, subgroup performance and operational costs.
  • Prefer simpler or constrained models when performance is comparable.
  • Explain predictions without presenting feature importance as causation.
  • Document assumptions, version data and monitor drift after deployment.

Chapter Summary

Decision trees recursively partition data using impurity reduction for classification or residual variation for regression. Entropy, Gini impurity and squared-error criteria provide principled split measures. Deep trees can have low training error but high variance, whereas pruning and structural constraints improve generalisation.

Bagging reduces variance through bootstrap aggregation. Random forests additionally randomise candidate features, lowering correlation among trees. Boosting constructs an additive model sequentially; gradient boosting fits learners to loss gradients and can achieve excellent performance on structured data. Stacking combines out-of-sample predictions from diverse base models through a meta-learner, but requires strict leakage control.

The bias–variance framework explains why model complexity and ensemble diversity matter. Robust tuning requires deployment-relevant validation, appropriate metrics, calibration analysis and an untouched final test set. Interpretability tools are informative but do not establish causality. Responsible deployment requires attention to fairness, privacy, documentation, human oversight, drift and the consequences of errors.

Key Terms

Decision tree: A hierarchical model that predicts through sequential feature-based decisions.

Node: A region or decision point in a tree.

Leaf: A terminal tree region that produces a prediction.

Entropy: An information-based measure of class heterogeneity.

Gini impurity: A classification impurity measure based on squared class proportions.

Information gain: Reduction in entropy produced by a split.

Pruning: Removal of tree branches to reduce unnecessary complexity.

Bagging: Bootstrap aggregation of independently fitted base learners.

Bootstrap sample: A sample formed by drawing observations with replacement.

Out-of-bag estimate: Prediction or performance estimate based on observations omitted from a bootstrap sample.

Random forest: An ensemble using bootstrap trees and random feature subsets.

Boosting: Sequential combination of learners that focuses on correcting prior errors.

Gradient boosting: Boosting that fits learners to negative loss gradients.

Learning rate: The multiplier controlling each boosting update.

Early stopping: Termination of training when validation performance ceases to improve.

Stacking: Combination of base-model predictions using a meta-learner.

Bias: Systematic prediction error caused by restrictive assumptions.

Variance: Prediction sensitivity to the training sample.

Calibration: Agreement between predicted probabilities and observed frequencies.

Data leakage: Unintended use of information unavailable at prediction time.

Review Questions

5 Short Answer Questions

  1. What is the purpose of an impurity measure in a decision tree?
  2. Distinguish entropy from Gini impurity.
  3. Why does random feature selection help a random forest?
  4. What is the role of the learning rate in gradient boosting?
  5. Why must stacking use out-of-sample base-model predictions?

5 Long Answer Questions

  1. Explain how a classification tree is grown, including split evaluation, stopping and pruning.
  2. Compare bagging, random forests and gradient boosting in terms of construction, bias, variance and computational behaviour.
  3. Derive and interpret the weighted impurity reduction for a binary split.
  4. Discuss how validation design should change for temporal, grouped and imbalanced data.
  5. Evaluate the benefits and limitations of permutation importance, partial dependence and Shapley-based explanations.

3 Analytical / Application Questions

  1. A classification node contains 12 observations, 9 belonging to class A and 3 to class B. Calculate its Gini impurity. Then evaluate a split whose child nodes contain 5 A observations and 1 B observation on the left, and 4 A observations and 2 B observations on the right. Determine the impurity reduction.
  2. Design a validation strategy for a fraud model in which the same customer may appear many times and transaction patterns change over time. Justify the split, metric and monitoring choices.
  3. A random forest and a gradient-boosted model have similar test AUC, but the boosted model is poorly calibrated and the forest has unstable subgroup error rates. Recommend a deployment decision and identify the additional analyses required before release.

Multiple Choice Questions with Answers

Question 1

Which measure is zero for a completely pure classification node?

A. Entropy and Gini impurity

B. RMSE only

C. Bias only

D. Bootstrap variance

Correct Answer: A. Entropy and Gini impurity

Question 2

What is the principal variance-reduction mechanism in bagging?

A. Sequentially increasing observation weights

B. Averaging models trained on bootstrap samples

C. Scaling all input features

D. Removing all noisy observations

Correct Answer: B. Averaging models trained on bootstrap samples

Question 3

How does a random forest usually decorrelate its trees?

A. It uses only one tree at a time

B. It forces every tree to have identical splits

C. It randomly restricts features considered at splits

D. It fits trees only to misclassified observations

Correct Answer: C. It randomly restricts features considered at splits

Question 4

For squared-error regression, the negative gradient used by gradient boosting is what?

A. The absolute prediction

B. The residual y − prediction

C. The class proportion

D. The feature variance

Correct Answer: B. The residual y − prediction

Question 5

What is the main danger of using in-sample base-model predictions in stacking?

A. Underflow

B. Data leakage and optimistic meta-model training

C. Automatic feature scaling

D. Excessive class balance

Correct Answer: B. Data leakage and optimistic meta-model training

Question 6

Which change generally increases the complexity of an individual tree?

A. Reducing maximum depth

B. Increasing minimum leaf size

C. Increasing maximum depth

D. Increasing pruning strength

Correct Answer: C. Increasing maximum depth

Question 7

If two ensemble models make highly correlated errors, averaging them will usually provide what benefit?

A. A limited variance reduction

B. Guaranteed elimination of bias

C. Guaranteed causal interpretation

D. Perfect calibration

Correct Answer: A. A limited variance reduction

Question 8

For a highly imbalanced classification problem, which metric is often more informative than accuracy for identifying positive cases?

A. Precision–recall AUC

B. The number of tree leaves

C. Training entropy only

D. Feature scale

Correct Answer: A. Precision–recall AUC

Question 9

What does a larger cost-complexity parameter generally encourage?

A. Larger and deeper trees

B. Smaller trees with fewer leaves

C. More leakage

D. More bootstrap observations per row

Correct Answer: B. Smaller trees with fewer leaves

Question 10

Which statement about feature importance is correct?

A. Importance proves that a feature causes the outcome

B. Impurity importance can favour high-cardinality features

C. Permutation importance is independent of the evaluation data

D. Importance values guarantee fairness

Correct Answer: B. Impurity importance can favour high-cardinality features

Machine Learning
Chapter 6 of 15

Machine Learning

Chapter 7
Model Evaluation, Selection and Experimentation

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Model Evaluation, Selection and Experimentation

Introduction

A machine-learning model is not valuable merely because it can fit a training set. Its practical value depends on how reliably it predicts previously unseen cases, whether its errors are acceptable for the application, and whether the reported evidence can be reproduced. Model evaluation is therefore an inferential activity: from finite observations, we estimate performance on a future population. Model selection extends this activity by comparing algorithms, representations, and hyperparameters. Experimentation adds disciplined control of data, randomness, software, and reporting.

The central danger is overfitting to the evaluation process. A model may overfit training data, but an analyst may also overfit a validation set by repeatedly trying alternatives and retaining the one that happens to perform best. A sound experimental design separates data used for learning, data used for decisions, and data reserved for final confirmation.

Learning objectives

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

  • distinguish training, validation, and test data;
  • select appropriate metrics for classification, regression, ranking, and imbalanced problems;
  • use cross-validation, learning curves, and baselines to diagnose models;
  • formulate and execute hyperparameter searches without leakage;
  • compare models using uncertainty estimates and suitable statistical procedures;
  • design reproducible, auditable machine-learning experiments;
  • interpret performance results in technical, operational, and ethical contexts.

1. The logic of generalization

Let a dataset contain input–outcome pairs (xi,yi), where xi is a feature vector and yi is the observed target. A learning algorithm uses training data Dtrain to produce a predictor f. For a loss function L(y,f(x)), the population risk is

R(f) = E(X,Y)∼P[L(Y,f(X))]

Here P is the unknown joint distribution of future cases, and the expectation represents average future loss. Because P is unknown, it is estimated using finite samples. The empirical risk on a sample of size n is

R̂(f) = (1/n) ∑i=1n L(yi,f(xi))

The empirical risk is useful only when the observations represent the intended deployment population and the evaluation data were not used improperly during model development. Standard statistical assumptions include independent and identically distributed observations for ordinary random splitting. Time series, clustered observations, repeated measurements, and grouped individuals violate this assumption and require specialized splitting schemes.

Bias, variance, and irreducible error

Prediction error can arise from:

  • Irreducible noise: uncertainty in the data-generating process that no predictor can remove;
  • Bias: systematic error caused by an overly restricted model or representation;
  • Variance: sensitivity to the particular sample used for training;
  • Estimation error: uncertainty because performance is measured on a finite evaluation sample;
  • Distribution shift: a difference between development and deployment populations.

For squared-error prediction, a useful conceptual decomposition at a fixed input is

E[(Y − f̂(X))²] = σ² + Bias[f̂(X)]² + Var[f̂(X)]

where σ² is conditional noise variance, Bias[f̂(X)] is the expected prediction minus the true conditional mean, and Var[f̂(X)] is variation across training samples. This decomposition assumes squared loss and is an explanatory framework rather than a direct recipe for measuring each term in ordinary data.

2. Train–validation–test designs

A conventional design divides data into three mutually exclusive portions:

PartitionPrimary useShould influence final fitted parameters?
TrainingEstimate model parametersYes
ValidationSelect model family, features, threshold, and hyperparametersNo, except through selection decisions
TestOne-time estimate of final generalizationNo

A common split might be 60% training, 20% validation, and 20% test, but proportions depend on sample size, data cost, and variance. With limited data, cross-validation is generally preferable to sacrificing a large fixed validation set. The test set should be locked away until the analysis plan and model-selection decisions are complete.

After selecting hyperparameters using training and validation information, practitioners often refit the chosen configuration on the combined training and validation data, then evaluate once on the untouched test set. This is valid only if the test set has not influenced the choice of configuration, preprocessing, threshold, or stopping point.

Data leakage

Data leakage occurs when information unavailable at prediction time enters training or evaluation. Examples include:

  • standardizing the full dataset before splitting;
  • imputing missing values using test-set statistics;
  • allowing a patient to appear in both training and test sets;
  • using a post-outcome variable as a predictor;
  • selecting features using all labels before cross-validation;
  • tuning a classification threshold on the test set.

The safe pattern is to place preprocessing and the estimator in one pipeline. Within each fold, fit imputation, scaling, encoding, feature selection, and model parameters only on that fold’s training portion. Grouped or temporal leakage requires an appropriate split, not merely a pipeline.

Split strategies

  • Stratified splitting approximately preserves class proportions and is useful for classification.
  • Group splitting keeps all observations from an individual, household, hospital, or device in one partition.
  • Time-based splitting trains on earlier observations and evaluates on later observations.
  • Nested cross-validation separates hyperparameter selection from performance estimation when data are limited or many configurations are tested.

Random splitting is inappropriate when the deployment task is forecasting, when observations are correlated, or when future information can be inferred from identifiers or temporal structure.

3. Metrics must match the decision

A metric is a formalization of what counts as error. No metric is universally best.

Classification metrics

For binary classification, define:

  • true positives (TP): positive cases correctly predicted;
  • true negatives (TN): negative cases correctly predicted;
  • false positives (FP): negative cases predicted positive;
  • false negatives (FN): positive cases predicted negative.

Accuracy is

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

It is informative when class proportions and error costs are reasonably balanced. Otherwise, it can be misleading. Precision and recall are

Precision = TP/(TP + FP)
Recall = TP/(TP + FN)

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

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

These ratios are undefined when their denominators are zero; software must specify a zero-division policy.

Specificity, also called the true-negative rate, is TN/(TN+FP). Balanced accuracy is the mean of recall and specificity:

Balanced accuracy = (Recall + Specificity)/2

For probabilistic classifiers, log loss rewards calibrated probabilities and strongly penalizes confident wrong predictions. For binary outcomes with yi∈{0,1} and predicted probability pi,

Log loss = −(1/n)∑[yi log(pi) + (1−yi)log(1−pi)]

Probabilities are typically clipped away from exactly 0 and 1 for numerical stability. Brier score is the mean squared probability error, (1/n)∑(pi−yi)², with lower values better.

ROC–AUC measures ranking across classification thresholds and can remain high in highly imbalanced data even when precision is poor. Precision–recall curves are often more informative when the positive class is rare. The selected operating threshold should reflect costs, capacity, and safety requirements rather than defaulting to 0.5.

Regression metrics

For observed values yi and predictions ŷi,

MAE = (1/n)∑|yi−ŷi|

MAE is in the target’s units and is relatively robust to outliers. Mean squared error is

MSE = (1/n)∑(yi−ŷi)²

and RMSE is √MSE, also in target units but more sensitive to large errors. Mean absolute percentage error can be problematic when actual values are zero or near zero. R² = 1 − SSE/SST compares residual sum of squares with total sum of squares; it can be negative on test data and does not by itself establish practical usefulness.

Metrics should be reported with confidence intervals where feasible, alongside subgroup performance, calibration, and a confusion matrix or error distribution. A small improvement in a metric may not justify greater latency, cost, complexity, or fairness risk.

4. Cross-validation and uncertainty

In k-fold cross-validation, the development data are divided into k folds. Each fold is used once for validation while the remaining k−1 folds are used for training. The cross-validation estimate is

CVk = (1/k)∑j=1k sj

where sj is the score on fold j. The fold standard deviation is

s = √[(1/(k−1))∑j=1k(sj−CVk)²]

This describes variation across folds; it is not automatically a valid standard error because fold scores are dependent through overlapping training sets. A rough standard error is s/√k, but confidence intervals require care, particularly for model comparisons.

Stratified k-fold cross-validation is common for classification. Repeated cross-validation changes the partitions and can reveal instability. Nested cross-validation has an outer loop for unbiased performance estimation and an inner loop for hyperparameter selection:

  1. Hold out one outer fold.
  2. Search hyperparameters using only the remaining outer-training data and inner cross-validation.
  3. Fit the selected configuration on all outer-training data.
  4. Score it on the outer test fold.
  5. Aggregate outer-fold scores.

Nested validation is computationally expensive but protects against optimistic estimates caused by reporting the best inner-fold result.

5. Baselines, learning curves, and diagnostic analysis

A baseline establishes whether complexity is justified. Examples include majority-class prediction, stratified random prediction, linear or logistic regression, a seasonal naive forecast, or a current operational rule. A baseline should use the same evaluation protocol as the proposed model.

A learning curve plots training and validation performance against training-set size. Typical interpretations are:

  • high training error and similar validation error: underfitting; improve features or model capacity;
  • low training error but much higher validation error: high variance; obtain more data, regularize, or simplify;
  • both errors improving with more data: additional data may help;
  • a persistent gap despite more data: investigate noise, leakage, distribution shift, or representation limits.

Validation curves vary one hyperparameter while holding others under a defined search procedure. Error analysis should inspect examples, segments, confidence, missingness, and systematic failure modes—not only aggregate scores.

6. Hyperparameter optimization and model selection

Model parameters are learned from data; hyperparameters are choices controlling the learning process, such as regularization strength, tree depth, learning rate, or number of neighbors. A search space should be specified before evaluation. Logarithmic scales are appropriate for quantities spanning orders of magnitude, such as a regularization coefficient.

Common strategies include:

  • Grid search: evaluates a predetermined Cartesian grid; transparent but inefficient in high dimensions.
  • Random search: samples configurations and often explores important dimensions more effectively.
  • Bayesian optimization: uses results from previous trials to select promising configurations, often through a surrogate model and acquisition rule.
  • Successive halving or Hyperband: allocates increasing resources to promising trials and stops weak ones early.

The optimization objective must be explicit: metric, direction, resource budget, random seeds, and constraints. Searching many configurations increases selection bias. If hundreds of trials are run, the final test estimate must remain untouched, and nested validation or a second holdout may be necessary.

A model should not be selected solely by the largest point estimate. Consider uncertainty, operational constraints, interpretability, training and inference cost, robustness, calibration, fairness, maintainability, and the consequences of failure.

7. Statistical comparison of models

Suppose two models produce paired predictions for the same test cases. The meaningful object is often the per-case loss difference

di = L(yi,fA(xi)) − L(yi,fB(xi))

A positive mean d̄ indicates lower average loss for model B under this definition. Paired comparisons are more powerful than treating model scores from the same cases as independent.

For classification errors on paired cases, McNemar’s test uses the numbers of cases where A is correct and B is wrong, and vice versa. For repeated cross-validation, corrected procedures or hierarchical models may be needed because scores are correlated. Bootstrap resampling can estimate uncertainty in a metric or difference, but resampling units must respect groups and time. Permutation tests are useful when exchangeability assumptions are plausible.

Statistical significance is not practical significance. A tiny improvement can be statistically detectable in a huge dataset yet operationally irrelevant. Conversely, a meaningful improvement may be uncertain in a small sample. Report effect size, uncertainty interval, sample size, and decision relevance.

8. Reproducible experimentation

A reproducible experiment records:

  1. dataset versions, provenance, inclusion criteria, and split assignments;
  2. preprocessing code and fitted transformations;
  3. model, hyperparameter search space, objective, and stopping rules;
  4. software versions, hardware, random seeds, and environment configuration;
  5. all trials, not only the winning trial;
  6. evaluation scripts, metric definitions, confidence intervals, and subgroup results;
  7. model artifacts, predictions, and a concise experiment report.

Random seeds improve repeatability but do not guarantee identical results across hardware, parallel libraries, or nondeterministic operations. Reproducibility has levels: repeatability within the same environment, reproducibility by another team using the same materials, and replicability under an independently implemented study. Version control, data validation, immutable artifacts, automated tests, and experiment-tracking systems support all three.

A preregistered analysis plan can specify primary metrics, exclusions, subgroup analyses, and stopping rules before results are inspected. This reduces researcher degrees of freedom and makes negative results informative.

9. Case study: hospital readmission risk

The following is an illustrative case.

Background and problem

A hospital wishes to identify patients at high risk of readmission within 30 days. Missing a high-risk patient may reduce the opportunity for follow-up care, while excessive alerts burden clinicians. The dataset contains multiple encounters per patient, laboratory measurements, diagnoses, and discharge information.

Analysis and design

A random row split would leak patient-specific patterns because the same patient could occur in training and test sets. Further, a variable recorded after discharge would not be available at prediction time. The team therefore defines a prediction timestamp, removes post-timestamp variables, and uses a patient-grouped, time-ordered split: earlier patients and encounters for development, later encounters for testing, with no patient overlap.

The baseline is a regularized logistic regression. A gradient-boosted tree is a candidate model. Imputation, encoding, and feature selection are implemented within each training fold. Hyperparameters are tuned with grouped inner cross-validation. The primary metric is recall at a fixed alert capacity of 15% of discharges; secondary metrics are precision, PR–AUC, calibration, subgroup recall, and alert latency.

Approach and outcome

The boosted model has higher PR–AUC, but at the operational threshold its recall is only slightly greater than the baseline and its calibration is poorer. After calibration using development data, the model achieves a more reliable risk interpretation. Subgroup analysis finds lower recall for a language group with sparse documentation. The team adds a missingness audit, consults clinical stakeholders, and declines deployment until the disparity is investigated and the workflow is tested prospectively.

Learning points

The technically highest aggregate score is not automatically the best system. Group-aware splitting prevented leakage; capacity-aware evaluation connected metrics to operations; calibration supported meaningful risk communication; and subgroup analysis exposed a governance issue invisible in the overall score. A prospective monitoring plan is required because prevalence, clinical practice, and documentation may change.

10. Limitations, ethics, and governance

Evaluation results are conditional on the sample, labels, time period, and measurement process. Label noise can make a model appear poor or reward it for reproducing historical decisions. Missing data may encode access barriers. A high-performing model can still be unfair if errors are unevenly distributed or if its use changes the data-generating process.

Governance should address data minimization, lawful use, consent where applicable, privacy, access controls, documentation, auditability, human oversight, contestability, and retention. Protected attributes may be needed for fairness auditing even when they are excluded from prediction; this requires careful legal and ethical review. Fairness criteria can conflict—for example, equal calibration and equal error rates are not generally simultaneously achievable when base rates differ. Therefore, fairness is a sociotechnical decision, not a single metric optimization problem.

Contemporary practice increasingly emphasizes distribution-shift monitoring, conformal prediction for uncertainty sets under stated assumptions, data-centric evaluation, foundation-model benchmarking, and continuous post-deployment evaluation. These methods supplement rather than replace sound splits, clear metrics, and independent testing.

Best-practice checklist

  • Define the deployment population, prediction time, and decision cost.
  • Choose splits that reflect dependence and chronology.
  • Fit every learned preprocessing step inside the training data of each fold.
  • Establish simple, credible baselines.
  • Predefine primary metrics and operational thresholds.
  • Use nested validation when selection is extensive or data are limited.
  • Report uncertainty, subgroup results, calibration, and resource costs.
  • Preserve the test set for final confirmation.
  • Log every experiment and retain unsuccessful trials.
  • Monitor performance, drift, fairness, and calibration after deployment.

Chapter Summary

Model evaluation estimates future performance, while model selection chooses among algorithms and configurations under uncertainty. Train–validation–test designs protect against optimistic conclusions; cross-validation improves data efficiency but must respect groups and time. Metrics should reflect the decision, class balance, costs, probability quality, and operational constraints. Baselines, learning curves, and error analysis reveal whether more capacity, data, or better representation is needed. Hyperparameter optimization must be isolated from final testing, and extensive searching calls for nested validation or an independent holdout. Statistical comparisons should use paired predictions, uncertainty intervals, and practical effect sizes. Reproducible experiments require versioned data, pipelines, environments, seeds, search records, and evaluation protocols. Finally, fairness, privacy, calibration, drift, and human consequences are part of model quality rather than optional additions.

Key Terms

Generalization: Performance on previously unseen cases from the intended population.

Empirical risk: Average observed loss on a finite sample.

Training set: Data used to estimate model parameters.

Validation set: Data used to make development and selection decisions.

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

Data leakage: Unintended use of information unavailable at prediction time.

Cross-validation: Repeated partitioning of data into training and validation folds.

Nested cross-validation: Cross-validation with separate inner selection and outer evaluation loops.

Baseline: A simple reference method against which a proposed model is compared.

Hyperparameter: A learning-process setting selected outside parameter fitting.

Calibration: Agreement between predicted probabilities and observed frequencies.

Precision: Proportion of predicted positives that are truly positive.

Recall: Proportion of actual positives that are detected.

PR–AUC: Area under the precision–recall curve.

Learning curve: Performance as a function of training-set size.

Distribution shift: Change between development and deployment data distributions.

Effect size: Magnitude of a performance difference, independent of significance alone.

Reproducibility: Ability to obtain results again using the documented materials and procedure.

Review Questions

5 Short Answer Questions

  1. Why should the test set remain untouched during hyperparameter tuning?
  2. State one situation in which random splitting is inappropriate.
  3. Distinguish precision from recall.
  4. What does a large training–validation performance gap suggest?
  5. Why is a baseline necessary in model evaluation?

5 Long Answer Questions

  1. Explain the roles of training, validation, and test data, including the conditions under which training and validation data may be combined for final fitting.
  2. Compare accuracy, F1, PR–AUC, log loss, MAE, and RMSE, and identify circumstances in which each may be useful.
  3. Describe nested cross-validation and explain why ordinary cross-validation can produce an optimistic estimate after extensive tuning.
  4. Discuss the sources of data leakage in a clinical or financial prediction system and propose controls.
  5. Explain how reproducibility, statistical uncertainty, and practical significance should shape a model-selection report.

3 Analytical / Application Questions

  1. A fraud dataset has 1% positive cases. A classifier obtains 99% accuracy but detects only 10% of fraud. Which additional metrics and evaluation design would you recommend, and why?
  2. A forecasting model performs well under random five-fold cross-validation but poorly on the next calendar quarter. Diagnose the likely causes and design a more valid experiment.
  3. Two models have mean test losses of 0.42 and 0.40, but their per-case errors are correlated and the difference is uncertain. Design a statistical comparison and explain what evidence would justify deployment.

Multiple Choice Questions with Answers

Question 1

Which partition should be reserved for the final, one-time estimate of generalization?

A. Training set

B. Validation set

C. Test set

D. Feature-selection set

Correct Answer: C. Test set

Question 2

Which metric is usually most informative for a rare-positive classification problem when identifying positives is important?

A. Precision–recall analysis

B. Raw accuracy alone

C. Mean absolute error

D. R-squared

Correct Answer: A. Precision–recall analysis

Question 3

A model has very low training error but much higher validation error. What is the most likely diagnosis?

A. High variance or overfitting

B. Strong underfitting

C. Perfect calibration

D. Data standardization

Correct Answer: A. High variance or overfitting

Question 4

What is the main purpose of nested cross-validation?

A. To eliminate all measurement noise

B. To separate hyperparameter selection from performance estimation

C. To guarantee the best possible model

D. To replace the need for a test metric

Correct Answer: B. To separate hyperparameter selection from performance estimation

Question 5

Which operation must be fitted separately within each training fold to avoid leakage?

A. A fixed mathematical definition of accuracy

B. An imputer that learns feature medians

C. The name of the target variable

D. A deployment hardware specification

Correct Answer: B. An imputer that learns feature medians

Question 6

If TP = 80 and FN = 20, what is recall?

A. 20%

B. 40%

C. 80%

D. 100%

Correct Answer: C. 80%

Question 7

What does calibration assess for a probabilistic classifier?

A. Whether predicted probabilities correspond to observed frequencies

B. Whether the model has the shortest training time

C. Whether all classes have equal size

D. Whether every feature is normally distributed

Correct Answer: A. Whether predicted probabilities correspond to observed frequencies

Question 8

Which split is most appropriate when multiple records belong to the same patient?

A. A row-wise random split allowing patient overlap

B. A group split keeping each patient in one partition

C. A split based only on feature magnitude

D. A split after using all labels for feature selection

Correct Answer: B. A group split keeping each patient in one partition

Question 9

Why can a statistically significant performance improvement still be unsuitable for deployment?

A. Significance always indicates data leakage

B. The improvement may be too small to justify operational cost or risk

C. Statistical tests cannot compare models

D. Significance means the model is uncalibrated

Correct Answer: B. The improvement may be too small to justify operational cost or risk

Question 10

Which practice most directly supports reproducible experimentation?

A. Reporting only the winning trial

B. Changing the evaluation metric after viewing results

C. Versioning data, code, environments, and experiment records

D. Using a test set repeatedly until performance stabilizes

Correct Answer: C. Versioning data, code, environments, and experiment records

Machine Learning
Chapter 7 of 15

Machine Learning

Chapter 8
Unsupervised Learning and Representation

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Unsupervised Learning and Representation

Introduction and Context

Unsupervised learning addresses situations in which observations are available but reliable target labels are absent. Rather than learning a mapping from inputs to known outputs, the learner seeks structure in the data itself: groups of similar observations, lower-dimensional representations, recurring co-occurrences, unusual cases, or latent factors that explain observed variation.

This distinction is important in modern machine learning. A retailer may have millions of customers but no authoritative labels describing their needs. A cybersecurity team may possess network logs but only a small number of confirmed attacks. A biologist may measure thousands of gene-expression variables without knowing which biological mechanisms are active. Unsupervised methods can provide hypotheses, summaries and operational signals, but they do not automatically discover objective truth. Their results depend strongly on representation, distance, scaling, assumptions and evaluation choices.

This chapter develops five connected families of methods:

  1. clustering for discovering groups;
  2. dimensionality reduction for compression and visualization;
  3. manifold learning for nonlinear geometric structure;
  4. association analysis for co-occurrence and rule discovery; and
  5. anomaly detection for identifying observations that differ from a reference population.

A central theme is representation. An algorithm does not operate on an abstract object called “meaning”; it operates on a numerical representation and a similarity or probability model. Consequently, representation learning and evaluation must be treated as first-class design decisions.

Learning Objectives

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

  • distinguish clustering, dimensionality reduction, manifold learning, association analysis and anomaly detection;
  • formulate and interpret common unsupervised-learning objectives;
  • select distance measures, scaling procedures and algorithms appropriate to data type;
  • use PCA, k-means, hierarchical clustering, density-based methods and association rules responsibly;
  • explain the assumptions and limitations of latent representations;
  • evaluate results using internal, external and stability-based evidence;
  • design an end-to-end unsupervised workflow for segmentation or discovery; and
  • identify ethical, governance and operational risks in deploying unsupervised models.

1. A General View of Unsupervised Learning

Let a dataset be represented by a matrix X ∈ ℝⁿˣᵖ, where n is the number of observations and p is the number of measured features. Observation i is the vector xᵢ. In supervised learning, a target y is supplied. In unsupervised learning, the task is to infer a useful structure z, a representation h, a density p(x), or a set of relations from X alone.

The phrase “structure” has several meanings. A cluster is a group whose members are more similar to one another under a chosen criterion than to members of other groups. A latent representation is a lower-dimensional or hidden description that preserves selected information. A density model describes regions of high and low probability. An association rule describes frequent co-occurrence, not necessarily causation.

Representation, similarity and inductive bias

Three choices control most unsupervised outcomes:

  • Representation: Which variables, transformations and encodings describe each observation?
  • Similarity: What does it mean for two observations to be close?
  • Inductive bias: What type of structure is the algorithm encouraged to find?

For numerical vectors, Euclidean distance is

d(xᵢ, xⱼ) = √(∑ₖ₌₁ᵖ (xᵢₖ − xⱼₖ)²)

where xᵢₖ is feature k for observation i. Euclidean distance is meaningful when features are commensurable and differences are appropriately measured. If income is recorded in dollars and age in years, the income variable may dominate unless scaling is applied. For standardized variables, each feature is transformed as

zᵢₖ = (xᵢₖ − μₖ) / σₖ

where μₖ and σₖ are the training-sample mean and standard deviation of feature k. This transformation is appropriate when relative deviations from the mean are meaningful and extreme outliers do not make σₖ misleading. Robust alternatives use the median and interquartile range.

For binary or categorical data, Hamming, Jaccard, Gower or model-based similarities may be preferable. Applying Euclidean distance mechanically to arbitrary categories can create artificial relationships.

2. Clustering

Clustering partitions or organizes observations into groups. It is used for customer segmentation, image organization, document exploration, biological subtype discovery and operational monitoring. Clusters are not inherent facts waiting to be revealed; they are interpretations produced under a particular representation and objective.

2.1 k-means clustering

Given a selected number K of clusters, k-means seeks centroids μ₁,…,μK that minimize within-cluster squared Euclidean distance:

J = ∑ᵢ₌₁ⁿ minⱼ∈{1,…,K} ||xᵢ − μⱼ||²

Here ||·|| is the Euclidean norm. The objective assumes numerical features, meaningful averages and roughly compact, convex groups. The standard algorithm alternates two steps:

  1. assign each observation to its nearest centroid;
  2. replace each centroid with the mean of its assigned observations.

The process stops when assignments or the objective change negligibly. Because initialization affects the result, multiple random starts are recommended. k-means is sensitive to scale, outliers and K. It tends to produce spherical clusters of similar scale even when the data do not have that geometry.

Illustrative calculation. Suppose one-dimensional observations are 2, 3, 8 and 9, and K = 2. Assigning {2, 3} to centroid 2.5 and {8, 9} to centroid 8.5 gives

J = (2 − 2.5)² + (3 − 2.5)² + (8 − 8.5)² + (9 − 8.5)² = 1.

This is a compact partition, but it is not evidence that two natural populations exist. Alternative K values and stability should also be examined.

2.2 k-medoids and hierarchical clustering

k-medoids is similar to k-means but chooses actual observations as representatives, called medoids. Because medoids are less affected by extreme values and can use arbitrary dissimilarities, the method is useful when means are not meaningful.

Hierarchical clustering produces a nested structure rather than requiring one final K immediately. In agglomerative clustering, every observation begins as a separate cluster and the closest clusters are repeatedly merged. Linkage determines distance between clusters:

  • single linkage: minimum pairwise distance;
  • complete linkage: maximum pairwise distance;
  • average linkage: average pairwise distance; and
  • Ward linkage: merge that produces the smallest increase in within-cluster squared error, generally for Euclidean numerical data.

The output is a dendrogram. Cutting it at a chosen height produces a partition. Single linkage can detect elongated chains but is vulnerable to bridging through noise; complete linkage favors compact groups.

2.3 Density-based clustering

Density-based spatial clustering methods identify dense regions separated by sparse regions. DBSCAN uses a radius ε and a minimum number of points minPts. A point is a core point if at least minPts observations lie within its ε-neighborhood. Density-reachable points are assigned to the same cluster, while isolated observations may be labeled noise.

DBSCAN can discover nonconvex shapes and explicitly identify noise. It does not require K, but ε and minPts can be difficult to select, especially when cluster densities differ. HDBSCAN extends the density perspective by constructing a hierarchy and extracting relatively stable density-based clusters.

Gaussian mixture models (GMMs) provide a probabilistic alternative. A K-component mixture models

p(x) = ∑ⱼ₌₁ᴷ πⱼ 𝓝(x | μⱼ, Σⱼ)

where πⱼ ≥ 0, ∑πⱼ = 1, μⱼ is a component mean, Σⱼ is a covariance matrix, and 𝓝 denotes a multivariate Gaussian density. The expectation-maximization algorithm alternates between estimating component-membership probabilities and updating parameters. GMMs supply soft assignments, but their Gaussian and covariance assumptions must be checked.

2.4 Choosing and interpreting clusters

The silhouette coefficient for observation i compares its average distance a(i) to its own cluster with the smallest average distance b(i) to another cluster:

s(i) = [b(i) − a(i)] / max(a(i), b(i))

It lies between −1 and 1 when distances are nonnegative. Larger values indicate better separation relative to cohesion, but the measure favors some geometries and should not be used alone.

The Calinski–Harabasz index compares between-cluster dispersion with within-cluster dispersion; the Davies–Bouldin index summarizes similarity between each cluster and its most similar competitor, with lower values preferred. These are internal metrics: they evaluate structure using the same data and assumptions used to create it.

If domain labels exist after the analysis, adjusted Rand index or normalized mutual information can provide external comparisons. However, agreement with an existing label is not automatically the scientific objective. Stability under resampling, perturbation, alternative preprocessing and alternative algorithms is often more informative than one favorable score.

3. Dimensionality Reduction and Latent Representation

High-dimensional data are difficult to visualize, store, model and interpret. Dimensionality reduction maps x ∈ ℝᵖ to a representation z ∈ ℝᵈ where d < p. The goal may be compression, noise reduction, visualization, computational efficiency or discovery of latent factors. No reduction preserves all relationships simultaneously; every method preserves selected properties.

3.1 Principal component analysis

PCA finds orthogonal directions of maximum variance. After centering the data, let S be the sample covariance matrix. Its eigenvectors v₁,…,vₚ satisfy

S vⱼ = λⱼ vⱼ

where λⱼ is the variance explained by component j. Components are ordered λ₁ ≥ λ₂ ≥ … ≥ 0. The score of observation i on component j is zᵢⱼ = (xᵢ − x̄)ᵀvⱼ, where x̄ is the feature mean vector.

The proportion of variance explained by the first d components is

EV(d) = (∑ⱼ₌₁ᵈ λⱼ) / (∑ⱼ₌₁ᵖ λⱼ)

This ratio is dimensionless. PCA is equivalent to finding the rank-d linear reconstruction with minimum squared reconstruction error under standard centering assumptions. It is not a causal model, and a high-variance direction is not necessarily the most predictive or meaningful direction.

PCA should be performed inside a training pipeline when it precedes a downstream predictive model, particularly if preprocessing parameters could leak information from a test set. Standardization is often appropriate when units differ, but not when absolute scale carries the intended meaning.

3.2 Nonnegative matrix factorization and autoencoders

Nonnegative matrix factorization approximates a nonnegative matrix X by WH, with W and H constrained to be nonnegative. This can produce additive, parts-based representations in text, images and biological data. The solution is generally nonunique and depends on rank, regularization and initialization.

An autoencoder consists of an encoder h = fθ(x) and decoder x̂ = gϕ(h). It minimizes a reconstruction loss such as

L(θ, ϕ) = (1/n) ∑ᵢ₌₁ⁿ ||xᵢ − gϕ(fθ(xᵢ))||²

for continuous data under a squared-error objective. For binary features, cross-entropy may be more appropriate. Bottlenecks, sparsity, denoising objectives or variational constraints can encourage useful representations, but an autoencoder can also learn nuisance variation or memorize training data.

3.3 t-SNE and UMAP

t-SNE constructs a low-dimensional map that attempts to preserve local neighborhood relationships. It is valuable for exploratory visualization but distances between far-separated groups, cluster sizes and apparent gaps can be misleading. Perplexity, initialization and random seed influence results.

UMAP constructs a graph-based representation intended to preserve local and some global structure. It is often faster and more scalable than t-SNE, but its interpretation remains parameter-dependent. Neither method should be treated as proof that visible islands are real clusters. A visualization should be checked against original variables, alternative settings and quantitative stability.

4. Manifold Learning

The manifold hypothesis proposes that high-dimensional observations may lie near a lower-dimensional, smoothly varying structure. For example, images of one object may vary primarily with pose, illumination and viewpoint even though each image has thousands of pixels.

Isomap preserves approximate geodesic distances: distances measured along a neighborhood graph rather than through the ambient space. Locally Linear Embedding reconstructs each point from nearby points and seeks low-dimensional coordinates preserving those reconstruction weights. Laplacian eigenmaps preserve local graph relationships through a graph Laplacian.

Manifold methods depend on neighborhood construction. Too few neighbors can fragment the graph; too many can connect distinct regions and destroy local geometry. Noise, disconnected components, nonuniform sampling and boundaries can distort the result. Manifold learning is therefore best understood as a hypothesis about geometry, not an automatic discovery of intrinsic truth.

5. Association Analysis

Association analysis discovers items or events that occur together. In market-basket data, a transaction T is a set of items drawn from an item universe I. An association rule has the form A → B, where A and B are disjoint itemsets.

Support measures prevalence:

support(A → B) = P(A ∪ B)

In a finite dataset, this is the proportion of transactions containing every item in A ∪ B.

Confidence measures conditional frequency:

confidence(A → B) = P(B | A) = P(A ∪ B) / P(A)

provided P(A) > 0. Confidence can be high simply because B is common. Therefore, lift is often more informative:

lift(A → B) = P(B | A) / P(B) = P(A ∪ B) / [P(A)P(B)]

Lift greater than 1 indicates positive association relative to independence; lift equal to 1 is consistent with independence under the measure; lift below 1 indicates negative association. None of these measures establishes causation.

Apriori exploits the downward-closure property: if an itemset is infrequent, every larger itemset containing it is also infrequent. This prunes candidate search. FP-Growth compresses transactions into an FP-tree and avoids generating many explicit candidates. In large-scale applications, minimum support must balance computational feasibility with business relevance. Statistical significance, multiple testing and temporal confounding should also be considered.

6. Anomaly Detection

An anomaly is an observation that is unusual relative to a reference distribution, operating regime or population. The meaning of unusual is context-dependent: a rare transaction may be fraud, a sensor reading may be a fault, or a legitimate holiday peak may merely be uncommon.

6.1 Statistical and distance-based methods

If a variable is modeled as Gaussian with mean μ and standard deviation σ, a standardized score is

z = (x − μ) / σ

Large absolute values may indicate unusual observations, but the threshold depends on distributional assumptions and the cost of errors. In multivariate settings, the squared Mahalanobis distance is

D² = (x − μ)ᵀΣ⁻¹(x − μ)

where Σ must be invertible or suitably regularized. This distance accounts for correlation and reduces to squared standardized distance in the one-dimensional case. It is unreliable when covariance is poorly estimated, dimensions are high relative to sample size, or the population is multimodal.

6.2 Isolation Forest, LOF and one-class learning

Isolation Forest randomly partitions features and tends to isolate anomalies in fewer splits because they are rare or different. It is computationally useful for tabular data but its score depends on subsampling and feature representation.

Local Outlier Factor compares an observation’s local density with those of its neighbors. It can detect local anomalies that global distance methods miss, although neighborhood size is consequential.

One-class support vector machines estimate a boundary around typical data. Kernel choice, ν and scaling affect the boundary. These methods are generally novelty detectors when trained mostly on normal data; contamination by many anomalies can distort the reference.

6.3 Evaluation

Anomaly labels are often scarce and delayed. Precision, recall and precision–recall curves are useful when positive cases are rare. If a threshold produces TP true positives, FP false positives, FN false negatives and TN true negatives, then

precision = TP / (TP + FP)

and

recall = TP / (TP + FN)

Threshold selection should reflect the relative cost of investigation, missed incidents and customer harm. Time-based validation is essential for streaming systems; random splits can allow future information to influence the past.

7. A Professional Workflow for Unsupervised Projects

A defensible project commonly follows these stages:

  1. Define the decision or discovery question. “Find clusters” is insufficient; specify how the result will be used.
  2. Audit data provenance and quality. Examine missingness, duplicates, leakage, changing definitions and sampling bias.
  3. Choose the unit of analysis. A customer, transaction, session and customer-month can produce different structures.
  4. Engineer and scale representations. Use domain-informed transformations, text or image embeddings where justified, and fit preprocessing only on permitted data.
  5. Establish baselines. Compare simple summaries, random structure and meaningful domain rules.
  6. Fit several plausible methods. Vary K, distance, hyperparameters and random seeds.
  7. Evaluate quantitatively and qualitatively. Use internal metrics, stability, external evidence, visualization and expert review.
  8. Profile and label discovered groups. Describe distributions, not merely centroids; identify actionable differences and uncertainty.
  9. Test operational consequences. Estimate workload, drift, false alarms, fairness and intervention effects.
  10. Monitor and govern. Track data drift, cluster instability, threshold performance, access controls and documentation.

8. Detailed Illustrative Case: Retail Customer Segmentation

This is a fictional illustrative case.

Background and problem

A subscription retailer wants to tailor retention programs. Its data contain twelve months of customer-level purchase frequency, average order value, discount usage, return rate, tenure and customer-service contacts. Marketing requests “four customer segments,” but no verified segment labels exist.

Analysis and approach

The team first defines the unit as one active customer over the previous twelve months and excludes information recorded after the planned campaign date. Monetary variables are log-transformed because of strong right skew. Features are standardized using training-period parameters. Missing service-contact counts are distinguished from unknown values rather than automatically replaced by zero.

The team compares k-means, k-medoids and a Gaussian mixture model for K from 2 through 6. Silhouette values alone favor K = 2, but K = 4 yields more stable profiles under bootstrap resampling and supports distinct operational actions. A two-dimensional PCA plot is used for communication, not as the clustering input alone. The fourth group is small and has high returns and service contacts; the team investigates whether it represents a data-quality artifact before naming it a “high-risk” segment.

Outcome

The resulting profiles are described as behavioral patterns: frequent full-price purchasers, discount-sensitive occasional purchasers, new low-history customers and high-return customers. Marketing receives profile distributions and uncertainty intervals rather than deterministic claims about individual identity. A controlled campaign tests whether segment-specific interventions improve retention without increasing discounts unnecessarily.

Learning points

The project demonstrates that segmentation is not complete when an algorithm returns labels. The unit of analysis, leakage controls, scaling, algorithm comparison, stability, interpretation and intervention testing determine whether the result is useful. It also illustrates that labels such as “valuable” or “risky” can embed managerial judgments and should be documented.

9. Challenges, Ethics and Governance

Unsupervised systems can reproduce historical inequality even without protected attributes. Proxy variables, unequal measurement quality and different population densities may cause one group to be labeled anomalous more often. A “high-risk” cluster can become a self-fulfilling label if it triggers reduced service or surveillance.

Important safeguards include documenting features and exclusions, testing subgroup stability, examining disparate false-positive rates, limiting access to sensitive representations, and providing human review for consequential decisions. Organizations should distinguish exploratory use from automated decision-making and preserve an audit trail of preprocessing, seeds, model versions and threshold changes.

Privacy risks also arise because embeddings can encode sensitive information or permit linkage attacks. Data minimization, retention limits, encryption and purpose limitation remain relevant even when the model is unsupervised. Association rules can reveal private co-occurrences, and anomaly detection can expose legitimate but unusual behavior. Governance must therefore address both model output and downstream use.

10. Contemporary Developments and Best Practices

Representation learning increasingly uses pretrained language, vision and multimodal encoders. These embeddings can make semantic similarity practical, but they inherit training-data bias, may be difficult to interpret and can encode information inappropriate for a particular decision. Before clustering embeddings, practitioners should examine normalization, cosine versus Euclidean similarity, duplicate content and domain shift.

Self-supervised learning creates supervisory signals from the data itself, such as masked-token prediction or contrastive agreement between augmented views. It bridges unsupervised representation learning and later supervised tasks. Generative models provide another route to latent variables, but latent dimensions are not automatically identifiable or semantically disentangled without additional assumptions.

Best practice is therefore not to seek a universally best unsupervised algorithm. It is to align the representation and objective with the decision, quantify uncertainty and stability, compare plausible alternatives, involve domain experts, and validate downstream value.

Chapter Summary

Unsupervised learning discovers structure without supplied target labels. Clustering organizes observations according to a distance, density or probabilistic model; k-means is efficient but favors compact groups, while hierarchical, density-based and mixture methods provide different geometries and uncertainty representations. Dimensionality reduction compresses or visualizes data, with PCA preserving linear variance and manifold methods emphasizing neighborhood geometry. Association analysis measures item co-occurrence through support, confidence and lift, none of which proves causation. Anomaly detection identifies observations unusual relative to a reference population and requires careful thresholding and cost-sensitive evaluation.

Across all methods, representation, scaling, similarity, assumptions and validation determine the result. Internal scores are insufficient: stability, external evidence, domain interpretation and operational outcomes matter. Ethical practice requires attention to privacy, proxy discrimination, subgroup performance, human oversight and documentation.

Key Terms

Representation: Numerical or structured description supplied to a learning algorithm.

Unsupervised learning: Learning patterns from data without target labels.

Distance measure: Function quantifying dissimilarity between observations.

Clustering: Grouping observations according to a chosen similarity criterion.

Centroid: Mean vector representing a cluster in k-means.

Medoid: Actual observation selected as a cluster representative.

Density-based clustering: Clustering based on dense regions separated by sparse regions.

Gaussian mixture model: Probabilistic model representing data as a mixture of Gaussian components.

Silhouette coefficient: Internal measure comparing within-cluster cohesion with nearest-cluster separation.

Dimensionality reduction: Mapping data into fewer dimensions while preserving selected properties.

Principal component analysis: Linear method finding orthogonal maximum-variance directions.

Explained variance: Fraction of total variance represented by selected principal components.

Manifold learning: Learning lower-dimensional nonlinear geometric structure.

Association rule: Statement of the form A → B describing item co-occurrence.

Support: Proportion of records containing an itemset.

Confidence: Conditional frequency of the consequent given the antecedent.

Lift: Association strength relative to independence.

Anomaly detection: Identification of observations unusual relative to a reference population.

Novelty detection: Detecting deviations from a model trained primarily on normal data.

Representation drift: Change in the distribution or meaning of model inputs or embeddings over time.

Review Questions

Short Answer Questions

  1. Why can feature scaling change the result of k-means clustering?
  2. State the difference between support, confidence and lift.
  3. What does PCA optimize under its standard linear formulation?
  4. Why can t-SNE visualizations not by themselves establish the existence of clusters?
  5. Distinguish anomaly detection from novelty detection.

Long Answer Questions

  1. Compare k-means, hierarchical clustering, DBSCAN and Gaussian mixture models in terms of assumptions, strengths and limitations.
  2. Explain PCA mathematically and discuss when standardization is and is not appropriate.
  3. Describe a complete workflow for developing a customer-segmentation system, including validation and governance.
  4. Explain why internal clustering metrics should be supplemented by stability and domain-based evaluation.
  5. Discuss ethical and privacy risks in embedding-based segmentation and anomaly detection.

Analytical / Application Questions

  1. A dataset contains age in years, annual income in dollars and a binary membership indicator. Recommend a representation and similarity strategy before clustering, and justify each preprocessing decision.
  2. An association rule has support 0.08, confidence 0.40 and consequent prevalence 0.20. Compute its lift and interpret the result. What conclusion cannot be drawn from the lift?
  3. An anomaly detector produces 18 true positives, 12 false positives and 30 false negatives. Compute precision and recall, and recommend what additional information is needed before selecting a threshold.

Multiple Choice Questions with Answers

Question 1

Which objective is minimized by standard k-means clustering?

A. Within-cluster sum of squared distances to centroids

B. Between-cluster sum of squared distances to medoids

C. Total entropy of categorical variables

D. Average pairwise covariance between features

Correct Answer: A. Within-cluster sum of squared distances to centroids

Question 2

What does a lift value greater than 1 for an association rule indicate?

A. The antecedent occurs in every transaction

B. The consequent is independent of the antecedent

C. The consequent occurs with the antecedent more often than expected under independence

D. The rule proves that the antecedent causes the consequent

Correct Answer: C. The consequent occurs with the antecedent more often than expected under independence

Question 3

In PCA, the eigenvalue associated with a principal component represents what quantity?

A. The number of observations assigned to a cluster

B. The variance explained by that component

C. The probability that an observation is anomalous

D. The confidence of an association rule

Correct Answer: B. The variance explained by that component

Question 4

Which clustering method can explicitly label sparse observations as noise without requiring the number of clusters in advance?

A. k-means

B. DBSCAN

C. PCA

D. Apriori

Correct Answer: B. DBSCAN

Question 5

If TP = 18 and FP = 12, what is anomaly-detection precision?

A. 0.40

B. 0.50

C. 0.60

D. 0.67

Correct Answer: C. 0.60

Question 6

Why is Euclidean distance often inappropriate for unscaled features measured in very different units?

A. It cannot be computed for numerical data

B. The feature with the largest numerical scale may dominate the distance

C. It always produces negative distances

D. It requires target labels

Correct Answer: B. The feature with the largest numerical scale may dominate the distance

Question 7

Which statement about t-SNE is most accurate?

A. It guarantees that displayed clusters are statistically real

B. It preserves all global distances exactly

C. It is primarily an exploratory visualization method emphasizing local neighborhoods

D. It requires a predefined number of clusters

Correct Answer: C. It is primarily an exploratory visualization method emphasizing local neighborhoods

Question 8

What is the main purpose of bootstrap stability analysis for clustering?

A. To increase the number of features automatically

B. To examine whether discovered assignments persist under data perturbations

C. To convert an unsupervised task into a supervised task

D. To guarantee causal interpretation of clusters

Correct Answer: B. To examine whether discovered assignments persist under data perturbations

Question 9

In the Mahalanobis distance, what role does the covariance matrix play?

A. It converts categorical variables into labels

B. It accounts for feature scale and correlation

C. It determines the number of clusters

D. It measures association-rule confidence

Correct Answer: B. It accounts for feature scale and correlation

Question 10

Which statement is a valid governance principle for unsupervised models used in consequential decisions?

A. Unsupervised models need no fairness testing because they use no labels

B. Embeddings should always be publicly released for transparency

C. Human review, subgroup analysis and documentation should accompany consequential use

D. A high silhouette score proves that deployment is safe

Correct Answer: C. Human review, subgroup analysis and documentation should accompany consequential use

Machine Learning
Chapter 8 of 15

Machine Learning

Chapter 9
Neural Networks and Deep Learning

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Neural Networks and Deep Learning

Introduction and Context

Neural networks are function-approximation systems composed of interconnected computational units. Their modern importance arises from the ability to learn hierarchical representations directly from data: early layers may detect simple patterns, while later layers combine them into increasingly abstract concepts. Deep learning refers principally to the use of networks with many trainable layers, together with large datasets, effective optimization methods, specialized hardware, and representation-learning objectives.

Neural networks are not universally superior to classical machine-learning methods. On small, structured tabular datasets, carefully tuned tree ensembles may outperform deep models. Deep learning is especially valuable when the input contains complex structure—images, speech, language, sequences, graphs, or multimodal observations—and when sufficient data or pretrained representations are available.

Learning Objectives

After studying this chapter, a learner should be able to:

  1. Explain multilayer perceptrons, forward propagation, loss functions, and backpropagation.
  2. Compare activation functions and identify their optimization consequences.
  3. Analyze gradient-based optimization, initialization, normalization, and regularization.
  4. Select convolutional, recurrent, attention-based, and transformer architectures for different data types.
  5. Design an appropriate neural-network workflow for tabular, image, text, and multimodal problems.
  6. Evaluate reliability, fairness, interpretability, computational cost, and deployment risks.

1. The Multilayer Perceptron

A perceptron computes a weighted sum of inputs followed by a threshold or nonlinear transformation. A multilayer perceptron (MLP) stacks affine transformations and nonlinear activation functions. For layer ℓ,

z^(ℓ) = W^(ℓ) a^(ℓ−1) + b^(ℓ), a^(ℓ) = φ^(ℓ)(z^(ℓ))

Here, a^(ℓ−1) is the input activation, W^(ℓ) is a weight matrix, b^(ℓ) is a bias vector, z^(ℓ) is the pre-activation, and φ^(ℓ) is applied elementwise. The input is a^(0) = x. The final activation is selected according to the prediction task.

For binary classification, a common output is the sigmoid function:

σ(z) = 1 ÷ (1 + e^(−z))

It maps a real-valued logit to a number between 0 and 1. For K-class classification, softmax gives

pk = exp(zk) ÷ ∑_(j=1)K exp(zj)

where pk is the predicted probability of class k. Softmax probabilities sum to one. For numerical stability, implementations subtract the largest logit before exponentiation; this leaves the probabilities unchanged.

For regression, the output layer is usually linear, ŷ = z, although transformations may be used when the target has constraints such as positivity.

Expressive Power

A network with at least one hidden layer and a suitable nonconstant activation can approximate broad classes of continuous functions on compact domains. This universal-approximation result does not imply that the network will learn the function efficiently, generalize well, or require only one hidden layer in practice. Depth often represents compositional structure more efficiently than a shallow network.

2. Activation Functions

Activation functions introduce nonlinearity. Without them, a composition of affine transformations remains affine, regardless of the number of layers.

FunctionDefinitionStrengthLimitation
ReLUmax(0,z)Simple, efficient, useful gradients for positive inputsCan become permanently inactive for negative inputs
Leaky ReLUmax(αz,z), small α>0Reduces dead unitsExtra hyperparameter; still nonsmooth
Sigmoid1÷(1+e^(−z))Interpretable probability outputSaturating gradients; not zero-centered
tanh(ez−e^(−z))÷(ez+e^(−z))Zero-centered hidden activationSaturates at both extremes
GELUSmooth gating based on input magnitudeEffective in many transformer modelsMore computationally involved
SoftmaxNormalized exponential vectorMulticlass output probabilitiesNot generally used in hidden layers

The derivative of sigmoid is σ′(z)=σ(z)(1−σ(z)). Its maximum is 1/4, so repeated multiplication through many layers can make gradients very small. ReLU has derivative 1 for positive inputs and 0 for negative inputs (apart from the undefined point at zero, handled by an implementation convention). This helps optimization but can create dead neurons when parameters place most inputs in the negative region.

Activation choice is connected to initialization, normalization, architecture, and task. The output activation must also match the likelihood or target representation: sigmoid for independent binary outcomes, softmax for one mutually exclusive class among K classes, and often a linear output for unconstrained regression.

3. Loss Functions and Probabilistic Interpretation

Training minimizes an empirical objective. Given n examples, a regularized objective can be written as

J(θ) = (1÷n) ∑_(i=1)n ℓ(f_θ(xi), yi) + λ Ω(θ)

where θ contains all parameters, f_θ is the network, ℓ is the per-example loss, λ≥0 controls regularization, and Ω penalizes selected parameters.

For binary classification with label y∈{0,1} and predicted probability p, binary cross-entropy is

ℓ = −[y log(p) + (1−y) log(1−p)]

For one-hot multiclass targets, categorical cross-entropy is

ℓ = −∑_(k=1)K yk log(pk)

Cross-entropy is the negative log-likelihood under Bernoulli or categorical assumptions. With a sigmoid or softmax output, it strongly penalizes confident incorrect predictions. For regression, mean squared error is

MSE = (1÷n) ∑_(i=1)n (yi−ŷi)2

It corresponds to a Gaussian-noise likelihood under a constant-variance assumption. Mean absolute error is more robust to some outliers but has a nondifferentiable point at zero, normally managed with subgradients or a smooth approximation.

4. Backpropagation

Backpropagation efficiently computes derivatives of the loss with respect to every parameter by applying the chain rule from the output layer toward the input. It is not itself an optimization algorithm; it supplies gradients to algorithms such as stochastic gradient descent.

Consider a single example and a two-layer network:

z^(1)=W^(1)x+b^(1), a^(1)=φ(z^(1))
z^(2)=W^(2)a^(1)+b^(2), ŷ=g(z^(2))

If the loss is ℓ(ŷ,y), define δ^(ℓ)=∂ℓ÷∂z^(ℓ). The output-layer error is

δ^(2) = (∂ℓ÷∂ŷ) ⊙ g′(z^(2))

where ⊙ denotes elementwise multiplication. For a hidden layer,

δ^(ℓ) = ((W^(ℓ+1))ᵀ δ^(ℓ+1)) ⊙ φ′(z^(ℓ))

The parameter gradients are

∂ℓ÷∂W^(ℓ) = δ^(ℓ)(a^(ℓ−1))ᵀ, ∂ℓ÷∂b^(ℓ)=δ^(ℓ)

For a minibatch, these quantities are summed or averaged according to the implementation. Matrix dimensions must agree: if a layer has m output units and q input units, W has shape m × q, b has shape m, and the gradient with respect to W has the same shape as W.

For softmax combined with categorical cross-entropy, the derivative with respect to the logits simplifies to

∂ℓ÷∂zk = pk − yk

This simplification is both mathematically useful and numerically more stable than separately differentiating potentially extreme exponential values.

5. Optimization and Training Dynamics

The basic gradient-descent update is

θ_(t+1) = θt − η ∇J(θt)

where η>0 is the learning rate and ∇J is the gradient. Batch gradient descent uses all training examples; stochastic gradient descent uses one; minibatch training uses a subset and is the dominant practical approach.

Momentum accumulates a direction over time. Adaptive methods such as Adam maintain estimates of first and second moments of gradients. Their effectiveness does not remove the need for learning-rate selection, validation, and stability checks. A large learning rate can cause divergence; a small one can make training impractically slow or trap optimization in poor regions.

Important training controls include:

  • Learning-rate schedules: step decay, cosine decay, warm-up, or validation-based reduction.
  • Batch size: affects computational efficiency, gradient noise, and sometimes generalization.
  • Early stopping: stops training when validation performance ceases to improve.
  • Gradient clipping: limits unusually large gradients, especially in recurrent networks.
  • Checkpointing: saves parameters associated with the best validation criterion rather than merely the final iteration.

Initialization

Poor initialization can cause exploding or vanishing activations and gradients. Xavier/Glorot initialization is commonly suited to approximately symmetric activations; He initialization is designed for ReLU-like activations and uses a variance related to the number of incoming units. Exact distributions and scaling depend on fan-in, fan-out, activation, and framework convention. Biases are often initialized at zero, although specialized architectures may use different choices.

Normalization

Batch normalization standardizes intermediate activations using minibatch statistics during training and moving estimates during inference. Layer normalization normalizes across features within an individual example and is particularly compatible with sequence models and transformers. Normalization can improve conditioning, but it changes the optimization and inference behavior; it is not a substitute for sound data preprocessing.

6. Regularization and Generalization

A model generalizes when it performs well on unseen data drawn from the intended population. Deep networks can have many more parameters than training examples yet still generalize, because optimization bias, data structure, augmentation, and regularization constrain the functions actually learned.

Common methods include:

  1. Weight decay: adds a penalty proportional to the squared parameter norm. Under the conventional objective, Ω(θ)=½||θ||²2. It discourages large weights, although decoupled weight decay in optimizers such as AdamW is not identical to adding an L2 penalty in every setting.
  2. Dropout: during training, randomly masks units and rescales retained activations. It reduces reliance on particular paths but may be unnecessary or harmful in some modern architectures.
  3. Data augmentation: creates label-preserving variations, such as crops for images or controlled perturbations for audio. Augmentations must preserve the problem’s semantics.
  4. Label smoothing: replaces hard target probabilities with a less extreme distribution, reducing overconfidence in classification.
  5. Pretraining and transfer learning: initialize a model from a broader dataset, then adapt it to a target task. This can improve accuracy and reduce data requirements, but distribution mismatch can cause negative transfer.

Data splitting must occur before operations that could leak information. For example, normalization statistics, feature selection, augmentation policy design, and pretrained adaptation must not use held-out test information. In temporal or grouped data, random splitting may be invalid; chronological or group-aware splitting is often required.

7. Convolutional Neural Networks

Convolutional neural networks (CNNs) exploit local connectivity and shared parameters. A two-dimensional cross-correlation, commonly called convolution in deep-learning software, is

S(i,j)=∑u ∑v K(u,v) X(i+u,j+v)

where X is an input image or feature map and K is a kernel. Stride controls the displacement between applications; padding controls boundary treatment; multiple kernels produce multiple output channels. For a one-dimensional input of length L, kernel size k, padding p, and stride s, the output length is

⌊(L + 2p − k) ÷ s⌋ + 1

assuming dilation 1 and integer floor operation.

CNNs are effective for images because nearby pixels often have related meaning and the same visual feature may occur at different locations. Pooling or strided convolution reduces spatial resolution, increasing receptive field and reducing computation. Residual connections learn a correction to an identity mapping, helping optimization in deep networks.

CNN limitations include sensitivity to distribution shifts, possible loss of fine spatial information, and substantial data and compute requirements. Vision transformers can replace convolutions by processing image patches with attention, although hybrid designs remain common.

8. Recurrent Networks and Sequential Data

A recurrent neural network maintains a state that evolves over time:

ht = φ(Wx xt + Wh h_(t−1) + b)

where xt is the input at time t and ht is the hidden state. The same parameters are reused across positions, allowing variable-length sequences. Backpropagation through time unfolds the recurrence, but repeated Jacobian multiplication can create vanishing or exploding gradients.

Long short-term memory (LSTM) and gated recurrent unit (GRU) architectures use gates to regulate information flow. LSTM gates include input, forget, and output controls and a cell state. They can model longer dependencies than a basic RNN, but recurrent computation is sequential and therefore less parallelizable than attention.

RNNs remain useful for streaming, low-latency, or resource-constrained tasks. For long-range language modeling and large-scale training, attention and transformers have largely become dominant.

9. Attention and Transformers

Attention allows a representation at one position to selectively combine information from other positions. Scaled dot-product attention is

Attention(Q,K,V) = softmax(QKᵀ ÷ √dk)V

Q, K, and V are query, key, and value matrices; dk is the key-vector dimension. Scaling by √dk prevents dot products from becoming excessively large, which would make softmax gradients small. Multi-head attention projects inputs into several subspaces, performs attention independently, and concatenates the results.

A transformer layer generally contains multi-head attention, a position-wise feed-forward network, residual connections, and normalization. Because attention alone does not encode order, positional information must be supplied through learned positional embeddings, fixed functions, relative positions, or rotary representations.

In an autoregressive decoder, a causal mask prevents position t from attending to future positions. Encoder-only models are well suited to representation and classification tasks; decoder-only models generate sequences; encoder–decoder models map one sequence or modality to another.

Attention has a conventional quadratic cost in sequence length for dense self-attention: approximately proportional to n²d, where n is sequence length and d is representation width. This motivates sparse, linearized, recurrent, retrieval-augmented, and long-context attention methods. Longer context also increases memory, latency, and opportunities for irrelevant or misleading information.

10. Architecture Selection Across Data Modalities

Tabular Data

An MLP can process normalized numerical features and embeddings for categorical variables. Missingness indicators, careful target encoding, and leakage-resistant validation are often more important than increasing depth. Tree-based baselines should be included. Neural additive models, tabular transformers, and entity embeddings can be useful when there are many categories, relational structure, or large-scale data.

Images

CNNs offer strong inductive bias and efficient local processing. Transfer learning from a pretrained vision model is usually preferable to training from scratch unless the dataset is large or the domain is highly specialized. Evaluation should consider class imbalance, calibration, subgroup performance, and whether image acquisition conditions differ between training and deployment.

Text

Tokenization converts text into discrete units, which are mapped to vectors. Transformer encoders support classification and retrieval; decoder models support generation; encoder–decoder models support translation and structured transformation. Fine-tuning, parameter-efficient adaptation, retrieval augmentation, and prompt-based methods should be compared against simpler lexical or linear baselines.

Multimodal Data

A multimodal system may use early fusion (joining features), late fusion (combining predictions), or intermediate fusion (cross-attention between modality representations). Missing modalities, asynchronous timestamps, inconsistent sampling, and spurious correlations are central design concerns. Modality-specific encoders followed by a shared projection space can support contrastive alignment, but similarity in embedding space does not guarantee factual or causal alignment.

11. Worked Example: A Small Classifier

Suppose a binary classifier produces logit z=2. Its predicted probability is

p=1÷(1+e^(−2))≈0.881.

If the true label is y = 1, the binary cross-entropy is

−log(0.881)≈0.127.

For sigmoid output combined with binary cross-entropy, the derivative with respect to the logit is p−y≈−0.119. Thus, gradient descent will tend to increase the logit for this correctly classified but not perfectly confident positive example. If instead y = 0, the loss would be approximately −log(1−0.881)≈2.127, and the derivative would be approximately 0.881, producing a strong update that lowers the logit.

The example illustrates why cross-entropy distinguishes confident correct predictions from confident errors more sharply than an uncalibrated accuracy measure.

12. Applied Case: Illustrative Hospital Imaging System

Background. A hospital seeks to identify pulmonary abnormalities in chest radiographs. The proposed system is illustrative, not a report of a specific institution.

Problem. The dataset contains images from several hospitals, labels produced partly by radiologists and partly by automated report mining, and substantial class imbalance. Images from the same patient may occur more than once.

Analysis. A random image-level split would leak patient-specific patterns and overestimate generalization. Differences in portable versus fixed imaging devices may act as shortcuts. A high area under the ROC curve would not by itself establish clinical usefulness, because threshold selection, calibration, sensitivity at an operational specificity, and subgroup performance matter.

Approach. The team uses patient-level and hospital-aware splits, retains a locked test set, and compares a pretrained CNN with a simpler baseline. It audits labels, records acquisition metadata, uses class-weighted or sampled training where justified, and evaluates calibration and decision curves. A model card documents intended use, contraindications, training population, uncertainty, and known failure modes. The system is positioned as decision support rather than autonomous diagnosis. Human reviewers can override it, and monitoring detects drift in devices, patient populations, and prevalence.

Outcome. The CNN improves sensitivity at the selected operating point, but performance decreases on an external hospital. Calibration is repaired using validation data from the deployment context, and the deployment scope is narrowed until prospective monitoring provides stronger evidence.

Learning points. Architecture is only one part of a reliable system. Dataset provenance, leakage control, external validation, calibration, workflow integration, accountability, and post-deployment monitoring are equally important.

13. Evaluation, Interpretability, and Governance

Accuracy can be misleading for imbalanced tasks. Precision, recall, specificity, F1 score, area under the precision–recall curve, calibration error, and task-specific utility should be selected according to consequences. For regression, MAE, RMSE, calibration of predictive intervals, and subgroup error are useful alternatives.

Interpretability methods include saliency maps, feature occlusion, integrated gradients, attention visualization, counterfactual analysis, and surrogate models. These methods answer different questions and can be unstable or misleading. Attention weights should not automatically be treated as explanations of causation.

Major risks include privacy leakage, memorization, copyright and data-licensing violations, demographic bias, automation bias, adversarial manipulation, insecure model endpoints, and environmental cost. Governance should specify data rights, access controls, retention, documentation, auditability, human oversight, incident response, and criteria for withdrawal. In regulated domains, model development must be aligned with applicable safety, privacy, medical, financial, or employment requirements.

14. Contemporary Directions and Best Practices

Current research includes self-supervised pretraining, foundation models, parameter-efficient fine-tuning, retrieval-augmented generation, diffusion models, mixture-of-experts systems, neural operators, graph neural networks, and efficient on-device inference. These developments increase capability but also intensify concerns about evaluation contamination, reproducibility, compute concentration, and deployment risk.

A disciplined project workflow is:

  1. Define the decision, users, constraints, and failure costs.
  2. Establish leakage-resistant splits and strong non-neural baselines.
  3. Inspect data quality, missingness, imbalance, provenance, and subgroup coverage.
  4. Select an architecture whose inductive bias matches the modality.
  5. Train with reproducible seeds, tracked configurations, validation-based selection, and checkpointing.
  6. Evaluate discrimination, calibration, robustness, fairness, latency, memory, and cost.
  7. Perform external or temporal validation before operational use.
  8. Document limitations and monitor drift after deployment.

Chapter Summary

Neural networks learn compositions of affine transformations and nonlinear activations. Multilayer perceptrons provide a general foundation, while convolutional networks exploit spatial locality, recurrent networks represent sequential state, and attention-based transformers model flexible relationships among positions or modalities. Backpropagation computes gradients through the chain rule; optimizers use those gradients to update parameters. Activation functions, initialization, normalization, learning-rate schedules, and regularization strongly influence training and generalization. Cross-entropy and squared-error losses have probabilistic interpretations under specific assumptions. Architecture selection must reflect data structure, computational constraints, and deployment requirements. Reliable deep learning additionally requires leakage-resistant evaluation, calibration, fairness analysis, security, documentation, human oversight, and monitoring.

Key Terms

Multilayer perceptron: A feed-forward network with one or more hidden layers.

Activation function: A nonlinear function applied to a layer’s pre-activation.

Logit: An unconstrained score before a sigmoid or softmax transformation.

Softmax: A function converting class scores into probabilities that sum to one.

Cross-entropy: Negative log-likelihood loss for probabilistic classification.

Backpropagation: Efficient reverse-mode computation of parameter gradients.

Gradient descent: Optimization by moving opposite the loss gradient.

Learning rate: Step-size parameter controlling each optimization update.

Weight decay: A regularization mechanism discouraging large parameters.

Dropout: Stochastic masking of units during training.

Batch normalization: Normalization using minibatch statistics during training.

Convolution: Local, shared-weight operation producing feature maps.

Receptive field: Input region capable of influencing a unit.

Recurrent neural network: A network whose state depends on prior sequence states.

LSTM: A gated recurrent architecture designed to preserve information over time.

Attention: Weighted combination of representations selected by queries and keys.

Transformer: An architecture built primarily from attention and feed-forward blocks.

Transfer learning: Reusing learned representations for a new task.

Calibration: Agreement between predicted probabilities and observed frequencies.

Data leakage: Unintended use of information unavailable at prediction time.

Review Questions

Short Answer Questions

  1. Why are nonlinear activation functions necessary in a multilayer network?
  2. Distinguish backpropagation from gradient descent.
  3. State two causes of vanishing gradients and one mitigation strategy.
  4. Why does scaled dot-product attention divide by √dk?
  5. Give one reason patient-level splitting is necessary in medical-image evaluation.

Long Answer Questions

  1. Derive the hidden-layer backpropagation equation and explain the dimensions of each term.
  2. Compare sigmoid, tanh, ReLU, and GELU with respect to gradients and typical use.
  3. Explain how regularization, augmentation, early stopping, and transfer learning affect generalization.
  4. Compare CNNs, RNNs, and transformers for image, streaming sequence, and long-context language tasks.
  5. Design a complete evaluation plan for a multimodal prediction system, including leakage control and governance.

Analytical / Application Questions

  1. A classifier has excellent accuracy but poor minority-class recall and unreliable probabilities. Which metrics, training adjustments, and deployment safeguards should be considered?
  2. A transformer performs well on randomly split transaction records but poorly on future months. Diagnose likely causes and propose a revised experimental design.
  3. Design an architecture for combining medical images and clinical notes. Justify the fusion strategy, missing-modality handling, validation split, and human-oversight process.

Multiple Choice Questions with Answers

Question 1

Which expression gives the gradient of sigmoid cross-entropy with respect to a binary logit?

A. p + y

B. p − y

C. y − 1

D. p(1 − p)

Correct Answer: B. p − y

Question 2

What is the primary purpose of nonlinear activations in a multilayer network?

A. To make all outputs probabilities

B. To eliminate the need for data

C. To prevent a composition of layers from collapsing to one affine transformation

D. To guarantee global optimization

Correct Answer: C. To prevent a composition of layers from collapsing to one affine transformation

Question 3

For a one-dimensional input of length 10, kernel size 3, padding 1, and stride 2, what is the output length?

A. 4

B. 5

C. 6

D. 7

Correct Answer: B. 5

Question 4

Which architecture most directly exploits local spatial connectivity and shared visual filters?

A. Convolutional neural network

B. Basic recurrent neural network

C. Linear regression

D. Naive Bayes classifier

Correct Answer: A. Convolutional neural network

Question 5

Why is √dk used in scaled dot-product attention?

A. To force attention weights to be uniform

B. To reduce the magnitude of dot products and improve softmax conditioning

C. To remove positional information

D. To make sequence length constant

Correct Answer: B. To reduce the magnitude of dot products and improve softmax conditioning

Question 6

Which practice is most appropriate for preventing patient-level leakage in an imaging dataset?

A. Randomly split individual image files

B. Use the test set to choose the threshold

C. Split by patient before model selection

D. Duplicate rare images across all splits

Correct Answer: C. Split by patient before model selection

Question 7

Which output activation is generally appropriate for mutually exclusive K-class classification?

A. Linear

B. ReLU

C. Softmax

D. tanh

Correct Answer: C. Softmax

Question 8

What is a defining computational limitation of dense self-attention for sequence length n?

A. It cannot process variable-length inputs

B. Its pairwise attention computation grows approximately quadratically with n

C. It requires a convolutional kernel

D. It cannot use positional information

Correct Answer: B. Its pairwise attention computation grows approximately quadratically with n

Question 9

Which method directly reduces reliance on particular hidden-unit paths during training?

A. Dropout

B. Label encoding

C. One-hot output coding

D. Min–max target definition

Correct Answer: A. Dropout

Question 10

A model predicts p ≈ 0.881 for a positive example. What is its binary cross-entropy approximately?

A. 0.013

B. 0.119

C. 0.881

D. 2.127

Correct Answer: B. 0.119

Machine Learning
Chapter 9 of 15

Machine Learning

Chapter 10
Time-Series and Sequential Machine Learning

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Time-Series and Sequential Machine Learning

Introduction and Context

A time series is an ordered sequence of observations indexed by time: demand recorded hourly, electricity consumption measured every 15 minutes, transactions observed daily, or patient vital signs sampled continuously. Unlike ordinary independent and identically distributed data, time-series observations are usually dependent. Recent values may contain information about future values, and the data-generating process may change because of seasonality, policy, competition, weather, technological adoption, or abnormal events.

Time-series machine learning therefore differs from ordinary supervised learning in three important ways. First, the order of observations must be preserved. Second, predictors must be constructed using only information available at the forecast origin. Third, evaluation must reproduce the way the model will actually be used. Randomly shuffling observations, computing features from the complete dataset, or selecting a model using future information can produce apparently excellent but operationally invalid forecasts.

Forecasting is not limited to predicting a single future value. A system may produce point forecasts, prediction intervals, quantiles, multiple future steps, event probabilities, or decisions such as inventory replenishment. The appropriate method depends on the forecast horizon, data frequency, scale, business loss, structural stability, and availability of external variables.

Learning Objectives

After studying this chapter, the learner should be able to:

  1. Explain trend, seasonality, cycles, autocorrelation, stationarity, and temporal dependence.
  2. Construct leakage-safe lag, rolling, calendar, and exogenous features.
  3. Distinguish statistical forecasting, machine-learning forecasting, and deep sequential models.
  4. Design chronological validation and rolling-origin evaluation.
  5. Select and interpret suitable forecasting metrics and prediction intervals.
  6. Diagnose model failure under drift, shocks, sparse data, and changing seasonality.
  7. Develop a responsible forecasting workflow for professional deployment.

1. Structure of Temporal Data

Let yt denote the observed value at time t. A useful conceptual decomposition is

yt = Tt + St + Ct + Rt

where Tt is trend, St is seasonal structure, Ct represents longer-term cyclical movement, and Rt is the irregular or remainder component. This additive decomposition is appropriate when seasonal fluctuations have approximately constant absolute magnitude. A multiplicative decomposition is more suitable when seasonal amplitude grows with the level:

yt = Tt × St × Ct × Rt

The multiplicative form requires positive or otherwise appropriately transformed observations. Taking logarithms converts multiplication into addition, provided values are positive:

log(yt) = log(Tt) + log(St) + log(Ct) + log(Rt)

The components are descriptive rather than universally identifiable. A gradual rise could be a trend, a portion of a long cycle, or the result of changing explanatory variables. Decomposition should therefore support, not replace, domain analysis.

Important temporal concepts

  • Trend: persistent long-term movement in the level or slope.
  • Seasonality: systematic and repeating variation linked to a known calendar or operational period.
  • Cycle: recurring movement whose period is not necessarily fixed, such as an economic cycle.
  • Autocorrelation: dependence between observations separated by a lag.
  • Lag: a previous observation, such as yt-1.
  • Forecast horizon: the number of future periods being predicted.
  • Forecast origin: the latest time at which information is available when a forecast is produced.
  • Exogenous variable: a predictor external to the target series, such as temperature or price.
  • Stationarity: a stability condition in which relevant statistical properties do not change over time.
  • Concept drift: change in the relationship between predictors and the target.

Autocorrelation and dependence

The lag-k autocorrelation is commonly defined as

ρk = Cov(yt, yt-k) / Var(yt)

where Cov is covariance and Var is variance. This expression assumes a weakly stationary process so that means, variances, and covariances do not depend on calendar time. In practice, the sample autocorrelation is estimated from finite data and may be distorted by trend or seasonality. A large autocorrelation at lag 1 may indicate persistence, but it does not by itself establish causation or guarantee forecastability.

A stationary series has a constant mean, constant finite variance, and autocovariance depending only on the lag rather than the date. Many economic and operational series are not stationary in their raw form. Differencing can remove some nonstationarity:

∆yt = yt − yt-1

Seasonal differencing at period m is

∆m yt = yt − yt-m

Differencing may improve stability, but excessive differencing removes useful signal and increases noise. Stationarity tests, plots, domain knowledge, and residual diagnostics should be considered together; no test should be treated as a mechanical decision rule.

2. Statistical Forecasting Models

Statistical models explicitly represent temporal dependence and often perform strongly with short or moderate data histories.

Baselines

A naïve forecast sets the next forecast equal to the latest observation:

ŷt+h|t = yt

for horizon h. A seasonal naïve forecast uses the most recent value from the corresponding season:

ŷt+h|t = yt+h-mk

where m is the seasonal period and k is chosen so that the referenced observation is available. Baselines are essential: a complex model that cannot outperform a relevant naïve benchmark may not justify its cost.

Moving averages and exponential smoothing

A simple moving average over window w is

ŷt+1|t = (1/w) × ∑i=0w−1 yt−i

It smooths noise but responds slowly to sudden changes. Simple exponential smoothing updates a level estimate recursively:

ℓt = αyt + (1−α)ℓt−1

where 0 < α ≤ 1. A larger α gives more weight to recent observations. Holt’s method adds a trend component, and Holt–Winters methods add seasonality. Additive seasonality is suitable for approximately constant seasonal magnitude; multiplicative seasonality is suitable when magnitude scales with the level.

Autoregressive models

An autoregressive model of order p is

yt = c + φ1yt−1 + φ2yt−2 + … + φpyt−p + εt

where c is an intercept, φi are coefficients, and εt is an error term with conditional mean zero. The model assumes that the selected lag structure captures relevant dependence and that the remaining errors are sufficiently well behaved. Stability conditions depend on the roots of the characteristic polynomial; informally, coefficients must not imply explosive dynamics.

An ARIMA model combines autoregression, differencing, and moving-average errors. It is often written as ARIMA(p,d,q), where p is the autoregressive order, d the number of ordinary differences, and q the moving-average error order. Seasonal ARIMA extends this structure with seasonal terms. These models are interpretable and effective for univariate series, but model identification and maintenance become difficult with many related series or nonlinear effects.

3. Leakage-Safe Temporal Features

Feature engineering is often the strongest determinant of machine-learning forecasting performance. Every feature must be computable at the forecast origin. For a one-step forecast at time t, a feature may use yt and earlier information only if yt is already known when predicting yt+1. For an h-step-ahead forecast, it must not use any value unavailable at the corresponding forecast time.

Lag features

Typical features include yt−1, yt−7, and yt−12 for daily, weekly, and monthly structures, provided those lags are meaningful for the data frequency. Lags should reflect operational mechanisms rather than being added indiscriminately.

Rolling features

A past-only rolling mean with window w is

rt = (1/w) × ∑i=1w yt−i

The current target yt is excluded. In software, this normally means calculating a rolling statistic and then shifting it by one period before using it to predict the current observation. Rolling standard deviation can represent recent volatility, while rolling minimum and maximum can capture local range.

Calendar and event features

Calendar predictors include hour, day of week, month, holiday indicator, pay-cycle position, and school-term status. Cyclic variables should not always be represented as ordinary integers because December and January are close in time. For a periodic variable [[MATH}q_t]] with period [[MATH}P]], a common encoding is

xtsin = sin(2πqt/P), xtcos = cos(2πqt/P)

The two variables preserve circular geometry. Event features should represent only events known at forecasting time. A planned promotion may be known; an unplanned stockout recorded after the fact is not.

Exogenous variables and transformations

Weather forecasts, prices, marketing plans, capacity, and macroeconomic indicators can improve predictions. The model must distinguish observed historical values from future values. If future temperature is unknown, using actual future temperature during evaluation creates optimistic results; the deployed system must use weather forecasts or scenario estimates instead.

Scaling, imputation, target encoding, feature selection, and dimensionality reduction must be fitted within each training window. Fitting them on the complete dataset allows future distributional information to enter the past and constitutes leakage.

4. Machine-Learning Forecasting Strategies

Direct, recursive, and multi-output forecasting

For a forecast horizon [[MATH}H]], a recursive strategy trains one model for one-step prediction and feeds predictions back as future inputs. It is computationally simple but errors can accumulate. A direct strategy trains a separate model for each horizon:

ŷt+h = fh(Xt), h = 1, …, H

It avoids feeding predictions back into the model but requires multiple models. A multi-output model predicts all horizons jointly. It can learn cross-horizon structure but may be more difficult to train and calibrate.

Tree ensembles such as random forests and gradient-boosted trees are effective when lag, rolling, calendar, and exogenous features are informative. They handle nonlinear interactions and mixed feature types, but ordinary trees do not inherently understand time. Temporal structure must be represented explicitly, and extrapolating a long smooth trend can be difficult.

Regularized linear models provide useful transparency. Ridge regression estimates coefficients by minimizing squared error plus an L2 penalty:

min_β { ∑i=1n(yi − xiTβ)2 + λ∑j=1pβj2 }

where [[MATH}λ ≥ 0]] controls shrinkage. The penalty can reduce variance when lag and seasonal features are correlated. The intercept may or may not be penalized according to implementation convention; the convention should be documented.

5. Deep Sequential Models

Deep learning is useful when there are large volumes of data, many related series, complex nonlinear relationships, or rich covariates. It is not automatically superior for small, stable univariate datasets.

Recurrent models

A recurrent neural network maintains a hidden state:

ht = σ(Wxx xt + Whh ht−1 + bh)

where [[MATH}x_t]] is the input, [[MATH}h_t]] is the hidden state, [[MATH}W]] are learned weight matrices, [[MATH}b_h]] is a bias, and [[MATH}σ]] is an activation function. Vanilla RNNs can suffer from vanishing or exploding gradients over long sequences. LSTM and GRU architectures introduce gating mechanisms to preserve or discard information more effectively.

Convolutional and attention-based models

Temporal convolutional networks use causal convolutions, ensuring that output at time [[MATH}t]] does not use future inputs. Dilated convolutions expand the receptive field efficiently. Transformer-style models use attention to assign data-dependent weights to elements of a sequence. They can model long-range relationships, but their computational and data requirements, sensitivity to positional representation, and risk of overfitting must be considered.

Deep forecasting systems may use quantile loss rather than squared error. For quantile level [[MATH}τ ∈ (0,1)]], the pinball loss is

L_τ(y,q) = (τ − I(y < q))(y − q)

where [[MATH}I(·)]] is an indicator and [[MATH}q]] is the predicted quantile. Minimizing expected pinball loss estimates the conditional [[MATH}τ]]-quantile under standard regularity conditions. Multiple quantiles should be checked for crossing, since a higher quantile should not be below a lower quantile.

6. Temporal Validation and Evaluation

Random train-test splits are generally invalid for forecasting because they permit training on observations later than those used for testing. A chronological split trains on an early interval and tests on a later interval. A stronger design uses rolling-origin evaluation:

  1. Select an initial training window.
  2. Fit preprocessing and model only on that window.
  3. Forecast the next validation period.
  4. Move the origin forward.
  5. Repeat and aggregate errors.

An expanding window retains all historical observations; a sliding window retains only the most recent observations. Expanding windows are useful when old data remain relevant. Sliding windows can respond better to drift but may increase variance and discard valuable long-run information.

For actual [[MATH}y_i]] and forecast [[MATH}ŷ_i]], mean absolute error is

MAE = (1/n) × ∑i=1n|yi − ŷi|

and root mean squared error is

RMSE = √[(1/n) × ∑i=1n(yi − ŷi)2]

MAE is in the target’s units and weights errors linearly. RMSE is also in target units but penalizes large errors more strongly. Mean absolute percentage error is

MAPE = (100/n) × ∑i=1n|(yi − ŷi)/yi|

and is undefined when [[MATH}y_i = 0]] and unstable near zero. Weighted absolute percentage metrics or scale-free alternatives may be preferable for intermittent demand.

For an interval [[MATH}[L_i,U_i]]] with nominal coverage [[MATH}1−α]], empirical coverage is

Coverage = (1/n) × ∑i=1n I(Li ≤ yi ≤ Ui)

Coverage should be evaluated together with interval width. A very wide interval can achieve high coverage but provide little decision value. Forecast evaluation should be segmented by horizon, geography, product, regime, and event status rather than reduced to one average score.

7. Forecast Uncertainty and Changing Conditions

A point forecast is incomplete when decisions depend on risk. Uncertainty arises from irreducible randomness, parameter estimation, model choice, future covariates, and structural change. Prediction intervals can be generated from statistical model assumptions, bootstrap procedures, ensembles, conformal methods, or quantile models.

Conformal forecasting constructs prediction sets using calibration residuals or nonconformity scores. Its practical appeal is limited reliance on a parametric error distribution, but validity assumptions are most naturally expressed under exchangeability or related stability conditions. Time dependence and drift can weaken coverage; adaptations such as rolling calibration or weighted conformity scores must be evaluated empirically.

Changing conditions require monitoring. Useful signals include residual mean, residual variance, feature distributions, forecast error by horizon, interval coverage, and business outcomes. A residual sequence should not display systematic autocorrelation if the model has captured the major predictable structure. The Ljung–Box test can assess whether a group of autocorrelations is jointly consistent with white noise, but statistical significance is affected by sample size and repeated monitoring.

Possible responses to drift include shorter training windows, time-varying parameters, retraining schedules, change-point detection, regime-specific models, external covariates, and human override procedures. Retraining should not be triggered solely by a generic data-drift alert; the relevant question is whether predictive performance or decision quality has deteriorated.

8. Worked Example: Daily Retail Demand

Suppose a retailer records daily demand for 28 days. The business predicts demand for the next day and replenishes inventory each evening. Assume the latest observed demand is 120 units, demand seven days earlier was 110 units, and the previous six daily observations are 118, 125, 121, 117, 119, and 123 units.

A one-day naïve forecast is 120 units. A seasonal naïve forecast using a seven-day period is 110 units. A past-only six-day rolling mean is

(118 + 125 + 121 + 117 + 119 + 123) / 6 = 723 / 6 = 120.5 units

Suppose a fitted model produces 122 units. If the actual next-day demand is 130 units, the absolute errors are 10, 20, 9, and 8 units for the naïve, seasonal naïve, rolling-mean, and fitted model respectively. The fitted model performs best for this one observation, but no conclusion should be based on one day. Rolling-origin evaluation might reveal that the model performs poorly during promotions or weekends.

A leakage-safe feature row for forecasting the next day may include the current demand of 120, demand seven days earlier of 110, the past-only rolling mean of 120.5, day-of-week indicators, and a promotion flag known in advance. It must not include next-day actual demand, the next day’s realized weather, or a rolling mean that accidentally includes the target.

If inventory cost is asymmetric, MAE alone may be inadequate. Underage may cause lost sales while overage causes holding or spoilage cost. The retailer should evaluate forecasts using a decision-oriented loss, service-level targets, and stockout rates, while preserving statistical diagnostics.

9. Applied Case: Illustrative Electricity-Load Forecasting

Background

Consider an illustrative regional electricity operator forecasting hourly load 24 hours ahead. Load exhibits daily and weekly seasonality, temperature sensitivity, holidays, and occasional extreme peaks.

Problem

An initial gradient-boosting model achieved low random-split RMSE but failed during summer peaks. Investigation showed that the data pipeline randomly mixed hours and used realized future temperature, which would not be available at prediction time.

Analysis

The team created chronological rolling-origin backtests. Features included lagged load at 1, 24, 48, and 168 hours; past-only rolling averages; hour-of-day and day-of-week cyclic encodings; holiday indicators; and forecast temperature rather than realized temperature. A seasonal naïve benchmark based on load 168 hours earlier was retained. Models were assessed separately for 1–6, 7–12, and 13–24 hour horizons.

Approach and solution

A direct multi-horizon boosted-tree system was compared with exponential smoothing and a recurrent neural network. Preprocessing was fitted independently in each training window. Prediction intervals were generated using rolling residual calibration. The operator selected a model not only by average RMSE but also by peak-load error, interval coverage, and operational cost.

Outcome

The corrected evaluation produced a less impressive but more credible performance estimate. The model reduced peak error relative to the seasonal naïve benchmark while maintaining acceptable coverage. A monitoring dashboard tracked horizon-specific error, temperature forecast error, residual autocorrelation, and coverage. During an unusual heatwave, the system flagged a regime change and invoked a wider-interval policy and expert review.

Learning points

The case demonstrates that leakage control can reduce apparent accuracy while improving real-world validity. Exogenous forecasts must match deployment information. Benchmarks expose whether complexity is useful. Evaluation should reflect high-impact periods, not only average conditions. Finally, uncertainty communication and override governance are part of the forecasting system, not optional additions.

10. Professional, Ethical, and Governance Considerations

Forecasts influence staffing, credit, healthcare capacity, energy dispatch, and public services. A forecast can therefore redistribute risk even when it is statistically accurate on average. Practitioners should document the target definition, forecast origin, data cut-off, horizon, missing-data treatment, model version, training window, and evaluation protocol.

Sensitive variables may create discriminatory effects in sequential decisions. For example, a demand or risk forecast used to allocate service capacity may systematically under-serve a population because historical data reflect unequal access. Fairness analysis should examine errors, coverage, and downstream outcomes across relevant groups, while respecting privacy and data-protection law.

Data provenance, access controls, retention limits, and audit logs are necessary when time series contain personal, location, medical, or financial information. Human oversight is particularly important during unprecedented events, when historical relationships may no longer apply. Forecasts should be presented with uncertainty, assumptions, and known failure modes rather than as unquestionable predictions.

11. Best-Practice Workflow

  1. Define the decision, target, horizon, frequency, and forecast origin.
  2. Establish naïve and seasonal-naïve benchmarks.
  3. Inspect missingness, outliers, aggregation, trend, seasonality, and regime changes.
  4. Build leakage-safe lag, rolling, calendar, and exogenous features.
  5. Choose a model family consistent with data volume, complexity, and interpretability needs.
  6. Use chronological or rolling-origin validation.
  7. Fit all preprocessing inside each training fold.
  8. Evaluate point accuracy, uncertainty, horizon effects, subgroup performance, and business loss.
  9. Diagnose residual dependence and drift.
  10. Deploy monitoring, retraining rules, versioning, documentation, and escalation procedures.

Contemporary Developments

Current forecasting research increasingly addresses global models trained across many related series, probabilistic forecasting, foundation models for sequences, hierarchical reconciliation, causal forecasting, and hybrid systems that combine statistical structure with neural representation learning. These developments are promising but require careful evidence. Large pretrained sequence models may transfer useful representations, yet transfer can fail when the target domain, frequency, measurement process, or decision loss differs from pretraining data. Research-grade evaluation should include strong baselines, realistic information sets, multiple forecast origins, uncertainty assessment, and reproducible data pipelines.

Chapter Summary

Time-series and sequential machine learning model ordered observations in which dependence, seasonality, trend, and changing conditions matter. Decomposition provides a useful vocabulary, while autocorrelation and stationarity clarify temporal structure. Statistical approaches such as naïve benchmarks, exponential smoothing, autoregression, and ARIMA remain valuable, especially with limited data. Machine-learning methods can exploit nonlinear lag, calendar, and exogenous relationships, but they require explicit temporal features. Deep recurrent, convolutional, and attention-based models can represent complex sequences when sufficient data and engineering resources are available.

The central operational principle is leakage prevention: every feature, transformation, and external variable must reflect information available at the forecast origin. Chronological and rolling-origin evaluation provide more credible evidence than random splitting. MAE, RMSE, percentage metrics, quantile loss, interval coverage, and decision-oriented costs measure different aspects of performance. Because relationships change, monitoring, drift adaptation, uncertainty communication, governance, and human review are essential components of a professional forecasting system.

Key Terms

Time series: Ordered observations indexed by time.

Forecast origin: The latest time at which information is available for producing a forecast.

Forecast horizon: Number of future periods being predicted.

Trend: Persistent long-term movement in a series.

Seasonality: Repeating variation associated with a known period.

Cycle: Recurrent movement with a potentially changing or non-fixed period.

Autocorrelation: Dependence between observations separated by a time lag.

Stationarity: Stability of key statistical properties over time.

Lag feature: A previous observation used as a predictor.

Rolling feature: A statistic computed over a past moving window.

Exogenous variable: An external predictor used to explain or forecast the target.

Data leakage: Use of information unavailable at prediction time.

Recursive forecasting: Iterative multi-step forecasting using previous predictions as inputs.

Direct forecasting: Separate forecasting model for each future horizon.

Exponential smoothing: Recursive method assigning greater weight to recent observations.

ARIMA: Autoregressive integrated moving-average model.

Concept drift: Change in the relationship between inputs and target over time.

Prediction interval: Range intended to contain a future observation with specified coverage.

Pinball loss: Asymmetric loss used to estimate conditional quantiles.

Rolling-origin evaluation: Repeated chronological training and testing at advancing forecast origins.

Review Questions

Short Answer Questions

  1. Distinguish trend, seasonality, and cycle in a time series.
  2. Why is random shuffling generally inappropriate for forecasting evaluation?
  3. Define a leakage-safe rolling mean for predicting yt.
  4. State one difference between recursive and direct multi-step forecasting.
  5. Why can MAPE be problematic when actual values are zero or close to zero?

Long Answer Questions

  1. Explain the additive and multiplicative decomposition of a time series, including when each is appropriate.
  2. Compare exponential smoothing, ARIMA, tree-based machine learning, and deep sequential models.
  3. Design a rolling-origin evaluation procedure for a 24-hour-ahead load-forecasting problem.
  4. Discuss how trend, seasonality, exogenous variables, and concept drift affect feature engineering.
  5. Explain why uncertainty intervals and governance are necessary in operational forecasting.

Analytical / Application Questions

  1. A retailer uses a seven-day seasonal lag and a rolling mean that includes the current day’s target. Identify the leakage and specify how the feature pipeline should be corrected.
  2. A model has lower average RMSE than a benchmark but much worse error during demand peaks. Recommend an evaluation and model-selection framework for this business.
  3. Design a forecasting solution for a series with daily seasonality, intermittent zeros, known future promotions, and frequent regime changes. Justify the target transformation, features, validation scheme, metrics, and monitoring plan.

Multiple Choice Questions with Answers

Question 1

Which feature is leakage-safe when predicting yt?

A. A rolling mean including yt

B. The realized value of yt+1

C. A rolling mean of [[MATH:y_{t−1}], y_{t−2}, …]

D. The future realized temperature at time t+1

Correct Answer: C. A rolling mean of [[MATH:y_{t−1}], y_{t−2}, …]

Question 2

What is the main purpose of a seasonal-naïve benchmark?

A. To guarantee the best forecast

B. To provide a relevant simple comparison using the previous season

C. To eliminate all seasonal variation

D. To estimate neural-network weights

Correct Answer: B. To provide a relevant simple comparison using the previous season

Question 3

In the expression ∆yt = yt − yt−1, what operation is performed?

A. Seasonal scaling

B. First-order differencing

C. Exponential smoothing

D. Quantile estimation

Correct Answer: B. First-order differencing

Question 4

Which metric is expressed in the same units as the target and penalizes large errors more strongly than MAE?

A. RMSE

B. MAPE

C. Coverage

D. Pinball-loss level

Correct Answer: A. RMSE

Question 5

Which forecasting strategy trains a separate model for each future horizon?

A. Recursive

B. Direct

C. Seasonal naïve

D. Moving average

Correct Answer: B. Direct

Question 6

For a periodic variable with period P, why are sine and cosine features often used together?

A. They encode circular proximity

B. They remove all missing values

C. They guarantee stationarity

D. They convert forecasts into intervals

Correct Answer: A. They encode circular proximity

Question 7

What does a sliding training window do?

A. Uses only the most recent fixed-length observations

B. Uses every observation from the beginning of time

C. Randomly selects training observations

D. Removes the forecast horizon

Correct Answer: A. Uses only the most recent fixed-length observations

Question 8

A prediction interval has high empirical coverage but is extremely wide. What does this most directly indicate?

A. Perfect calibration and maximum usefulness

B. High coverage with potentially poor sharpness

C. Certain data leakage

D. Zero forecast uncertainty

Correct Answer: B. High coverage with potentially poor sharpness

Question 9

Which condition is central to a standard weakly stationary series?

A. Its mean and variance change every period

B. Its autocovariance depends on calendar date rather than lag

C. Its mean and variance are stable and dependence is lag-based

D. It contains no random component

Correct Answer: C. Its mean and variance are stable and dependence is lag-based

Question 10

Why must realized future weather generally not be used in deployed demand forecasting?

A. Weather is never predictive

B. It is unavailable at the forecast origin

C. It always creates seasonality

D. It prevents calculation of MAE

Correct Answer: B. It is unavailable at the forecast origin

Machine Learning
Chapter 10 of 15

Machine Learning

Chapter 11
Reinforcement Learning and Sequential Decision-Making

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Reinforcement Learning and Sequential Decision-Making

Introduction

Many machine-learning problems involve a fixed dataset in which examples are treated as independent observations. Reinforcement learning (RL) addresses a different class of problem: an agent repeatedly interacts with an environment, chooses actions, receives rewards, and learns a strategy for making better decisions over time. The consequences of an action may be delayed, uncertain, and dependent on later decisions.

RL therefore provides a formal language for problems such as robot control, inventory management, recommendation, dynamic pricing, energy storage, autonomous navigation, network routing, and adaptive resource allocation. Its central challenge is not merely predicting an outcome, but learning which action should be taken now when the value of that action depends on future states and future choices.

This chapter develops the foundations of sequential decision-making, beginning with Markov decision processes (MDPs). It then explains policies, returns, value functions, Bellman equations, dynamic programming, Monte Carlo estimation, temporal-difference learning, Q-learning, SARSA, policy-gradient methods, exploration, reward design, simulation, and operational deployment.

Learning Objectives

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

  1. Formulate a sequential decision problem as a Markov decision process.
  2. Distinguish states, actions, rewards, transitions, policies, returns, and value functions.
  3. Derive and interpret Bellman expectation and optimality equations.
  4. Explain dynamic programming, Monte Carlo learning, and temporal-difference learning.
  5. Compare Q-learning, SARSA, actor–critic, and policy-gradient approaches.
  6. Analyse exploration, reward specification, off-policy learning, and sample efficiency.
  7. Design a simulation-based RL workflow and evaluate operational risks.
  8. Assess ethical, safety, governance, and deployment considerations.

1. Sequential Decision-Making as an MDP

A finite Markov decision process is commonly represented by the tuple (S, A, P, R, γ), where:

  • S is the set of states;
  • A is the set of actions;
  • P(s'|s,a) is the transition probability of reaching state s' after taking action a in state s;
  • R(s,a,s') is the immediate reward; and
  • γ ∈ [0,1) is the discount factor.

The Markov property states that, conditional on the current state and action, the future does not depend on the full history:

P(St+1=s' | St=s, At=a, St-1, At-1, …) = P(St+1=s' | St=s, At=a)

This property is a modelling assumption, not a universal fact. If the current observation omits relevant information—for example, the velocity of a vehicle or the hidden health condition of a machine—the observed process may not be Markov. Such problems may require state augmentation, recurrent models, or a partially observable MDP.

At time t, the agent observes state St, selects action At, receives reward Rt+1, and transitions to St+1. A policy specifies how actions are selected. A stochastic policy is written π(a|s), where π(a|s) = P(At=a|St=s). A deterministic policy is written a = π(s).

The episode may terminate at a terminal state, such as a completed delivery or failed mission. In continuing tasks, discounting is especially useful because it makes the infinite-horizon return finite under bounded rewards. The discount factor expresses both mathematical preference for earlier rewards and, in some applications, economic time preference.

2. Returns and Value Functions

The discounted return from time t is

Gt = Rt+1 + γRt+2 + γ²Rt+3 + … = ∑k=0∞ γk Rt+k+1

Here Gt is the cumulative future reward, Rt+k+1 is the reward received k+1 steps after time t, and γ is the discount factor. For an episodic task, the sum ends at termination. If rewards are bounded by |Rt| ≤ Rmax and γ<1, then the absolute return is bounded by Rmax/(1−γ).

The state-value function for policy π is

V^π(s) = E_π[Gt | St=s]

It measures the expected return from state s when the agent follows π. The action-value function is

Q^π(s,a) = E_π[Gt | St=s, At=a]

It measures the expected return after taking action a initially and subsequently following π. The optimal value functions are

V*(s) = max_π V^π(s)

and

Q*(s,a) = max_π Q^π(s,a)

They satisfy the relationship

V*(s) = maxa Q*(s,a)

Value functions are not probabilities and need not lie between zero and one. Their units are reward units, although discounting may give rewards received at different times unequal effective weight.

3. Bellman Equations

The return can be decomposed into its first reward and the remaining return:

Gt = Rt+1 + γGt+1

Taking conditional expectations gives the Bellman expectation equation for a fixed policy:

V^π(s) = ∑a π(a|s) ∑s' P(s'|s,a)[R(s,a,s') + γV^π(s')]

For continuous state or reward spaces, sums are replaced by integrals or expectations. The corresponding action-value equation is

Q^π(s,a) = ∑s' P(s'|s,a)[R(s,a,s') + γ∑a'π(a'|s')Q^π(s',a')]

The Bellman optimality equation is

V*(s) = maxa ∑s'P(s'|s,a)[R(s,a,s') + γV*(s')]

and its action-value form is

Q*(s,a) = ∑s'P(s'|s,a)[R(s,a,s') + γ maxa'Q*(s',a')]

These equations assume an MDP model and an additive discounted reward objective. They are central because they express a long-horizon decision problem recursively: the value of a choice equals its immediate reward plus the discounted value of the next state.

Worked example: one-step value calculation

Suppose an action from state s leads to state s1 with probability 0.7 and to s2 with probability 0.3. The immediate reward is 4 in either case, V(s1)=10, V(s2)=2, and γ=0.9. The expected backed-up value is

4 + 0.9(0.7×10 + 0.3×2) = 4 + 0.9(7.6) = 10.84.

This is the value estimate for that action under the stated successor values. It is not necessarily the globally optimal value unless the successor values and action choice satisfy the optimality conditions.

4. Planning and Learning Methods

Dynamic programming

If P and R are known, dynamic programming can compute values through iterative updates. Value iteration uses

Vk+1(s) = maxa ∑s'P(s'|s,a)[R(s,a,s') + γVk(s')]

Under standard finite-MDP conditions and γ<1, repeated application converges to V* from bounded initial values. A greedy policy is then selected using the maximizing action. Policy iteration alternates policy evaluation with policy improvement. Dynamic programming can be computationally expensive when the state space is large, but it provides an important theoretical benchmark.

Monte Carlo methods

Monte Carlo (MC) methods estimate values from complete episodes. If an occurrence of state s is followed by return Gt, the value estimate can be updated by

V(s) ← V(s) + α[Gt − V(s)]

where α∈(0,1] is a learning rate. MC methods do not require a transition model and use actual returns, but they must generally wait until the episode ends. They can therefore have high variance and may be unsuitable for very long or continuing tasks.

Temporal-difference learning

Temporal-difference (TD) learning combines sampling with bootstrapping. The TD(0) target and error are

Targett = Rt+1 + γV(St+1)
δt = Rt+1 + γV(St+1) − V(St)

with update

V(St) ← V(St) + αδt

For a terminal next state, the continuation term is conventionally set to zero. TD learning updates after each transition, usually has lower variance than MC estimation, and can learn before an episode terminates. It is biased because its target uses an estimated value, but often has favourable practical speed.

The distinction can be summarized as follows:

MethodRequires model?TargetUpdate timingMain issue
Dynamic programmingYesExpected backed-up valuePlanning sweepsState-space cost
Monte CarloNoComplete sampled returnEnd of episodeHigh variance and delay
TD learningNoReward plus estimated next valueEach transitionBootstrapping bias

5. Q-Learning and SARSA

For discrete actions, a tabular action-value method can learn directly which actions are useful. Q-learning uses the target

Yt = Rt+1 + γ maxa'Q(St+1,a')

and update

Q(St,At) ← Q(St,At) + α[Yt − Q(St,At)]

The terminal-state continuation term is zero. Q-learning is off-policy: the behaviour policy may explore, while the target evaluates the greedy action in the next state. Under finite-state, finite-action conditions, sufficient exploration, suitable learning-rate conditions, and a stationary environment, tabular Q-learning has strong convergence results. Function approximation, non-stationarity, partial observability, and inadequate exploration weaken these guarantees.

SARSA updates using the action actually selected next:

Q(St,At) ← Q(St,At) + α[Rt+1 + γQ(St+1,At+1) − Q(St,At)]

SARSA is on-policy because its target reflects the behaviour policy. This distinction can matter in risky environments. If exploratory actions may lead to severe penalties, SARSA may learn a safer policy than Q-learning because it accounts for the consequences of continued exploration.

Worked Q-learning update

Assume Q(s,a)=5, reward r=2, γ=0.8, and the next-state action values are 6 and 3. With α=0.25, the target is 2+0.8×6=6.8. Therefore,

Qnew = 5 + 0.25(6.8−5) = 5.45.

The update moves the estimate toward the greedy one-step target; it does not replace the estimate completely unless α=1.

6. Exploration and Exploitation

An agent must exploit actions currently believed to be valuable while exploring actions whose value is uncertain. Pure exploitation can lock the agent into a poor policy; unrestricted exploration can be costly or unsafe.

A common strategy is ε-greedy action selection. With probability 1−ε, the agent selects a greedy action; with probability ε, it selects an exploratory action, often uniformly among available actions. Ties among greedy actions should be handled consistently, preferably with random tie-breaking to avoid artificial bias.

Other approaches include:

  • Decaying ε: exploration decreases as learning progresses, although it should not disappear prematurely when uncertainty remains.
  • Softmax selection: actions are sampled according to probabilities proportional to exp(Q(s,a)/τ), where temperature τ>0 controls randomness.
  • Upper-confidence methods: estimated value is augmented by an uncertainty bonus.
  • Optimistic initialization: initially high values encourage trying actions.
  • Count-based or posterior exploration: actions or states with greater uncertainty receive priority.
  • Safe exploration: constraints, risk limits, human approval, or a trusted baseline restrict dangerous actions.

Exploration must be evaluated against operational cost. In an online advertising system, a poor exploratory recommendation may reduce engagement; in a chemical plant, it may create a safety hazard. Exploration in simulation does not automatically make real-world exploration safe because the simulator may omit rare failures or distribution shifts.

7. Policy-Gradient and Actor–Critic Methods

Value-based methods are convenient for small discrete action sets. For continuous actions or highly structured policies, direct policy optimization is often more appropriate. Let π_θ(a|s) be a differentiable policy parameterized by θ. The objective is

J(θ) = Eτ∼π_θ[G0]

where τ denotes a trajectory generated by the policy. The policy-gradient theorem gives, under standard regularity conditions,

∇_θJ(θ) = Eπ_θ[∇_θ log π_θ(At|St) Qπ_θ(St,At)]

A policy-gradient update is approximately

θ ← θ + β ∇_θ log π_θ(At|St) Ât

where β is a policy learning rate and Ât is an estimate of the action advantage or return. Subtracting a state-dependent baseline from the return can reduce variance without changing the expected gradient when the baseline does not depend on the action.

Actor–critic methods maintain an actor, representing the policy, and a critic, estimating a value function. A common advantage estimate is

At ≈ Rt+1 + γV(St+1) − V(St)

The critic supplies a lower-variance learning signal to the actor. Modern deep RL systems often combine neural networks, replay buffers, target networks, entropy regularization, and carefully designed stabilisation methods. These techniques can solve complex tasks but introduce sensitivity to architecture, data distribution, random seeds, reward scaling, and hyperparameters.

8. Reward Design and Objective Specification

The reward function operationalizes what the system is encouraged to do. It is therefore a specification of an objective, not merely a technical detail. Poor reward design can produce reward hacking: behaviour that maximizes the numerical reward while violating the intended purpose.

A reward should be:

  • aligned with the actual organizational or social objective;
  • measurable with acceptable latency and noise;
  • resistant to manipulation;
  • explicit about safety, fairness, and constraints; and
  • tested against foreseeable unintended strategies.

Reward shaping can accelerate learning by adding informative intermediate feedback. Potential-based shaping has a useful policy-preservation property. If Φ(s) is a potential function, define

F(s,a,s') = γΦ(s') − Φ(s)

and add F to the original reward. Under the usual discounted-MDP assumptions and consistent terminal treatment, this form preserves the optimal policy while changing learning signals. Arbitrary reward shaping does not have this guarantee.

In safety-critical systems, constraints should not be hidden solely inside a weighted reward. A constrained formulation may maximize expected return subject to a cost limit:

max_π JR(π) subject to JC(π) ≤ d

where JR is expected task reward, JC is expected safety cost, and d is an acceptable limit. Multi-objective problems require explicit discussion of trade-offs rather than assuming that one scalar reward captures every stakeholder value.

9. Simulation, Evaluation, and Deployment

Simulation is often essential because real-world data collection may be slow, expensive, or dangerous. A credible simulation workflow includes:

  1. defining the state, action, transition, reward, and termination rules;
  2. validating the simulator against historical or experimental data;
  3. modelling uncertainty, delays, constraints, and rare events;
  4. training under multiple seeds and scenarios;
  5. testing on held-out conditions and stress cases; and
  6. transferring cautiously through monitoring and staged deployment.

Offline RL learns from previously collected trajectories without freely interacting with the environment. It is useful when online experimentation is unsafe, but it faces distribution shift: the learned policy may select actions poorly represented in the dataset. Conservative methods, uncertainty estimation, behaviour constraints, and rigorous off-policy evaluation are therefore important.

Evaluation should not rely solely on mean episode return. Relevant measures may include:

  • cumulative reward and discounted return;
  • success rate and completion time;
  • variance and lower-tail performance;
  • constraint violations and near misses;
  • robustness under changed conditions;
  • sample efficiency and computational cost;
  • fairness across users or subpopulations; and
  • comparison with a strong baseline or existing operational policy.

For stochastic policies, confidence intervals and repeated trials are preferable to reporting a single run. A policy that has a slightly lower mean reward but far fewer catastrophic outcomes may be preferable in practice.

10. Applied Case: Warehouse Robot Replenishment

The following is an illustrative case, not a report of a particular company.

Background and problem

A warehouse operates mobile robots that transport inventory from storage locations to picking stations. The system must decide which request to serve next and which route to follow. Delays reduce throughput, while congestion and battery depletion create operational failures.

MDP formulation

A state may contain robot location, battery level, outstanding requests, aisle congestion, time of day, and station queues. Actions may include selecting a request, moving to an adjacent location, waiting, or visiting a charging station. The transition model captures travel time, congestion, battery consumption, and stochastic equipment events.

A possible reward is a throughput benefit minus delay, energy, congestion, and safety costs. However, a single weighted reward can create undesirable behaviour—for example, aggressively pursuing throughput while approaching unsafe battery levels. A safer design adds explicit action constraints, such as prohibiting dispatch when predicted battery reserve is below a validated threshold.

Approach

The team first builds a discrete-event simulator calibrated with historical travel times and queue data. A heuristic dispatch policy provides a baseline. A constrained actor–critic agent is trained in simulation using randomized demand, blocked-aisle events, and sensor noise. Training uses several random seeds and evaluates both average throughput and worst-case congestion.

Before deployment, the learned policy operates in shadow mode: it recommends actions while the existing system remains in control. Recommendations are reviewed for constraint violations, unexpected routing patterns, and performance under peak demand. A staged rollout then limits the number of robots controlled by the policy. A rule-based fallback remains available, and intervention logs are retained for audit.

Outcome and interpretation

Suppose simulation shows a 9% improvement in throughput and a 14% reduction in average delay, but a small increase in energy consumption. The result is not automatically a success. Managers must compare the improvement with electricity cost, battery replacement, safety margins, service-level commitments, and failure-tail behaviour. If gains disappear under unseen congestion patterns, the policy is not robust enough for unrestricted deployment.

Learning points

The case illustrates that RL is a socio-technical decision system. Good performance depends on state representation, reward and constraint design, simulator fidelity, baseline comparison, safe exploration, monitoring, and accountability—not merely on selecting a sophisticated algorithm.

11. Limitations, Governance, and Contemporary Directions

RL remains difficult in high-dimensional, partially observable, and non-stationary environments. The curse of dimensionality makes tabular representation impractical. Neural function approximation can generalize, but it may also extrapolate unpredictably. Long-horizon credit assignment makes it difficult to determine which earlier action caused a later outcome. Sparse rewards can lead to very slow learning.

Other limitations include instability from bootstrapping with approximation, overestimation of action values, sensitivity to reward scaling, and reproducibility problems. Reported improvements may depend on tuning budgets or favourable random seeds. Researchers should disclose environments, preprocessing, evaluation protocols, seeds, baselines, and computational resources.

Governance is particularly important when RL affects people. Systems should have defined authority boundaries, human override, incident response, access controls, privacy protection, and change-management procedures. Decisions that allocate healthcare, credit, employment opportunities, or public resources require fairness analysis and legal review. A reward function should never be treated as a complete substitute for policy, law, or professional judgment.

Contemporary research includes model-based RL, offline and batch RL, multi-agent learning, hierarchical policies, world models, safe and constrained RL, distributional value estimation, causal decision-making, and combinations of RL with foundation models. These directions expand capability but do not remove the need for valid assumptions, reliable evaluation, and operational controls.

Best-Practice Checklist

Before approving an RL system, ask:

  1. Is the state sufficiently Markov, or is important information hidden?
  2. Are rewards, costs, constraints, and terminal conditions specified clearly?
  3. Is there a strong non-RL baseline?
  4. Are exploration and failure modes safe?
  5. Has the simulator been validated and stress-tested?
  6. Has performance been assessed across seeds, subgroups, and distribution shifts?
  7. Are offline-data coverage and extrapolation risks understood?
  8. Are human oversight, rollback, monitoring, and audit mechanisms defined?
  9. Does the deployment objective reflect stakeholder and regulatory requirements?
  10. Can the organization explain why an action was selected and detect degradation?

Chapter Summary

Reinforcement learning studies how an agent learns sequential decisions through interaction with an environment. The Markov decision process formalizes states, actions, transitions, rewards, and discounting. Policies determine action selection, while state-value and action-value functions quantify expected future return. Bellman equations express the recursive structure of these values.

Dynamic programming assumes a known model; Monte Carlo methods learn from complete returns; temporal-difference methods update from sampled transitions and bootstrap from current estimates. Q-learning is an off-policy action-value method, whereas SARSA is on-policy. Policy-gradient and actor–critic methods directly optimize parameterized policies and are especially useful for continuous or complex action spaces.

Effective RL requires careful management of exploration, reward design, constraints, simulation fidelity, evaluation, and deployment risk. Reward hacking, partial observability, distribution shift, approximation error, and unsafe exploration remain serious challenges. In operational applications, technical performance must be considered alongside safety, fairness, accountability, privacy, and governance.

Key Terms

Markov decision process: A mathematical framework for modelling sequential decisions under uncertainty.

State: Information representing the agent’s relevant situation at a decision time.

Action: A choice available to the agent.

Transition probability: The probability of moving to a successor state after an action.

Reward: Immediate scalar feedback associated with a transition or action.

Policy: A rule or probability distribution for selecting actions.

Return: The cumulative, often discounted, future reward.

Discount factor: A parameter controlling the weight assigned to future rewards.

Value function: Expected return from a state under a policy.

Action-value function: Expected return from a state–action pair.

Bellman equation: A recursive equation relating current value to immediate reward and future value.

Dynamic programming: Model-based planning through recursive value computation.

Monte Carlo learning: Estimation from complete sampled episodes.

Temporal-difference learning: Learning from sampled rewards and bootstrapped estimates.

Q-learning: Off-policy learning of optimal action values.

SARSA: On-policy temporal-difference action-value learning.

Exploration: Trying actions to obtain information.

Reward shaping: Modifying rewards to provide more useful learning feedback.

Policy gradient: A method that optimizes parameters of a stochastic policy.

Actor–critic: An architecture combining a policy learner and a value estimator.

Review Questions

Short Answer Questions

  1. What is the Markov property, and why can an inadequate state representation violate it?
  2. Distinguish a policy, a state-value function, and an action-value function.
  3. Why is Q-learning described as an off-policy method?
  4. State one advantage and one limitation of temporal-difference learning.
  5. What is reward hacking?

Long Answer Questions

  1. Derive the Bellman expectation equation for a fixed policy and explain each term.
  2. Compare dynamic programming, Monte Carlo learning, and temporal-difference learning.
  3. Explain the differences between Q-learning and SARSA and discuss when each may be preferable.
  4. Describe how reward design and explicit constraints should be handled in a safety-critical RL system.
  5. Develop a simulation and deployment plan for an RL system used in logistics or energy management.

Analytical / Application Questions

  1. An agent has Q(s,a)=5, receives reward 2, uses γ=0.8, observes next-state action values 6 and 3, and uses α=0.25. Calculate the Q-learning update and interpret it.
  2. Design an MDP for dynamic pricing. Identify states, actions, transitions, rewards, constraints, and at least two possible reward-hacking behaviours.
  3. A policy improves average reward but increases the frequency of rare catastrophic failures. Propose an evaluation framework and a deployment decision, explaining which metrics and safeguards you would require.

Multiple Choice Questions with Answers

Question 1

Which property defines a Markov decision process?

A. The next state depends only on the complete historical sequence

B. The future is conditionally independent of history given the current state and action

C. All rewards must be positive

D. The agent must use a deterministic policy

Correct Answer: B. The future is conditionally independent of history given the current state and action

Question 2

If the discount factor is zero, which objective is optimized?

A. Only the immediate next reward

B. The undiscounted infinite-horizon return

C. Only the final terminal reward

D. The variance of future rewards

Correct Answer: A. Only the immediate next reward

Question 3

Which equation represents the TD(0) value update?

A. V(S_t) ← V(S_t) + α[R_{t+1} + γV(S_{t+1}) − V(S_t)]

B. V(S_t) ← V(S_t) + α[R_{t+1} − γV(S_{t+1})]

C. V(S_t) ← max_a V(S_t)

D. V(S_t) ← R_{t+1}/γ

Correct Answer: A. V(S_t) ← V(S_t) + α[R_{t+1} + γV(S_{t+1}) − V(S_t)]

Question 4

Why is Q-learning off-policy?

A. It cannot use rewards

B. Its target evaluates a greedy next action even when behaviour explores

C. It requires a known transition model

D. It updates only after an episode ends

Correct Answer: B. Its target evaluates a greedy next action even when behaviour explores

Question 5

In the worked Q-learning example, what is the updated value when Q=5, reward=2, γ=0.8, max next Q=6, and α=0.25?

A. 5.20

B. 5.45

C. 6.80

D. 7.25

Correct Answer: B. 5.45

Question 6

What is the principal trade-off addressed by ε-greedy action selection?

A. Bias versus dimensionality

B. Exploration versus exploitation

C. Reward versus transition modelling

D. Variance versus observability

Correct Answer: B. Exploration versus exploitation

Question 7

Which reward-shaping form is associated with preserving the optimal policy under standard assumptions?

A. Φ(s)+Φ(s')

B. γΦ(s')−Φ(s)

C. γΦ(s)+Φ(s')

D. Φ(s)/γ

Correct Answer: B. γΦ(s')−Φ(s)

Question 8

What does SARSA use in its temporal-difference target?

A. The maximum possible next action value only

B. The action actually selected under the behaviour policy

C. The initial state value

D. A model-derived expectation over all complete episodes

Correct Answer: B. The action actually selected under the behaviour policy

Question 9

Which problem is especially associated with offline reinforcement learning?

A. The absence of any historical data

B. Distribution shift when the learned policy selects poorly represented actions

C. The impossibility of using constraints

D. The requirement that all actions be continuous

Correct Answer: B. Distribution shift when the learned policy selects poorly represented actions

Question 10

Why should an RL deployment be evaluated beyond mean episode reward?

A. Mean reward is always mathematically invalid

B. Operational safety, fairness, robustness, and rare failures may not be reflected by the mean

C. Episode reward cannot be measured in simulation

D. Value functions never use rewards

Correct Answer: B. Operational safety, fairness, robustness, and rare failures may not be reflected by the mean

Machine Learning
Chapter 11 of 15

Machine Learning

Chapter 12
Explainability, Causal Analysis and Decision Analytics

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Explainability, Causal Analysis and Decision Analytics

Introduction and Context

Machine-learning systems increasingly influence credit approval, medical triage, industrial maintenance, recruitment, pricing and public-sector resource allocation. High predictive accuracy is valuable, but accuracy alone does not answer several managerial and scientific questions:

  • Why did the model produce this prediction?
  • Does a feature merely correlate with an outcome, or does changing it cause the outcome to change?
  • How certain is the prediction?
  • What action should a manager take, given costs, constraints and competing objectives?

Explainability, causal analysis and decision analytics address these questions from related but distinct perspectives. Explainability studies how a model generates or is associated with predictions. Causal analysis studies whether interventions change outcomes. Decision analytics combines predictions, uncertainty, feasible actions and preferences to support decisions.

A central discipline is to avoid conflating prediction with causation. A model may accurately predict employee attrition using commute distance, for example, without implying that reducing commute distance will prevent every resignation. Similarly, a feature-importance ranking may describe model behaviour without identifying the true drivers of the phenomenon.

Learning Objectives

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

  1. Distinguish global and local explanations and select appropriate methods.
  2. Interpret permutation importance, partial dependence, ICE, SHAP and counterfactual explanations.
  3. Explain why association in predictive models does not establish causation.
  4. Formulate causal questions using treatment, outcome, confounders and counterfactuals.
  5. Connect predictive distributions and uncertainty with optimization and managerial utility.
  6. Evaluate explanations for fidelity, stability, fairness, actionability and governance.
  7. Design an interpretable decision workflow for a realistic organizational problem.

1. A Conceptual Architecture

Let X denote observed features, Y an outcome, and f(X) a fitted prediction function. In supervised learning, the objective is commonly to minimize expected predictive loss:

R(f) = E[L(Y, f(X))]

Here, L is a loss function and E denotes expectation over the data-generating distribution. This objective concerns prediction, not explanation or intervention.

An explanation can concern either the model or the real-world phenomenon. A model explanation asks, “Which inputs were influential for f?” A causal explanation asks, “What would happen to Y if an intervention changed X?” These are different estimands. Decision analytics adds an action a and a utility U:

π*(x) = arg maxa ∈ A(x) E[U(a, Y) | X = x]

where A(x) is the set of feasible actions for case x. The optimal policy π* depends not only on predicted outcomes but also on consequences, costs, constraints and preferences. If action changes the outcome, the relevant quantity should generally be an intervention-based prediction such as E[Y | do(a), X = x], rather than merely E[Y | X = x].

Important distinctions

QuestionObject of interestTypical method
What does the model use?Prediction function fFeature importance, SHAP
How does prediction vary with a feature?Response surfacePDP, ICE
What small change alters this decision?Alternative inputCounterfactual explanation
What causes the outcome?Intervention effectCausal design and identification
What should be done?Action and utilityDecision analysis, optimization

2. Global and Local Explainability

A global explanation summarizes behaviour across a population. It can reveal dominant variables, nonlinearities, interactions and regions where a model is unreliable. A local explanation describes one observation, such as why a loan application was declined or why a machine was assigned high failure risk.

Global explanations are useful for model development, monitoring and policy review. Local explanations support case management, contestability and human oversight. Neither is automatically truthful: a global average can conceal subgroup differences, while a local approximation can be unstable or misleading away from the observed point.

2.1 Feature importance

For a tree model, impurity-based importance aggregates reductions in a split criterion attributable to a feature. Such importance can be biased toward high-cardinality or continuous variables and can distribute credit unpredictably among correlated predictors.

Permutation importance measures the change in performance after randomly permuting one feature in a validation set. If M is a performance score, the importance of feature j can be estimated as:

Ij = M(D) − M(permutej(D))

D is held-out data and higher values indicate greater loss of performance after permutation when M is higher-is-better. For a loss metric where lower is better, the sign and interpretation must be adjusted. The procedure assumes that permutation produces a meaningful disruption; correlated features violate this assumption because information may remain available through correlated variables.

Feature importance is not a causal effect. A postcode may be important because it proxies for income, access or historical institutional practices. Removing it from a model does not necessarily remove the underlying proxy information.

2.2 Partial dependence

The partial-dependence function for feature j is:

PDj(z) = E_{X−j}[f(z, X−j)]

X−j denotes all features other than j, and z is a selected value of feature j. In a sample of n observations, it is estimated by:

PD̂j(z) = (1/n) ∑i=1n f(z, xi,−j)

This replaces feature j by z for every observation and averages predictions. It is useful for identifying average nonlinearity, thresholds and saturation. For classification, the plotted quantity should be specified: probability, log-odds or a raw score are not interchangeable.

PDPs can create unrealistic combinations when features are correlated. For instance, setting pregnancy status and age combinations independently may produce impossible or rare records. Conditional or accumulated local effects can reduce some problems, but they require careful interpretation.

2.3 ICE and interactions

An individual conditional expectation (ICE) curve retains one curve per observation:

ICEi(z) = f(z, xi,−j)

ICE reveals heterogeneity hidden by a population average. Curves that cross indicate that the feature has different effects across cases, often because of interactions. A two-feature PDP, PDj,k(z,w), can visualize average interaction, but correlated features and extrapolation remain concerns.

3. SHAP and Additive Attributions

SHAP, based on Shapley values from cooperative game theory, allocates a prediction among features relative to a baseline. For an observation x:

f(x) = φ0 + ∑j=1p φj

Here, φ0 is the expected model output under a specified background distribution, p is the number of features, and φj is feature j’s contribution. The decomposition is exact for the chosen model output and background definition, subject to the SHAP implementation and feature-dependence assumptions.

For a set of features S, let v(S) be the model value when only features in S are known. The Shapley value is:

φj = ∑_{S ⊆ N\{j}} [|S|!(p−|S|−1)!/p!] [v(S ∪ {j}) − v(S)]

N is the set of all features. The coefficient averages the marginal contribution of feature j across all possible feature-ordering contexts. Exact calculation is exponential in p, so practical algorithms use model-specific or sampling-based approximations.

SHAP explanations are additive, which supports global summaries based on mean absolute attribution:

GlobalImportancej = (1/n) ∑i=1n |φij|

This measures average contribution magnitude, not direction or causality. A feature may have a high mean absolute value while increasing predictions for some cases and decreasing them for others. Explanations should therefore inspect distributions and dependence plots, not only rankings.

The background distribution is consequential. Interventional SHAP treats features as separable under intervention, whereas conditional approaches account for dependence among features. Neither interpretation is universally correct; the choice must match the scientific question. Correlated variables may share or compete for attribution, so a precise-looking SHAP number should not be overinterpreted.

4. Counterfactual Explanations

A counterfactual explanation describes a minimally changed input that would produce a different decision. If x is the original case and x′ a candidate alternative, a common formulation is:

minx′ d(x, x′) + λ·L(f(x′), y*)

subject to feasibility constraints, where d measures change cost, y* is the desired outcome, L penalizes failure to reach it, and λ controls the trade-off. Features may be continuous, ordinal, categorical or immutable. A valid counterfactual must respect domain constraints such as age non-decrease, valid category combinations and affordability.

Counterfactuals are not automatically causal. “Increase income by £5,000” may be a model-valid change but not a feasible or sufficient intervention. Actionability requires identifying who can change the feature, at what cost, over what time horizon, and whether the relationship is stable.

A robust counterfactual workflow should:

  1. State the decision boundary and desired outcome.
  2. Separate actionable from immutable variables.
  3. Encode feasibility and legal constraints.
  4. Penalize implausibly large or costly changes.
  5. Generate multiple diverse alternatives where appropriate.
  6. Test whether the explanation remains valid under model uncertainty and perturbation.

5. Prediction Is Not Causation

A predictive relationship estimates association, often represented by E[Y | X = x]. A causal question concerns a potential outcome under intervention. For a binary treatment T, define Y(1) as the outcome if treated and Y(0) if untreated. The individual treatment effect is:

τi = Yi(1) − Yi(0)

Only one potential outcome is observed for each person. The average treatment effect is:

ATE = E[Y(1) − Y(0)]

Under standard identification assumptions, observational data may estimate an adjusted effect. One common assumption is conditional exchangeability:

Y(1), Y(0) ⟂ T | X

This states that, conditional on measured covariates X, treatment assignment is independent of potential outcomes. It is not testable from observational data alone. Identification also requires positivity, 0 < P(T = 1 | X = x) < 1 for relevant x, and consistency, meaning the observed treatment corresponds to the defined intervention.

A causal graph can clarify confounding. Suppose socioeconomic status affects both education and income. Education and income may be associated even if the desired intervention is not adequately represented by the observed variables. Conditioning on a mediator or collider can also bias an estimate. Therefore, adjustment sets should be selected using substantive knowledge and a defensible causal diagram, not by automatically including every available feature.

Causal methods

Randomized experiments support causal inference because random assignment breaks systematic treatment-outcome confounding in expectation. When experiments are infeasible, researchers may use matching, inverse-probability weighting, regression adjustment, instrumental variables, regression discontinuity, difference-in-differences or synthetic controls. Each method has assumptions that must be argued, assessed and reported.

Machine learning can assist causal analysis through flexible nuisance-function estimation, heterogeneous treatment-effect modeling and causal forests. Flexibility does not eliminate identification assumptions. A powerful model trained on biased observational data cannot recover a causal effect without a credible design.

6. Uncertainty and Reliable Interpretation

A point prediction hides uncertainty. For a predicted quantity ŷ, distinguish:

  • Aleatoric uncertainty: irreducible variability in outcomes.
  • Epistemic uncertainty: uncertainty due to limited data or model knowledge.
  • Distribution shift: uncertainty caused by deployment conditions differing from training conditions.

For a regression model, a predictive interval concerns a future observation, whereas a confidence interval concerns an estimated parameter or mean response. They should not be conflated. Classification probabilities should be calibrated: among cases assigned probability 0.7, approximately 70% should experience the event over a suitable evaluation population and time period.

Explanations also have uncertainty. Bootstrap resampling, repeated training, subgroup analysis and sensitivity to background data can reveal whether an importance ranking or SHAP attribution is stable. A feature that appears important in one sample but not across resamples should not drive a major policy without further investigation.

Calibration can be assessed with reliability diagrams and proper scoring rules. For binary outcomes, the Brier score is:

BS = (1/n) ∑i=1n (pi − yi)2

where pi is the predicted probability and yi ∈ {0,1}. Lower values are better, although the score combines calibration and discrimination. Evaluation should also examine subgroup calibration, false-positive and false-negative costs, and performance under temporal or geographic shift.

7. From Model Output to Decision Analytics

A predictive score is not a decision rule. A decision rule requires an action space, constraints and a value model. For a binary intervention with benefit B, intervention cost C, and event probability p, a simple expected net benefit is:

ENB(a) = p·B − C

The intervention is preferred to doing nothing when p·B − C > 0, or equivalently when p > C/B, assuming B and C are positive, known and expressed in comparable units. In practice, benefits may be delayed, uncertain or distributed unevenly, and actions may have capacity constraints.

For multiple periods, discounted net present value can be represented as:

NPV = ∑t=0T [Bt − Ct]/(1+r)t

Bt and Ct are benefits and costs at time t, r is the discount rate per period, and T is the planning horizon. The formula assumes commensurable monetary values and a chosen discounting convention.

Optimization may allocate limited interventions. If xi indicates whether case i receives an intervention, a basic formulation is:

max ∑i=1n xi·(piBi − Ci)

subject to:

∑i=1n xi ≤ K, xi ∈ {0,1}

K is available capacity. This formulation is illustrative; real systems may require fairness constraints, staffing schedules, risk limits, minimum service levels and uncertainty-aware robust optimization.

The explanation should match the decision. If a manager needs to know which cases are high risk, global and local predictive explanations may suffice. If the manager must choose an intervention, treatment-effect estimates and action costs are more relevant. If the decision affects rights or access, explanations must also support due process, contestability and legal review.

8. Illustrative Case: Preventive Maintenance in a Manufacturing Plant

Background

This fictional case concerns a plant operating 400 industrial pumps. A gradient-boosted model predicts whether a pump will fail within 30 days using vibration, temperature, operating hours, pressure and maintenance history.

Problem

The maintenance team has capacity to inspect only 40 pumps weekly. Managers need to know which pumps to prioritize, why the model assigns risk, whether a proposed inspection is likely to prevent failure, and whether the allocation is fair across production lines.

Analysis

The model’s global permutation importance ranks vibration and temperature highly. A PDP shows that predicted risk rises sharply above a vibration threshold, while ICE curves reveal that the threshold is much lower for pumps operating at high pressure. This indicates an interaction that a single global curve might hide.

For one pump, the baseline predicted failure probability is 0.18. SHAP analysis in probability or an appropriately specified transformed output shows that unusually high vibration and recent pressure spikes increase the prediction, while a recent bearing replacement decreases it. The explanation describes model behaviour; it does not prove that replacing the bearing caused the lower risk.

A counterfactual search proposes reducing vibration below the model threshold. Engineers reject the proposal as incomplete because vibration cannot be directly controlled; it is a symptom. A feasible counterfactual instead recommends inspection of the coupling and alignment, with an estimated cost and downtime.

Decision approach

Suppose pump i has failure probability pi, avoided failure cost Bi, inspection cost Ci, and inspection capacity K = 40. The team ranks pumps by estimated net benefit piBi − Ci, not by risk alone. If an inspection changes future failure probability, the benefit should be based on a causal or engineering estimate of the intervention effect, not simply on the predictive risk.

The team validates calibration by production line and month, audits false negatives, and reports uncertainty intervals from repeated temporal validation. It also checks whether the model relies on maintenance codes that are entered differently across lines. A governance committee requires a human engineer to approve high-impact interventions and records the reason, evidence and outcome.

Outcome

The combined workflow reduces emergency failures in a pilot period, but the organization does not claim that the model alone caused the improvement. The evaluation compares the policy with a pre-specified baseline and examines operational changes, inspection compliance and seasonal effects. The resulting system is a decision-support tool rather than an autonomous maintenance authority.

Learning points

  1. Feature importance identifies predictive reliance, not causal leverage.
  2. ICE can reveal interactions hidden by PDP averages.
  3. Counterfactuals must be physically and operationally feasible.
  4. Capacity and costs transform prediction into prioritization.
  5. Monitoring and evaluation must include calibration, drift and intervention outcomes.

9. Limitations, Ethics and Governance

Explainability methods can produce plausible but inaccurate narratives. Post-hoc explanations may approximate a complex model locally without faithfully representing its internal computation. Different background samples, model versions or correlated-feature treatments can produce different explanations. Explanations may also expose sensitive information or enable strategic gaming.

Causal analysis faces unmeasured confounding, treatment heterogeneity, interference between units and changing policies. A causal estimate is conditional on its estimand, population, intervention definition and assumptions. Claims should therefore distinguish “associated with,” “predicts,” “estimated effect,” and “causes.”

Fairness requires more than removing protected attributes. Proxies may remain, historical labels may encode unequal treatment, and fairness criteria can conflict. Calibration, equalized error rates, equal opportunity, demographic parity and individual notions of fairness answer different questions and may be mutually incompatible under unequal base rates. The selected criterion must be justified by the application and legal context.

Good governance includes:

  • documented data provenance, target definitions and feature engineering;
  • pre-deployment validation and subgroup analysis;
  • versioned models, explanation settings and background data;
  • uncertainty reporting and drift monitoring;
  • human review for consequential decisions;
  • appeal, correction and recourse mechanisms;
  • access controls for sensitive explanations;
  • post-deployment outcome and impact evaluation.

Explanations should be meaningful to their audience. A data scientist may inspect SHAP distributions, while a customer may require a concise, truthful reason and an actionable recourse path. Simplicity should not be purchased by hiding uncertainty or making causal claims the evidence cannot support.

10. Contemporary Directions and Best Practice

Current research increasingly combines predictive modeling with causal representation, heterogeneous treatment effects, distributionally robust learning and uncertainty-aware optimization. Large foundation models are also being used to generate explanations, but natural-language fluency is not evidence of faithfulness. Generated explanations should be checked against model outputs, perturbation tests and domain review.

A robust professional workflow is:

  1. Define the scientific or managerial question.
  2. Separate prediction, explanation, causal effect and decision objectives.
  3. Establish the deployment population, time horizon and action constraints.
  4. Validate predictive performance and calibration on suitable held-out data.
  5. Use complementary global and local explanation methods.
  6. Test sensitivity to correlated features, sampling, model versions and distribution shift.
  7. Specify causal assumptions before estimating intervention effects.
  8. Translate probabilities into utility-aware policies with explicit costs.
  9. Evaluate subgroup impacts, recourse and operational feasibility.
  10. Monitor outcomes and revise the system when evidence changes.

Chapter Summary

Explainability clarifies how a predictive model behaves, while causal analysis addresses the effects of interventions. Global tools such as permutation importance, partial dependence and aggregate SHAP summaries describe population-level behaviour; local tools such as ICE, individual SHAP values and counterfactuals explain particular predictions. Each method has assumptions and failure modes, especially with correlated features and unrealistic data combinations.

Prediction does not establish causation. Causal claims require a defined intervention, potential outcomes, identification assumptions and an appropriate research design. Uncertainty applies both to predictions and to explanations, and should be assessed through calibration, resampling, subgroup analysis and shift monitoring. Decision analytics connects predictions with actions by incorporating utility, costs, capacity and constraints. Responsible deployment requires technical validation, causal humility, fairness analysis, governance, contestability and ongoing evaluation.

Key Terms

Explainability: The study and communication of how a model produces or is associated with its outputs.

Global explanation: A summary of model behaviour across a population.

Local explanation: An explanation for one observation or decision.

Permutation importance: Performance change caused by permuting one feature in evaluation data.

Partial dependence: Average model prediction as a feature is varied while averaging over other features.

ICE: Individual conditional expectation showing a separate response curve for each observation.

SHAP value: An additive attribution based on Shapley-value principles.

Background distribution: Reference data used to define the baseline and missing-feature expectations in an explanation.

Counterfactual explanation: A feasible alternative input that would change a model output or decision.

Actionability: The extent to which a proposed change can realistically be made.

Association: A statistical relationship between variables.

Causation: A relationship in which an intervention changes an outcome.

Potential outcome: The outcome that would occur under a specified treatment or intervention.

ATE: Average treatment effect across a defined population.

Confounder: A variable influencing both treatment and outcome.

Positivity: The condition that each relevant treatment has positive probability for covariate profiles.

Calibration: Agreement between predicted probabilities and observed frequencies.

Aleatoric uncertainty: Irreducible variability in outcomes.

Epistemic uncertainty: Uncertainty arising from limited knowledge or data.

Decision analytics: The integration of predictions, uncertainty, actions, constraints and utility for decision support.

Review Questions

Short Answer Questions

  1. Distinguish global from local explainability.
  2. Why can permutation importance be misleading with correlated features?
  3. What does the baseline term φ0 represent in a SHAP decomposition?
  4. State two assumptions commonly required for causal identification from observational data.
  5. Why is a predictive probability not, by itself, a decision rule?

Long Answer Questions

  1. Compare feature importance, partial dependence, ICE and SHAP with respect to purpose, interpretation and limitations.
  2. Explain why a counterfactual explanation may be model-valid but operationally or causally invalid.
  3. Discuss the difference between predictive association and causal effect using potential outcomes.
  4. Explain how calibration and uncertainty should influence deployment of a high-stakes classifier.
  5. Design a governance framework for an explainable machine-learning system used in public-service allocation.

Analytical / Application Questions

  1. A retention model identifies salary as an important predictor of resignation. Explain why this finding does not prove that increasing salary will prevent resignation, and propose a causal study.
  2. A hospital has capacity to contact 100 high-risk patients. Formulate an expected-net-benefit policy and identify the additional information required before implementation.
  3. A SHAP summary ranks postcode first, but its attribution is unstable across samples and differs by demographic group. Analyze possible causes and recommend validation and governance actions.

Multiple Choice Questions with Answers

Question 1

Which statement best distinguishes prediction from causation?

A. Prediction always requires a randomized experiment.

B. Causation concerns the outcome under an intervention, whereas prediction concerns expected outcomes given observed information.

C. Causal effects are always larger than predictive associations.

D. Prediction and causation are identical when model accuracy is high.

Correct Answer: B. Causation concerns the outcome under an intervention, whereas prediction concerns expected outcomes given observed information.

Question 2

What does a partial-dependence function estimate?

A. The causal effect of intervening on a feature.

B. The average model prediction as a feature is varied over the data distribution of other features.

C. The probability that a feature is statistically significant.

D. The uncertainty of the model parameters only.

Correct Answer: B. The average model prediction as a feature is varied over the data distribution of other features.

Question 3

In the SHAP decomposition f(x) = φ0 + ∑φj, what is φ0?

A. The largest feature contribution.

B. The model’s classification threshold.

C. The baseline expected model output under a specified background distribution.

D. The residual error for the observation.

Correct Answer: C. The baseline expected model output under a specified background distribution.

Question 4

Why can correlated features complicate SHAP interpretation?

A. SHAP cannot produce numerical values for correlated features.

B. Credit may be shared or allocated differently among features that contain overlapping information.

C. Correlation guarantees that all features have zero importance.

D. Correlation converts a predictive model into a causal model.

Correct Answer: B. Credit may be shared or allocated differently among features that contain overlapping information.

Question 5

Which condition is required for standard observational causal identification?

A. Every treatment must have probability exactly 0.5.

B. There must be no observed covariates.

C. Relevant treatment options must have positive probability for covariate profiles of interest.

D. The outcome must be continuous.

Correct Answer: C. Relevant treatment options must have positive probability for covariate profiles of interest.

Question 6

A binary intervention has expected benefit £500 and cost £100. Ignoring other considerations, intervention is preferred when predicted event probability exceeds:

A. 0.05

B. 0.20

C. 0.50

D. 0.80

Correct Answer: B. 0.20

Question 7

What does calibration mean for a group assigned predicted probability 0.7?

A. Exactly 70% of every small subgroup must experience the event.

B. Approximately 70% should experience the event over an appropriate evaluation population and period.

C. The model must have 70% accuracy.

D. The model must produce a causal estimate of 0.7.

Correct Answer: B. Approximately 70% should experience the event over an appropriate evaluation population and period.

Question 8

Which characteristic is essential for a useful counterfactual explanation?

A. It changes every feature simultaneously.

B. It ignores immutable variables to simplify optimization.

C. It respects domain constraints and identifies changes that are realistically actionable.

D. It always provides a causal effect estimate.

Correct Answer: C. It respects domain constraints and identifies changes that are realistically actionable.

Question 9

What does ICE reveal that a standard PDP may hide?

A. The training-set size.

B. Heterogeneity of individual response curves and possible interactions.

C. The legal status of each feature.

D. The exact true causal graph.

Correct Answer: B. Heterogeneity of individual response curves and possible interactions.

Question 10

In a capacity-constrained intervention problem, why should managers not rank cases solely by predicted risk?

A. Risk scores cannot be calculated for individuals.

B. Risk does not incorporate intervention cost, benefit, feasibility or available capacity.

C. High-risk cases never benefit from intervention.

D. Optimization is unrelated to prediction.

Correct Answer: B. Risk does not incorporate intervention cost, benefit, feasibility or available capacity.

Machine Learning
Chapter 12 of 15

Machine Learning

Chapter 13
Scalable ML Systems and MLOps

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Scalable ML Systems and MLOps

Introduction

A machine-learning model becomes valuable only when it can be used reliably in a real environment. Training a model in a notebook is primarily an experimental activity; operating that model for thousands of users, millions of records, or continuously changing data is a systems-engineering problem. Production machine learning combines statistical modelling, software engineering, data engineering, cloud infrastructure, security, and organizational governance.

MLOps is the set of practices, technologies, and responsibilities used to develop, deploy, monitor, reproduce, and retire machine-learning systems. It extends DevOps to account for data, features, model artefacts, experiments, stochastic training, changing populations, and model performance that may degrade without any software defect.

A scalable ML system must answer several questions:

  • Which data and code produced a particular model?
  • Can the model be reproduced or rolled back?
  • Can training and inference handle increasing demand?
  • How are data quality, latency, fairness, and prediction quality monitored?
  • What happens when data distributions or business conditions change?
  • Who is responsible for approving, operating, and retiring the model?

This chapter presents an end-to-end framework for answering these questions.

Learning Objectives

After studying this chapter, a learner should be able to:

  1. Design a reproducible ML pipeline using data, code, feature, and model versioning.
  2. Select suitable batch, online, streaming, cloud, and distributed-computing architectures.
  3. Package models with containers and expose them through reliable APIs.
  4. Apply automated testing, continuous integration, continuous delivery, and controlled deployment.
  5. Explain the role of feature stores and prevent training–serving skew.
  6. Design monitoring for system health, data quality, model performance, drift, and fairness.
  7. Define retraining policies, rollback procedures, governance controls, and model-lifecycle stages.
  8. Evaluate operational, economic, ethical, and regulatory trade-offs in production ML.

1. From Model Development to ML Systems

A production ML system is more than a model. Its principal components are:

ComponentPurpose
Data sourcesSupply historical and real-time observations
Data pipelineIngest, validate, transform, and store data
Feature computationConstruct variables used by training and prediction
Training pipelineFit, evaluate, and register models
Model registryStore approved model versions and metadata
Serving layerGenerate predictions in batch, online, or streaming mode
MonitoringDetect failures, degradation, drift, and abnormal usage
GovernanceControl access, approvals, documentation, and auditability

The system may contain several interacting feedback loops. New data supports retraining; monitoring identifies problems; human review may approve or reject a candidate model; and business outcomes may reveal that the prediction target or decision policy has changed.

A useful distinction is between model correctness and system correctness. Model correctness concerns whether the algorithm produces valid predictions under defined assumptions. System correctness additionally concerns data freshness, feature availability, serialization, latency, concurrency, security, deployment configuration, and operational recovery.

Batch, online, and streaming inference

In batch inference, predictions are generated periodically for a defined dataset. It is appropriate for weekly demand forecasts, monthly risk reviews, or recommendation tables refreshed overnight. It generally has lower infrastructure complexity and can exploit large computational jobs.

Online inference produces a response to an individual request, often through HTTP or gRPC. It is appropriate for fraud screening or real-time recommendation. The design must specify latency targets, availability, request schemas, timeouts, and behavior when a feature or dependency is unavailable.

Streaming inference processes events continuously. It is useful when predictions must react to transactions, sensors, or user activity. Streaming systems require event-time handling, ordering policies, late-event treatment, checkpointing, and recovery from partial failure.

The choice should be driven by decision latency, data arrival patterns, cost, consistency requirements, and the consequences of stale predictions—not by a presumption that online inference is always superior.

2. Reproducibility and Versioning

Reproducibility means that a result can be regenerated, or at least explained and traced, from recorded inputs and procedures. A model version should be associated with:

  • source-code commit;
  • training-data snapshot or immutable data reference;
  • feature definitions and feature-data version;
  • dependency and operating-system versions;
  • hyperparameters and random seeds;
  • training and evaluation metrics;
  • model artefact checksum;
  • responsible persons, timestamps, and approval status.

Data versioning

Data versioning is more difficult than code versioning because datasets are large, mutable, distributed, and subject to privacy restrictions. A robust design separates immutable raw data from derived datasets. Raw objects may be stored in an object store, while metadata records schema, lineage, partition, retention, and access policy.

A data snapshot is not necessarily a perfect copy of every source record. It may be a logical definition such as “all transactions available before time T,” together with source-system versions and extraction rules. This distinction matters when source systems are corrected retroactively.

Data validation should check schema, type, null rates, allowed ranges, uniqueness, referential integrity, freshness, and distributional summaries. Validation failures should either stop the pipeline or route data to quarantine, depending on the severity and documented policy.

Model registries and promotion

A model registry provides a controlled record of model versions. Typical lifecycle states include development, validation, staging, production, and retired. Registration should not imply approval. Promotion should require evidence such as evaluation on a fixed test set, comparison with the incumbent model, fairness checks, security review, and operational tests.

A model artefact should be immutable after registration. If a defect is found, a new version should be created rather than silently replacing the old one. This supports auditability and rollback.

Experiment tracking

Experiment tracking records parameters, metrics, data references, artefacts, and environmental information. It prevents a common failure mode in which a team remembers that a model performed well but cannot identify the exact preprocessing or data sample that produced it.

Random seeds improve repeatability but do not guarantee bit-for-bit identity across hardware, distributed execution, libraries, or nondeterministic kernels. Therefore, reproducibility claims should state their scope and tolerance.

3. Pipeline Architecture and Distributed Computing

A production pipeline commonly contains the following stages:

  1. Ingestion from databases, files, message queues, or APIs.
  2. Validation and schema enforcement.
  3. Cleaning and transformation.
  4. Feature generation.
  5. Dataset splitting without temporal or entity leakage.
  6. Training and hyperparameter selection.
  7. Evaluation against statistical and business criteria.
  8. Registration and approval.
  9. Deployment and monitoring.

Dependencies should be represented as a directed acyclic graph (DAG), in which each task executes after its inputs are available. DAG scheduling supports retries, partial reruns, lineage, and backfills. A pipeline should be idempotent: rerunning the same logical interval should not create duplicate or inconsistent outputs.

Scaling dimensions

  • Vertical scaling increases CPU, memory, or accelerator capacity on one machine.
  • Horizontal scaling adds machines or service replicas.
  • Data parallelism partitions training examples across workers.
  • Model parallelism partitions a large model across devices.
  • Pipeline parallelism overlaps different computation stages.

Distributed computation introduces communication overhead, serialization cost, network bottlenecks, synchronization delays, and failure probability. A distributed job is not automatically faster. If a task has total useful computation W, communication cost C, and coordination overhead O, its practical execution time is influenced by W + C + O; adding workers may reduce W while increasing C and O.

For data-parallel training, each worker commonly computes gradients on a mini-batch and participates in gradient aggregation. The effective batch size is approximately

Beff = n × B

where n is the number of workers and B is the per-worker batch size. This relationship assumes each worker processes B examples per update and that updates are synchronized. It does not imply that optimization behaviour remains unchanged: learning-rate schedules, generalization, memory, and communication cost may change with B_eff.

Cloud platforms provide elastic compute, managed storage, networking, identity, orchestration, and observability. However, elasticity does not eliminate design decisions. Teams must manage data egress, regional availability, confidential data, vendor dependence, quota limits, cost controls, and failure recovery.

4. Containers, APIs, and Serving

A container packages application code, dependencies, configuration defaults, and runtime assumptions into a deployable unit. It improves consistency between development, testing, and production, but it is not a complete security boundary or a replacement for architecture.

A production image should be small, reproducibly built, scanned for vulnerabilities, run as a non-privileged user where possible, and avoid embedding secrets. Dependencies should be pinned or constrained, and the image should be identified by an immutable digest rather than only a mutable tag.

Prediction APIs

An online prediction API should define:

  • request and response schemas;
  • feature names, types, units, and permissible ranges;
  • authentication and authorization;
  • timeout and retry behaviour;
  • error codes and fallback behaviour;
  • model version returned in response metadata or logs;
  • rate limits and maximum payload size.

Retries require care. A client retry may duplicate a request or overload an already failing service. Idempotency keys, bounded exponential backoff, circuit breakers, and explicit timeout budgets are preferable to unlimited retries.

Latency is often summarized by percentiles rather than averages. The p95 latency is the value below which 95% of observed requests complete. A service can have a satisfactory average while its p99 latency violates user requirements. Measurement should include preprocessing, model computation, postprocessing, network time, and downstream dependencies.

A serving design may use a synchronous model server, an asynchronous queue, a batch endpoint, or a hybrid. Model caching, autoscaling, hardware acceleration, and request batching can improve throughput, but batching may increase individual-request latency. The appropriate choice depends on the service-level objective.

5. Feature Stores and Feature Management

A feature store manages reusable feature definitions and often provides both an offline store for training and an online store for low-latency serving. It addresses duplication, inconsistent transformations, feature discovery, ownership, freshness, and point-in-time correctness.

The central risk is training–serving skew: the feature calculated during training differs from the feature calculated during production inference. Reusing feature definitions, shared transformation code, or a common feature computation framework reduces this risk.

A second risk is temporal leakage. A training example must use only information available at its prediction time. For an entity e and prediction time t, a feature value should be selected from records whose event time is not later than t, subject to the feature’s availability delay. Joining with future information can produce impressive offline scores and poor production performance.

Feature metadata should include owner, definition, data type, unit, permitted range, freshness expectation, source, transformation logic, privacy classification, and deprecation status. Features containing personal or sensitive information require access controls, minimization, retention limits, and purpose limitation.

6. Automated Testing and CI/CD

Testing must cover both software and statistical behaviour.

Test categories

  • Unit tests verify individual transformations and functions.
  • Schema tests verify column names, types, constraints, and compatibility.
  • Data-quality tests detect null, range, uniqueness, and distribution problems.
  • Integration tests verify interaction among storage, feature computation, and serving.
  • Contract tests confirm that producers and consumers agree on interfaces.
  • Model tests verify output shape, numerical stability, calibration where relevant, and behaviour on known cases.
  • Performance tests measure throughput, latency, memory, and concurrency.
  • Security tests inspect access control, dependency vulnerabilities, injection risks, and secret handling.
  • Regression tests compare a candidate model with an approved baseline.

A model test should not assert that every prediction equals a fixed value when the algorithm is legitimately stochastic. It may instead assert metric tolerances, invariant properties, deterministic seeds within a supported environment, and acceptable changes on a reference dataset.

Continuous integration (CI) automatically builds and tests changes. Continuous delivery (CD) keeps a deployable artefact ready, whereas continuous deployment automatically releases changes after required gates. In ML, automatic deployment should be conditional on data, model, operational, and governance checks.

A common promotion pattern is:

  1. Build a versioned image and model artefact.
  2. Run unit, data, security, and integration tests.
  3. Evaluate on frozen validation and recent holdout data.
  4. Deploy to a staging environment.
  5. Perform load and contract tests.
  6. Release using canary or blue–green deployment.
  7. Compare live metrics with the incumbent.
  8. Promote, pause, or roll back.

Canary deployment sends a small fraction of traffic to the new version. Blue–green deployment maintains two environments and switches traffic between them. Shadow deployment sends copied traffic to a candidate without using its predictions for decisions. Shadowing is useful for observing latency and predictions before exposure to users.

7. Monitoring, Drift, and Reliability

Monitoring should answer four different questions:

  1. Is the service functioning?
  2. Is the input data valid and timely?
  3. Are predictions behaving as expected?
  4. Does the model create useful and fair outcomes?

System monitoring

Important system indicators include availability, request rate, error rate, timeout rate, CPU and memory utilization, queue depth, accelerator utilization, and latency percentiles. A service-level objective (SLO) defines a target, such as a maximum error rate or a latency percentile over a specified period.

Data monitoring

Input monitoring includes schema changes, missingness, cardinality, ranges, freshness, duplicate records, and unexpected categories. A model may remain available while receiving meaningless inputs; therefore, operational uptime alone is insufficient.

Distribution drift

Let p(x) be the reference distribution and q(x) the current distribution of a feature or population. A common divergence measure is the Kullback–Leibler divergence:

DKL(p ∥ q) = ∑x p(x) log(p(x) ÷ q(x))

For continuous variables, the sum is replaced by an integral. The distributions must be defined on a common support, and q(x) must be positive wherever p(x) is positive; otherwise the divergence may be undefined or infinite. In practice, continuous data are often binned or estimated using density methods. KL divergence is asymmetric and is not, by itself, a universal drift decision rule.

The population stability index is another operational measure based on bins:

PSI = ∑i (qi − pi) ln(qi ÷ pi)

Here p_i and q_i are reference and current proportions in bin i, with positive proportions. Thresholds are context-dependent and should be calibrated rather than treated as scientific laws.

Performance monitoring

Labels often arrive later than predictions. Once outcomes are available, teams can monitor accuracy, precision, recall, calibration, ranking quality, cost-weighted loss, and subgroup performance. If the positive class is rare, accuracy may be misleading. For a binary classifier:

Precision = TP ÷ (TP + FP)
Recall = TP ÷ (TP + FN)

TP, FP, and FN denote true positives, false positives, and false negatives. These measures require a selected classification threshold and should be interpreted with the business costs of errors.

Concept drift and data drift

Data drift concerns changes in inputs. Concept drift concerns changes in the relationship between inputs and outcomes, often represented as a change in P(y|x). A system can have stable input distributions but declining predictive validity because customer behaviour, policy, or the environment has changed.

Monitoring should use alert severity, ownership, runbooks, and escalation. Alerts without an action plan create fatigue. A drift signal should initiate investigation, not automatically trigger retraining in every case.

8. Retraining and Model Lifecycle Management

Retraining policies may be:

  • schedule-based, such as monthly;
  • event-based, such as after a detected schema or drift event;
  • performance-based, such as after a sustained decline;
  • demand-based, such as when a new region or product is introduced.

Automatic retraining is safe only when data selection, validation, approval, and rollback are automated and governed. Otherwise it can amplify corrupted data, feedback loops, or adversarial behaviour.

A retraining decision should consider:

  • statistical evidence of degradation;
  • business impact and error asymmetry;
  • label delay and label quality;
  • data representativeness;
  • fairness and subgroup effects;
  • computational and operational cost;
  • whether the target itself remains appropriate.

Lifecycle stages normally include ideation, development, validation, staging, production, maintenance, and retirement. Retirement requires replacing dependencies, preserving records, deleting or archiving artefacts according to policy, and communicating the change to affected stakeholders.

Rollback should be tested before it is needed. A rollback may involve switching a model pointer, restoring a container image, reverting feature definitions, or replaying a prior pipeline. It is not sufficient to retain the model file if the former feature logic or runtime environment is unavailable.

9. Governance, Security, and Responsible Operation

Production ML can affect employment, credit, health, safety, access to services, and personal privacy. Governance should therefore be integrated into engineering rather than added at the end.

Important controls include:

  • documented purpose and intended population;
  • data-provenance and consent records where applicable;
  • role-based access and least privilege;
  • encryption in transit and at rest;
  • secrets management and key rotation;
  • audit logs that resist unauthorized alteration;
  • retention and deletion procedures;
  • human oversight for high-impact decisions;
  • incident response and notification procedures;
  • model cards, data documentation, and limitations;
  • bias and subgroup-performance assessment.

Fairness cannot be reduced to a single universal metric. Different criteria may conflict when base rates differ, and a technically accurate model can still produce unacceptable effects because of the decision threshold, access policy, or surrounding workflow. Teams should specify the relevant harm, affected groups, legal obligations, and remediation process.

Security threats include data poisoning, model extraction, prompt or input manipulation, membership inference, supply-chain compromise, and unauthorized model access. Threat modelling should cover the full pipeline, including training data, registries, build systems, endpoints, logs, and monitoring infrastructure.

10. Cost and Architectural Trade-offs

An architecture should be evaluated by total cost of ownership rather than compute price alone. Relevant costs include storage, training, inference, network transfer, observability, engineering labour, incident response, compliance, and opportunity cost.

Online serving may reduce decision latency but require highly available infrastructure and duplicated feature computation. Batch processing may be cheaper and simpler but cannot support immediate decisions. Larger models may improve quality while increasing latency, energy use, and operational risk. Managed cloud services may accelerate delivery while increasing vendor dependence and recurring cost.

Capacity planning should use measured workload. If arrival rate is λ requests per second and average service time is S seconds, a basic utilization estimate for one equivalent server is:

ρ = λ × S

This estimate assumes stable average rates and is not a complete queueing model. As ρ approaches 1, waiting times can grow sharply, and real systems require headroom for bursts, failures, and variability. Thus, capacity should not be set merely equal to average demand.

11. Contemporary Developments

Modern ML platforms increasingly support feature lineage, model registries, policy-as-code, automated evaluation, GPU scheduling, and event-driven pipelines. Kubernetes-like orchestration can provide scheduling and service recovery, while workflow engines coordinate data and training jobs. Infrastructure-as-code makes environments reviewable and reproducible.

Large foundation models introduce additional concerns: prompt and retrieval versioning, evaluation sets, token-cost monitoring, vector-index freshness, content safety, model-provider dependency, and the distinction between retrieved evidence and generated text. The same MLOps principles remain applicable: version inputs, test interfaces, monitor outcomes, control access, and define rollback or fallback behaviour.

Research and industry increasingly emphasize platform engineering: reusable templates, standardized pipelines, self-service deployment, and centrally managed controls. This can improve productivity, but excessive standardization may constrain legitimate experimentation. A mature platform provides paved paths while allowing documented exceptions.

12. Illustrative Case: Demand Forecasting for a Retail Network

Background

A retailer forecasts daily demand for 20,000 product–store combinations. Forecasts determine replenishment quantities. Stockouts reduce revenue, while excess inventory increases holding and markdown costs.

Problem

The existing process runs a monthly script on manually exported data. It lacks data lineage, cannot distinguish a changed product definition from a real demand change, and provides no alert when forecasts deteriorate. A new system must support daily batch inference and weekly retraining.

Analysis

The team identifies sales, promotions, price, holidays, inventory, store attributes, and product lifecycle status as candidate sources. It discovers that cancelled orders were sometimes included in historical demand and that future promotion information was accidentally used for some training rows. The team therefore defines an event-time policy and constructs point-in-time-correct features.

The business chooses weighted error costs: under-forecasting is more expensive for high-margin products, while over-forecasting is more expensive for perishables. Evaluation therefore includes a business-weighted loss in addition to standard forecast metrics. The incumbent method is retained as a baseline.

Approach and solution

Raw source tables are stored immutably with extraction timestamps. A validation pipeline checks schema, duplicate keys, negative quantities, missing product identifiers, and freshness. Feature definitions are registered with owners and expected update intervals. The offline feature store creates training datasets using only information available before each forecast date; the online store is unnecessary because inference is batch-based.

Training runs in a container with a pinned dependency environment. Each run records the code commit, data snapshot, feature definitions, parameters, metrics, and model checksum. Candidate models must outperform the incumbent on a recent holdout period, satisfy subgroup checks by store type, and pass data-quality and performance tests.

The pipeline produces daily predictions to a controlled table. A model registry identifies the production version, while the previous version remains available for rollback. Monitoring reports pipeline success, data freshness, missingness, forecast error after sales labels arrive, and drift in promotion and product-lifecycle features. Retraining occurs weekly only when the data-quality gate passes; a severe anomaly pauses promotion and alerts the data owner.

Outcome

The retailer gains reproducible forecasts, traceable data lineage, and a tested rollback path. The most important improvement is not simply a lower validation error; it is the ability to detect faulty inputs before replenishment decisions are generated. The organization also discovers that some forecast errors arise from inventory constraints rather than model quality, leading to a separate improvement in stock-availability data.

Learning points

  1. Point-in-time correctness is essential in temporal prediction.
  2. A baseline and business-weighted evaluation are more informative than one generic metric.
  3. Monitoring must include data and business outcomes, not only job completion.
  4. Retraining should be conditional on validated evidence.
  5. Operational traceability is part of model quality.

Challenges and Limitations

No MLOps architecture eliminates uncertainty. Labels may be delayed, biased, or unavailable. Drift metrics can produce false alarms or miss changes in conditional relationships. Automated retraining can create feedback loops when model decisions influence the data later used for training. Distributed systems add operational complexity and can make reproducibility harder. Feature stores can become centralized bottlenecks if ownership and interfaces are poorly designed.

There is also a risk of measuring what is easy rather than what matters. High availability does not guarantee beneficial decisions; a lower aggregate error may conceal harm to a subgroup; and an impressive benchmark may not represent deployment conditions. Human review, domain knowledge, causal reasoning, and post-deployment evaluation remain necessary.

Best-Practice Checklist

  • Define the prediction task, decision owner, population, and harm model.
  • Version data, code, features, environments, and model artefacts.
  • Enforce point-in-time correctness and prevent leakage.
  • Build validation and rollback before production launch.
  • Test schemas, transformations, APIs, security, performance, and model behaviour.
  • Monitor system, data, prediction, outcome, and subgroup metrics.
  • Use alerts with owners, severity levels, and runbooks.
  • Treat drift as an investigation signal, not automatic proof of failure.
  • Gate retraining on data quality, evaluation, governance, and operational checks.
  • Document limitations and retire models deliberately.

Chapter Summary

Scalable ML systems connect data engineering, statistical modelling, software delivery, infrastructure, and governance. Reproducibility depends on versioning data, code, features, environments, experiments, and model artefacts. Production pipelines should be represented as reliable, idempotent workflows and should select batch, online, or streaming inference according to decision requirements.

Containers improve deployment consistency, while APIs require explicit contracts, security, timeout, latency, and fallback policies. Feature stores can promote reuse and reduce training–serving skew, but point-in-time correctness is essential. CI/CD for ML must test software, data, models, security, and operational performance before controlled promotion.

Monitoring covers service health, data quality, distribution drift, concept drift, model performance, and fairness. Retraining should be evidence-based and governed, with tested rollback and retirement procedures. Finally, responsible MLOps treats privacy, security, explainability, human oversight, and subgroup impact as lifecycle concerns rather than afterthoughts.

Key Terms

MLOps: Practices for developing, deploying, monitoring, and governing machine-learning systems.

Data versioning: Recording immutable or logically reproducible versions of datasets and their metadata.

Model registry: A controlled repository of model artefacts, metadata, versions, and approval states.

Reproducibility: Ability to regenerate or trace a result from recorded inputs and procedures.

DAG: Directed acyclic graph representing dependencies among pipeline tasks.

Idempotence: Property that repeating an operation produces the same intended result without harmful duplication.

Batch inference: Generating predictions for a collection of records on a schedule.

Online inference: Producing a prediction in response to an individual request.

Streaming inference: Processing events continuously as they arrive.

Container: Portable package containing application code and runtime dependencies.

CI/CD: Continuous integration and continuous delivery or deployment of tested software artefacts.

Feature store: System for managing feature definitions, data, lineage, and serving representations.

Training–serving skew: Difference between feature computation during training and production inference.

Temporal leakage: Use of information unavailable at the prediction time in model development.

Data drift: Change in the distribution of model inputs.

Concept drift: Change in the relationship between inputs and outcomes.

Canary deployment: Gradual release of a new version to a small traffic fraction.

SLO: Defined target for a service property such as availability or latency.

Rollback: Reversion to a previously approved model, code, or environment.

Model lifecycle: Stages from development and validation through production, maintenance, and retirement.

Review Questions

Short Answer Questions

  1. Why is a production ML system more than a trained model?
  2. Distinguish batch inference from online inference.
  3. What information should be recorded with a model version?
  4. What is training–serving skew?
  5. Why should drift detection not automatically imply retraining?

Long Answer Questions

  1. Explain how data versioning, experiment tracking, and model registries support reproducibility and auditability.
  2. Compare vertical scaling, horizontal scaling, data parallelism, and model parallelism, including their principal trade-offs.
  3. Design a CI/CD workflow for promoting a candidate model to production.
  4. Explain the roles of system, data, model-performance, and fairness monitoring.
  5. Discuss governance, privacy, security, and human-oversight requirements for high-impact ML applications.

Analytical / Application Questions

  1. A fraud model has stable input distributions but declining recall after a regulatory policy change. Is this data drift, concept drift, or both? Design an investigation and response plan.
  2. Design a feature and serving architecture for a recommendation system requiring sub-100-millisecond responses and hourly updates. Justify storage, caching, API, monitoring, and rollback choices.
  3. A retraining job improves aggregate accuracy but reduces recall for a small vulnerable subgroup. Decide whether to deploy it and specify the additional evidence, controls, and stakeholder approvals required.

Multiple Choice Questions with Answers

Question 1

Which artefact most directly supports tracing a production model to the exact data and code used to train it?

A. A mutable deployment tag

B. A model record containing data snapshot, code commit, and environment metadata

C. A dashboard showing average latency

D. A manually edited prediction file

Correct Answer: B. A model record containing data snapshot, code commit, and environment metadata

Question 2

Which inference mode is generally most suitable for generating predictions for an overnight replenishment table?

A. Batch inference

B. Online inference

C. Interactive streaming only

D. Manual inference

Correct Answer: A. Batch inference

Question 3

What is the main purpose of a feature store?

A. To replace all model registries

B. To manage reusable feature definitions and their offline and online representations

C. To eliminate the need for data validation

D. To guarantee that every model is unbiased

Correct Answer: B. To manage reusable feature definitions and their offline and online representations

Question 4

Which practice best prevents temporal leakage?

A. Using the largest possible feature set

B. Selecting feature records available no later than the prediction time

C. Randomly shuffling all future records into training

D. Removing the test set

Correct Answer: B. Selecting feature records available no later than the prediction time

Question 5

A canary deployment primarily means that:

A. The old model is permanently deleted before release

B. A new version receives a small fraction of traffic before wider promotion

C. Predictions are generated without monitoring

D. The model is trained only on synthetic data

Correct Answer: B. A new version receives a small fraction of traffic before wider promotion

Question 6

If TP = 80 and FP = 20, what is precision?

A. 0.20

B. 0.50

C. 0.80

D. 0.้อม

Correct Answer: C. 0.80

Question 7

Which statement correctly distinguishes data drift from concept drift?

A. Data drift concerns input distributions, whereas concept drift concerns the input–outcome relationship

B. Data drift concerns latency, whereas concept drift concerns memory

C. They are identical terms in all contexts

D. Concept drift can occur only when schemas change

Correct Answer: A. Data drift concerns input distributions, whereas concept drift concerns the input–outcome relationship

Question 8

What does idempotence mean in a production pipeline?

A. Every task must run on a GPU

B. Repeating a logical operation does not create harmful duplicate effects

C. All data must be deleted after training

D. A model must have exactly one feature

Correct Answer: B. Repeating a logical operation does not create harmful duplicate effects

Question 9

If request rate λ = 10 requests per second and average service time S = 0.05 seconds, the basic single-server utilization estimate ρ = λ × S is:

A. 0.05

B. 0.50

C. 5.00

D. 10.05

Correct Answer: B. 0.50

Question 10

Which action is most appropriate when a drift alert is generated?

A. Automatically deploy any newly trained model without testing

B. Ignore the alert because service availability is unchanged

C. Investigate the signal using data quality, outcome, and business evidence

D. Delete all historical data

Correct Answer: C. Investigate the signal using data quality, outcome, and business evidence

Machine Learning
Chapter 13 of 15

Machine Learning

Chapter 14
Responsible AI, Security and ML Governance

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Responsible AI, Security and ML Governance

Introduction and Context

Machine learning systems increasingly influence lending, insurance, recruitment, healthcare, education, policing, advertising, industrial control and public administration. Their strategic value arises from their capacity to detect patterns and support decisions at a scale that exceeds unaided human analysis. The same scale creates distinctive risks: a model may reproduce historical discrimination, expose personal information, fail under distribution shift, be manipulated by an attacker, consume substantial energy, or make a decision that no responsible person can explain or challenge.

Responsible AI is therefore not a single algorithmic property. It is a socio-technical discipline concerned with how data, models, people, institutions and legal rules interact. ML governance converts broad principles into decision rights, controls, evidence, monitoring and accountability throughout the system life cycle. Security protects the confidentiality, integrity, availability and intended behaviour of models and their data. These concerns overlap but are not identical: a secure model may still be unfair, and a fair model may still be vulnerable to data poisoning.

This chapter develops a practical governance framework for postgraduate practitioners. It treats fairness, privacy, robustness, adversarial resilience, sustainability, documentation, human oversight, auditability and regulatory compliance as connected design requirements rather than as post-deployment public-relations activities.

Learning Objectives

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

  1. Distinguish fairness, privacy, security, robustness, transparency and accountability.
  2. Identify sources of bias across the ML life cycle and select suitable fairness measures.
  3. Explain privacy risks and apply privacy-preserving design principles.
  4. Analyse adversarial threats, distribution shift and model failure modes.
  5. Design documentation, human-oversight and audit mechanisms.
  6. Construct a risk-based ML governance process aligned with regulatory expectations.
  7. Evaluate environmental impacts and propose proportionate controls.
  8. Critically assess trade-offs between predictive utility, rights, safety and organisational objectives.

1. Responsible AI as a Socio-Technical System

A model is embedded in a larger decision process. A credit model, for example, does not itself determine whether a person receives a loan; data collection, feature engineering, thresholds, human review, appeals, incentives and institutional policy all contribute. Responsibility must consequently be allocated across developers, data providers, deployers, operators, executives, auditors and affected communities.

Important principles include:

  • Beneficence and non-maleficence: pursue legitimate benefits and prevent foreseeable harm.
  • Respect for autonomy: provide meaningful information, contestability and choice where appropriate.
  • Justice: distribute benefits, burdens and error rates fairly.
  • Accountability: ensure that identifiable people or bodies are answerable for decisions.
  • Transparency: make relevant purposes, limitations and decision logic understandable.
  • Proportionality: match controls and restrictions to the severity and likelihood of harm.

These principles can conflict. Increasing fraud detection may reduce financial loss but increase false positives for a particular group. Stronger privacy may reduce available training information. A highly interpretable model may be less accurate than a complex model, although the trade-off should be tested rather than assumed. Governance does not eliminate such conflicts; it makes them explicit and subject to reasoned decision-making.

A useful risk formulation is qualitative or semi-quantitative:

Risk = Likelihood of harm × Severity of harm

Likelihood represents the chance that an unwanted event occurs; severity represents its impact on people, organisations or the environment. The expression is a prioritisation aid, not a universal scientific law. Scales must be defined consistently, and low-probability catastrophic harms may require controls even when their product is modest.

2. Bias and Fairness

2.1 Sources of bias

Bias is a systematic distortion that can produce unjustified differences in outcomes or performance. It can arise through:

  1. Historical bias: the world reflected in the data already contains unequal treatment.
  2. Representation bias: some populations are absent or under-sampled.
  3. Measurement bias: variables measure a construct differently across groups.
  4. Label bias: outcomes used as labels reflect unequal decisions rather than true need.
  5. Aggregation bias: one model is imposed on groups with different relationships between features and outcomes.
  6. Evaluation bias: test data or metrics do not represent deployment conditions.
  7. Deployment bias: a technically valid model is used for a purpose or population different from that intended.
  8. Feedback-loop bias: predictions change behaviour and thereby alter future training data.

Removing a protected attribute does not necessarily remove discrimination. Proxies such as postcode, language, occupation or browsing behaviour may encode it. Conversely, protected attributes may be necessary for auditing and fairness analysis, even when they are excluded from operational prediction.

2.2 Fairness metrics

Let Ŷ be a binary prediction, Y the actual outcome, and A a protected-group attribute. For group a:

TPRa = P(Ŷ = 1 | Y = 1, A = a)

TPR, or true-positive rate, is the proportion of actual positives correctly identified. Equal opportunity requires approximately equal TPR across groups. Equalized odds requires both TPR and false-positive rate to be approximately equal:

FPRa = P(Ŷ = 1 | Y = 0, A = a)

Demographic parity requires equal selection rates:

P(Ŷ = 1 | A = a) = P(Ŷ = 1 | A = b)

Predictive parity requires equal positive predictive value:

PPVa = P(Y = 1 | Ŷ = 1, A = a)

These definitions apply to specified populations, labels and decision thresholds. They are not interchangeable. When base rates differ between groups, equalized error rates and equal predictive values generally cannot all be achieved simultaneously except in special cases. This is a structural trade-off, not evidence that one metric is universally correct.

For a selected group, the selection-rate ratio is:

Disparate impact ratio = P(Ŷ = 1 | A = disadvantaged) ÷ P(Ŷ = 1 | A = advantaged)

The ratio is dimensionless and must be interpreted with context, sample uncertainty and domain law; a threshold such as 0.80 used in some employment contexts is a screening convention, not a complete definition of fairness.

2.3 A worked fairness example

Suppose a hiring model selects 60 of 100 applicants in group A and 30 of 100 applicants in group B. The selection rates are 0.60 and 0.30, so the selection-rate ratio is 0.30 ÷ 0.60 = 0.50. This indicates a substantial disparity in selection rates. It does not by itself prove unlawful discrimination: job-relatedness, applicant qualifications, sample uncertainty, process design and alternative explanations must also be investigated. The result does justify further analysis, including intersectional groups, calibration, error rates and feature review.

2.4 Fairness practice

A defensible assessment begins before modelling: define the decision, affected groups, legitimate purpose, foreseeable harms and acceptable error trade-offs. During development, examine data provenance, missingness, label construction and subgroup performance. Before release, evaluate relevant metrics with confidence intervals where sample sizes permit. After release, monitor drift, complaints, overrides and outcomes. Fairness interventions may include improved sampling, relabelling, group-aware thresholds, constrained optimisation, human review or redesign of the decision itself. Technical adjustment cannot cure an illegitimate objective.

3. Privacy and Data Protection

Privacy concerns how information about people is collected, used, inferred, shared and retained. ML creates risks beyond direct disclosure: models may memorise rare examples, infer sensitive attributes, enable membership inference, or reveal information through generated outputs.

Core controls include purpose limitation, data minimisation, accuracy, retention limits, access control, encryption, secure deletion, lawful processing and documented accountability. Data should be collected for specified purposes and not casually reused for unrelated objectives. Sensitive data require stronger justification and safeguards.

3.1 Anonymisation and pseudonymisation

Pseudonymisation replaces direct identifiers with tokens while preserving a re-identification key. It reduces exposure but remains personal data when re-identification is reasonably possible. Anonymisation aims to make identification infeasible, but high-dimensional datasets are difficult to anonymise reliably because auxiliary datasets can enable linkage.

3.2 Differential privacy

Differential privacy provides a formal privacy guarantee for a mechanism M. For neighbouring datasets D and D′ differing in one person, an ε-differentially private mechanism satisfies:

P(M(D) ∈ S) ≤ e^ε P(M(D′) ∈ S)

for every possible output set S. Here ε ≥ 0 is the privacy-loss parameter; smaller ε generally means stronger privacy, though utility usually decreases. The guarantee depends on the neighbouring-dataset definition, mechanism, composition across repeated releases and implementation assumptions. It does not guarantee that an individual is safe from all external information or that the result is socially fair.

A common training approach clips per-record gradients to a bound C and adds calibrated noise. Clipping limits sensitivity; noise provides privacy. The privacy budget must be tracked across iterations and queries rather than treated as free. Privacy-preserving computation may also use federated learning, secure aggregation, trusted execution environments or homomorphic encryption, but each introduces communication, computational, implementation or leakage trade-offs.

4. Robustness, Reliability and Adversarial Threats

Robustness is the capacity to maintain acceptable performance under expected perturbations and foreseeable changes. Reliability additionally concerns consistent operation, availability and safe failure. A model may be accurate on an independent test set yet unsafe under distribution shift.

4.1 Threat taxonomy

  • Data poisoning: an attacker inserts or alters training examples.
  • Backdoors or trojans: a hidden trigger causes targeted misclassification.
  • Evasion: an input is modified at inference time to change the prediction.
  • Model extraction: repeated queries reveal parameters or functionality.
  • Membership inference: an attacker estimates whether a record was in training data.
  • Model inversion: outputs help reconstruct sensitive features.
  • Supply-chain compromise: dependencies, pretrained models or datasets are malicious or tampered with.
  • Prompt or instruction attacks: in systems using language models, untrusted content manipulates tool use or disclosure.

Threat modelling should identify assets, actors, attack surfaces, assumptions, controls and residual risk. Security is not achieved by obscurity alone. Authentication, authorisation, rate limits, network segmentation, signed artefacts, dependency scanning, secrets management, logging and incident response are foundational controls.

4.2 Robust evaluation

Evaluation should include temporal and geographic validation, subgroup tests, missing and corrupted inputs, stress scenarios, calibration, worst-case or distributionally robust tests where justified, and operational simulation. For safety-critical systems, define abstention or fail-safe behaviour. A model that cannot reliably distinguish a familiar case from an unfamiliar one should be able to defer to a human or safer procedure.

5. Documentation, Explainability and Auditability

Documentation converts intentions into inspectable evidence. It should be proportionate to risk but sufficiently detailed for an independent reviewer to reconstruct important decisions.

A model card commonly records purpose, intended users, prohibited uses, training data, evaluation populations, metrics, subgroup limitations, ethical considerations and change history. A dataset record should document source, collection process, consent or legal basis, transformations, labels, known gaps, licensing, retention and access restrictions. A system record should additionally describe interfaces, dependencies, human roles, thresholds, fallback modes and monitoring.

Explainability has several audiences:

  • Developers need debugging and feature diagnostics.
  • Operators need actionable reasons and uncertainty.
  • Auditors need evidence of process and control effectiveness.
  • Affected people may need understandable reasons, recourse and correction routes.
  • Executives and regulators need risk, performance and accountability information.

Feature importance is not automatically causal explanation. Post-hoc methods may be unstable or misleading outside the data distribution. Explanations should therefore be tested for fidelity, stability, comprehensibility and appropriate use. Where a consequential decision cannot be explained adequately, the organisation should consider a different model or restrict its use.

Auditability requires immutable or access-controlled logs recording model version, input and output references where lawful, user or service identity, timestamp, override, explanation, policy version and incident status. Logs themselves contain sensitive information and require retention and access controls. Reproducibility requires versioned code, data snapshots or hashes, configurations, random seeds where relevant and documented environment dependencies.

6. Human Oversight and Contestability

Human-in-the-loop is not automatically meaningful oversight. A human who merely approves almost every machine recommendation may provide a rubber stamp. Effective oversight requires competence, authority, time, access to relevant evidence, awareness of automation bias and a genuine ability to override or stop the system.

Oversight may occur at three levels:

  1. Human-in-the-loop: a person reviews each consequential decision.
  2. Human-on-the-loop: the system operates automatically while trained personnel monitor and intervene.
  3. Human-in-command: authorised people can suspend, restrict or redesign the entire system.

The appropriate level depends on consequence, reversibility, uncertainty, scale and speed. A process should define escalation thresholds, mandatory review cases, appeal rights, correction procedures, service-level targets and emergency shutdown authority. Individuals affected by high-impact decisions should receive accessible information about the decision and a meaningful opportunity to challenge relevant facts or process errors, subject to legitimate confidentiality and security constraints.

7. ML Governance Across the Life Cycle

Governance is an operating model, not a single approval meeting. A practical life-cycle control structure is:

  1. Problem framing: establish purpose, necessity, affected stakeholders, prohibited uses and risk classification.
  2. Data governance: verify provenance, legal basis, quality, representativeness, access and retention.
  3. Development: apply secure coding, peer review, fairness analysis and reproducible experimentation.
  4. Validation: conduct independent testing, red teaming, privacy assessment, robustness testing and impact assessment.
  5. Approval: document residual risks, controls, accountable owner and release conditions.
  6. Deployment: use controlled rollout, monitoring, rollback and incident channels.
  7. Operation: monitor performance, drift, fairness, privacy events, security alerts, overrides and complaints.
  8. Change management: revalidate after data, model, interface, threshold or purpose changes.
  9. Retirement: revoke access, preserve necessary evidence, delete data appropriately and communicate closure.

A governance committee should not replace operational ownership. Each system needs a named business owner, technical owner, risk or compliance contact, data steward and incident authority. The three-lines model is useful: operational teams own and manage risk; risk/compliance functions advise and challenge; internal audit independently evaluates governance and controls.

Risk classification

Classification should consider affected rights, scale, vulnerability of users, reversibility, autonomy, safety implications, data sensitivity and potential environmental impact. High-risk systems warrant stronger validation, independent review, documented human oversight, continuous monitoring and tested incident response. A model used to recommend music does not require the same controls as one used to determine access to essential services.

8. Regulatory and Standards Landscape

Regulatory obligations vary by jurisdiction and application. Relevant themes across modern data-protection and AI regimes include lawful and transparent processing, purpose limitation, data-subject rights, security safeguards, impact assessments, restrictions on certain biometric or high-risk uses, human oversight, record keeping and demonstrable accountability. Organisations must obtain current legal advice for the jurisdictions and sectors in which they operate.

International and national frameworks commonly provide risk-management guidance rather than a single technical recipe. Examples include the NIST AI Risk Management Framework, ISO/IEC 23894 for AI risk management, ISO/IEC 42001 for AI management systems, ISO/IEC 27001 for information security management and ISO/IEC 27701 for privacy information management. These frameworks are complementary: certification or alignment does not prove that a specific model is fair or safe.

Compliance should be treated as a baseline. A system can meet a documentation requirement while still producing unacceptable harm. Conversely, strong engineering controls may need to be translated into legally meaningful evidence. Governance teams should maintain a requirements register mapping obligations to owners, controls, evidence and review dates.

9. Environmental Sustainability

ML impacts the environment through data-centre electricity, cooling, hardware manufacture, water consumption, electronic waste and network use. Impact occurs during training, hyperparameter search, inference and repeated retraining. Larger models are not automatically better when a smaller model meets the required performance and safety level.

Energy measurement should define system boundaries and include hardware, workload, duration and location. A simple operational estimate is:

E = P × t

where E is energy in kilowatt-hours when P is average power in kilowatts and t is operating time in hours. If power varies, the more general relationship is:

E = ∫ P(t) dt

The integral sums instantaneous power over time. Carbon emissions can be estimated as:

CO₂e = E × I

where I is the electricity carbon intensity in kilograms of CO₂-equivalent per kilowatt-hour. This estimate depends on accurate measurement, the chosen emissions boundary and time- and location-specific intensity; it should not be presented as exact without those assumptions.

Sustainability controls include efficient architectures, transfer learning, early stopping, mixed precision where numerically safe, workload scheduling, caching, distillation, quantisation, smaller deployment models, hardware utilisation and measuring inference at realistic scale. Environmental optimisation must not weaken privacy, safety or fairness controls without explicit review.

10. Applied Case: Illustrative Public-Benefit Eligibility System

Background

A public agency develops an ML system to prioritise applications for a housing-support programme. The system predicts which applications are likely to require urgent manual attention. It is intended as a triage aid, not an automatic eligibility decision.

Problem

After deployment, community organisations report that applicants from several neighbourhoods receive slower service. Investigation shows that historical approval records were used as labels. Those records reflected earlier staffing patterns and unequal access to documentation. A postcode feature also acts as a proxy for ethnicity and socioeconomic status. The system has no documented appeal route, and operators cannot see why an application was prioritised.

Analysis

The problem combines historical, label, representation and deployment bias. It also creates procedural harm: a prediction intended for triage is effectively being used as a gate. Privacy risks arise because applications contain health and household information. Security review identifies excessive staff access and unmonitored exports. The model card describes accuracy but omits subgroup performance and prohibited uses.

Approach and solution

The agency first suspends automated prioritisation for decisions that could deny or delay essential support. It establishes a multidisciplinary review involving service users, caseworkers, legal advisers, data stewards, security specialists and model developers. The team redefines the target as verified urgency using a carefully reviewed annotation protocol, rather than historical approval alone. It removes unnecessary features, limits access, encrypts data and establishes retention rules.

The agency evaluates recall, false-negative rates, calibration and service time by intersectional groups, with uncertainty intervals where feasible. It uses the model only to suggest review order, displays uncertainty and reasons, and requires caseworker confirmation for urgent cases. Applicants receive information about the process and a route to correct records or request review. A dashboard monitors delay, overrides, complaints, subgroup outcomes, drift and security events. Release is staged, with rollback criteria and quarterly independent review.

Outcome

In this illustrative case, predictive accuracy decreases slightly, but missed urgent cases and disparities in processing time decline. More importantly, the system’s purpose becomes narrower and accountable. The agency can explain who is responsible, what the model may do, how it is monitored and how an affected person can challenge an error.

Learning points

  1. Historical outcomes are not automatically valid labels.
  2. A model’s actual use may differ from its stated use.
  3. Fairness requires process redesign, not only threshold adjustment.
  4. Human oversight must include authority, evidence and recourse.
  5. Governance evidence should be created before deployment, not reconstructed after an incident.

11. Challenges and Critical Limitations

No fairness metric captures justice in every context. Group averages can hide intersectional harms, and measuring protected attributes may itself create privacy concerns. Privacy mechanisms reduce risk but can complicate utility, debugging and accountability. Explainability methods may encourage false confidence. Human review can introduce discretion, inconsistency and automation bias. Audits are snapshots and may miss rare failures or adaptive attackers.

Regulation can fragment requirements across jurisdictions, while organisational incentives may reward rapid deployment over careful assessment. Third-party foundation models create uncertainty about training data, hidden dependencies, updates and downstream responsibility. Monitoring also creates a surveillance risk if logs or feedback are collected without restraint. Finally, technical controls cannot resolve fundamentally political questions about which decisions should be automated, whose values should govern, or whether a service should exist in its current form.

A mature organisation therefore combines technical testing with participatory consultation, legal analysis, operational controls, independent challenge and willingness not to deploy. The most responsible system may sometimes be a simpler model, a human process, or no automated system at all.

12. Best-Practice Checklist

Before deployment, an accountable team should be able to answer yes to the following questions:

  • Is the purpose necessary, legitimate and clearly bounded?
  • Are affected people and vulnerable groups identified?
  • Are data provenance, legal basis, quality and retention documented?
  • Have relevant fairness, privacy, security and robustness tests been completed?
  • Are limitations, prohibited uses and uncertainty explicit?
  • Is there a named accountable owner and independent challenge function?
  • Are human review, appeal, correction and shutdown procedures operational?
  • Are model, data and dependency versions reproducible?
  • Are monitoring thresholds, alerts and rollback criteria defined?
  • Have environmental costs been measured and reduced where practical?
  • Is evidence sufficient for internal audit, regulators and affected stakeholders?

Chapter Summary

Responsible AI is a socio-technical approach to ensuring that ML systems serve legitimate purposes without imposing unjustified harm. Bias may enter through history, sampling, measurement, labels, aggregation, evaluation and deployment; fairness metrics such as demographic parity, equal opportunity, equalized odds and predictive parity answer different questions and may conflict. Privacy requires minimisation, purpose limitation, access control and retention governance, while differential privacy offers a formal but bounded guarantee.

Robustness and security require threat modelling, secure supply chains, adversarial testing, monitoring for distribution shift and safe fallback behaviour. Documentation, audit logs, model and dataset records, explainability and reproducibility make systems inspectable. Human oversight is meaningful only when people have competence, authority and recourse mechanisms. ML governance spans framing, data, development, validation, approval, deployment, monitoring, change and retirement, supported by clear ownership and risk classification. Regulatory frameworks provide important baselines but do not replace substantive ethical and technical judgement. Finally, sustainability requires measuring energy and emissions across training and inference and choosing efficient solutions without compromising safety or rights.

Key Terms

Responsible AI: Design and use of AI systems that account for social, ethical, legal, safety and environmental consequences.

ML governance: Policies, roles, processes and controls that direct and oversee machine-learning systems.

Historical bias: Distortion inherited from unequal conditions or decisions represented in data.

Representation bias: Systematic under- or over-representation of a population in data.

Demographic parity: Equality of prediction or selection rates across specified groups.

Equal opportunity: Approximate equality of true-positive rates across groups.

Equalized odds: Approximate equality of true-positive and false-positive rates across groups.

Predictive parity: Equality of positive predictive values across groups.

Pseudonymisation: Replacement of identifiers with tokens while retaining possible re-identification.

Differential privacy: A formal bound on how much one person’s data changes a mechanism’s output distribution.

Distribution shift: Change between training conditions and deployment conditions.

Data poisoning: Malicious or harmful alteration of training data.

Evasion attack: Manipulation of an input to cause an incorrect inference at deployment.

Model card: Documentation describing a model’s purpose, performance, limitations and appropriate use.

Auditability: Capacity to reconstruct and independently examine decisions and controls.

Contestability: Ability of affected people to question, correct or appeal a decision.

Human-on-the-loop: Automated operation with human monitoring and intervention authority.

Risk classification: Categorisation of a system according to potential impact and required controls.

Carbon intensity: CO₂-equivalent emissions associated with a unit of consumed electricity.

Residual risk: Risk remaining after controls have been implemented.

Review Questions

Short Answer Questions

  1. Why is removing a protected attribute insufficient to guarantee fairness?
  2. Distinguish pseudonymisation from anonymisation.
  3. What is the difference between equal opportunity and demographic parity?
  4. Why should model documentation include prohibited uses?
  5. Give two examples of meaningful human oversight.

Long Answer Questions

  1. Explain how bias can enter an ML life cycle from problem formulation through deployment. Propose controls for each stage.
  2. Compare demographic parity, equalized odds and predictive parity. Explain why they may be mutually incompatible.
  3. Describe a governance process for a high-impact ML system, including roles, evidence and monitoring.
  4. Analyse the relationship between privacy, security and auditability. Give examples of tensions among them.
  5. Discuss how environmental sustainability should influence model selection, training and deployment decisions.

Analytical / Application Questions

  1. A fraud model has equal accuracy across two groups but a substantially higher false-positive rate for one group. Analyse what this means for fairness and recommend an evaluation plan.
  2. Design a threat model for an ML service exposed through a public API. Identify assets, attack vectors, controls and residual risks.
  3. An organisation wants to use historical promotion decisions as labels for an employee-promotion model. Assess the proposal and design a safer alternative, including validation, oversight and appeal mechanisms.

Multiple Choice Questions with Answers

Question 1

Which fairness condition requires approximately equal true-positive rates across protected groups?

A. Demographic parity

B. Equal opportunity

C. Predictive parity

D. Calibration

Correct Answer: B. Equal opportunity

Question 2

A model selects 60% of group A and 30% of group B. What is the selection-rate ratio for B relative to A?

A. 0.20

B. 0.50

C. 1.50

D. 2.00

Correct Answer: B. 0.50

Question 3

In differential privacy, ε primarily represents what?

A. The model’s classification accuracy

B. The number of protected groups

C. A bound on privacy loss

D. The number of training epochs

Correct Answer: C. A bound on privacy loss

Question 4

Which attack alters training examples to influence a model’s later behaviour?

A. Data poisoning

B. Model extraction

C. Evasion

D. Denial-of-service monitoring

Correct Answer: A. Data poisoning

Question 5

Which practice most directly supports reproducibility?

A. Removing all experiment logs

B. Versioning code, data references and configurations

C. Using only the largest available model

D. Disabling independent validation

Correct Answer: B. Versioning code, data references and configurations

Question 6

What makes human oversight meaningful rather than symbolic?

A. The reviewer always accepts the model output

B. The reviewer lacks access to reasons

C. The reviewer has competence, authority and time to intervene

D. The system prevents appeals

Correct Answer: C. The reviewer has competence, authority and time to intervene

Question 7

If average power is 2 kW for 5 hours, what energy is consumed under the stated assumptions?

A. 0.4 kWh

B. 2 kWh

C. 7 kWh

D. 10 kWh

Correct Answer: D. 10 kWh

Question 8

Which is an example of deployment bias?

A. A label contains historical discrimination

B. A population is absent from the training data

C. A model is used for a purpose different from its validated purpose

D. A sensor measures one group less accurately

Correct Answer: C. A model is used for a purpose different from its validated purpose

Question 9

What is the primary purpose of a model card?

A. To conceal model limitations

B. To document intended use, performance and limitations

C. To replace security controls

D. To guarantee legal compliance in every jurisdiction

Correct Answer: B. To document intended use, performance and limitations

Question 10

Which governance action is most appropriate after a consequential change in model purpose?

A. Deploy immediately without testing

B. Reuse the original approval automatically

C. Reassess risks and revalidate the system

D. Delete all monitoring records

Correct Answer: C. Reassess risks and revalidate the system

Machine Learning
Chapter 14 of 15

Machine Learning

Chapter 15
Enterprise Applications, Strategy and Emerging Directions

Post Graduate Program in AI and ML
Machine Learning   •   Post Graduate

Enterprise Applications, Strategy and Emerging Directions

Introduction

Machine learning (ML) has moved from experimental laboratories into customer platforms, financial systems, factories, hospitals, logistics networks and public services. The central enterprise question is no longer whether an organization can train a model, but whether it can identify a valuable decision, obtain reliable data, integrate predictions into work, govern the resulting system and sustain measurable benefits.

Enterprise ML is therefore a socio-technical capability. A technically accurate model may create little value if its predictions arrive too late, are not trusted, cannot be acted upon, or expose the organization to unacceptable legal and reputational risk. Conversely, a relatively simple model may produce substantial value when it is embedded in a high-volume decision process with clear ownership.

This chapter evaluates major enterprise applications, build-versus-buy decisions, value and return on investment, organizational readiness, digital transformation, and emerging directions including generative AI, foundation models, AutoML, federated learning and edge AI.

Learning Objectives

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

  1. Evaluate ML use cases in terms of business value, feasibility, risk and organizational fit.
  2. Compare building, buying and hybrid sourcing strategies.
  3. Construct a defensible value and ROI assessment for an ML initiative.
  4. Explain organizational readiness, MLOps and the relationship between ML and digital transformation.
  5. Analyse recommendation, fraud detection and predictive-operations applications.
  6. Critically assess generative AI, foundation models, AutoML, federated learning and edge AI.
  7. Identify workforce, governance, ethical and regulatory implications.

1. From Model Accuracy to Enterprise Value

An ML project usually has five linked layers:

  • Decision: the business action to be improved, such as approving a transaction or scheduling maintenance.
  • Prediction: an estimate, ranking, classification or generated output.
  • Workflow: the human or automated process that consumes the output.
  • Outcome: revenue, cost, risk, quality, speed or customer experience.
  • Governance: controls that determine whether the system is lawful, safe, explainable and accountable.

A use case should begin with a decision rather than with a fashionable algorithm. A useful framing question is: Which decision is repeated often enough, consequential enough and observable enough that better information could improve it? The answer should specify the decision owner, the intervention available after prediction and the cost of errors.

A portfolio view of use cases

Organizations commonly classify opportunities using four dimensions:

DimensionQuestions
ValueDoes the use case increase revenue, reduce cost, reduce risk or improve service?
FeasibilityAre data, infrastructure, skills and integration available?
RiskCould errors cause discrimination, safety harm, privacy loss or regulatory breach?
Change readinessWill employees, customers and managers adopt the new process?

A high-value, low-feasibility project may be a strategic investment, but it should not be confused with a quick operational win. A low-risk pilot can build capability, whereas a high-risk automated decision requires stronger validation, human oversight and formal approval.

2. Major Enterprise Applications

2.1 Recommendation and personalization

Recommendation systems select or rank products, media, advertisements, learning resources or next-best actions. Collaborative filtering uses relationships among users and items; content-based methods use item attributes; modern systems often combine interaction history, context, embeddings and business constraints.

The technical objective is not merely to predict what a user might like. It is to optimize an organizational objective subject to constraints such as inventory, fairness, diversity, latency and regulation. Offline metrics such as precision, recall, normalized discounted cumulative gain and mean reciprocal rank are useful, but online experiments are normally required because a ranking change can alter user behaviour. Long-term measures may include retention, contribution margin and customer trust.

Important risks include popularity bias, filter bubbles, exclusion of new users or products, manipulation, and feedback loops. If the system recommends only items that have historically received attention, the resulting data may reinforce the original imbalance.

2.2 Fraud and anomaly detection

Fraud systems combine supervised learning, anomaly detection, graph analysis, rules and investigator feedback. A transaction may be represented by amount, location, device, account history, merchant, time and relationships to other entities.

Fraud is highly imbalanced: legitimate transactions greatly outnumber fraudulent ones. Accuracy is therefore often misleading. Precision measures the proportion of flagged cases that are fraudulent, while recall measures the proportion of fraud that is detected:

Precision = TP ÷ (TP + FP)
Recall = TP ÷ (TP + FN)

Here TP is true positives, FP false positives and FN false negatives. These metrics assume a defined positive class and a fixed decision threshold. Precision is important when investigations are expensive or customers are frequently inconvenienced; recall is important when missed fraud is costly. The operating threshold should be selected using business costs, not a default probability such as 0.5.

A cost-sensitive formulation is:

Expected loss = CFN × FN + CFP × FP + CFTP × FTP + CTN × TN

where C denotes the cost associated with each outcome. Usually C_TN is zero or very small, while C_FN, the cost of missed fraud, and C_FP, the cost of an incorrect alert, vary by context. The model should be evaluated over time because fraudsters adapt and data distributions change.

2.3 Predictive operations

Predictive operations includes predictive maintenance, demand forecasting, quality monitoring, energy optimization and supply-chain planning. A maintenance model may estimate the probability of failure within a time window. The value comes only when the organization can inspect, repair or replace equipment at an appropriate time.

A simple expected-value analysis illustrates the logic. Suppose a plant has 100 machines. Without prediction, 10 failures occur annually, each costing £20,000. A model identifies 80% of failures, and preventive intervention costs £4,000 per identified machine. Ignoring false alarms and assuming each identified failure is prevented, the avoided failure cost is 8 × £20,000 = £160,000, while intervention costs are 8 × £4,000 = £32,000. The estimated gross benefit is £128,000 before model, integration and change costs. This is an illustrative calculation, not a guarantee: false positives, lead time, spare-parts availability and intervention effectiveness must be included in a real business case.

Forecasting systems also require decision-aware evaluation. A small numerical error during a stockout period may matter more than a larger error during low demand. Model metrics should therefore be connected to inventory, service-level and working-capital consequences.

3. Build, Buy or Hybrid?

The sourcing decision is strategic. Building internally can provide differentiation, control and learning, but it requires sustained investment in data engineering, security, evaluation, deployment and operations. Buying can reduce time to value and provide specialist capabilities, but may introduce vendor dependence, opaque models, data-transfer risks and limited customization.

CriterionBuildBuyHybrid
Time to deploymentUsually slowerUsually fasterModerate
CustomizationHighVendor-dependentHigh at selected layers
Internal learningHighLowerModerate to high
Cost profileSignificant fixed capability costSubscription, usage or licence costMixed
Control and portabilityHigh if well engineeredMay be limitedMust be contractually designed
Best fitStrategic differentiationCommodity capabilityPlatform plus proprietary workflow

A capability is a candidate for internal development when it is central to competitive advantage, depends on proprietary data, requires unusual constraints or must be controlled for regulatory reasons. Buying is attractive for generic components such as document extraction, common forecasting, infrastructure or foundation-model access, provided security and performance are acceptable.

A hybrid strategy may use a cloud platform and pretrained model while retaining proprietary features, decision rules, evaluation data and workflow integration. Contracts should address data ownership, retention, model changes, service levels, audit rights, incident notification, portability and termination assistance. A vendor's benchmark performance is not evidence of performance on the organization's population or decision costs.

4. Measuring Value and ROI

The financial case should separate benefits, costs, timing and uncertainty. A basic return-on-investment measure is:

ROI = (Total benefits − Total costs) ÷ Total costs × 100%

Benefits and costs must use the same currency and time horizon. Benefits may include incremental contribution margin, avoided loss, reduced processing time, lower inventory or improved retention. Costs include data preparation, software, cloud compute, integration, validation, training, monitoring, support and organizational change.

For projects with multi-year cash flows, net present value is more informative:

NPV = −I0 + ∑(t=1 to T) [CFt ÷ (1 + r)t]

I₀ is the initial investment, CFₜ is net cash flow in period t, r is the discount rate per period and T is the number of periods. The formula assumes cash flows are measured consistently and that the discount rate reflects the relevant risk and timing. A positive NPV indicates value under the chosen assumptions; it does not remove estimation uncertainty.

A credible business case should include:

  1. A baseline measured before deployment.
  2. A counterfactual or control group where feasible.
  3. Incremental rather than merely correlated outcomes.
  4. Costs of errors and human review.
  5. Sensitivity analysis for adoption, performance, volume and price.
  6. Non-financial effects such as safety, fairness, resilience and trust.

For example, if an intervention produces £300,000 in annual incremental contribution, annual operating cost is £90,000 and initial implementation cost is £210,000, first-year net benefit is £0 and first-year ROI is 0% under the simple formula. A claim of success would therefore require either later benefits, strategic value or a different verified baseline—not inflated attribution.

5. Organizational Readiness and Digital Transformation

Readiness is multidimensional. A technically mature organization may still lack accountable product owners, process redesign or employee trust. A practical readiness assessment examines:

  • Strategic readiness: an explicit problem, sponsor, target outcome and funding model.
  • Data readiness: lawful access, quality, lineage, representativeness and reliable labels.
  • Technical readiness: APIs, identity management, cloud or on-premises infrastructure, observability and deployment pipelines.
  • Operating readiness: product ownership, domain experts, data scientists, engineers, security and model-risk specialists.
  • Cultural readiness: willingness to test, learn, challenge outputs and redesign work.
  • Governance readiness: policies, risk classification, documentation, approval and incident response.

Digital transformation is broader than adding prediction to an existing process. It often involves redesigning the process around continuous data, experimentation and rapid feedback. ML projects fail when organizations automate a broken process, treat data as a by-product, or assign responsibility only to the data-science team.

MLOps as an operating discipline

MLOps connects development and operations through versioned data and code, reproducible training, automated testing, deployment controls, monitoring and retraining procedures. Monitoring should cover not only infrastructure but also data drift, prediction drift, calibration, subgroup performance, latency, cost and outcome quality.

A deployed model is a changing system. Population shifts, policy changes, seasonality, adversarial behaviour and upstream data modifications can invalidate assumptions. Retraining should not be triggered blindly by every statistical change; teams must distinguish harmless drift from degradation in decision outcomes.

6. Generative AI and Foundation Models

Generative AI produces text, code, images, audio or other content from learned representations. Foundation models are large pretrained models adapted to many downstream tasks through prompting, retrieval, fine-tuning or tool use. Their generality can reduce the cost of prototyping, but it does not remove the need for domain evaluation.

Enterprise patterns include:

  • retrieval-augmented generation, in which relevant organizational documents are retrieved and supplied as context;
  • structured extraction from invoices, contracts and support messages;
  • coding assistance and test generation;
  • conversational service and internal knowledge search;
  • summarization and drafting with human approval;
  • agentic workflows that call tools under explicit permissions.

A language model can produce fluent but false content, commonly called hallucination. Retrieval can improve grounding but cannot guarantee correctness when documents are incomplete, conflicting or maliciously constructed. Evaluation should test factuality, task success, refusal behaviour, privacy leakage, robustness, latency and cost.

Foundation-model governance includes data provenance, copyright and licensing, prompt and output retention, confidential-data controls, access management, content safety and human accountability. High-impact decisions should not be delegated merely because a model is persuasive. An enterprise should define which uses are assistive, which require review and which are prohibited.

7. AutoML, Federated Learning and Edge AI

AutoML automates parts of feature construction, model selection, hyperparameter search and deployment. It can improve accessibility and establish strong baselines. However, automation does not determine the correct target, sampling frame, label policy, fairness standard or intervention. AutoML can accelerate a poorly posed problem just as efficiently as a well-posed one. Search procedures also require validation discipline to avoid leakage and overfitting to a test set.

Federated learning trains a shared model across decentralized devices or institutions while keeping local training data in place. In a simplified round, a coordinator sends a model to participants; each participant computes an update on local data; the coordinator aggregates updates, often using a data-weighted average; and the process repeats. Keeping raw data local can reduce data movement, but it does not automatically guarantee privacy. Model updates may leak information, participants may be malicious, and institutions may have non-identically distributed data. Secure aggregation, differential privacy, authentication and robust aggregation may be required.

Edge AI runs inference near sensors, devices or users rather than exclusively in a central cloud. Advantages include lower latency, reduced bandwidth, operation during intermittent connectivity and improved local control. Constraints include limited compute, energy, storage, physical tampering and more difficult fleet management. Edge deployment should define update, rollback, telemetry and security procedures before scale-up.

8. Responsible Enterprise ML

Responsible ML is both an ethical obligation and a risk-management requirement. Relevant principles include legitimacy, purpose limitation, data minimization, security, fairness, transparency, contestability, accessibility and accountability. Requirements differ by jurisdiction and application; organizations should consult applicable privacy, consumer-protection, employment, sectoral and AI regulation.

Fairness cannot be reduced to one universal metric. Demographic parity, equalized error rates and calibration may conflict when base rates differ. Teams must identify the harm being addressed, select a justified measure, examine subgroup performance and involve affected stakeholders. Explanations should be appropriate to the audience: a customer may need a meaningful reason and appeal route, while an engineer may need feature, data and version detail.

Human oversight must be substantive. A reviewer who is pressured to accept every automated recommendation does not provide meaningful oversight. Governance should allocate decision rights, define escalation thresholds, preserve audit logs and test for automation bias.

Security threats include data poisoning, adversarial inputs, model extraction, prompt injection, insecure tools and supply-chain compromise. Threat modelling should cover the full pipeline rather than only the model artifact.

9. Illustrative Case: Predictive Maintenance in a Logistics Network

Background

A fictional parcel company operates 1,200 automated sorting units. Unexpected stoppages delay deliveries and require expensive emergency repairs. Maintenance is mainly calendar-based, causing both premature replacement and occasional catastrophic failure.

Problem and analysis

The company proposes predicting whether a unit will fail within seven days using vibration, temperature, motor current and maintenance history. Initial analysis finds that sensors are missing for some sites and that failure labels are inconsistently recorded. Engineers also report that a prediction is useful only if issued at least 48 hours before failure and if replacement parts are available.

The project team therefore defines success as a reduction in unplanned downtime, not merely a high classification score. It creates a data dictionary, reconciles maintenance records, and uses a time-based evaluation split to avoid training on future information. It measures recall for failures with at least 48 hours' lead time, false-alert workload, calibration and site-level performance.

Approach and solution

A transparent baseline model is compared with a gradient-boosted model. The system sends alerts to a maintenance queue rather than directly shutting down equipment. Engineers confirm or reject alerts, and their decisions become feedback data. A pilot runs at selected sites with a comparison group. Parts inventory and technician capacity are included in the intervention policy.

Outcome

In this illustrative case, the pilot reduces unplanned downtime by 14% and emergency repair expenditure by 9%, but only after the company changes its parts-replenishment process. The model performs poorly at one site because sensor calibration differs; the team pauses expansion there rather than hiding the weakness. The business case is approved for wider deployment with site-specific monitoring, quarterly review and a rollback procedure.

Learning points

The case demonstrates that value arose from data quality, lead time, workflow redesign and maintenance capacity—not from model selection alone. It also shows why a controlled pilot, operational metrics and explicit failure handling are essential.

10. Future Workforce and Strategic Directions

ML changes tasks more often than it eliminates whole occupations. Repetitive classification, drafting, search and monitoring may be automated or accelerated, while problem formulation, judgment, relationship management, verification and accountability become more important. New roles include ML product manager, data steward, model-risk specialist, AI auditor, prompt and workflow designer, evaluation engineer and responsible-AI lead.

Organizations should invest in three forms of literacy: technical literacy for specialists, operational literacy for managers and critical AI literacy for all affected workers. Reskilling should be linked to redesigned roles rather than offered as an isolated course. Worker consultation is particularly important when systems influence performance evaluation, scheduling or employment decisions.

Strategically, the durable advantage is unlikely to be access to a generic model alone. It is more likely to arise from proprietary data, trusted workflows, rapid experimentation, domain expertise, distribution, governance and the ability to learn from outcomes. Future enterprise architectures will combine general models with specialized models, retrieval, tools, rules, human review and edge devices. The key managerial task is to allocate autonomy according to consequence: low-risk tasks may be automated, while high-impact decisions require stronger evidence, review and appeal.

Best-Practice Checklist

  1. Start with a measurable decision and named owner.
  2. Establish a baseline and define the counterfactual.
  3. Assess data legality, quality, representativeness and lineage.
  4. Compare a simple baseline with more complex alternatives.
  5. Evaluate at the operating threshold and under realistic costs.
  6. Pilot in the real workflow with monitoring and rollback.
  7. Document data, model, limitations, stakeholders and decisions.
  8. Design security, privacy, fairness and human oversight from the beginning.
  9. Contract for vendor transparency, portability and incident response.
  10. Review benefits after deployment and retire systems that no longer justify their risks or costs.

Chapter Summary

Enterprise ML succeeds when a reliable prediction improves an owned decision inside a workable process. Recommendation systems, fraud detection and predictive operations illustrate different objectives, error costs and feedback loops. Build-versus-buy choices should consider differentiation, control, skills, time, risk and portability. ROI requires a measured baseline, credible attribution, complete costs and explicit uncertainty; NPV is useful for multi-period investment decisions.

Organizational readiness includes strategy, data, technology, operating model, culture and governance. MLOps sustains deployed systems through reproducibility, monitoring, controlled change and incident response. Generative AI and foundation models expand capability but introduce hallucination, privacy, copyright, security and accountability risks. AutoML accelerates modelling without replacing problem definition. Federated learning keeps raw data decentralized but is not automatically private, while edge AI offers low latency at the cost of device and fleet-management complexity.

The strategic future of ML depends on responsible integration with human work. Durable value comes from proprietary context, redesigned workflows, measurement, trust and organizational learning rather than from model novelty alone.

Key Terms

Enterprise ML: Application of machine learning within organizational decisions and workflows.

Use case: A defined problem in which an ML output supports an action or decision.

Recommendation system: A system that ranks or selects items, content or actions for users.

Fraud detection: Identification of transactions or behaviour suspected to be deceptive or unauthorized.

Predictive operations: Use of predictions to improve maintenance, quality, demand or process control.

Precision: The proportion of predicted positive cases that are truly positive.

Recall: The proportion of actual positive cases that are detected.

Build-versus-buy: A strategic choice between internal development and external sourcing.

Hybrid sourcing: Combination of purchased platforms or models with internally developed components.

ROI: Net benefit divided by total cost, expressed as a percentage.

NPV: Present value of future cash flows less initial investment.

MLOps: Practices for developing, deploying, monitoring and governing ML systems.

Data drift: Change in the distribution or quality of input data over time.

Foundation model: A broadly pretrained model adapted to multiple downstream tasks.

Generative AI: AI that produces new content such as text, code, images or audio.

AutoML: Automation of selected steps in model development and optimization.

Federated learning: Collaborative model training in which raw data remains decentralized.

Edge AI: AI inference performed near the data source or user device.

Human oversight: Meaningful human responsibility, review and intervention in an AI-enabled process.

Model risk: The possibility that model errors, misuse or limitations cause adverse outcomes.

Review Questions

Short Answer Questions

  1. Why should an enterprise ML project begin with a decision rather than an algorithm?
  2. Distinguish precision from recall in fraud detection.
  3. State two advantages and two limitations of buying an ML capability.
  4. What organizational dimensions should be examined in an ML-readiness assessment?
  5. Why does federated learning not automatically guarantee privacy?

Long Answer Questions

  1. Explain how recommendation systems should be evaluated using both technical and business measures.
  2. Compare build, buy and hybrid strategies, including their implications for control, capability and vendor risk.
  3. Discuss how MLOps supports the reliability of ML systems after deployment.
  4. Analyse the opportunities and governance challenges associated with generative AI and foundation models.
  5. Explain why digital transformation requires process and organizational redesign rather than simply deploying predictive models.

Analytical / Application Questions

  1. A fraud team has a high false-positive rate but is under pressure to increase fraud detection. Design a threshold-selection and evaluation approach using costs, precision, recall and investigator capacity.
  2. A manufacturer is considering predictive maintenance. Specify the baseline, intervention policy, pilot design, metrics and financial assumptions needed to justify investment.
  3. An organization wants to deploy a foundation model to answer employee policy questions. Propose an architecture and governance plan addressing retrieval, hallucination, confidential data, evaluation, human escalation and auditability.

Multiple Choice Questions with Answers

Question 1

Which statement best explains why model accuracy alone is insufficient for enterprise ML?

A. Accuracy cannot be calculated for supervised models.

B. A model must improve an actionable decision and produce acceptable outcomes in context.

C. Accuracy is always less important than training speed.

D. Enterprise models do not require evaluation.

Correct Answer: B. A model must improve an actionable decision and produce acceptable outcomes in context.

Question 2

If TP = 80 and FP = 20, what is precision?

A. 20%

B. 40%

C. 80%

D. 100%

Correct Answer: C. 80%

Question 3

Which situation most strongly supports building an ML capability internally?

A. The capability is a generic commodity with many reliable vendors.

B. The capability depends on proprietary data and is central to competitive advantage.

C. The organization has no technical staff or data access.

D. The project is a short-lived experiment with no strategic relevance.

Correct Answer: B. The capability depends on proprietary data and is central to competitive advantage.

Question 4

What does NPV incorporate that a simple first-year ROI may omit?

A. Only model accuracy

B. The number of training features

C. The timing and discounting of multi-period cash flows

D. The number of employees using the system

Correct Answer: C. The timing and discounting of multi-period cash flows

Question 5

Which is an essential MLOps activity after deployment?

A. Removing all version control to speed experimentation

B. Monitoring data, predictions, outcomes and system performance

C. Preventing any model update under all circumstances

D. Evaluating only the training-set score

Correct Answer: B. Monitoring data, predictions, outcomes and system performance

Question 6

What is a principal limitation of AutoML?

A. It makes data quality irrelevant.

B. It guarantees fairness across all groups.

C. It cannot replace correct problem formulation and governance.

D. It can be used only for unsupervised learning.

Correct Answer: C. It cannot replace correct problem formulation and governance.

Question 7

Which description best characterizes federated learning?

A. All raw data is copied to one central database.

B. Models are trained collaboratively while raw data generally remains at local participants.

C. Only edge devices can perform inference, not training.

D. It eliminates the need for security controls.

Correct Answer: B. Models are trained collaboratively while raw data generally remains at local participants.

Question 8

Which is a common advantage of edge AI?

A. Unlimited computational capacity

B. Lower latency and reduced dependence on continuous connectivity

C. Automatic protection against all attacks

D. Guaranteed higher accuracy than cloud models

Correct Answer: B. Lower latency and reduced dependence on continuous connectivity

Question 9

Why can a recommendation system create a feedback loop?

A. Recommendations never affect user behaviour.

B. Items shown more often may receive more interactions, reinforcing their future visibility.

C. Ranking systems cannot use historical data.

D. Feedback loops occur only in hardware systems.

Correct Answer: B. Items shown more often may receive more interactions, reinforcing their future visibility.

Question 10

Which control is most appropriate for a high-impact generative-AI workflow?

A. Allowing unrestricted automatic decisions because the output is fluent

B. Removing audit logs to protect privacy

C. Defined human review, access controls, evaluation and escalation procedures

D. Using only the vendor's benchmark results

Correct Answer: C. Defined human review, access controls, evaluation and escalation procedures

Machine Learning
Chapter 15 of 15