Machine Learning, Ensembles and Explainable AIIntroduction and contextStatistical and predictive analytics increasingly combine two traditions. Statistical modelling emphasises probability, uncertainty, estimation, and interpretable relationships. Machine learning (ML) emphasises predictive performance, flexible function approximation, and learning patterns from data. These traditions overlap, but they are not identical. A linear regression can be used as a machine-learning predictor, while a highly accurate neural network may provide limited insight into causal relationships. This chapter examines regularisation, random forests, gradient boosting, and introductory neural networks. It then compares statistical and machine-learning approaches, introduces explainability techniques, and evaluates the opportunities and limitations of generative artificial intelligence (AI) in analytics workflows. The central professional principle is that model quality is not determined by accuracy alone. A useful model must also be appropriately validated, operationally reliable, understandable enough for its purpose, and governed responsibly. Learning objectivesBy the end of this chapter, you should be able to: - Explain overfitting, underfitting, bias, variance, and regularisation.
- Describe how random forests and gradient-boosting models generate predictions.
- Explain the structure and training logic of a basic neural network.
- Compare statistical and machine-learning approaches for different business purposes.
- Apply global and local explainability techniques, including permutation importance, partial dependence, SHAP-style explanations, and counterfactuals.
- Design a responsible predictive-analytics workflow involving validation, monitoring, documentation, and human oversight.
- Evaluate appropriate uses and limitations of generative AI in analytics.
1. Generalisation, overfitting and regularisationA predictive model is trained using observations such as (xi, yi), where xi is a vector of input features and yi is the observed outcome for case i. The model produces a prediction \hat{y}i = f(xi; θ), where θ denotes estimated model parameters. The training error measures performance on data used for fitting. The more important quantity is generalisation error: expected error on new observations drawn from the relevant target population. A model that memorises training observations may have low training error but poor generalisation. This is overfitting. A model that is too simple to represent important structure has high training and test error; this is underfitting. For squared-error regression, a common loss function is: MSE = (1/n) ∑ᵢ₌₁ⁿ (yᵢ − ŷᵢ)² Here, n is the number of observations, yi is the observed response, and \hat{y}i is the prediction. The units are the square of the response units. Root mean squared error is RMSE = √MSE, which returns to the original response units. The bias–variance perspectiveFor a new input, expected prediction error can be conceptually decomposed into bias, variance, and irreducible noise: Expected test error ≈ Bias² + Variance + Irreducible noise Bias is systematic error caused by restrictive assumptions. Variance is sensitivity to the particular training sample. Irreducible noise is uncertainty in the outcome that cannot be removed using the available predictors. The decomposition is most directly justified under squared loss and a specified data-generating framework; it is a conceptual guide rather than a universal exact identity for every metric. Increasing model complexity often reduces bias but increases variance. Regularisation controls complexity by penalising parameter values or structures that are likely to produce unstable predictions. Ridge and lasso regressionFor a linear model with an intercept, ridge regression estimates coefficients by minimising: minβ₀,β { ∑ᵢ₌₁ⁿ (yᵢ − β₀ − xᵢᵀβ)² + λ ∑ⱼ₌₁ᵖ βⱼ² } Lasso regression instead uses: minβ₀,β { ∑ᵢ₌₁ⁿ (yᵢ − β₀ − xᵢᵀβ)² + λ ∑ⱼ₌₁ᵖ |βⱼ| } In these expressions, β₀ is the intercept, β is the coefficient vector, p is the number of predictors, λ ≥ 0 controls the penalty, and xiᵀβ is the weighted predictor value. The unnormalised residual sum of squares is used here; implementations may divide it by n or use a factor of 1/2, which changes the numerical scale of the selected penalty but not the principle. Before penalisation, numerical predictors are usually centred and scaled. Otherwise, a coefficient’s penalty is influenced by its measurement unit. The intercept is normally not penalised. Ridge shrinks correlated coefficients towards one another and generally does not set them exactly to zero. Lasso can set some coefficients exactly to zero, supporting sparse models and feature selection. With highly correlated predictors, however, lasso’s selected variable can be unstable; elastic net combines both penalties: Loss + λ [α ∑ⱼ |βⱼ| + (1 − α) ∑ⱼ βⱼ²] where 0 ≤ α ≤ 1. The penalty strength and, where applicable, the mixing parameter should be selected using validation data or cross-validation—not by optimising performance on the test set. A small regularisation exampleSuppose two candidate models have test RMSE values of 8.4 and 8.1. The second model is preferable for prediction if the evaluation design is sound. However, if it uses 80 unstable features while the first uses 12 well-supported features, the first may be preferable operationally where interpretability, data collection cost, or model maintenance matters. Regularisation does not automatically produce a causal model or remove data leakage; it only addresses a particular form of complexity. 2. Ensemble learningAn ensemble combines multiple models. The benefit arises when component models make errors that are not perfectly correlated. If unbiased prediction errors have variance σ² and pairwise correlation ρ, the variance of an average of B equal-weight models is: Var(average) = σ² [ ρ + (1 − ρ)/B ] This formula assumes equal error variance and a common pairwise error correlation. As B increases, the independent component of error declines, but the correlated component remains. Thus, diversity matters as well as the number of models. Bagging and random forestsBootstrap aggregation, or bagging, repeatedly samples training cases with replacement, fits a model to each sample, and averages predictions for regression or votes for classification. A random forest adds random feature selection at each tree split. These two sources of randomness create diverse decision trees. For regression, a forest prediction is commonly: ŷ(x) = (1/B) ∑ᵦ₌₁ᴮ Tᵦ(x) where T_β(x) is the prediction of tree β and B is the number of trees. For binary classification, the forest may use majority vote or average class probabilities, depending on implementation. A tree recursively partitions feature space. At a candidate regression split, an algorithm seeks reductions in within-node squared error. For classification, impurity measures such as Gini impurity may be used: Gini = 1 − ∑ₖ pₖ² where pk is the proportion of observations in the node belonging to class k. Gini impurity is zero when a node contains one class only. Random forests usually require limited feature scaling and capture nonlinearities and interactions automatically. Their limitations include reduced transparency compared with a single shallow tree, potentially large memory requirements, biased variable-importance measures when predictors have different cardinalities or correlated structure, and poor extrapolation beyond the range represented in training data. Out-of-bag (OOB) evaluation uses observations not included in a tree’s bootstrap sample. Each observation can be predicted using trees for which it was OOB. This provides a useful internal performance estimate, but it should not replace a carefully designed final test set where decisions are consequential. Gradient boostingBoosting constructs an additive model sequentially. At each stage, a new weak learner is fitted to reduce the current loss. A simplified regression form is: Fm(x) = Fm−1(x) + η hm(x) Here, Fm(x) is the model after stage m, hm(x) is the new tree or weak learner, and η is the learning rate, usually between 0 and 1. Smaller learning rates often require more trees and can improve generalisation when appropriately regularised. For squared-error loss, the next learner can be fitted to residuals: rᵢₘ = yᵢ − Fm−1(xᵢ) For a general differentiable loss L, boosting uses negative gradients: rᵢₘ = − ∂L(yᵢ, F(xᵢ)) / ∂F(xᵢ) |F = Fₘ₋₁ The exact fitting and line-search procedures differ among implementations. Important controls include tree depth, learning rate, number of iterations, minimum leaf size, subsampling, and early stopping. Boosting often achieves excellent tabular-data performance but is sensitive to tuning, leakage, noisy labels, and distribution shift. It can also be difficult to explain when many interactions are present. Random forests reduce variance primarily through averaging diverse trees. Boosting reduces error sequentially by concentrating on current weaknesses. Neither method is universally superior; validation on the target task is decisive. 3. Introductory neural networksA feed-forward neural network transforms inputs through layers. A neuron first computes a weighted sum and then applies an activation function: z = wᵀx + b, a = g(z) Here, x is the input vector, w is a weight vector, b is a bias, z is the pre-activation value, g is an activation function, and a is the output. Common hidden-layer activations include ReLU, g(z) = max(0,z), and sigmoid, g(z) = 1/(1 + e−z). A multilayer network repeatedly applies this transformation: h(ℓ) = g(W(ℓ)h(ℓ−1) + b(ℓ)) where ℓ identifies the layer, W(ℓ) is a weight matrix, and b(ℓ) is a bias vector. The output layer depends on the task: a linear output is common for continuous regression, while a sigmoid output is common for binary classification and a softmax output for mutually exclusive multiclass classification. Training minimises a loss function using gradient-based optimisation. Backpropagation applies the chain rule to calculate derivatives of the loss with respect to weights. For binary classification, a common loss is cross-entropy: L = − (1/n) ∑ᵢ [ yᵢ log(pᵢ) + (1 − yᵢ) log(1 − pᵢ) ] Here, yi ∈ {0,1} is the observed class and pi is the predicted probability of class 1. The expression assumes valid probabilities strictly between 0 and 1; numerical software uses stable implementations. Neural networks can represent complex nonlinear functions, but success depends on data volume, feature representation, architecture, optimisation, regularisation, and computational resources. Dropout, weight decay, early stopping, data augmentation, and validation-based hyperparameter tuning can reduce overfitting. For ordinary structured business data, tree ensembles frequently provide a strong baseline and may be easier to deploy than a neural network. Neural networks become particularly attractive for images, language, audio, high-dimensional signals, and large-scale representation learning. 4. Statistical modelling and machine learning: complementary purposesStatistical models commonly begin with a probability model for data and parameters. They may support estimation, confidence intervals, hypothesis tests, causal reasoning, and explicit uncertainty quantification. Machine-learning models commonly prioritise predictive risk and flexible functional forms. These are tendencies, not strict categories. | Concern | Statistical emphasis | Machine-learning emphasis |
|---|
| Main question | How are variables related, and with what uncertainty? | How accurately can new cases be predicted? | | Assumptions | Often explicit and interpretable | Often fewer structural assumptions but more data dependence | | Complexity | Controlled through specification and theory | Controlled through validation and regularisation | | Uncertainty | Standard errors, intervals, probability models | Prediction intervals, resampling, ensembles, calibration | | Interpretation | Coefficients may have defined conditional meanings | Importance and explanation methods may be model-dependent | | Typical risk | Misspecification and confounding | Leakage, overfitting, drift, and opaque decisions |
A regression coefficient is not automatically causal. In a conditional model, a coefficient describes an association conditional on included variables and the model assumptions. Omitted confounding, selection bias, measurement error, reverse causality, and post-treatment variables can invalidate causal interpretation. Predictive importance likewise does not establish that changing a feature will change an outcome. A practical workflow can combine both traditions: use domain theory to define the target and predictors; use interpretable statistical models as baselines; evaluate flexible learners using nested or time-aware validation; quantify uncertainty; and select the model according to the decision’s consequences rather than a single leaderboard metric. 5. Explainable AI and interpretable analyticsExplainability concerns how a model’s behaviour is communicated to people. Interpretability may be built into the model, as in a small decision tree or constrained additive model. Post-hoc explanation methods analyse a fitted black-box model after training. An explanation is not a proof of causality, nor does it guarantee that the model is fair. Global and local explanationsGlobal explanations describe average model behaviour across a population. Examples include permutation importance, partial-dependence plots, accumulated local effects, and surrogate models. Local explanations describe one prediction, such as why a particular loan application received a high-risk score. Permutation importance measures the performance deterioration after shuffling one feature, ideally within an evaluation set: Importancej = Scoreoriginal − Scorepermuted(j) The definition depends on the score direction. If lower loss is better, one may instead use Losspermuted(j) − Lossoriginal. Correlated predictors can share or mask importance, so permutation importance should not be interpreted as a unique causal contribution. Partial dependence for feature xj is conceptually: PDj(z) = E_{X−j} [ f(z, X−j) ] It averages predictions after setting feature xj to z while retaining other features. The expectation is approximated using the observed data. Partial dependence can be misleading when xj is strongly correlated with other features, because it may evaluate unrealistic combinations. Individual conditional expectation (ICE) displays prediction curves for individual cases rather than only their average. Accumulated local effects often behave better in correlated settings because they focus on local changes within observed feature regions. SHAP-style explanations assign contributions relative to a baseline prediction. In a simplified additive presentation: f(x) = φ₀ + ∑ⱼ φⱼ Here, φ₀ is a baseline value and φj is the contribution attributed to feature j under a specified feature-coalition and dependence convention. Different explainers make different assumptions about how features are removed or conditioned. Therefore, SHAP values are explanations of a model under a chosen reference framework, not universal causal effects. Counterfactual explanations ask what minimally should change to obtain a desired prediction, for example: “If monthly debt had been lower by a specified amount and employment history longer, the predicted risk category would have changed.” A responsible counterfactual must be actionable, feasible, lawful, and respectful of immutable or sensitive attributes. “Minimal change” also requires a defined distance metric and constraints; otherwise, it may recommend impossible or harmful interventions. Explanation quality and governanceA useful explanation should be faithful to the model, understandable to its audience, stable under small perturbations when stability is expected, and relevant to the decision. Explanations should be tested with domain experts and affected users. They should disclose the model version, reference population, time period, feature definitions, and limitations. Fairness assessment should examine outcomes across relevant groups, while recognising that group definitions and legal requirements vary by jurisdiction. Potential measures include selection-rate differences, false-positive and false-negative disparities, calibration, and equalised-error criteria. These measures can conflict mathematically and operationally; no single fairness metric resolves all ethical questions. 6. A professional modelling workflowA robust workflow is iterative rather than a one-time algorithm choice. - Define the decision. Specify who acts, what outcome is predicted, the prediction horizon, and the cost of errors.
- Audit data provenance. Record source systems, collection mechanisms, permissions, missingness, duplicates, and measurement changes.
- Split data correctly. Use stratification where appropriate, grouped splits where entities recur, and chronological splits for forecasting or time-dependent decisions.
- Establish baselines. Compare against a naive rule, historical rate, linear model, or simple tree.
- Engineer features without leakage. Every feature must be available at prediction time. Aggregations must respect the information cutoff.
- Tune using training data. Cross-validation, ideally nested when estimating generalisation after extensive tuning, should not expose the final test set.
- Evaluate decision-relevant metrics. Classification may require precision, recall, specificity, F1, log loss, area under a relevant curve, calibration, and cost-weighted utility. Regression may require MAE, RMSE, quantile loss, or business-impact measures.
- Assess robustness. Examine subgroups, missing-data patterns, plausible shifts, adversarial or extreme cases, and sensitivity to preprocessing choices.
- Explain and document. Maintain a model card or equivalent record covering purpose, data, performance, limitations, intended use, and prohibited use.
- Deploy with controls. Monitor data drift, prediction drift, performance, calibration, latency, failures, and human overrides. Define retraining and rollback procedures.
Calibration is especially important when probabilities drive resource allocation. A classifier is calibrated if, among cases assigned probability near p, approximately that fraction experiences the event, subject to sampling uncertainty and the chosen population. High discrimination does not imply good calibration. 7. Generative AI in analytics workflowsGenerative AI systems produce text, code, images, synthetic records, or other outputs from learned patterns. In analytics, they can accelerate SQL drafting, code explanation, documentation, exploratory-question generation, data-dictionary creation, scenario analysis, and communication of results. They can act as an interface to tools, provided tool permissions, validation, and audit logs are controlled. The principal limitations are hallucinated facts or code, sensitivity to prompts, hidden training-data issues, confidentiality risks, prompt injection, copyright and licensing uncertainty, automation bias, and weak awareness of organisational context. Generated synthetic data may reproduce privacy-sensitive patterns or fail to preserve rare but important relationships. A fluent explanation is not evidence that an analysis is correct. Best practice is to treat generative AI as an assistant rather than an autonomous analyst. Do not submit confidential data to an unapproved service. Require executable code review, independent recalculation, source verification, tests, reproducibility, and human sign-off. Separate generated hypotheses from validated findings. In regulated or high-impact settings, retain records of prompts, model versions, retrieved materials, outputs, approvals, and changes. 8. Illustrative case: predicting customer churnBackgroundA subscription company wants to identify customers at risk of cancelling within the next 30 days. The organisation has 18 months of account activity, support contacts, payment history, contract information, and service-usage data. ProblemThe initial model reported 94% accuracy, but churn was only 6% of accounts. A trivial model predicting “no churn” for everyone would achieve 94% accuracy, so the result was operationally unhelpful. Investigation also found that a feature recording a cancellation request had been created after the prediction date, causing leakage. Analysis and approachThe analytics team defined a prediction timestamp and excluded all information created after it. It used a chronological split: earlier months for training, a later period for validation, and the most recent month as a locked test set. A regularised logistic regression provided an interpretable baseline. A random forest and gradient-boosting model were then tuned using validation data. The company selected a probability threshold based on retention-team capacity and the relative costs of contacting a customer unnecessarily versus missing a likely cancellation. It assessed precision, recall, calibration, expected intervention cost, and performance by customer segment. Permutation importance and local explanations were reviewed with service managers. A counterfactual recommendation was constrained to actionable variables, such as unresolved support issues, and excluded protected characteristics and immutable personal attributes. OutcomeThe boosting model produced the best validation utility, but the performance advantage over the forest was modest. The final decision therefore considered explanation stability, scoring latency, and maintenance cost. The model was deployed with a human review queue, weekly drift checks, monthly performance review, and a rollback rule. Retention actions were tested through a controlled experiment rather than assuming that a high-risk prediction implied that an offer would cause retention. Learning pointsThe case demonstrates that accuracy can be misleading under class imbalance; leakage can create false confidence; threshold selection is a business decision; explanation is distinct from causation; and a predictive score should not be confused with evidence that an intervention will work. Challenges and limitationsMachine-learning systems can fail because training data do not represent future cases, labels are inconsistent, sensors or processes change, or the target itself reflects past institutional decisions. Class imbalance can make aggregate metrics deceptive. Correlated observations can inflate apparent sample size. Hyperparameter search can overfit the validation process. A model may be statistically accurate but operationally useless if predictions arrive too late or no action is available. Explainability methods also have technical limitations: feature attribution depends on a baseline and dependence assumptions; surrogate models may be unfaithful; importance rankings can be unstable; and explanations can be manipulated to create false reassurance. Governance must therefore combine technical evaluation with accountability, privacy protection, security, documentation, contestability, and appropriate human responsibility. Chapter SummaryRegularisation controls model complexity and can improve generalisation, although it does not solve leakage, confounding, or poor measurement. Ridge shrinks coefficients smoothly, lasso can produce sparse solutions, and elastic net combines both behaviours. Random forests average diverse trees to reduce variance, while gradient boosting builds an additive model by correcting previous errors. Neural networks use layered transformations and gradient-based training to learn flexible representations, but they require careful validation and regularisation. Statistical and machine-learning approaches are complementary. Statistical models can offer explicit assumptions and uncertainty, while machine-learning models often provide strong predictive performance in complex settings. Neither predictive importance nor post-hoc explanation establishes causality. Explainability should be evaluated for faithfulness, stability, usefulness, and fairness, with attention to correlated features and unrealistic counterfactuals. A professional workflow begins with a clearly defined decision, prevents leakage, uses appropriate data splitting, compares baselines, evaluates decision-relevant metrics, monitors deployment, and documents limitations. Generative AI can accelerate analytics work but requires confidentiality controls, verification, testing, provenance, and human accountability. Key TermsRegularisation: A method that penalises model complexity to improve generalisation. Ridge regression: Penalised regression using squared coefficient magnitudes. Lasso regression: Penalised regression using absolute coefficient magnitudes. Elastic net: A mixture of lasso and ridge penalties. Overfitting: Learning training-specific noise or structure that does not generalise. Underfitting: Using a model too simple to capture important patterns. Bias: Systematic prediction error arising from restrictive assumptions or specification. Variance: Sensitivity of a fitted model to the particular training sample. Bagging: Bootstrap aggregation of multiple fitted models. Random forest: An ensemble of randomised decision trees. Boosting: Sequential construction of learners that reduce current error. Learning rate: The step size controlling each boosting update. Neural network: A layered parameterised function using weighted transformations and activations. Permutation importance: Performance change after randomly permuting a feature. Partial dependence: Average model response as a feature is varied over a population. SHAP-style attribution: A feature-contribution explanation relative to a baseline under specified assumptions. Counterfactual explanation: A description of changes that could alter a model prediction. Calibration: Agreement between predicted probabilities and observed event frequencies. Data leakage: Use of information unavailable at prediction time. Model drift: Degradation or behavioural change caused by changing data or relationships. Review QuestionsShort Answer Questions- Distinguish overfitting from underfitting.
- What is the main difference between ridge and lasso penalties?
- How does a random forest create diversity among its trees?
- Why does feature correlation complicate permutation importance?
- Explain why high classification accuracy may be uninformative for a rare event.
Long Answer Questions- Explain the bias–variance perspective and discuss how regularisation affects it.
- Compare random forests and gradient boosting in terms of construction, strengths, and limitations.
- Describe the architecture and training process of a basic feed-forward neural network.
- Compare statistical modelling and machine learning for prediction, explanation, and uncertainty assessment.
- Explain how a responsible model-governance process should operate from problem definition through deployment.
Analytical / Application Questions- A bank has a model with excellent test discrimination but poorly calibrated probabilities. Explain why this matters and propose corrective actions.
- Design a leakage-resistant validation strategy for a churn model where customers generate repeated monthly records.
- A manager requests a counterfactual explanation that changes a protected attribute to improve a customer’s predicted outcome. Explain why this is problematic and design a more responsible alternative.
Multiple Choice Questions with AnswersQuestion 1Which regularisation penalty can set some regression coefficients exactly to zero? A. Lasso penalty B. Ridge penalty C. Bootstrap penalty D. Softmax penalty Correct Answer: A. Lasso penalty Question 2What is the principal purpose of averaging diverse trees in a random forest? A. To reduce variance B. To guarantee causal interpretation C. To eliminate the need for test data D. To ensure linear relationships Correct Answer: A. To reduce variance Question 3In gradient boosting, the learning rate primarily controls what? A. The step size of each new learner’s contribution B. The number of outcome classes C. The number of observations in the test set D. The measurement unit of the target Correct Answer: A. The step size of each new learner’s contribution Question 4For binary classification, which output activation commonly produces a probability for class 1? A. Linear activation B. Sigmoid activation C. ReLU activation only D. Identity permutation Correct Answer: B. Sigmoid activation Question 5What does permutation importance measure? A. The change in model performance after shuffling a feature B. The causal effect of changing a feature C. The number of missing values in a feature D. The correlation between two residuals Correct Answer: A. The change in model performance after shuffling a feature Question 6Why can partial-dependence results be misleading with strongly correlated features? A. They may evaluate unrealistic combinations of feature values B. They always remove nonlinear relationships C. They require every model to be linear D. They measure only training-set accuracy Correct Answer: A. They may evaluate unrealistic combinations of feature values Question 7Which practice directly prevents a feature created after the prediction time from entering training? A. Defining an information cutoff and enforcing it during feature construction B. Increasing the number of trees C. Using a deeper neural network D. Reporting accuracy instead of recall Correct Answer: A. Defining an information cutoff and enforcing it during feature construction Question 8A classifier predicts that 100% of customers will not churn, while only 6% churn. What is its accuracy? A. 6% B. 50% C. 94% D. 100% Correct Answer: C. 94% Question 9What does calibration assess? A. Whether predicted probabilities correspond to observed frequencies B. Whether features have identical units C. Whether a tree has maximum depth D. Whether a model has zero training error Correct Answer: A. Whether predicted probabilities correspond to observed frequencies Question 10Which is an appropriate control when using generative AI to draft analytics code? A. Execute and independently review the generated code before relying on results B. Assume fluent code is correct C. Upload confidential data to any public service D. Replace all test data with generated examples Correct Answer: A. Execute and independently review the generated code before relying on results
|