← All algorithms

Classification

Support Vector Machine, SVM

2023-12-18

SVM is a widely used and important method in classification, particularly known for its ability to solve complex classification problems using Kernel SVM. The main difference between SVM and Logistic Regression is that SVM does not provide the probability of a data point being close to 1, denoted as P.

To understand the algorithm, let's assume a two-dimensional scenario where we classify one value as +1 and the other as -1 for binary classification as shown in the picture below. Then we would like to define the Positive Hyperplane for the group belonging to +1 and the Negative Hyperplane for the group belonging to -1. The purpose is to find the Hyperplanes to maximize the distance between them and determine the decision boundary. In other words, we would like to pave the widest road between two types of samples.

​

​

Step by step algorithm development procedure is as follows:

Step 1. Define the equations for the two planes satisfying the following conditions.

For all class member y=+1: wTx+b ≥ 1

For all class member y=-1: wTx+b ≤ -1

​Here w is the coefficients of the plane and c is the bias. Both can be merged into a single equation utilizing the sign of y.

y(wTx+b)-1 ≥ 0

1-y(wTx+b) ≤ 0

Step 2. Since the perpendicular direction from the plane at any point is defined by w/‖w‖, the distance between the two planes is 2/||w||. Please refer the Note at the bottom. In order to maximize the distance, we need to minimize the ‖w‖ and it does have the same meaning as to minimize ‖w‖2

‖w‖2 = wTw = SUMSQ(W) in Excel and for two source parameters case, it will be w1*w1+w2*w2.

Step 3. Our object is to find w so as to minimize wTw while w and c are subject to the inequality formula.

​\(\min_w \dfrac {1}{2} w^Tw\)

subject to

\(1-y(w^Tx+b) \le 0\)

Step 4. We use the Lagrange multiplier method to find w and c. We will introduce as many Lagrange multipliers, ai, as the number of samples, and later convert the objective to the dual problem. It is very important to note that we assume all the Lagrange multipliers are positive in the model.

\(\text{min}_w^{\ }\ L=\dfrac{1}{2}w^Tw+\sum _i^{\ }\lambda_i\lbrace 1-y_i(w^Tx_i+b)\rbrace\)

​

Step 5. Expanding L and taking the derivative L w.r.t. w and c, we have

From \(\partial L/\partial w_j=w_j-\sum _i^{\ }\lambda_iy_ix_{ji}=0 \rightarrow w_j=\sum _i^{\ }\lambda_iy_ix_{ji}\)

As an example, two source parameter case, \(w_1=\sum _{\ }^{\ }\lambda_iy_ix_{1i},w_2=\sum _{\ }^{\ }\lambda_iy_ix_{2i}\)

From \(\partial L/\partial b=\sum _i^{\ }\lambda_iy_i=0\)

Expanding L and substituting the results from the derivatives into, we have the dual form.

\(L=\dfrac{1}{2}w^Tw+\sum _{\ }^{\ }\lambda_i\lbrace 1-y_i(w^Tx_i+b)\rbrace\)

\(=\dfrac{1}{2}w^Tw+\sum _{\ }^{\ }\lambda_i-\sum _{\ }^{\ }\lambda_iy_iw^Tx_i+b\sum _{\ }^{\ }\lambda_iy_i\)

\(=\dfrac{1}{2}w^Tw+\sum _{\ }^{\ }\lambda_i-w^Tw+b\sum _{\ }^{\ }\lambda_iy_i\)

\(=-\dfrac{1}{2}w^Tw+\sum _{\ }^{\ }\lambda_i\)

Final dual form to minimize becomes

\(\max_w L=-\dfrac{1}{2}w^Tw+\sum _{\ }^{\ }\lambda_i\) Or

\(\min_w L=\dfrac{1}{2}w^Tw-\sum _{\ }^{\ }\lambda_i\)

Here again, \(w_j = \sum \lambda_iy_ix_{ji}\)

Unlike logistic regression, we obtain the coefficients by using Lagrange multipliers which behave like weight per each sample for a weighted sum calculation.

The constraints are

\(\sum _{\ }^{\ }\lambda_iy_i=0\)

\(\lambda_i\ge 0\)

Step 6. Now we can use Excel's Solver to find all the multipliers, λi, to minimize the cost function, L.

Step 7. After the optimization, any sample data with λi ≥ 0 are called "Support Vectors" and they define the slope (i.e. coefficients vector, w) and all the decision boundary.

​Step 8. The c is calculated by using the support vectors.

for y=1 samples, find b+ = 1-wTx, for y=-1 samples, find b- = -1-wTx, and then take the average of b+ and b- as b.

The x vector plane satisfying wTx+b = 0 defines decision boundary,

The x vector plane satisfying wTx+b = -1 defines Negative hyperplane,

The x vector plane satisfying wTx+b = 1 defines Positive hyperplane.

​

​

We use the same Iris dataset for SVM demonstration with Logistic regression. Steps and Excel functions used are as follows:

Step 1. Binary classifying the Iris class with +1 or -1 and assign the values Y to Column E.

​

Step 2. Assign Lagrange Multiplier, A, per each sample at Column G and randomize between 0 and 1 using RAND() function

​

Step 3. Calculate w1 and w2.

w1 = SUMPRODUCT(X1,Y,A)

w2 = SUMPRODUCT(X2,Y,A)

​

Step 4. Calculate the constraint which must be zero after an optimization.

SUMPRODUCT(Y,A)

​Step 5. Calculate the cost function to be maximized as a function of the Lagrange Multipliers, A at Column G​

L=-0.5*(w1*w1+w2*w2)+SUM(A)

​

Step 6. Run Solver w/ the following setting - you may have to repeat a few times to converge.

​Set the constraints

(1)SUMPRODUCT(Y,A)=0

(2) all A > 0 by selecting the box for "Non-Negative"

​After completion, most of the multipliers are zero except 4 samples which are Supportive vectors as shown below.

Step 7. Set Hyperplanes and Decision boundary.

​

​

<Note> Proof of the distance of two hyperplane is 2/||W||

Let vector x0 is on the hyperplane wx−b=−1, there for wx0-b = -1. The distance to the other hyperplane wx-b=1 from x0 is assumed to be r which we need to calculate. The vector location at the other plane is x0+rw/‖w‖ because w/‖w‖ is the vertical direction of the plane. This new vector must satisfy the following:

w(x0+rw/‖w‖)-b=1

By expanding the equation, we have

wx0+rwTw/‖w‖ - b =1

wx0+r‖w‖2/‖w‖ - b =1

By substituting wx0-b = -1 into above equation, we can prove the formula for the distance

r‖w‖2/‖w‖ = 2

r = 2/‖w‖