Explanation of the Classification and Kernels Simulator — What the Border Line and the Wide Road Compute

This page explains what the “line that separates houses of two colors” and the “widest road” seen in the simulator correspond to in the language of classification, and what formulas draw them, in the order words → formulas → calculation procedure. The procedure for the detailed view, the conditions of the calculation, and the questions for checking are also collected here.

1. About this page

The Classification and Kernels Simulator is a device for seeing how points divided into two classes are separated by a boundary. It treats logistic regression from Chapter 9 (Linear Classification Models) and the kernel SVM from Chapter 10 (Kernel Methods) and Chapter 11 (Support Vector Machines, SVM).

The simulator has two views.

The two scenes of the simple view are computed with the same functions as ① and ② of the detailed view. We recommend reading in this order: try the simple view, check what it really shows in Section 2, read the formulas in Section 3, and operate the detailed view with the procedure in Section 4.

2. What the simple view really shows

The “map” of the simple view is the plane with the two descriptors x1 and x2 (standardized values, dimensionless) on the horizontal and vertical axes. A “house” is one data point xn = (xn1, xn2), and the ● houses and the ■ houses are the two classes. There are 60 houses (N = 60); they are synthetic data for teaching purposes made with the same formulas as in the detailed view. One grid square of the map corresponds to one unit of a descriptor.

2.1 Scene A: Can one line separate the ● houses from the ■ houses?

Scene A is the logistic regression (first-order features) of ① in the detailed view. At a point x, we compute a = w1x1 + w2x2 + b and take p = σ(a) = 1/(1 + e−a) as “the probability that the place belongs to the ● village,” p(y = 1 | x) (Section 9.3). “Training” is gradient descent, which makes the loss L smaller by moving (w, b) little by little in the direction that reduces it.

Table 1: Words on the screen in Scene A and the corresponding terms and symbols
Words on the screenTermSymbol or formula
Houses (● village, ■ village)Data points and their classesxn, yn = 1 (●), 0 (■)
Border line (now)Decision boundarya = 0, that is, p = 0.5
Background color (Likely ● village to Likely ■ village)Posterior probability of class 1p(y = 1 | x) = σ(wTx + b)
“Can’t tell” band (between the two dotted lines)Range where p is from 0.1 to 0.9|a| ≤ ln 9, width 2 ln 9/‖w‖
Houses on the correct sideNumber of correct answers on the training dataNumber of points with an > 0 and yn = 1, or an < 0 and yn = 0
Red ✕: House on the wrong sideMisclassified pointsxn on the opposite side of the boundary
Size of the mistakes (loss)Cross-entropy loss + regularization termL(w, b), λ = 0.01
▶ Train (20 steps), Training steps doneIterations of gradient descent and their numberw ← w − η∇wL, η = 0.01, t
Line before training (the dashed line is the “Previous line”)Initial values of the iterationw = (−0.6, 1.4), b = 0.6
Apart / Mixed / RingsData type“Linearly separable,” “Overlapping,” and “Concentric circles” in the detailed view

The background color becomes darker the farther a place is from the 0.5 line (the border). Because p = 0.9 corresponds to a = ln 9 ≈ 2.20, the “Can’t tell” band lies between the two parallel lines a = ±ln 9, and its width is 2 ln 9/‖w‖. The larger ‖w‖ is, the narrower the band and the sharper the color change.

Numbers for “Try it” (all computed with the first data set (seed 20261112)):

Where the analogy fits and where it does not

Where it fits: the positions and classes of the houses are exactly the data made with the same formulas and the same seed as ① of the detailed view, and one training step is the same calculation as one iteration of gradient descent in ① (λ = 0.01, η = 0.01). The background color is p(y = 1 | x) itself.

Where it does not fit: the “villages” and “houses” are an analogy that treats the descriptor space as a map; they are not data of a real material or region. The line where training starts is deliberately set at a wrong angle (w = (−0.6, 1.4), b = 0.6) so that the movement can be seen. ① of the detailed view starts from w = 0, b = 0 (p = 0.5 everywhere), so the intermediate numbers (the loss and the number correct) do not agree with ①. After enough training, the line is the same (the minimum point of the loss) whichever start is used.

