Lawfare Academy

AI 301 — Machine Learning, Neural Networks and Language · Weeks 3–5

Linear and logistic models, regularization and calibration

Full teaching sequence and cumulative subject work

Study the complete on-site source sections below, then work the original example, changed-condition problem and transfer laboratory. Study scikit-learn Linear Models and Probability Calibration; Dive into Deep Learning Linear Regression, Softmax Regression and Weight Decay. Work derivations and source exercises.

Train a logistic model and expose a changed mechanism — primary material, constructed-response tasks, complete worked model and changed-case examination →

Scores, thresholds and calibrated probabilities

A logistic model computes a score z, transforms it with a sigmoid and applies a chosen threshold for a decision. These are three distinct objects. A sigmoid maps numbers into (0,1), but that range alone does not establish calibration. A set of predictions near .8 is calibrated only if the relevant outcome occurs about 80% of the time in the defined setting, subject to sampling uncertainty.

Changing the threshold changes sensitivity and false alerts without changing the fitted probability model. Regularization constrains fitting; it does not select a universally correct operational threshold. Use numerically stable sigmoid evaluation and assess calibration on data independent of fitting.

Worked problem

Four predicted probabilities are [.2,.4,.7,.9] and outcomes are [0,1,1,0]. Compare confusion matrices at thresholds .5 and .8, defining positive as probability≥threshold.

Trace the complete reasoning
  1. At .5, the last two are predicted positive: TP=1, FP=1, FN=1, TN=1.

  2. At .8, only .9 is positive: TP=0, FP=1, FN=2, TN=1. Precision becomes 0/1=0 and recall 0/2=0.

  3. This small example shows that a higher threshold does not guarantee an improved empirical precision on every finite dataset.

  4. The .9 error does not alone prove global miscalibration. Assess calibration across enough cases and relevant subgroups rather than interpreting a single probability as certainty.

Independent transfer problem

A stakeholder demands that every .9 prediction be correct. Explain the probability misconception and propose an evaluation.

Compare the transfer solution

A calibrated .9 forecast permits about 10% negatives in the corresponding population; it is not a deterministic guarantee. Evaluate reliability across probability bins with counts and uncertainty, inspect subgroup differences, and assess whether the deployment distribution matches evaluation. If errors have severe consequences, add review or abstention rather than promising certainty from a threshold.

Learning objectives

  • Explain and trace the mechanisms in linear and logistic models, regularization and calibration.
  • Apply the method to the worked example and changed-condition exercise.
  • Identify assumptions, failure cases and the evidence needed to justify a result.

Study the mechanism

A linear regression model forms a weighted sum of features plus an intercept. Least-squares fitting minimizes squared residuals, emphasizing large errors. The coefficient is conditional on the model's other features and data construction; it is not automatically a causal effect. Outliers, correlated features and omitted nonlinear structure can make naive interpretation misleading.

Logistic regression maps a linear score through a sigmoid to a number between zero and one for a binary target. A probability-like output still needs calibration evaluation. Thresholding at 0.5 is a policy choice, not a mathematical obligation. When false negatives and false positives have different consequences, threshold selection should reflect development evidence and the decision context.

Regularization penalizes some forms of model complexity. L2 discourages large weights; L1 can promote sparse solutions. Feature scale affects penalty interpretation, so scaling belongs within the training pipeline when needed. Regularization strength is chosen without consulting the reserved test set. A simpler model can generalize better, but excessive regularization can underfit. Inspect residual patterns or calibration curves rather than relying on one average score.

Worked example

import math

def sigmoid(z):
    if z >= 0:
        return 1 / (1 + math.exp(-z))
    e = math.exp(z)
    return e / (1 + e)

assert sigmoid(0) == 0.5
assert sigmoid(2) > 0.88
assert sigmoid(-1000) == 0.0

The two branches evaluate the same sigmoid identity while avoiding exp(1000) overflow for very negative input. A score of zero maps to 0.5, but does not prove that half of such cases are truly positive. Calibration is an empirical property under a defined population. Numerical stability and statistical validity are separate requirements.

Check your understanding

What does regularization address?

  1. A tradeoff between fitting data and constraining model complexity
  2. A guarantee that every coefficient is causal
  3. Automatic correction of leaked features
Answer and reasoning for every option

Correct: A tradeoff between fitting data and constraining model complexity Penalty strength can reduce variance while introducing bias.

Reconsider: A guarantee that every coefficient is causal Predictive fitting does not establish causal identification.

Reconsider: Automatic correction of leaked features Leakage requires correcting the data and evaluation design.

Transfer laboratory: Probability score versus action

A calibrated model assigns 0.2 probability of a missed payment. Review costs $5 and prevents an expected $50 loss only when a missed payment would otherwise occur. Under these assumptions, calculate the expected net benefit of review.

Work through the full solution

Expected prevented loss is 0.2×$50=$10, less review cost $5, net $5. The break-even probability is 5/50=0.1. This depends on effective prevention, calibration and no omitted costs; if review prevents only half the loss, the break-even probability doubles to 0.2. The decision threshold is therefore a policy derived from assumptions rather than automatically 0.5.

Evidence of understanding

Calculate $5, derive the threshold and examine a changed intervention-effect assumption.

Complete on-site teaching readings

These are full attributed source sections, not previews. Read them alongside the original Academy explanation and code.

  1. 3.4. Linear Regression Implementation from Scratch Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
  2. 3.7. Weight Decay Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
  3. 4.1. Softmax Regression Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
  4. 4.4. Softmax Regression Implementation from Scratch Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
  5. 1.1. Linear Models · The scikit-learn developers
  6. 1.16. Probability calibration · The scikit-learn developers

Open in My Learning to record progress →