Older material, kept last
Open the interactive lessons →Legacy: logistic, softmax, SVM past questions
Contents
- Orientation: what you will learn and why
- Big picture: concept map, comparison table and the recipe
- Start here: signs, the sigmoid and logs
- Linear classifiers: geometry and separability
- Logistic regression: probability, loss, update, threshold
- Softmax regression for many classes
- Hard-margin SVM: the biggest safe margin
- Soft-margin SVM: slack variables and accuracy
1. Orientation: what you will learn and why
This older material is about classification with a straight boundary. A linear classifier splits the space by a line (a plane in more dimensions). Logistic and softmax regression turn the distance from that line into a probability; the support vector machine (SVM) chooses the line with the biggest safety margin. It is kept last because it matters less for Quiz 2 than the earlier topics, but it still appears in past papers.
What you will be able to do
- Decide whether a labelled set is linearly separable and what condition on $w$ gives zero error.
- Read the geometry of $w$, the boundary and signed distances from a picture.
- Use the logistic model: probability, binary cross-entropy loss, the gradient step, the threshold, and which feature map gives a circular boundary.
- Check a softmax output vector and give the shape of the weight matrix.
- Write the hard-margin SVM constraints and recognise the maximum-margin boundary.
- Compute slack variables and the accuracy of a soft-margin SVM.
Why this topic matters for Quiz 2
Counted only from the PYQs in this project (not a prediction):
| What the PYQs ask | PYQs | Marks |
|---|---|---|
| Linear separability and boundary geometry | 4 | 15 |
| Logistic regression | 6 | 14 |
| Softmax regression | 3 | 7 |
| Hard-margin SVM | 2 | 4 |
| Soft-margin SVM, slack and accuracy | 5 | 9 |
In total 20 PYQs worth 49 marks across 4 papers (11 MCQ, 5 MSQ, 4 numeric). Most of the logistic, softmax and SVM questions come from one 2022 paper.
How this topic connects
The linear form $w^Tx$ is the same as in linear regression. Kernels (earlier topic) are the way to make SVM boundaries curved.
Read each section, do the quick checks, then attempt each PYQ before opening its solution. Figures are described in words; where the bank gives coordinates they are used.
What is new in this upgrade [ADDED]
- A big picture section (next): concept map, comparison table, mnemonics.
- Figures: sigmoid, hinge against logistic loss, and a hard-margin SVM with its support vectors.
- Recall questions, a PYQ pattern map, variations and a flowchart. All new material is tagged [ADDED].
2. Big picture: concept map, comparison table and the recipe
[ADDED] Read this page first. Logistic regression, softmax and SVM all start from the same score.
The concept map
Comparison [ADDED]
| Logistic | Softmax | Hard-margin SVM | Soft-margin SVM | |
|---|---|---|---|---|
| Classes | 2 | $K$ | 2 | 2 |
| Output | $P(y=1\mid x)$ | $K$ probabilities, sum $1$ | a side of the line | a side of the line |
| Loss | cross-entropy | cross-entropy | $\tfrac12\|w\|^2$ with constraints | $\tfrac12\|w\|^2+C\sum\xi_i$ |
| Needs separable data? | no | no | yes | no |
| Parameter shape | $w\in\mathbb R^d$ | $W\in\mathbb R^{d\times K}$ | $w,b$ | $w,b,\xi$ |
Mnemonics and recognition cues [ADDED]
- "Score first." Compute $w^Tx+b$ before anything else.
- "Sigmoid: 0.5 at zero." $\sigma(-z)=1-\sigma(z)$.
- "Softmax: positive, sums to one." Check this first for "which could be the output" questions.
- "$W$ is $d\times K$, columns are classes."
- "Margin is $2/\|w\|$." Small $\|w\|$ means a wide street.
- "Slack is zero for points that are safe."
Analogy and its limit [ADDED]
| Analogy | Good for | Where it breaks |
|---|---|---|
| Fence between villages (SVM) | why the widest street is the safest boundary | only the nearest houses (support vectors) matter, unlike the fence story where every house seems to count |
Recall questions
$\sigma(0)$?
$0.5$.
Shape of $W$ for $d=5$ features, $K=3$ classes?
$5\times3$.
3. Start here: signs, the sigmoid and logs
Piece 1. The score $w^Tx+b$
$w^Tx+b=w_1x_1+\dots+w_dx_d+b$ is a number called the score. Its sign tells which side of the boundary $w^Tx+b=0$ the point is on.
Piece 2. The sigmoid
$$\sigma(z)=\frac1{1+e^{-z}}$$ $z$ is any real number; $\sigma(z)$ is always between $0$ and $1$, equals $0.5$ at $z=0$ and increases with $z$.
Piece 3. Logarithms of probabilities
For $0<p<1$, $-\log p$ is positive, large when $p$ is small and $0$ when $p=1$. This is why $-\log p$ works as a "cost of being wrong".
$\sigma(2)=\dfrac1{1+e^{-2}}=0.881$ and $\sigma(-2)=0.119$; they add to $1$ because $\sigma(-z)=1-\sigma(z)$. If the true label is $1$, the loss is $-\ln p$: $-\ln0.881=0.127$ (confident and right, small) but $-\ln0.119=2.13$ (confident and wrong, large).
4. Linear classifiers: geometry and separability
Intuition
Draw a fence (line). Everything on one side is labelled $+$, the other side $-$. The vector $w$ is like an arrow standing perpendicular to the fence and pointing toward the $+$ side.
Predict $+1$ if $w^Tx+b>0$ and $-1$ (or $0$) otherwise. The boundary is the set $w^Tx+b=0$. The vector $w$ is perpendicular to the boundary and points toward the positive side.
$$\text{signed distance of }x=\frac{w^Tx+b}{\|w\|}$$ Positive on the $w$-side, $0$ on the boundary, negative on the other side. So $w^Tx$ (with $b=0$) grows as a point moves further in the direction of $w$.
Linear separability
A data set is linearly separable if some line puts all $+$ points on one side and all $-$ points on the other.
- XOR pattern (two diagonal $+$, two diagonal $-$): not separable.
- A $+$ point surrounded by $-$ points on all sides: not separable.
- Two groups that can be split by a gap: separable.
Worked examples
Problem. Data $\{(-v,1),(-3v,1),(v,0),(2v,0)\}$ with $v\ne0$ and labels in $\{0,1\}$; predict $1$ when $w^Tx>0$.
What is asked. The condition on $w^Tv$ for zero training mistakes.
Idea / formula used. Write the score of each point.
Problem. Red ($-1$) and green ($+1$): Dataset 3: red $(-2,1),(1,-2)$; green $(-0.5,1),(1,1)$. Dataset 4: red $(-1,1),(2,1)$; green $(-1,-1),(1,-2)$. Dataset 1 (XOR): red $(-1,1),(1,-1)$, green $(1,1),(-1,-1)$. Dataset 2: green $(0,0)$ inside red $(\pm1,\pm1)$.
What is asked. The separable datasets.
Idea / formula used. Search for a separating line.
Problem. A boundary through the origin has $w$ as its normal. $P,Q,R$ lie on the side $w$ points to, $P$ farthest from the boundary and $R$ nearest. $S$ lies on the boundary. $T,U$ lie on the other side, $U$ farther.
What is asked. Order all six scores.
Idea / formula used. A score is signed distance along $w$ times $\|w\|$.
Separable or not: a quick test [ADDED]
| Pattern | Separable by one line? | Why |
|---|---|---|
| two blobs apart | yes | a line fits between |
| XOR (opposite corners share a label) | no | any line leaves a point on the wrong side |
| one class inside a ring of the other | no | a line cannot enclose |
| all points on a line, labels alternate | no | a single threshold cannot alternate |
Is XOR linearly separable?
No.
5. Logistic regression: probability, loss, update, threshold
Intuition
The score $w^Tx$ is a "confidence meter". The sigmoid squashes it into a probability: far on the positive side means near $1$, far on the negative side near $0$, on the boundary exactly $0.5$.
$$P(y=1\mid x)=\sigma(w^Tx)=\frac1{1+e^{-w^Tx}}$$ $w$ are the weights (a dummy feature $1$ can carry the bias). The predicted class is $1$ when this probability exceeds the threshold (default $0.5$, i.e. $w^Tx>0$).
$$L(w)=\sum_{i:\,y_i=1}-\log p_i\ +\sum_{i:\,y_i=0}-\log(1-p_i),\qquad p_i=\sigma(w^Tx_i)$$ A point of class $1$ costs $-\log p_i$ (small if $p_i$ is near $1$); a point of class $0$ costs $-\log(1-p_i)$.
$$w\leftarrow w-\alpha\,X^T\big(\sigma(Xw)-y\big)$$ with $X$ the $n\times m$ feature matrix (one row per point), $y$ the $n$ labels and $\alpha$ the learning rate. The error vector $\sigma(Xw)-y$ is multiplied by $X^T$ and we move against the gradient (minus sign).
Worked examples
Loss $L=-[y\ln p+(1-y)\ln(1-p)]$ with $p=\sigma(w^Tx)$. The sigmoid’s slope is $p(1-p)$, so $\dfrac{\partial L}{\partial w}=\left(-\dfrac yp+\dfrac{1-y}{1-p}\right)p(1-p)\,x=(p-y)\,x$. Going downhill gives $w\leftarrow w+\eta\,(y-p)\,x$.
Numbers: $w=(0,0)$, $x=(2,1)$, $y=1$, $\eta=1$. Then $p=0.5$, the new $w=(1,0.5)$, and $p$ rises to $\sigma(2.5)\approx0.92$.
Problem. $w=(1,1)$, points $(-3,-3),(-0.5,-0.3),(0.5,0.3),(2,2)$ (ordered along $w$). Probabilities?
What is asked. The ordering of $p$.
Idea / formula used. $p=\sigma(w^Tx)$.
Problem. Points $1,2,3$ have class $0$ and points $4,5$ class $1$; the model gives $p=(0.2,0.1,0.5,0.9,0.6)$.
What is asked. The total log loss.
Idea / formula used. Class 1 pays $-\ln p$, class 0 pays $-\ln(1-p)$.
Problem. A video detector must catch almost all objectionable videos (positive class); false alarms are acceptable. Should the threshold be high or low?
What is asked. The threshold choice.
Idea / formula used. Predict positive when $p\ge\text{threshold}$.
Which features give a circular boundary?
A circle $(x_1-a)^2+(x_2-b)^2=r^2$ expands to $x_1^2+x_2^2-2ax_1-2bx_2+(a^2+b^2-r^2)=0$. A linear function of the features can represent it only if the features include $1$, $x_1$, $x_2$, $x_1^2$ and $x_2^2$. Without $x_1,x_2$ the circle must be centred at the origin; without the constant $1$ it must pass through the origin.
Loss on correct predictions
Even with $100\%$ accuracy the loss is not $0$: every $p_i<1$. Points far from the boundary have $p_i$ close to the true label, so their loss is small; points near the boundary have $p_i$ near $0.5$ and a larger loss. So correctly classified points far from the boundary give a smaller loss than correctly classified points close to it.
Gradient descent subtracts: $w\leftarrow w-\alpha X^T(\sigma(Xw)-y)$. Using $Xw-y$ (without sigmoid) is linear regression, and $X$ (not $X^T$) would not have the right dimensions.
The sigmoid, drawn [ADDED]
Hinge loss next to logistic loss [ADDED]
$z=-2$: probability of class 1?
$0.119$.
6. Softmax regression for many classes
Intuition
Logistic regression answers yes/no. Softmax is a multiple-choice question: it scores every class and turns the scores into probabilities that add up to $1$.
$$P(y=c\mid x)=\frac{e^{w_c^Tx}}{\sum_{j=1}^Ke^{w_j^Tx}}$$ $K$ is the number of classes and $w_c$ the weight vector of class $c$. The $K$ numbers are positive and sum to $1$.
$$W\in\mathbb R^{d\times K}$$ $d$ rows (one per feature), $K$ columns (one per class). Column $c$ holds the weights for class $c$.
Worked examples
Problem. Which can be a softmax output: $[0.5,0.3,0.2]$, $[0.7,0.4,-0.1]$, $[0.25,0.25,0.25,0.25]$, $[0.6,0.6,0.1]$?
What is asked. The valid vectors.
Idea / formula used. Every entry is positive and the entries sum to $1$.
Problem. $4$ classes, $7$ features, bias handled separately.
What is asked. The shape of $W$, and what its second row holds.
Idea / formula used. $W$ is $d\times K$.
Rows go with features, columns with classes (in this convention $W$ is $d\times K$).
A worked softmax [ADDED] (not a PYQ)
Scores $z=(2,1,0)$ for three classes. Exponentials $e^2,e^1,e^0$, normalised:
| Class | Score | Probability |
|---|---|---|
| 1 | 2 | 0.6652 |
| 2 | 1 | 0.2447 |
| 3 | 0 | 0.0900 |
They add to $1.0000$. The largest score gets the largest probability, and adding the same constant to every score changes nothing.
Can softmax output $(0.5,0.6,-0.1)$?
No: a negative entry and the sum is not 1.
7. Hard-margin SVM: the biggest safe margin
Intuition
Many fences separate two villages. The safest fence is the one furthest from both villages: it keeps the widest empty street between them.
For separable data with labels $y_i\in\{-1,+1\}$, choose the line $w^Tx+b=0$ that maximises the margin (the width of the empty strip), subject to every point being on the correct side beyond the strip.
$$\min_{w,b}\ \tfrac12\|w\|^2\quad\text{subject to}\quad y_i(w^Tx_i+b)\ge1\ \text{ for all }i$$ The margin width is $2/\|w\|$, so minimising $\|w\|$ maximises the margin. $y_i(w^Tx_i+b)\ge1$ says point $i$ is on its correct side and at least the margin edge away.
Worked examples
Problem. $w=(1,1)$, $b=-3$. Points: $A=(1,0)$ with $y=-1$, $B=(3,2)$ with $y=+1$, $C=(2,1.5)$ with $y=+1$.
What is asked. Which satisfy $y(w^Tx+b)\ge1$?
Idea / formula used. Compute the score, then multiply by the label.
Problem. $w=(3,4)$, $b=-5$.
What is asked. The distance between the lines $w^Tx+b=-1$ and $w^Tx+b=1$.
Idea / formula used. Walk from the boundary straight along the unit vector $w/\|w\|$.
A hard-margin SVM, drawn [ADDED]
Which points decide a hard-margin SVM?
Only the support vectors, those on the margin lines.
8. Soft-margin SVM: slack variables and accuracy
Intuition
If the villages overlap, no street is wide enough for everyone. Soft margin lets a few people stand inside the street or on the wrong side, but charges a fine proportional to how far they are over the line.
$\xi_i=\max\big(0,\ 1-y_i(w^Tx_i+b)\big)$: how far point $i$ violates its margin constraint. $\xi_i=0$ if the point is on or beyond its margin edge.
$$\min_{w,b,\xi}\ \tfrac12\|w\|^2+C\sum_i\xi_i\quad\text{s.t.}\ y_i(w^Tx_i+b)\ge1-\xi_i,\ \xi_i\ge0$$ $C>0$ is the price per unit of slack: large $C$ punishes violations heavily (narrow margin), small $C$ tolerates them.
| Position of a positive point | Score $w^Tx+b$ | Slack $\xi$ |
|---|---|---|
| on the margin edge or beyond | $\ge1$ | $0$ |
| on the boundary | $0$ | $1$ |
| on the negative margin edge | $-1$ | $2$ |
| deep in negative territory | $-3$ | $4$ |
Worked examples
Problem. Scores $w^Tx+b$: $P_1=2.5$, $P_2=0.4$, $P_3=-0.5$ (all class $+1$) and $N=-0.2$ (class $-1$).
What is asked. The four slack values.
Idea / formula used. $\xi=\max(0,\,1-y\cdot\text{score})$.
Problem. $w=(2,-1)$, $b=-1$. $(x_1,x_2,y)$: $(2,1,1),(1,3,1),(3,2,1),(0,0,-1),(1,-2,-1),(-1,1,-1)$.
What is asked. The fraction predicted correctly.
Idea / formula used. Predict $+1$ when the score is positive.
Problem. Three points on a line: $+$ at $0$ and $2$, $-$ at $1$. A linear classifier (threshold not at a point) is tested.
What is asked. The possible accuracies.
Idea / formula used. A threshold can only split the line into two sides.
Slack in four cases [ADDED]
$\xi=\max(0,1-m)$ with margin $m=y(w^Tx+b)$:
| $m$ | Position | Slack $\xi$ |
|---|---|---|
| 2 | on the right side, beyond the margin | 0.0 |
| 1 | exactly on the margin edge | 0.0 |
| 0.4 | right side but inside the street | 0.6 |
| -0.5 | wrong side | 1.5 |
A point has $y(w^Tx+b)=0.4$. Slack?
$0.6$.
What does a larger $C$ do?
Violations cost more, so the margin shrinks and fewer points violate it.