# SVM math

## **Reference**

[**The Hundred-Page Machine Learning Book**](http://themlbook.com/wiki/doku.php?id=start)

## Concept of SVM Classification

In machine learning, the boundary separating the examples of different classes is called the decision boundary.

In SVM, a hyperplane is used to make the boundary to classify the feature **X** as the label Y=+1 or Y=-1. The hyperplane is expressed with **two parameters w,** b

$$
{\bf{wx}}-b = 0
$$

where the expression **wx** means w(1)x(1) + w(2)x(2) + . . . + w(D)x(D), and D is the number of dimensions of the feature vector x.

The feature input X can be classified as Y=+1 or Y=-1, by the condition of

$$
y = sign({\bf wx} − b)
$$

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-cca9d75fa70b40211aa067400dc86cb773245aa0%2Fimage.png?alt=media)

### How does SVM find the optimal model w\* and b\*?

From solving an optimization problem.

> _For SVM, solves for Lagrange Multiplier with KKT conditions._

For SVM, it is solving optimization function with constraints as

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-b885c1f99b7640900c88761da03c8dc805a93b83%2Fimage.png?alt=media)

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-2e70401c1a1bf6bcf0c4f2ebd947177dcf6d849c%2Fimage.png?alt=media)

We would also prefer that the hyperplane separates positive examples from negative ones with the **largest margin.**

Since the margin is inverse of $$\bf ||w||$$, we need to minimize norm of **w.**

Thus, the concept of SVM optimization problem becomes:

#### Minimize $$\bf ||w||$$ with constraint subject to $$y_i({\bf wx_i} − b) \geq 1$$ for i = 1, . . . , N.

### Questions to considier

1.  What if there are outliers and no hyperplane can perfectly separate positive examples from negative ones.

    \--> Use Softmargin
2.  What if data cannot be separated by the given dimension hyperplane ? example: a plane for 3D?

    \--> Use Kernel Function

###

### Dealing with Noise/Outlier (Soft margin)

To extend SVM to cases in which the data is not linearly separable, we introduce the hinge loss function

#### Hinge Loss Function

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-a57bcd8987c65c8229b1b77c5a3655c57bf73a2c%2Fimage.png?alt=media)

The hinge loss function is zero if the constraints $$y_i({\bf wx_i} − b) \geq 1$$ for i = 1, . . . , N. are satisfied.

For data on wrong side of decision boundary, the loss function value is proportional to the distance from the decision boundary: $$1+ ({\bf wx_i} − b) >0$$

#### Soft-margin SVM

Thus, the minimization of the following cost function becomes

![\<Eq 1>](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-fc2b9866150d92828213a588316d2623df9760ce%2Fimage.png?alt=media)

where C is the hyperparamter.

* Sufficiently high values of C, the second term in the cost function will become negligible, so the SVM algorithm will try to find the highest margin by completely ignoring misclassification (generalization)
* As we decrease the value of C, making classification errors is becoming more costly, so the SVM algorithm tries to make fewer mistakes by sacrificing the margin size. (Minimizing empirical risk)

SVMs that optimize hinge loss are called soft-margin SVMs, while the original formulation is referred to as a hard-margin SVM.

The method traditionally used to solve the optimization problem is the method of **Lagrange multipliers.**

### **Dealing with Inherent Non-Linearity (Kernel Function)**

SVM can be adapted to work with datasets that cannot be separated by a hyperplane in its original space

it’s possible to transform a two-dimensional non-linearly-separable data into a linearly-separable three dimensional data using a specific mapping\\

When the 2D data is hard to classify linearly by a line

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-859c7d5534e8a41e6d5a88924a393563d4d99264%2Fimage.png?alt=media)

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-9e2d7633e9637d68a7dd2dae3b35ac1b4faa8646%2Fimage.png?alt=media)

However, we don’t know a priori which mapping φ would work for our data. We do not want to try all possible mapping function.

Instead, we can use **Kernel function** to efficiently work in higher-dimensional spaces without doing this transformation explicitly.

In SVM, **instead of directly** solving for Eq (1), it is **convenient to solve an equivalent problem of \_Lagrange Multiplier.**\_

#### **> See Method of Langrage Multiplier**

First, we have to see how the optimization algorithm for SVM finds the optimal values for w and b.

![Method of Langrage Multiplier](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-a341a0c767ae5247f773a7b0245d032262a2039a%2Fimage.png?alt=media)

This optimization problem becomes a convex quadratic optimization problem, efficiently solvable by quadratic programming algorithms.

_If we need to transform x into higher dimension,_ $$\Phi(x)$$

* Option 1) Do the transformation then do dot-product of \_\_ $$(\Phi(x_i) \cdot \Phi(x_k))$$ //Costly process
* Option 2) Kernel Trick

By using the kernel trick, we can get rid of a costly transformation of original feature vectors into higher-dimensional vectors and avoid computing their dot-product. We replace that by a simple operation on the original feature vectors that gives the same result.

Example: Using quadratic kernel for Kernel trick

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-9e2262f96551b7d54b9dc80276c1253ec2ff8a64%2Fimage.png?alt=media)

