Math Foundations — Lecture 2
Linear scoring, sigmoid, losses (hinge / CE), perceptron, logistic gradient, P / R / F1, ROC.
Linear classifier scoring
Feature vector \(\mathbf{x}\in\mathbb{R}^F\), weight vector \(\mathbf{w}\in\mathbb{R}^F\), optional bias \(b\):
Binary: threshold at 0 (or use two scores). Multi-class (\(|\mathcal{L}|\) labels): weight matrix \(W\in\mathbb{R}^{F\times|\mathcal{L}|}\), predict \(\hat y=\arg\max_\ell\;\mathbf{x}\,W_{:,\ell}\).
Sigmoid function
Maps any real number to \((0,1)\). Derivative: \(\sigma'(z)=\sigma(z)(1-\sigma(z))\). Used in logistic regression to turn scores into probabilities.
Loss functions
0–1 loss
Non-differentiable; cannot directly gradient-descend on it.
Hinge loss (SVM)
where \(y\in\{-1,+1\}\) and \(f(x)\) is the raw score. Margin-based; pushes score of correct class to exceed incorrect by at least 1.
Cross-entropy / log loss
where \(s_\ell=\mathbf{x}\cdot\mathbf{w}_\ell\). Smooth, differentiable; standard for neural NLP.
Perceptron update
If prediction \(\hat y\neq y\):
Add the input to the true-class weights; subtract from the predicted-class weights. No learning rate. Converges if data is linearly separable.
Logistic regression gradient
Binary case, \(y\in\{0,1\}\), prediction \(\hat p=\sigma(\mathbf{x}\cdot\mathbf{w})\):
Gradient descent step: \(w_j\leftarrow w_j - \eta(\hat p - y)x_j\), where \(\eta\) is the learning rate.
Precision, recall, F1
Confusion matrix entries: TP, FP, FN, TN.
\(F_1\) is the harmonic mean — penalises extremes more than arithmetic mean.
Macro vs micro averaging
Macro: compute P/R per class, then average. Micro: pool all TP/FP/FN across classes, then compute P/R. Micro P = micro R when every instance gets exactly one label (no "none" class).
Accuracy
Misleading with class imbalance (e.g. 99% negative → always-negative baseline = 99%).
ROC & AUC
Sweep the decision threshold; plot TPR (recall) vs FPR (\(=FP/(FP+TN)\)).
AUC = area under the ROC curve. Random classifier → AUC = 0.5; perfect → 1.0.
When positives are rare, Precision–Recall curve is more informative than ROC.