2.2 Scene B: How can we build the widest road between two villages?

Scene B is the support vector machine (SVM) of ② in the detailed view. The labels are y = +1 (●) and −1 (■); we compute the score f(x) and take f = 0 as the boundary and f = ±1 as the edges of the margin (Section 11.1). The “road” on the screen is the region |f(x)| ≤ 1; the center line of the road is the boundary f = 0, and the edges on both sides of the road are the margin edges f = ±1.

Table 2: Words on the screen in Scene B and the corresponding terms and symbols
Words on the screenTermSymbol or formula
● village, ■ villageData points and their classes (the convention of Chapter 11)xn, yn = +1 (●), −1 (■)
Road (gray band)Inside of the margin|f(x)| ≤ 1
Dashed center line of the roadDecision boundaryf(x) = 0
Road edgeEdge of the marginf(x) = ±1
Road widthMargin width2/‖w‖
○ Touching a road edgeSupport vectors on the margin0 < αn < C, ynf(xn) = 1
◇ In the road or wrong sideSupport vectors inside the margin or misclassifiedαn = C, ynf(xn) ≤ 1
Houses that set the roadSupport vectorsPoints with αn > 0
(How much overlap is allowed; fixed on the screen)Strength of the soft-margin penaltyC = 10
Straight roadLinear kernelk(x, x′) = xTx′
Road that may bendPolynomial kernel (degree 2)k(x, x′) = (xTx′ + 1)2

For a straight road, the road width 2/‖w‖ is the width on the map itself. When houses of two colors can be separated by a straight line, the SVM road is “the widest of the roads that can pass without touching any house” (Section 3.2). The movement when you press the main button (turning the road while measuring its width and stopping at the widest direction) and the small graph “Direction and road width” shown in the right column on a wide screen are a presentation to show this property; the road itself is found by solving the dual problem with the SMO method. The width and direction found by the two methods agree (for the first data set, road width 1.21 and a difference in direction of less than 0.5°).

For a road that may bend, the road width 2/‖w‖ is not the width on the map but the width in the feature space made by the kernel (six-dimensional). The width of the road drawn on the map changes from place to place, so the screen shows the road width as “Varies by place.”

Numbers for “Try it” (first data set):

Where the analogy fits and where it does not

Where it fits: the center, the two edges, and the width of the road are exactly the SVM’s f = 0, f = ±1, and margin width 2/‖w‖, computed with the same function (the SMO method) as ② of the detailed view. The way houses are divided into ○ and ◇ is also the same as in ②. The analogy of “the widest of the roads that touch no house” is exactly the definition of the hard-margin SVM when houses of two colors can be separated by a straight line.

Where it does not fit: when no road can pass without touching a house, as with the Mixed data, the SVM becomes a “soft margin” that widens the road while paying a penalty for overlap (◇), and the road is no longer the widest road. The road is decided by the balance between the road width and the penalty, and it is not the road that minimizes the number of houses left on the wrong side either. The penalty strength C is fixed at 10 on the screen, which differs from the default of ② in the detailed view (C = 1). The road that may bend is made with the degree-2 polynomial kernel, not with the RBF kernel that is the default of ② (with the RBF kernel, f returns to b far from the houses, and the “road” |f| ≤ 1 would cover most of the map). The width of the curved road is the width in the feature space of Section 3, not the width on the map.

3. How it works

3.1 Logistic regression: drawing a boundary with probabilities

Logistic regression expresses the probability that a point x belongs to class 1 by passing a linear expression a through the sigmoid function σ. The boundary p = 0.5 is the line a = 0, and the farther from the boundary, the closer p gets to 0 or 1. The loss L is the sum of “the logarithm of the probability given to the correct class” with a minus sign (the cross-entropy), and it becomes smaller the higher the probability given to the correct answer (Section 9.3). The gradient is the sum of “prediction − correct answer” multiplied by the features, and one training step subtracts it multiplied by the learning rate η.

The “Can’t tell” band of Scene A in the simple view lies between the lines p = 0.1 and 0.9.