**Example: RBF Kernel**

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-d11b4b879d1e07f2387318e02bfb4d38dfe70ccd%2Fimage.png?alt=media)

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-ef3ce5bce00fe78c2d0dfecd39ebf1f739aaa8b2%2Fimage.png?alt=media)

**I**t can be shown that the feature space of the RBF (for “radial basis function”) kernel has an infinite number of dimensions. By varying the hyperparameter σ, the data analyst can choose between getting a smooth or curvy decision boundary in the original space.

## Lagrange Multiplier Method **in SVM**

### Lagrange Multiplier Method

Aim: Minimize a function f(x) under constraints in the form g\_i(x) >= 0

The optimal solution can be found by optimizing Lagrangian of f(x) by

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-a36b26bb2224756606954e67a400261d83cb82ce%2Fimage.png?alt=media)

The minimum function f(x) with the constraints is finding the Stationary Point of Lagrangian in which we **maximize** with respect to all $$\alpha_i$$ and **minimize** with respect to **x**

### Stationary Point

The point in which the _gradient_ with respect to all $$\alpha_i$$ and X is **zero.**

> Note, the function can be either maximum or minimum at stationary point

### Lagrange Multipliers for SVM problem

The condition for SVM $$g_i(x) >= 0$$ is inequalities: $$y_i({\bf wx_i} − b) \geq 1$$

Thus, for inequalities, the Karush-Kuhn-Tucker (KKT) conditions have to be satisfied for all i :

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-2f75973e8bf2b679f716b9417a8174456456c2b1%2Fimage.png?alt=media)

#### SVM problem was

Minimize $$\bf ||w||$$ with constraint o $$y_i({\bf wx_i} − b) \geq 1$$ for i = 1, . . . , N.

> Minimizing ||w|| is similar to minimizing 1/2 ||w||^2

This can be written in Lagrangian as f(x)--> 1/2 ||w||^2

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-48cac9d55ab5918a9a9fe184171ec28089b03621%2Fimage.png?alt=media)

In SVM, function

* f = $$0.5* \bf ||w|| ^2$$ is a convex function, that has one minimum.
* constraint _g\_i_ are linear

For such case, the value of the Lagrangian in the stationary point is exactly the same as the minimum of f.

@Stationary Point, _L_ = _f , where f is at its minimum._

It means, **at the stationary point**, constraint $$\alpha_i g_i=0$$

* Then, for such conditions, if either $$y_i({\bf wx_i} − b) = 1$$ or $$\alpha_i =0$$ all conditions are satisfied

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-5131a6a04b5c1546a58e6ffe7609bb2d63c21737%2Fimage.png?alt=media)

## Dual SVM formulation (Derivation )

### Concept

SVM optimization problem (Primal) of

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-ba9862beb13848e701a4301a9a7d961f170408c3%2Fimage.png?alt=media)

is solved by using Lagrange Multiplier method by expressing as

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-f2591b2446d33a4436a087af40c8a2c598cceb90%2Fimage.png?alt=media)

But usally, SVM is re-written as Dual SVM formula, to be **independent to w** to use **only support vector x.,** so we can \*\*\*\* apply **Kernel functions (Kernel trick).**

**So it becomes the optimizing L w.r.t** $$\alpha_i$$ **only. Thus, we want to solve \_L to get maximum**\_ $$\alpha_i$$_**parameters to stationary points**_

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-37c0cd50b861556f22c02649de9ec3ef42d40266%2Fimage.png?alt=media)

* It now becomes finding stationary point of $$L(\alpha)$$ with only parameter of $$\alpha$$ (instead of w, b)
* It is a quadratic programming (convex) problem that has global solution.
* Conditions now become (1) one equal condition and (2) N inequality conditions
* We can use kernel trick on $$x_i \cdot x_k$$

#### Support vector in dual form

The data points _xj_ corresponding to nonzero _αj_ are the _support vectors_.

To make predictions, **only support vectors x are used, (** b is calculated from **x**) so the predictions in SVM are very fast:

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-675ae4f345088fed20b9340cdda9a3854396ee8f%2Fimage.png?alt=media)

### Derivation

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-f8c70468b1514dc9053c036e9066a8380b24e410%2Fimage.png?alt=media)

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-524139b1e310b1e71998bf7b019b6f07174bcf12%2Fimage.png?alt=media)

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-ecc303136eaa4ffd9c7187090fd0615013817219%2Fimage.png?alt=media)

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-d14806e7d93e8fb4f7d9501727946cb5c425eb04%2Fimage.png?alt=media)

## How to solve the Optimal parameters of Dual SVM formula?

This is solving for solving N number of $$\alpha_i$$ There are several algorithms that can be applied such as

* SMO: sequential minimal optimization
* Plane Cutting:
* Coordinate Descent:

## Non-Linear SVM

Just use the Kernel function on $$x_i \cdot x_k$$

## Reference

### \[집콕]딥러닝 시대에도 필요한 고급기계학습 - Support VectorMachine

{% embed url="http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_44+2021_T1/video" %}
