The Lawfare Academy

Lawfare Group → Knowledge Institute → Lawfare Academy

← AI 301 — Machine Learning, Neural Networks and Language

Weeks 3–5

Linear and logistic models, regularization and calibration

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.

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 reasoning 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.

Extended teaching and laboratory

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.

Step-by-step reasoning 1. At .5, the last two are predicted positive: TP=1, FP=1, FN=1, TN=1.

  1. At .8, only .9 is positive: TP=0, FP=1, FN=2, TN=1. Precision becomes 0/1=0 and recall 0/2=0.
  1. This small example shows that a higher threshold does not guarantee an improved empirical precision on every finite dataset.
  1. 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.

Apply the idea

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.

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

Read the worked reasoning

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.

Check understanding

What does regularization address?

  1. A tradeoff between fitting data and constraining model complexity
    Feedback

    Penalty strength can reduce variance while introducing bias.

  2. A guarantee that every coefficient is causal
    Feedback

    Predictive fitting does not establish causal identification.

  3. Automatic correction of leaked features
    Feedback

    Leakage requires correcting the data and evaluation design.

Further reading

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.

My learning →