Linear & Probabilistic Prediction
Linear and probabilistic models expose their assumptions. They connect likelihood, optimization, regularization, probabilities and decision thresholds before flexible models make those choices harder to see.
Section 2 of 5 · 8 units · about 28 hours
By the end you can Derive and independently implement linear regression, logistic regression and one naive Bayes variant, then diagnose one numerical failure and one assumption failure on a changed dataset.
Units
- Least squares: residuals, normal equations and stable solvers4h
- Conditioning, basis expansions, ridge, lasso and elastic net5h
- Logistic regression and stable binary cross-entropy4h
- Softmax regression and one-versus-rest3h
- Residual, leverage, separation and misspecification diagnostics3h
- LDA, QDA and generative versus discriminative classifiers4h
- Naive Bayes: event models, smoothing and log-space scoring3h
- Probability estimates, class priors and asymmetric thresholds2h
Build it
Mini project · about 10 to 14 hours
Linear and probabilistic models from scratch, with two failure diagnoses
Implement least squares, ridge, logistic regression and one naive Bayes variant in NumPy in your own repository, prove each against scikit-learn and finite differences, then diagnose one numerical failure and one assumption failure on changed datasets you construct.
- 1Implement least squares three ways: normal equations, an SVD-based solve and minibatch gradient descent with a decaying step. Pass a
float64gradient check and matchLinearRegression, handling the intercept the same way on both sides. - 2Add ridge with the centered closed form and a penalty path. Match
Ridgeto 1e-12 and report validation error across on a fixed split. - 3Implement logistic regression on logits with the stable loss and an penalty. Pass a gradient check, then match
LogisticRegressionon its own objective ( times the summed loss plus half the squared norm) withtoltightened. - 4Implement Bernoulli or multinomial naive Bayes with Laplace smoothing in log space. Match scikit-learn's
feature_log_prob_to 1e-8 and handle unseen values and single-class training data. - 5Numerical failure: build a changed dataset that breaks one naive path, such as a near-collinear design, extreme logits for a naive loss, or a separable set. Show the failure, the measurement that discriminates its cause, the repair and a regression test.
- 6Assumption failure: build a dataset that violates one model assumption, such as XOR-labeled binary features for naive Bayes, heteroscedastic noise for least squares, or non-Gaussian classes for LDA. Show the diagnostic that reveals it and the response, validated on held-out data.
- 7Turn one fitted logistic model into a decision: derive the cost-optimal threshold for stated costs, apply it on validation, and move it for a stated prevalence change without refitting.
What you hand in
- The NumPy package: least squares (three solvers), ridge, logistic regression and naive Bayes.
- The test suite: scikit-learn differential tests, finite-difference checks and the extreme-input probes.
- Derivation notes: normal equations, the ridge closed form, the logistic gradient and the smoothed MAP rule.
- Two failure reports, numerical and assumption, each with diagnostic, repair and regression test.
How to check it
- Deep-ML 15, 106 and 140
Public tests. Pass them after your local tests do; each card states what the tests pin.
- Self-grade with the rubric below
How it's graded
Correctness against oracles
Pass: Each implementation matches its scikit-learn counterpart within a stated tolerance.
Great: Property tests vary seeds, shapes and penalties, and every analytic gradient matches central differences in float64.
Numerical stability
Pass: The logit loss stays finite on extreme logit probes, and ill-conditioned designs use an SVD-based solve.
Great: The notes show where each naive form breaks and why the stable form stays exact.
Numerical failure diagnosis
Pass: One failure is reproduced, explained, repaired and guarded by a test.
Great: The discriminating measurement rules out a rival cause, for example a tighter tol showing that lbfgs stopped early rather than converged.
Assumption failure
Pass: One violated assumption is shown with a diagnostic and a response.
Great: The response is validated on held-out data and the report states what it still cannot fix.
Decision
Pass: The threshold follows from stated costs and is chosen on validation data.
Great: The prevalence adjustment is derived from prior odds and checked against a refit on resampled data.
Resources
- An Introduction to Statistical Learning with PythonJames, Witten, Hastie, Tibshirani and Taylor
Use Chapters 3, 4 and 6 with their Python labs as the first pass: least squares and its diagnostics, logistic regression, LDA, QDA and naive Bayes, basis expansion and regularization paths.
- Dive into Deep Learning - linear models sliceD2L authors
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.
- Tom Mitchell - Generative and Discriminative Classifiers: Naive Bayes and Logistic RegressionTom Mitchell, Carnegie Mellon University
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.
- Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear modelsAurelien Geron (O'Reilly, 2025)
Read the Chapter 3 and 4 sections each unit names, then run notebooks 03 and 04 for classification diagnostics, linear and polynomial regression, regularization and logistic regression. Recreate the target mechanism independently before inspecting worked exercise code.
- NeetCode ML - linear-model closing drillNeetCode
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
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.
- scikit-learn - linear models, discriminant analysis and naive Bayesscikit-learn
Use the official Linear Models, LDA and QDA, and Naive Bayes guides to verify estimator semantics and selected edge cases after the independent implementation works.