7 routes42 sections324 units

Unit 2.5 · about 3 h

140 / 324

Residual, leverage, separation and misspecification diagnostics

A fit that converges is not a fit that is right. Diagnostics separate a broken model from a broken assumption, and separation is the one failure that looks like perfect success.

  • measure

You can now

  • Read residual-versus-fitted plots to separate nonlinearity, heteroscedasticity and outliers, and name the repair each prescribes.
  • Compute leverages from the hat matrix, flag high-leverage points, and distinguish leverage from influence.
  • Detect complete separation in logistic fits from runaway coefficients and saturated probabilities, and repair it with an explicit penalty.

Covers

  • Residual, leverage, separation and misspecification diagnostics

Study, in this order

5 steps

  1. Potential problems in the regression model
    An Introduction to Statistical Learning with Python

    Chapter 3, Section 3.3.3 (pp. 100-108): nonlinearity, correlation of error terms, heteroscedasticity, outliers, high-leverage points and collinearity with the variance inflation factor

  2. Separation instability of logistic estimates
    An Introduction to Statistical Learning with Python

    Chapter 4, Section 4.4 opening (p. 146): logistic parameter estimates turn surprisingly unstable under substantial class separation, which motivates the generative alternatives

  3. Penalty strength as the separation lever
    scikit-learn - linear models, discriminant analysis and naive Bayes

    Section 1.1.11 Binary Case: the penalty enters as \(r(w)/(S C)\), so relaxing \(C\) toward \(\infty\) recovers the unpenalized fit whose weights are unbounded under separation

  4. Learning curves for bias-variance diagnosis
    Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear models

    Book Chapter 4: Learning Curves (printed pp. 154-159). Notebook 04 cells: Learning Curves section: sklearn learning_curve with RMSE on the linear fit, then the degree-10 polynomial pipeline, train versus validation error against training-set size

  5. Error-only confusion views
    Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear models

    Book Chapter 3: Error Analysis (printed pp. 126-130). Notebook 03 cells: Error Analysis section: row-normalized matrices, then error-only views with sample_weight, normalized by row and by column

Focus on

  • Leverage measures position in \(x\)-space, not wrongness. The \(x = 10\) point in a 0-to-2 design carries leverage 0.98 out of \(p = 2\) while anchoring the slope; residuals measure wrongness, and influence needs both.
  • Patterns have signatures. Curvature in residual-versus-fitted means nonlinearity, a funnel means heteroscedasticity, and isolated large standardized residuals mean outliers; each prescribes a different repair.
  • Separation shows as coefficients that keep growing as \(C\) is relaxed, with probabilities glued to 0 and 1. On \(x = 0,1,2,3\) with \(y = 0,0,1,1\) the slope is 0.96 at \(C = 1\) and 9.31 at \(C\) = 1e3. At \(C\) = 1e6 lbfgs stops at 16.57 under its default tol = 1e-4 with no warning, while tol = 1e-12 reaches 21.50. The fix is a finite penalty, not more iterations.

Reproduce closed-book

From a blank file compute hat diagonals on the design [[1,0],[1,1],[1,2],[1,10]] and match leverages [0.4183, 0.3307, 0.2749, 0.9761] summing to \(p = 2\). Then fit LogisticRegression on x = [0,1,2,3] with y = [0,0,1,1] at \(C = 1\), 1e3 and 1e6 with scikit-learn 1.9.1 defaults and match slopes [0.9583], [9.3057] and [16.5719]. Refit \(C\) = 1e6 with tol=1e-12 and watch the slope move to 21.4953: under separation the stopping rule, not the data, sets the weights.

Traps

  • Deleting every high-leverage point. Leverage is not guilt: a far-x point with a small residual anchors the fit. Act on influence, which combines residual and leverage (Cook's distance), not on position alone.
  • Reading saturated 0 and 1 probabilities as a great classifier. Under separation the likelihood rewards ever-larger weights, so perfect scores signal an unidentified model rather than a solved problem.
  • Raising max_iter or C to force a separated fit to converge. The penalized optimum keeps moving outward as C grows, and under the default tol lbfgs stops early without a ConvergenceWarning, so a quiet fit proves nothing. Keep the penalty finite or collect overlapping cases.

Practice

HoML 04 Learning Curves: bias-variance diagnosis from error curves

Learning Curves section: RMSE train and validation curves from learning_curve for the linear fit and the degree-10 polynomial pipeline

Rebuild both curve pairs from a blank script and name which regime, bias or variance dominated, each pair shows.

HoML 03 Error Analysis: error-only confusion views

Error Analysis section: row-normalized matrix, then the sample_weight error-only views normalized by row and by column

List the three most confused digit pairs from the error-only views and inspect sample images for each.

Self-test

  1. Residual-versus-fitted shows a U shape. What is wrong, and what changes?

  2. The \(x = 10\) point has leverage 0.98 out of \(p = 2\). Should it be deleted?

  3. Logistic coefficients keep growing as C is relaxed (0.96, 9.31, 16.57 at C = 1, 1e3, 1e6) and the probabilities print as 0 or 1. Diagnose and repair.

  4. Standardized residuals fan out as fitted values grow. Name the violation and one valid response.

Completion criterion

Stop when

You can name the violated assumption from any residual plot, reproduce the leverage table from a blank file, and catch separation from a \(C\) sweep before looking at accuracy.

Mark passed, then open 2.6 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

Also in this section

  • Dive into Deep Learning - linear models slice
    D2L authorsrequiredimplementation
    Use 3.1, 3.4, 3.6, 3.7, 4.1, 4.4, 22.7 and 22.9 for linear regression, generalization, weight decay, softmax, maximum likelihood and naive Bayes. Implement with NumPy first and use the PyTorch versions only as a differential reference.
  • NeetCode ML - linear-model closing drill
    NeetCodesupportpractice
    After the section gate, solve Gradient Descent, Sigmoid and ReLU, Softmax, Cross-Entropy Loss, Linear Regression Forward and Linear Regression Training cold. Repeat only failed items from a blank file with changed values.
  • Deep-ML - linear and probabilistic model problems
    Deep-MLsupportpractice
    Use 15 Linear Regression Using Gradient Descent, 106 Train Logistic Regression with Gradient Descent and 140 Bernoulli Naive Bayes Classifier as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin. The local implementation remains the evidence.
  • Tom Mitchell - Generative and Discriminative Classifiers: Naive Bayes and Logistic Regression
    Tom Mitchell, Carnegie Mellon Universityrequiredtheory
    Read Sections 2 (naive Bayes, the smoothed estimates (7) and (9), parameter counts), 3 (Gaussian naive Bayes implies the logistic form, gradient ascent, the \(L_2\) penalty, the \(K\)-class rules) and 4 (generative versus discriminative, Ng and Jordan rates). This is the CS446 reading for Lectures 6-8; rework each derivation on a hand-sized dataset.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels