Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Change as little as possible, but fix the mistake
An online classifier sees one example at a time. If it gets the example right with a comfortable margin, it does nothing (passive). If not, it changes its weights (aggressive), but only as much as needed to get this example right with a margin of 1, and no more. That is the whole idea. Because a single wrong label could then cause a big jump, the safer variants cap how far one example can move the weights.
Alert filters, operator-feedback loops and streaming anomaly labels arrive one at a time, with no time for retraining and often with some wrong labels. PA classifiers need no learning-rate tuning, update in O(features) time, and with PA-I’s cap they degrade gracefully when operators mislabel.
- 1. When an example already has margin ≥ 1, PA…
- 2. When it updates, PA moves the weights…
- 3. PA-I differs from PA by…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.One update by hand
Current weights w = (0, 0). New example x = (1, 2) with y = +1.
- The margin is y·w·x = 0, so the hinge loss is ℓ = 1 − 0 = 1.
- ‖x‖² = 1 + 4 = 5.
| Variant | Step τ | New w | New margin y·w·x |
|---|---|---|---|
| PA | ℓ/‖x‖² = 0.2 | (0.2, 0.4) | 1.0 (exactly) |
| PA-I (C = 0.1) | min(0.1, 0.2) = 0.1 | (0.1, 0.2) | 0.5 |
| PA-II (C = 1) | ℓ/(‖x‖² + 1/(2C)) = 1/5.5 ≈ 0.182 | (0.182, 0.364) | 0.91 |
PA’s new weights are the closest point to the old ones that satisfies y·w·x ≥ 1, the projection onto that half-space. Presenting the same example again causes no change, because its loss is now 0.
The steps in plain English:
- Start with w = 0.
- For each incoming example, predict sign(w·x). Then compute the hinge loss ℓ = max(0, 1 − y·w·x).
- If ℓ = 0, do nothing.
- Otherwise compute τ (PA, PA-I or PA-II) and set w ← w + τ·y·x.
- Choose C (PA-I/II) by validation: smaller C is more cautious, larger C reacts faster.
- 1. PA step for loss 1 and ‖x‖² = 4?
- 2. PA-I step for loss 2, ‖x‖² = 1, C = 0.5?
- 3. After a PA update, the example's margin is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Derivation
PA as an optimisation problem: w_(t+1) = argmin over w of ½‖w − w_t‖² subject to ℓ(w; x_t, y_t) = 0, where ℓ is the hinge loss.
Lagrangian: ½‖w − w_t‖² + τ(1 − y_t w·x_t), with τ ≥ 0.
- Setting the gradient to zero gives w = w_t + τ y_t x_t.
- Substituting into the active constraint gives τ = ℓ_t/‖x_t‖².
Soft versions trade the constraint against closeness:
- PA-I: minimise ½‖w − w_t‖² + Cξ with ξ ≥ ℓ(w). This gives τ = min(C, ℓ/‖x‖²).
- PA-II: minimise ½‖w − w_t‖² + Cξ². This gives τ = ℓ/(‖x‖² + 1/(2C)).
Extensions:
- Kernels: w = Σ τᵢyᵢφ(xᵢ), using only dot products.
- Multiclass: a margin between the true class and the highest-scoring wrong class.
- Regression: an ε-insensitive loss.
- Confidence-weighted learning and AROW keep a covariance over w for per-feature step sizes.
from sklearn.linear_model import PassiveAggressiveClassifier
clf = PassiveAggressiveClassifier(C=0.1, loss="hinge", max_iter=1, tol=None, shuffle=False) # loss="hinge": PA-I, "squared_hinge": PA-II
for X_batch, y_batch in stream:
clf.partial_fit(X_batch, y_batch, classes=[0, 1])Use it when learning online from streams, especially sparse text-like features, with no time to tune a learning rate. Use PA-I with a small C when labels may be noisy.
- 1. PA-II's step is…
- 2. In scikit-learn, loss='hinge' gives…
- 3. PA needs a learning-rate schedule?
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Guarantees and comparisons
Mistake bound for PA (separable case). Suppose some u gives margin 1 on every example (‖u‖ arbitrary) and ‖x_t‖ ≤ R. Then the cumulative squared hinge loss of PA is at most ‖u‖²R², which bounds the number of mistakes by ‖u‖²R². This is the perceptron’s (R/γ)² with γ = 1/‖u‖.
The non-separable case. For PA-I, the number of mistakes is at most max(R², 1/C)(‖u‖² + 2CΣℓₜ*), where ℓₜ* is the loss of any fixed comparator u (Crammer et al., 2006). The cap C bounds the influence of each noisy example. PA without a cap has no such guarantee: a mislabelled point with a small ‖x‖ can cause an arbitrarily large jump.
Comparison:
- Perceptron: updates only on mistakes, with a fixed step (τ = 1). PA also updates on correct but low-margin examples, and scales the step by the loss.
- SGD on the hinge loss: a step size from a schedule plus regularisation. PA-I is like SGD whose step adapts to each example’s loss and norm, capped by C.
- Batch SVM: PA-I’s objective per step mirrors the soft-margin SVM (D5.47). Over many passes with decaying C it approaches an SVM solution.
Interview questions.
- Why does plain PA suffer with noisy labels? Every wrong label is “fixed” exactly, so the model chases noise. PA-I limits each jump.
- What does C control? The trade-off between staying close to the old weights (conservative) and satisfying the new example (aggressive).
- 1. PA updates on examples that are…
- 2. The PA-I mistake bound depends on…
- 3. The perceptron is like PA with…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Filtering alerts with mislabelled feedback
History.
- Crammer, Dekel, Keshet, Shalev-Shwartz and Singer (2006) introduced PA in “Online passive-aggressive algorithms” (JMLR), unifying classification, regression and multiclass.
- The idea goes back to the relaxation methods of Agmon and of Motzkin and Schoenberg (1954).
- Confidence-weighted learning (Dredze et al., 2008) and AROW (Crammer et al., 2009) extended it with uncertainty.
- PA became a standard online learner for spam and text classification.
Exercise 1. The hand example gives τ = 0.2, with a new margin of exactly 1. The update is passive on repetition, PA-I caps the step at C, and no random weight vector that satisfies the constraint is closer to the old weights. A separable stream is learned with 4 mistakes in 3 passes.
Exercise 2: ground-station alert filter. Setup: 20 sparse count features, a linear truth, a single pass over 4000 alerts, and 0–20% mislabelled operator feedback. C = 0.1 for PA-I and PA-II. The table shows accuracy on clean test labels.
| Method | 0% noise | 10% noise | 20% noise |
|---|---|---|---|
| Perceptron | 0.963 | 0.786 | 0.712 |
| PA (no cap) | 0.968 | 0.780 | 0.758 |
| PA-I (C = 0.1) | 0.973 | 0.904 | 0.846 |
| PA-II (C = 0.1) | 0.974 | 0.840 | 0.791 |
| SGD (hinge) | 0.958 | 0.890 | 0.827 |
- Without noise, all methods are within 2 points. PA’s loss-scaled steps make it slightly better than the perceptron.
- With noise, uncapped PA is no better than the perceptron: each wrong label is fully “fixed”, throwing the weights around.
- PA-I’s cap makes it the most robust: 12 points better than PA at 10% noise, slightly better than SGD, and with no learning rate to tune.
- PA-II’s smooth shrinkage helps, but less than the hard cap for this C.
Passive-aggressive learning in AI, robotics and aerospace
- Operator-in-the-loop filtering: alert triage that learns from each acknowledgement.
- Text streams: anomaly-report and log classification with sparse features.
- Online regression (PA-R): tracking slowly varying calibration parameters.
- Structured and multiclass prediction: online learning for sequence labelling (MIRA).
References
- K. Crammer, O. Dekel, J. Keshet, S. Shalev-Shwartz, Y. Singer (2006). “Online passive-aggressive algorithms.” JMLR 7.
- K. Crammer, Y. Singer (2003). “Ultraconservative online algorithms for multiclass problems.” JMLR 3.
- M. Dredze, K. Crammer, F. Pereira (2008). “Confidence-weighted linear classification.” ICML.
- K. Crammer, A. Kulesza, M. Dredze (2009). “Adaptive regularization of weight vectors.” NeurIPS.
- T. S. Motzkin, I. J. Schoenberg (1954). “The relaxation method for linear inequalities.” Canadian Journal of Mathematics 6.