σ(a)=0.9⇔a=ln9,band width=2ln9‖w‖

When houses of two colors can be separated by a straight line, scaling up (w, b) in the same direction brings the probability of the correct side closer to 1 for every house, so the loss keeps decreasing. Without the regularization term (λ/2)‖w‖2, ‖w‖ grows without limit and the band keeps getting narrower (Section 9.6). This is why the band keeps narrowing in “What if the villages are clearly apart?” of the simple view even after every house is right; because of the regularization with λ = 0.01, it finally stops at a finite ‖w‖ (about 6.75).

① Logistic regression (labels yn ∈ {0, 1}). The features are φ(x) = (x1, x2)T for first order, and (x1, x2, x12, x1x2, x22)T with quadratic terms.

a=wTφ(x)+b,y^=p(y=1∣x)=σ(a)=11+e−aL(w,b)=−∑n=1N{ynlny^n+(1−yn)ln(1−y^n)}+λ2‖w‖2∇wL=∑n=1N(y^n−yn)φn+λw,∂L∂b=∑n=1N(y^n−yn)w←w−η∇wL,b←b−η∂L∂b

The decision boundary is a = 0 (p = 0.5). For first-order features the boundary is a line, and the signed distance from a point x to the boundary is a/‖w‖. One term of the loss is rewritten, without passing through σ(a), as ln(1 + ea) − ya, and ln(1 + ea) is computed in an overflow-free form (for a > 0, a + ln(1 + e−a)). b is not regularized.

Learning-rate guideline: the gradient of L is Lipschitz continuous with Lc = ¼ λmax(ATA) + λ (A is the design matrix Φ with a column of ones appended, and λmax is the largest eigenvalue). If η < 2/Lc, the loss decreases at every iteration (a sufficient condition). The comparison minimum L* is found by solving the Newton equations with a Cholesky factorization of the Hessian H = ATSA + λ diag(1, …, 1, 0) (S is the diagonal matrix of ŷn(1 − ŷn); the last component is b), without forming an inverse, and with a line search. When λ = 0 and all points are on the correct side of the boundary, the display says that the minimum is not attained at finite weights.

3.2 SVM: building the widest road

The boundary of the SVM is f(x) = wTφ(x) + b = 0. For the linear kernel, φ(x) = x. If every point satisfies ynf(xn) ≥ 1, no point enters between the two edges f = ±1 (the road), and the road width is 2/‖w‖. Maximizing the road width is the same as minimizing ‖w‖, so the hard-margin SVM becomes the following primal problem (Section 11.1).

minw,b12‖w‖2subject to: yn(wTxn+b)≥1(n=1,…,N)

That this is “the widest road that can pass without touching any house” can be checked as follows. Project the houses of the two colors onto the direction of a unit vector u, and let g(u) be the difference between the minimum for the ● houses and the maximum for the ■ houses.

g(u)=minyn=+1uTxn−maxyn=−1uTxn

If g(u) > 0, a road perpendicular to u with width g(u) can pass without touching any house. For g, the maximizing direction u* and the maximum value agree with w/‖w‖ and 2/‖w‖ of the hard-margin SVM (w = 2u/g(u) satisfies the constraints and ‖w‖ = 2/g(u), so minimizing ‖w‖ is the same as maximizing g). The movement of the main button in Scene B of the simple view measures this g for each direction, and the values 0.96 and 0.69 in “What happens to the road width when you turn it?” are obtained by turning ±20° from u* and measuring g.

When no road can pass without touching a house, we allow an overlap ξn ≥ 0 for each point and add their sum multiplied by the penalty C (the soft margin, Section 11.5).

minw,b,ξ12‖w‖2+C∑n=1Nξnsubject to: ynf(xn)≥1−ξn,ξn≥0

A ◇ house (αn = C) is a house with ynf(xn) = 1 − ξn ≤ 1, that is, a house in the road or on the wrong side (in rare cases it lies exactly on the edge of the road with ξn = 0; Section 11.5). The road is decided by the balance between the road width (the first term) and the penalty for overlap (the second term). When the houses can be separated by a straight line and C is made large enough, all ξn become 0 and the solution agrees with the hard-margin solution (for the first data set, C = 10 and 1000 give the same solution).

A road that may bend is made by moving x into a space of quadratic features and then separating it with a flat surface (a hyperplane). For two-dimensional x, the kernel (xTx′ + 1)2 equals the dot product of the following six-dimensional features (Section 10.3).

φ(x)=(1,2x1,2x2,x12,x22,2x1x2)T,(xTx′+1)2=φ(x)Tφ(x′)

In the six-dimensional space the boundary is a flat surface (a hyperplane), and the road width 2/‖w‖ is also the width in that space. Back on the map (two dimensions), f(x) becomes a quadratic expression in x1 and x2, and the boundary and the road edges become quadratic curves such as ellipses and hyperbolas. This is why a road forms around the inner ring for the ring-shaped data.

In the SVM calculation, w is not found directly; instead, the variables αn of the dual problem are found. Since f(x) is a sum over only the points with αn > 0 (the support vectors), removing the houses that do not touch an edge (αn = 0) does not change the road (Section 11.3).

② Kernel SVM (labels yn ∈ {−1, +1}). The dual problem

maxαD(α)=∑n=1Nαn−12∑n=1N∑m=1Nαnαmynymk(xn,xm)subject to: 0≤αn≤C,∑n=1Nαnyn=0f(x)=∑n:αn>0αnynk(xn,x)+b

is solved. The kernels are linear, k = xTx′; polynomial, k = (xTx′ + 1)d (d = 2, 3); and RBF (Gaussian), k = exp(−γ‖x − x′‖2). The RBF kernel is the text’s σf2 exp(−‖x − x′‖2/(2ℓ2)) with σf = 1 and γ = 1/(2ℓ2).

The KKT conditions (necessary and sufficient for optimality) require one of the following at each point.

αn=0⇒ynf(xn)≥10<αn<C⇒ynf(xn)=1αn=C⇒ynf(xn)≤1

The first line is for points that are not support vectors, the second for points on the margin, and the third for points inside the margin or misclassified. The “KKT residual” on the screen is the largest violation of this condition over the points (if αn = 0, max(0, 1 − ynfn); if 0 < αn < C, |ynfn − 1|; and if αn = C, max(0, ynfn − 1)). b is determined from the points with 0 < αn < C by requiring ynf(xn) = 1 there, and the average over those points is taken (if there are no such points, the midpoint of the range allowed by the KKT conditions).

SMO (sequential minimal optimization) repeats the following: to keep the equality constraint Σαnyn = 0, it selects two α as a pair, solves that two-variable problem analytically, and clips the result to the box [0, C]. The pair is the point that violates the KKT conditions most and the point that, paired with it, improves the objective most (the same selection as LIBSVM). With Ft = Σs αsysk(xs, xt) − yt, let m(α) = max (−Ft) (over points where ytαt can be increased) and M(α) = min (−Ft) (over points where it can be decreased); then the KKT conditions are equivalent to m ≤ M. The run stops at m − M ≤ ε = 10−3.

margin width=2‖w‖,
‖w‖2=∑n,mαnαmynymk(xn,xm)
P=12‖w‖2+C∑nmax(0,1−ynfn)≥D(α)

The difference between the primal objective P (in hinge-loss form) and the dual objective D (the duality gap) is 0 at the optimum.

3.3 Calculation procedure

  1. Make the data (the same makeData as in the detailed view, seed 20261112 + k, N = 60).
  2. A and ①: set (w, b) to the initial values ((−0.6, 1.4) and 0.6 for A; 0 and 0 for ①), compute the loss and the gradient, and move (w, b) by η × gradient; repeat this as many times as the button says.
  3. A and ①: compute p = σ(a) at each point of the map grid, paint the background color, and draw the contour lines p = 0.5, 0.1, and 0.9.
  4. B and ②: change the labels to ±1, build the kernel matrix, and solve the dual problem with the SMO method (stopping at m − M ≤ 10−3). From αn, find b, the division into ○ and ◇, ‖w‖2, and the road width 2/‖w‖.
  5. B and ②: compute f(x) at each point of the map grid, paint |f| ≤ 1 as the road, and draw the contour lines f = 0 and ±1.
  6. Only for the straight-road presentation in B: turn the direction θ in steps of 0.25°, measure g(u), and narrow down near the maximum with the golden-section method (the road itself is the solution of step 4).

The simple view calls the same functions as the detailed view (makeData, logisticEval, gdApply, makeKernel, smo, svmSummary, svmDecision, and gridValues in classifier.model.js). The only calculations added for the simple view are the width of the “Can’t tell” band, the road width g(u) for each direction, and the grids and motion of the pictures (classifier.easy.js).

4. How to use the detailed view

The detailed view has two tabs, “① Logistic Regression” and “② Kernels and the SVM.” In both, the figure and the “Putting the current numbers into the formula” box are on the left, and the controls and the readout are on the right.

4.1 Procedure and what to observe

  1. Leave ① at its defaults (overlapping data, N = 60, first-order features, λ = 0.01, η = 0.01) and press “Run.” What to observe: at first w = 0 and b = 0, so p = 0.5 everywhere (loss L = 60 ln 2 ≈ 41.6). As the iterations proceed, the boundary settles into a line sloping downward to the right, and the loss approaches the Newton minimum L* ≈ 19.09 (L − L* appears in the readout).
  2. Move the pointer over the figure, or enter coordinates in the “Query point” fields. What to observe: under “Putting the current numbers into the formula,” the numbers connect a = w1x1 + w2x2 + b and p = 1/(1 + e−a). Points with a = 0 form the boundary (p = 0.5), and the signed distance to the boundary is a/‖w‖.
  3. Choose the “Linearly separable” data, check “No regularization (λ = 0),” and press “Run.” What to observe: even after the training accuracy reaches 100 %, the loss keeps decreasing toward 0 and ‖w‖ keeps growing (the boundary hardly moves, but the shading changes more abruptly). The readout for the minimum shows “none at finite weights.” When you return λ to 0.01, ‖w‖ stops at a finite value.
  4. Choose the “Concentric circles” data and compare the features “First order” and “With quadratic terms.” What to observe: with first-order features the boundary is a line, so the accuracy stays at about 60 %; adding the quadratic terms x12, x1x2, x22 makes the boundary a closed curve (an ellipse).
  5. Set the learning rate η above the “stability guideline” 2/Lc and run. What to observe: for the default data the guideline is 2/Lc ≈ 0.097. With η = 0.32 (the value displayed on the slider; the actual value is 10−0.5 ≈ 0.316), the loss falls into an oscillation that alternates between about 22.64 and 22.94 at every iteration and never reaches the minimum 19.09. For η below the guideline the loss decreases every time (the guideline is a sufficient condition, so the iteration can converge even above it; for this data it converges even at η = 0.25).
  6. Look at ② at its defaults (two moons, RBF kernel, C = 1, γ = 1). What to observe: the boundary f = 0 (solid line) and the margins f = ±1 (dashed lines). Points marked ○ (0 < αn < C) lie exactly on the dashed lines, and points marked ◇ (αn = C) lie inside the dashed lines or on the opposite side. Unmarked points (αn = 0) do not enter the sum for f(x), so removing them would not change the boundary.
  7. Increase γ and C (for example γ = 20, C = 100). What to observe: the boundary bends in fine detail around individual points, and the number of support vectors increases (overfitting). When you decrease γ, the boundary approaches a line (ℓ = 1/√(2γ) becomes longer).
  8. Set the kernel to “Linear,” the data to “Overlapping,” C to 1000, and the “SMO iteration limit” to 100. What to observe: the display says that the limit was reached and that the KKT violation m − M is larger than the tolerance, and the difference between the primal and dual objectives (the duality gap) is far from 0. When you return the limit to 100000, the run converges.

With the keyboard, Tab moves to buttons and sliders, Enter (or Space) presses a button, and the arrow keys move a slider. The tabs can be switched with ← and →. Whatever you can do on the figure (pointer and click) you can also do with the “Query point” fields and the “Add a point here” button.

5. Conditions of the calculation

5.1 Data and random-number conditions

5.2 Conditions fixed in the simple view

5.3 Simplifications

5.4 Conditions under which results become unstable

6. Questions for checking

Questions 1 and 2 can be answered with the simple view alone. Questions 3–6 use the detailed view.

  1. In “What if the villages are clearly apart?” of Scene A in the simple view, what changes if training continues after all 60 houses have come to the correct side? Why?
    Sample answer

    The number of houses on the correct side stays at 60, but the loss keeps going down (5.69 → 0.37) and the “Can’t tell” band keeps getting narrower (1.93 → 0.76). When every house is on the correct side, just making the line “steeper” in the same direction (making ‖w‖ larger) brings the probability of the correct side closer to 1 for every house, so the loss decreases. Without regularization this would go on without limit. Because of the regularization with λ = 0.01, it finally stops at a finite ‖w‖ (about 6.75).

  2. In “What if we remove the houses not touching the edges?” of Scene B in the simple view, why does the road hardly move even when 57 houses are removed?
    Sample answer

    Because the SVM score f(x) is a sum over the points with αn > 0 (the support vectors), and the houses that do not touch an edge have αn = 0, so they do not enter the sum. The width of the widest road is set only by the houses touching the edges of the road (3 houses for this data), and removing houses farther away does not create any room to widen the road.

  1. What is the loss at the start of ① (w = 0, b = 0)? How is it related to the number of data points N?
    Sample answer

    At every point a = 0 and ŷ = 0.5, so the loss per point is −ln 0.5 = ln 2 ≈ 0.693. For N = 60, L = 60 ln 2 ≈ 41.59 (the regularization term is 0). This agrees with the value at t = 0 on the screen.

  2. For linearly separable data with λ = 0, why does ‖w‖ keep growing even after the accuracy reaches 100 %? Why does it stop when λ > 0?
    Sample answer

    When all points are on the correct side, multiplying (w, b) by a factor c (c > 1) in the same direction increases |an| at every point and brings ŷn closer to the correct label, so the loss always decreases. The loss therefore only approaches 0, and there is no finite weight that gives the minimum. Gradient descent keeps moving in the direction that reduces the loss, so ‖w‖ keeps growing (for seed 20261112 and η = 0.01, about 5.99 after 1000 iterations and about 8.98 after 10000). For λ > 0, (λ/2)‖w‖2 grows as the square of ‖w‖, so a minimum appears at a finite point where it balances the decrease of the cross-entropy (for λ = 0.01, ‖w‖ ≈ 6.75).

  3. In ②, when you place the pointer on a ○ point (0 < αn < C), roughly what is f(x)? What about a ◇ point (αn = C)?
    Sample answer

    A ○ point lies on the margin, so ynf(xn) = 1; that is, f ≈ +1 if y = +1 and f ≈ −1 if y = −1 (with a deviation of the order of the stopping tolerance). A ◇ point has ynf(xn) ≤ 1; it lies inside the margin (0 < yf < 1) or on the opposite side of the boundary (yf < 0, misclassified). In neither case is f a probability, so “f = 0.8, hence 80 %” cannot be read off.

  4. Starting from the ② default (two moons, RBF, C = 1), change γ from 1 to 20. How do the number of support vectors and the margin width change, and why?
    Sample answer

    At the default (seed 20261112, N = 60) there are 22 support vectors (18 ○, 4 ◇) and the margin width is 0.581. For γ = 20 there are 59 (57 ○, 2 ◇) and 0.323 (almost all of the 60 points). When γ is large (ℓ = 1/√(2γ) is short), the kernel takes appreciable values only between nearby points, and f becomes a superposition of small hills around each point. The boundary bends along individual points, and many points remain near the boundary, so the number of support vectors increases (fitting the training data too closely, that is, overfitting).

7. Corresponding sections of the text and references

References (from the end of Chapters 9, 10, and 11):

Back to the Classification and Kernels Simulator | Machine Learning — From Probability to GPT (Contents) | Equations are typeset with the browser’s built-in MathML (no external library).