Unit 5 of 8 · about 3 hours · measure

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.

You'll be able to

  • 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
1

Read

In this order, only the parts listed

0 of 5 done

  1. Potential problems in the regression modelAn 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 estimatesAn 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 leverscikit-learn - linear models, discriminant analysis and naive Bayes

    Section 1.1.11 Binary Case: the penalty enters as , so relaxing toward recovers the unpenalized fit whose weights are unbounded under separation

  4. Learning curves for bias-variance diagnosisHands-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 viewsHands-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

2

Focus on

  • Leverage measures position in -space, not wrongness. The point in a 0-to-2 design carries leverage 0.98 out of 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 is relaxed, with probabilities glued to 0 and 1. On with the slope is 0.96 at and 9.31 at = 1e3. At = 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.
Common mistakes · 3
  • 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.
3

Try it yourself

Closed-book, before checking anything

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 . Then fit LogisticRegression on x = [0,1,2,3] with y = [0,0,1,1] at , 1e3 and 1e6 with scikit-learn 1.9.1 defaults and match slopes [0.9583], [9.3057] and [16.5719]. Refit = 1e6 with tol=1e-12 and watch the slope move to 21.4953: under separation the stopping rule, not the data, sets the weights.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

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

    Nonlinearity the linear form cannot express. Add polynomial or interaction terms rather than collecting more data for the same line.

  2. 2.The point has leverage 0.98 out of . Should it be deleted?Answer

    Not on leverage alone. It dominates the slope estimate but a small residual there is informative. Check influence by refitting without it before deciding.

  3. 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.Answer

    Complete separation: the maximum likelihood weights are unbounded and only the penalty keeps them finite, so the penalized optimum moves outward roughly in proportion to . Keep finite, confirm the weights hold under a tighter tol, and look for overlapping cases to collect.

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

    Heteroscedasticity, non-constant error variance. Transform the response, model the variance, or use variance-robust inference; adding polynomial terms does not fix it.

5

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.

You're done when

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

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 4 more in the section

Also in this section

  • Dive into Deep Learning - linear models sliceD2L authors · required

    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 drillNeetCode · support

    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 problemsDeep-ML · support

    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 RegressionTom Mitchell, Carnegie Mellon University · required

    Read Sections 2 (naive Bayes, the smoothed estimates (7) and (9), parameter counts), 3 (Gaussian naive Bayes implies the logistic form, gradient ascent, the penalty, the -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.