← All algorithms

Classification

Soft-Margin SVM

2023-12-18

SVM assumes a hard margin where no margin errors occur, which means there is no mistake in the classification, and naturally has a tendency to overfit. Similar to the regularized regression, SVM can also introduce some degree of regularization to address overfitting problem. In SVM, this is achieved by allowing some margin errors, i.e., while maximizing the margin, it allows some data points between the positive and negative hyperplanes. This can be done by introducing slack variables, ζᵢ, which determine how much margin each sample is allowed to have. It is desirable to minimize the slack variables as much as possible (i.e., when they are 0, we have a hard margin case). The overall objective function, including the hyperparameter C, is as follows.

​

\(\min_{w,b,\zeta}^{\ }\left(\dfrac{1}{2}w^Tw+C\sum _{\ }^{\ }\zeta_i\right)\)

Constraints are

\(y_i(w^Tx_i+b)\ge 1-\zeta_i\)

\(\zeta_i\ge 0\)

When C is small, optimization is more influenced by the margin, ||w||, so that it allows for a larger classification mistake. On the other hand, when C is large, optimization aims to minimize the classification mistakes. Therefore, a smaller C value causes larger number of samples situated between two hyperplanes.

​

​

Mathematical derivation procedures are identical with the SVM. Starting from the Primal formulation to include the Lagrange Multipliers, λi, to each sample, taking derivative to the coefficients and bias to find a few relationships, in the end, we will get the Dual formulation. Only difference is to add slightly different constraints to the Lagrange multipliers, 0≤ λi ≤ C. Since

\(\text{if }C\ge \lambda_i, -\dfrac{1}{2}w^Tw+\sum_{\ }^{\ }\lambda_i+\sum_{\ }^{\ }(C-\lambda_i)\zeta_i \ge -\dfrac{1}{2}w^Tw+\sum_{\ }^{\ }\lambda_i\),

the solution satisfying

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

also satisfies

\(\max_\lambda -\dfrac{1}{2}w^Tw + \sum _{\ }^{\ }\lambda_i +\sum_{\ }^{\ }(C-\lambda_i)\zeta_i\) which is the original dual formulation.

Our object function becomes

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

Constrains are

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

\(0\le \lambda_i\le C\)

The coefficients, w, of the hyperplanes and decision boundary are 

\(w=\sum _{\ }^{\ }\lambda_iy_ix_i\)

After the minimization completed, i.e. after find all the Lagrange multipliers, λi, the bias, b, can be calculated by followings:

+ Hyperplane: for samples with y=1 and λ>0,

b+ = average(1-w1x1-w2x2)

- Hyperplane: for samples with y=-1 and λ>0,

b- = average(-1-w1x1-w2x2)

​The decision boundary: for samples λ>0,

b0 = average(-w1x1-w2x2)

​

Steps for the implementation of the soft margin SVM by Excel are as following:

Step 0. Set C > 0.

Step 1. Labeling data as 1 or -1

Step 2. Allocate a Lagrange multiplier per sample and assign a random number > 0. 
Step 3. Calculate w(s)

wj = sum Σλiyixji

Step 4. Calculate the cost function, L, to maximize

L -½(w1*w1+w2*w2)+Σλi

Step 5. Calculate constraint, Σ λiyi, should be zero.

Step 6. Run Solver to maximize L w.r.t λ with the constraint which should be zero, and 0 ≤ λ ≤ C.

Step 7. Calculate the bias.

​

This blog uses Iris dataset to follow the steps and shows the Excel functions used at each step.

Step 1. Labeling Iris flow at Column D by using the function.

IF(A2="Iris-versicolor",1,-1)

Step 2. Generate random numbers using RAND() at Column F as the Lagrange multipliers

​Step 3. Calculate bias b at Column E. We will finalize the bias b+,b-,b0

b = (y-w1x1-w2x2)​

Step 4. Compute the coefficients w1 and w2

​(Cell H8, w1)=SUMPRODUCT(B2:B101,D2:D101,F2:F101)

(Cell I8, w2)=SUMPRODUCT(C2:C101,D2:D101,F2:F101)

​

Step 5. Calculate the object function to maximize and constraint to be zero​

(Cell H2, Constraint for Σ λiyi = 0 ) =SUMPRODUCT(D2:D101,F2:F101)

(Cell H5, Object function) =-0.5*(H8^2+I8^2)+SUM(F2:F101)

(Cell H10, Constraint for λ<C) = max(F2:F101)

​

Step 6. Run Excel Solver. 

​(1) Maximize the objective function

(2) By changing all the Lagrange multipliers

(3) Constraints: max(λ) < 10 where C=10, Σ λiyi = 0

(4) Make unconstrained variable non-negative. λi >0

​

Step 7. After the completion of Solver, calculate the bias, b+,b-, & b0. If encounter any trouble during optimization, re-initialize the multipliers with rand() and retry. 

b+: =AVERAGEIFS(E2:E101,D2:D101,1)

b-: =AVERAGEIFS(E2:E101,D2:D101,-1)

b0: =AVERAGE(I13:I14)

​