Simple Linear SVM with Hard Margin

 

Simple Linear SVM with Hard Margin

Introduction

A Linear Support Vector Machine (Linear SVM) is used to classify data into two classes using a straight decision boundary.

For example, suppose we have two classes:

  • Class +1+1 : ○
  • Class −1-1 : ×
              ○   ○
           ○

        -----------------  ← Decision boundary

      ×
   ×     ×

Many straight lines may separate these two classes correctly.

The main idea of SVM is:

Among all possible separating lines, choose the one with the maximum margin.


 What is Hard-Margin SVM?

A Hard-Margin SVM assumes that:

  1. The two classes are perfectly separable.
  2. There is no overlap between the classes.
  3. Every training point must be correctly classified.

For example:

Class -1                         Class +1

   ×       ×                         ○       ○
       ×                                 ○


              |        |        |
              |        |        |
         Margin     Decision    Margin
                     Boundary

The SVM finds the decision boundary that maximizes the distance from the nearest points of both classes.


 What Is a Hyperplane?

A hyperplane is the mathematical decision boundary used by SVM.

For two features:

x1,x2x_1,\quad x_2

the equation is:

w1x1+w2x2+b=0\boxed{w_1x_1+w_2x_2+b=0}

This represents a line.

Using vector notation:

wTx+b=0\boxed{w^Tx+b=0}

where:

w=[w1w2]w= \begin{bmatrix} w_1\\ w_2 \end{bmatrix}

and

x=[x1x2]x= \begin{bmatrix} x_1\\ x_2 \end{bmatrix}

Meaning of the Terms

SymbolMeaning
xx    Input feature vector
ww    Weight vector
bb    Bias
wTx+bw^Tx+b    Decision function

The equation

wTx+b=0\boxed{w^Tx+b=0}

represents the decision boundary.


Classification Using the Hyperplane

Define the decision function:

f(x)=wTx+b\boxed{f(x)=w^Tx+b}

Now there are three possibilities.

Case 1

If:

f(x)>0f(x)>0

then predict:

+1\boxed{+1}

Case 2

If:

f(x)<0f(x)<0

then predict:

−1\boxed{-1}

Case 3

If:

f(x)=0f(x)=0

the point lies exactly on the decision boundary.

Therefore:

y^​=sign(wTx+b)​

Linear Decision Boundary

Suppose each data point has two features:

x=(x1,x2)x=(x_1,x_2)

A linear decision boundary is written as:

w1x1+w2x2+b=0\boxed{w_1x_1+w_2x_2+b=0}

Using vector notation:

wTx+b=0\boxed{w^Tx+b=0}

where:

  • xx = input vector
  • ww = weight vector
  • bb = bias

For two dimensions:

w=[w1w2]w= \begin{bmatrix} w_1\\ w_2 \end{bmatrix}

and

x=[x1x2]x= \begin{bmatrix} x_1\\ x_2 \end{bmatrix}

Therefore:

wTx=w1x1+w2x2w^Tx=w_1x_1+w_2x_2

 Classification Rule

The SVM calculates:

f(x)=wTx+b\boxed{f(x)=w^Tx+b}

Then:

If

f(x)>0f(x)>0

the point belongs to:

+1\boxed{+1}

If

f(x)<0f(x)<0

the point belongs to:

−1\boxed{-1}

Therefore:

y^=sign⁡(wTx+b)\boxed{\hat y=\operatorname{sign}(w^Tx+b)}

Example of Classification

Suppose:

w=[10]w= \begin{bmatrix} 1\\ 0 \end{bmatrix}

and

b=−3b=-3

Then:

f(x)=wTx+bf(x)=w^Tx+b

becomes:

f(x)=x1−3\boxed{f(x)=x_1-3}

The decision boundary is:

x1−3=0x_1-3=0

Therefore:

x1=3\boxed{x_1=3}

Now consider a point:

x=(5,2)x=(5,2)

Then:

f(x)=5−3=2f(x)=5-3=2

Since:

2>02>0

the predicted class is:

+1\boxed{+1}

Now consider:

x=(1,4)x=(1,4)

Then:

f(x)=1−3=−2f(x)=1-3=-2

Therefore:

−1\boxed{-1}

What are Support Vectors?

Consider the following points:
             ○       ○
       ○

---------------------------

            ×       ×
       ×

Not all points are equally important.

The points closest to the separating boundary are the most important.

             ○       ○
        [○]

===========================

        [×]
             ×       ×

The points inside brackets are:

Support Vectors\boxed{\text{Support Vectors}}

Simple Definition

Support vectors are the training points closest to the decision boundary.

They play a crucial role in determining the optimal separating hyperplane.



The Margin Boundaries

The decision boundary is:

wTx+b=0\boxed{w^Tx+b=0}

For Hard-Margin SVM, we define two additional parallel boundaries:

Positive margin boundary

wTx+b=1\boxed{w^Tx+b=1}

Negative margin boundary

wTx+b=−1\boxed{w^Tx+b=-1}

Thus, we have:

             Class +1

        wα΅€x+b = 1
        -------------------

        wα΅€x+b = 0
        ===================

        wα΅€x+b = -1
        -------------------

             Class -1

The support vectors lie on the two margin boundaries.

Class +1

       ○      ○
          ○

wα΅€x+b=+1  --------------------

                ↑
                │ Margin
                ↓

wα΅€x+b=0   ====================
             Decision Boundary

                ↑
                │ Margin
                ↓

wα΅€x+b=-1  --------------------

          ×
       ×      ×

Class -1

The points lying on:

wTx+b=+1w^Tx+b=+1

and

wTx+b=−1w^Tx+b=-1

are the support vectors.


Hard-Margin Classification Constraints

Suppose the class labels are:

yi∈{−1,+1}\boxed{y_i\in\{-1,+1\}}

For a point belonging to Class +1+1:

yi=+1y_i=+1

we require:

wTxi+b≥1\boxed{w^Tx_i+b\geq1}

For a point belonging to Class −1-1:

yi=−1y_i=-1

we require:

wTxi+b≤−1\boxed{w^Tx_i+b\leq-1}

 Combining Both Constraints

We can combine the two constraints into one equation:

yi(wTxi+b)≥1\boxed{ y_i(w^Tx_i+b)\geq1 }

Let us verify this.


For Class +1+1

If:

yi=+1y_i=+1

then:

(+1)(wTxi+b)≥1(+1)(w^Tx_i+b)\geq1

Therefore:

wTxi+b≥1\boxed{w^Tx_i+b\geq1}

For Class −1-1

If:

yi=−1y_i=-1

then:

(−1)(wTxi+b)≥1(-1)(w^Tx_i+b)\geq1

Therefore:

wTxi+b≤−1\boxed{w^Tx_i+b\leq-1}

Thus:

yi(wTxi+b)≥1\boxed{ y_i(w^Tx_i+b)\geq1 }

ensures that all points are correctly classified.


Mathematical Formulation of Hard-Margin SVM

The goal of SVM is to find:

  • the weight vector ww
  • the bias bb

such that the margin is maximum.

Distance from a Point to a Hyperplane

Consider the hyperplane:

wTx+b=0\boxed{w^Tx+b=0}

The perpendicular distance of a point xx from this hyperplane is:

∣wTx+b∣∣∣w∣∣\boxed{ \frac{|w^Tx+b|}{||w||} }

where:

∣∣w∣∣=w12+w22+⋯+wn2||w||=\sqrt{w_1^2+w_2^2+\cdots+w_n^2}

is the magnitude (Euclidean norm) of ww.


Deriving the Margin Width

The two margin boundaries are:

wTx+b=+1w^Tx+b=+1

and

wTx+b=−1w^Tx+b=-1

The distance between them is:

2∣∣w∣∣\boxed{ \frac{2}{||w||} }

Therefore:

Margin Width=2∣∣w∣∣\boxed{\text{Margin Width}=\frac{2}{||w||}}

The standard hard-margin SVM formulation follows directly from maximizing this margin while satisfying the classification constraints.


The Main Mathematical Idea of SVM

SVM wants to maximize:

2∣∣w∣∣\boxed{ \frac{2}{||w||} }

Since 22 is constant:

Maximize 2∣∣w∣∣\text{Maximize }\frac{2}{||w||}

is equivalent to:

Minimize ∣∣w∣∣\text{Minimize }||w||

For mathematical convenience, we minimize:

12∣∣w∣∣2\boxed{ \frac{1}{2}||w||^2 }


Hard-Margin Linear SVM Optimization Problem

Now we can write the complete mathematical model.

Objective Function

min⁡w,b12∣∣w∣∣2\boxed{ \min_{w,b}\frac{1}{2}||w||^2 }

Subject to

yi(wTxi+b)≥1\boxed{ y_i(w^Tx_i+b)\geq1 }

for every training example.


⭐ Complete Hard-Margin SVM Formula

Minimize:12∣∣w∣∣2Subject to:yi(wTxi+b)≥1\boxed{ \begin{aligned} \text{Minimize:}\quad& \frac{1}{2}||w||^2\\ \text{Subject to:}\quad& y_i(w^Tx_i+b)\geq1 \end{aligned} }

This is called the primal formulation of Hard-Margin SVM



Why Do We Minimize 12∥w∥2\frac12\|w\|^2?

The width of the margin is:

Margin Width=2∥w∥\boxed{ \text{Margin Width}=\frac{2}{\|w\|} }

Therefore, to maximize:

2∥w∥\frac{2}{\|w\|}

we need to minimize:

∥w∥\|w\|

For mathematical convenience, we minimize:

12∥w∥2\boxed{ \frac12\|w\|^2 }

Thus:

Maximizing the margin is equivalent to minimizing 12∥w∥2\frac12\|w\|^2.


Worked Example: Finding the Decision Boundary

Consider the following training data.

Class −1-1

x1=(1,1)x_1=(1,1) x2=(2,1)x_2=(2,1)

Class +1+1

x3=(4,1)x_3=(4,1) x4=(5,1)x_4=(5,1)

The data can be visualized as:

x₂
↑

1       ×       ×               ○       ○

        1       2               4       5

+------------------------------------------------→ x₁

Step 1: Identify the Closest Points

The closest points from the two classes are:

(2,1)(2,1)

and

(4,1)(4,1)

These are the:

Support Vectors\boxed{\text{Support Vectors}}

Step 2: Find the Decision Boundary

The boundary should lie halfway between:

x1=2x_1=2

and

x1=4x_1=4

Therefore:

2+42=3\frac{2+4}{2}=3

So the decision boundary is:

x1=3\boxed{x_1=3}

Scaling the Equation for SVM

The simple decision boundary is:

x1−3=0x_1-3=0

However, for the standard SVM formulation, we want the support vectors to satisfy:

wTx+b=+1w^Tx+b=+1

and

wTx+b=−1w^Tx+b=-1

The two support vectors are at:

  • x1=2x_1=2, Class −1-1
  • x1=4x_1=4, Class +1+1

Let:

f(x)=wx1+bf(x)=wx_1+b

We require:

For x1=4x_1=4

4w+b=14w+b=1

For x1=2x_1=2

2w+b=−12w+b=-1

Subtracting:

(4w+b)−(2w+b)=1−(−1)(4w+b)-(2w+b)=1-(-1) 2w=22w=2

Therefore:

w=1\boxed{w=1}

Substituting into:

4w+b=14w+b=1

we get:

4+b=14+b=1

Therefore:

b=−3\boxed{b=-3}

Thus, the SVM decision function is:

f(x)=x1−3\boxed{f(x)=x_1-3}

Checking the Support Vectors

Support Vector (4,1)(4,1)

f(x)=4−3=1f(x)=4-3=1

Therefore:

wTx+b=+1\boxed{w^Tx+b=+1}

Correct.


Support Vector (2,1)(2,1)

f(x)=2−3=−1f(x)=2-3=-1

Therefore:

wTx+b=−1\boxed{w^Tx+b=-1}

Correct.


Checking the Hard-Margin Constraints

The constraint is:

yi(wTxi+b)≥1\boxed{ y_i(w^Tx_i+b)\geq1 }

Let's check every point.

Point    Class    yiy_if(x) f(x)yif(x) y_if(x)Constraint
(1,1)(1,1)  -1-22✓
(2,1)(2,1)  -1-11✓
(4,1)(4,1) +111✓
(5,1)(5,1)    +122✓

All points satisfy:

yif(x)≥1\boxed{y_if(x)\geq1}

Therefore, the data is correctly separated with a hard margin.


Margin Boundaries in the Example

The decision boundary is:

x1−3=0x_1-3=0

Therefore:

x1=3\boxed{x_1=3}

The positive margin boundary is:

x1−3=1x_1-3=1

Therefore:

x1=4\boxed{x_1=4}

The negative margin boundary is:

x1−3=−1x_1-3=-1

Therefore:

x1=2\boxed{x_1=2}

Thus:

Class -1              Decision             Class +1

 ×        ×              |              ○        ○
          ↑              ↑              ↑
       x₁ = 2         x₁ = 3         x₁ = 4

       Support        Decision        Support
        Vector        Boundary         Vector

Calculating the Margin

We have:

w=[10]w= \begin{bmatrix} 1\\ 0 \end{bmatrix}

Therefore:

∥w∥=12+02=1\|w\|=\sqrt{1^2+0^2}=1

The total margin width is:

2∥w∥\boxed{ \frac{2}{\|w\|} }

Therefore:

21=2\boxed{ \frac{2}{1}=2 }

So the distance between the two margin boundaries is:

2\boxed{2}

The decision boundary is exactly in the middle.

Therefore, the distance from the decision boundary to each support vector is:

1\boxed{1}

Classifying New Points

The decision function is:

f(x)=x1−3\boxed{ f(x)=x_1-3 }

The classification rule is:

y^=sign⁡(x1−3)\boxed{ \hat y=\operatorname{sign}(x_1-3) }

Example 1

Consider:

x=(6,2)x=(6,2)

Then:

f(x)=6−3=3f(x)=6-3=3

Since:

3>03>0

we predict:

+1\boxed{+1}

Example 2

Consider:

x=(1.5,4)x=(1.5,4)

Then:

f(x)=1.5−3=−1.5f(x)=1.5-3=-1.5

Since:

−1.5<0-1.5<0

we predict:

−1
\boxed{-1}

Limitation of Hard-Margin SVM

Real-world data is often noisy.

Consider:

          ○ ○ ○

             ×   ← Outlier

---------------------

       × × ×

One unusual point can make perfect separation difficult or impossible.

Hard-Margin SVM can therefore be unsuitable for noisy or overlapping data and can be sensitive to outliers.

So we need a more practical approach.( Soft Margin SVM)

Final Summary

Hard-Margin Linear SVM finds a straight decision boundary that perfectly separates two classes and chooses the boundary with the maximum possible margin. The closest points that determine this boundary are called support vectors.

The complete idea of Hard-Margin Linear SVM can be summarized as follows:

Training Data

(xi,yi)(x_i,y_i)

where:

yi∈{−1,+1}\boxed{y_i\in\{-1,+1\}}

Decision Function

f(x)=wTx+b\boxed{ f(x)=w^Tx+b }

Decision Boundary

wTx+b=0\boxed{ w^Tx+b=0 }

Margin Boundaries

wTx+b=+1\boxed{ w^Tx+b=+1 } wTx+b=−1\boxed{ w^Tx+b=-1 }

Classification Constraint

yi(wTxi+b)≥1\boxed{ y_i(w^Tx_i+b)\geq1 }

Margin Width

2∥w∥\boxed{ \frac{2}{\|w\|} }

Optimization Problem

Minimize:12∥w∥2Subject to:yi(wTxi+b)≥1\boxed{ \begin{aligned} \text{Minimize:}\quad& \frac12\|w\|^2\\ \text{Subject to:}\quad& y_i(w^Tx_i+b)\geq1 \end{aligned} }


Example Problem:

Finding an SVM Classifier for the Given Data

Given data:

X1X_1X2X_2    Class
2    2    0
4    5    1
7    4    1

We have two classes:

  • Class 00: (2,2)(2,2)
  • Class 11: (4,5)(4,5), (7,4)(7,4)

To use the standard hard-margin SVM formulation, let us convert the class labels:

0→−1,1→+10\rightarrow -1,\qquad 1\rightarrow +1

Thus:

(2,2)→−1(4,5)→+1(7,4)→+1\begin{aligned} (2,2)&\rightarrow -1\\ (4,5)&\rightarrow +1\\ (7,4)&\rightarrow +1 \end{aligned}

1. Identify the Support Vectors

Geometrically, the negative point is:

A=(2,2)A=(2,2)

The two positive points are:

B=(4,5),C=(7,4)B=(4,5),\qquad C=(7,4)

The closest positive point to AA is B=(4,5)B=(4,5).

Distance between AA and BB:

d=(4−2)2+(5−2)2d=\sqrt{(4-2)^2+(5-2)^2} =4+9=\sqrt{4+9} =13=\sqrt{13}

Distance between AA and CC:

d=(7−2)2+(4−2)2d=\sqrt{(7-2)^2+(4-2)^2} =25+4=\sqrt{25+4} =29=\sqrt{29}

Since:

13<29\sqrt{13}<\sqrt{29}

the closest points are:

(2,2) and (4,5)\boxed{(2,2)\text{ and }(4,5)}

These will be the support vectors.


2. Find the Maximum-Margin Decision Boundary

The maximum-margin boundary is:

  1. Perpendicular to the line joining the two support vectors.
  2. Passes through the midpoint of the support vectors.

Step 1: Find the midpoint

The support vectors are:

(2,2)and(4,5)(2,2)\quad\text{and}\quad(4,5)

The midpoint is:

(2+42,2+52)\left( \frac{2+4}{2}, \frac{2+5}{2} \right)

Therefore:

M=(3,3.5)\boxed{M=(3,3.5)}

Step 2: Find a vector perpendicular to the decision boundary

The vector joining the support vectors is:

(4−2,  5−2)(4-2,\;5-2) (2,3)\boxed{(2,3)}

This vector is perpendicular (normal) to the decision boundary.

Therefore:

w=[23]w= \begin{bmatrix} 2\\ 3 \end{bmatrix}

So the decision boundary has the form:

2x1+3x2+b=0\boxed{2x_1+3x_2+b=0}

3. Find bb

The boundary passes through:

(3,3.5)(3,3.5)

Substitute into:

2x1+3x2+b=02x_1+3x_2+b=0 2(3)+3(3.5)+b=02(3)+3(3.5)+b=0 6+10.5+b=06+10.5+b=0

Therefore:

b=−16.5\boxed{b=-16.5}

So one form of the separating boundary is:

2x1+3x2−16.5=0\boxed{2x_1+3x_2-16.5=0}

Multiplying by 2:

4x1+6x2−33=0\boxed{4x_1+6x_2-33=0}

4. Convert to Standard SVM Form

For standard hard-margin SVM, the support vectors should satisfy:

wTx+b=+1w^Tx+b=+1

and

wTx+b=−1w^Tx+b=-1

Our current function is:

4x1+6x2−334x_1+6x_2-33

For (2,2)(2,2):

4(2)+6(2)−334(2)+6(2)-33 8+12−33=−138+12-33=-13

For (4,5)(4,5):

4(4)+6(5)−334(4)+6(5)-33 16+30−33=1316+30-33=13

Therefore, divide everything by 1313.

The standard SVM decision function is:

f(x)=413x1+613x2−3313\boxed{ f(x)=\frac{4}{13}x_1+\frac{6}{13}x_2-\frac{33}{13} }

Thus:

w=[413613]\boxed{ w= \begin{bmatrix} \frac{4}{13}\\ \frac{6}{13} \end{bmatrix} }

and:

b=−3313\boxed{ b=-\frac{33}{13} }

5. Final SVM Classifier

The classifier is:

f(x)=413x1+613x2−3313\boxed{ f(x)=\frac{4}{13}x_1+\frac{6}{13}x_2-\frac{33}{13} }

The decision rule is:

y^={1if f(x)>00if f(x)<0\boxed{ \hat y= \begin{cases} 1 & \text{if }f(x)>0\\ 0 & \text{if }f(x)<0 \end{cases} }

Or equivalently, without fractions:

4x1+6x2−33=0\boxed{ 4x_1+6x_2-33=0 }

Therefore:

y^={1if 4x1+6x2−33>00if 4x1+6x2−33<0\boxed{ \hat y= \begin{cases} 1 & \text{if }4x_1+6x_2-33>0\\ 0 & \text{if }4x_1+6x_2-33<0 \end{cases} }

6. Verify All Training Points

Point (2,2)(2,2)

4(2)+6(2)−33=−134(2)+6(2)-33=-13

Negative, so:

Class 0\boxed{\text{Class }0}

✓ Correct.


Point (4,5)(4,5)

4(4)+6(5)−33=134(4)+6(5)-33=13

Positive, so:

Class 1\boxed{\text{Class }1}

✓ Correct.


Point (7,4)(7,4)

4(7)+6(4)−334(7)+6(4)-33 28+24−33=1928+24-33=19

Positive, so:

Class 1\boxed{\text{Class }1}

✓ Correct.


Final Answer

Support Vectors

(2,2) and (4,5)\boxed{(2,2)\text{ and }(4,5)}

Maximum-Margin Decision Boundary

4X1+6X2−33=0\boxed{4X_1+6X_2-33=0}

Standard Hard-Margin SVM Classifier

f(X)=413X1+613X2−3313\boxed{ f(X)=\frac{4}{13}X_1+\frac{6}{13}X_2-\frac{33}{13} }

Classification Rule

Class 1 if 4X1+6X2−33>0\boxed{ \text{Class }1 \text{ if }4X_1+6X_2-33>0 }                                                            Class 0 if 4X1​+6X2​−33<0​

Comments

Popular posts from this blog

Machine Learning PCCST503 Semester5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Machine Learning (ML)

Distinguishing Machine Learning from Traditional Programming