7 routes42 sections324 units

Unit 2.8 · about 2 h

143 / 324

Probability estimates, class priors and asymmetric thresholds

A probability is not a decision. The same fitted scores need different thresholds under different costs and different priors, and 0.5 is a cost claim most projects never stated.

  • derive
  • measure

You can now

  • Derive the cost-optimal threshold from class probabilities and misclassification costs, and show why 0.5 is only optimal for symmetric costs.
  • Adjust decisions for changed class priors without refitting, through the prior-odds correction.
  • Measure precision-recall and ROC tradeoffs on fixed validation data and select a threshold from stated costs.

Covers

  • Probability estimates, class priors and asymmetric thresholds

Study, in this order

4 steps

  1. Precision-recall tradeoff and ROC thresholds
    Hands-On Machine Learning with Scikit-Learn and PyTorch - classification and linear models

    Book Chapter 3: The Precision/Recall Trade-Off and The ROC Curve (printed pp. 115-124). Notebook 03 cells: Precision/Recall Trade-off section (precision_recall_curve on the MNIST 5-detector, the threshold-3000 demo, idx_for_90_precision) and The ROC Curve section with the 90%-precision operating point

  2. Predicted probabilities and dummy encoding
    An Introduction to Statistical Learning with Python

    Chapter 4, Section 4.3.3 (pp. 141-142): probabilities computed from fitted coefficients with the balance arithmetic, plus dummy variables for qualitative predictors

  3. Default threshold, weights and calibration limits
    scikit-learn - linear models, discriminant analysis and naive Bayes

    Section 1.1.11 introduction (predicted probability becomes a classifier at a threshold, by default 0.5), Binary Case (sample and class weights rescale the effective C through S) and Solvers details (joint multinomial loss calibrates better than OvR)

  4. Priors inside the discriminant scores
    scikit-learn - linear models, discriminant analysis and naive Bayes

    Section 1.2.2 (posteriors combine class conditionals with \(P(y = k)\); LDA adds \(\log P(y = k)\) to the discriminant) and 1.2.4 (shrinkage as the small-sample stabilizer for those estimates)

Focus on

  • sklearn predicts the positive class at 0.5 by default. That bakes in equal costs and matched priors; any asymmetric cost matrix moves the optimal threshold to \(C_{\text{FP}}/(C_{\text{FP}} + C_{\text{FN}})\).
  • Naive Bayes scores rank well but estimate badly, and OvR margins are not calibrated across classifiers. Thresholding an uncalibrated score encodes its bias as policy.
  • Priors shift without refitting: multiply the posterior odds by the new-over-old prior-odds ratio. Thresholds move with prevalence even when the feature evidence stays fixed.

Reproduce closed-book

Derive \(t = C_{\text{FP}}/(C_{\text{FP}} + C_{\text{FN}})\) from predicting 1 exactly when \(p\) times \(C_{\text{FN}}\) exceeds \((1 - p)\) times \(C_{\text{FP}}\), and show costs of 1 and 9 give \(t = 0.1\). From a blank script recompute precisions, recalls and thresholds with precision_recall_curve on binary scores and implement the first-index rule that finds the 90%-precision threshold.

Traps

  • Tuning the threshold on the test set. Thresholds are fitted parameters: select on validation, report once on test, or the reported precision is optimistic.
  • Comparing thresholds across models with different score scales. A 0.3 cutoff on calibrated probabilities means something different from 3000 on raw margins; compare at matched precision or recall, not matched knob values.
  • Keeping 0.5 after reweighting classes. Class and sample weights change the score scale and the effective penalty, so retune the threshold after any weighting change.

Practice

HoML 03 Precision/Recall Trade-off: the 90%-precision threshold

Precision/Recall Trade-off section: the precision-recall-versus-threshold plot, idx_for_90_precision, and the ROC point for that threshold

Rebuild the curve and both threshold picks from a blank script; then move the threshold to the cost-optimal point for a stated 1:9 cost ratio and report the precision-recall change.

NeetCode ML: full linear-model closing drill

Math Foundations six: Gradient Descent, Sigmoid and ReLU, Softmax, Cross-Entropy Loss, Linear Regression Forward, Linear Regression Training

After the section gate, solve all six cold. Repeat only failed items from a blank file with changed values.

Self-test

  1. False negatives cost 9 times false positives. Which threshold, and why not 0.5?

  2. Deployment prevalence of positives halves with the same feature evidence. How do the optimal decisions change without refitting?

  3. Two detectors both report 90% precision, one at margin 3000 and one at probability 0.42. Which threshold is better?

  4. The guide warns naive Bayes is a bad estimator. What breaks if its raw scores are thresholded at 0.5?

Completion criterion

Stop when

You can derive the cost-optimal threshold, move it correctly under a stated prior shift, and replicate the 90%-precision procedure on new scores without touching the test set.

Mark passed, then open 3.1 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