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 : ○
- Class : ×
○ ○ ○ ----------------- ← 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:
- The two classes are perfectly separable.
- There is no overlap between the classes.
- 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:
the equation is:
This represents a line.
Using vector notation:
where:
and
Meaning of the Terms
| Symbol | Meaning |
|---|---|
| Input feature vector | |
| Weight vector | |
| Bias | |
| Decision function |
The equation
represents the decision boundary.
Classification Using the Hyperplane
Define the decision function:
Now there are three possibilities.
Case 1
If:
then predict:
Case 2
If:
then predict:
Case 3
If:
the point lies exactly on the decision boundary.
Therefore:
y^=sign(wTx+b)Linear Decision Boundary
Suppose each data point has two features:
A linear decision boundary is written as:
Using vector notation:
where:
- = input vector
- = weight vector
- = bias
For two dimensions:
and
Therefore:
Classification Rule
The SVM calculates:
Then:
If
the point belongs to:
If
the point belongs to:
Therefore:
Example of Classification
Suppose:
and
Then:
becomes:
The decision boundary is:
Therefore:
Now consider a point:
Then:
Since:
the predicted class is:
Now consider:
Then:
Therefore:
What are Support Vectors?
○ ○ ○ --------------------------- × × ×
Not all points are equally important.
The points closest to the separating boundary are the most important.
○ ○ [○] =========================== [×] × ×
The points inside brackets are:
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:
For Hard-Margin SVM, we define two additional parallel boundaries:
Positive margin boundary
Negative margin boundary
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:
and
are the support vectors.
Hard-Margin Classification Constraints
Suppose the class labels are:
For a point belonging to Class :
we require:
For a point belonging to Class :
we require:
Combining Both Constraints
We can combine the two constraints into one equation:
Let us verify this.
For Class
If:
then:
Therefore:
For Class
If:
then:
Therefore:
Thus:
ensures that all points are correctly classified.
Mathematical Formulation of Hard-Margin SVM
The goal of SVM is to find:
- the weight vector
- the bias
such that the margin is maximum.
Distance from a Point to a Hyperplane
Consider the hyperplane:
The perpendicular distance of a point from this hyperplane is:
where:
is the magnitude (Euclidean norm) of .
Deriving the Margin Width
The two margin boundaries are:
and
The distance between them is:
Therefore:
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:
Since is constant:
is equivalent to:
For mathematical convenience, we minimize:
Hard-Margin Linear SVM Optimization Problem
Now we can write the complete mathematical model.
Objective Function
Subject to
for every training example.
⭐ Complete Hard-Margin SVM Formula
This is called the primal formulation of Hard-Margin SVM
Why Do We Minimize ?
The width of the margin is:
Therefore, to maximize:
we need to minimize:
For mathematical convenience, we minimize:
Thus:
Maximizing the margin is equivalent to minimizing .
Worked Example: Finding the Decision Boundary
Consider the following training data.
Class
Class
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:
and
These are the:
Step 2: Find the Decision Boundary
The boundary should lie halfway between:
and
Therefore:
So the decision boundary is:
Scaling the Equation for SVM
The simple decision boundary is:
However, for the standard SVM formulation, we want the support vectors to satisfy:
and
The two support vectors are at:
- , Class
- , Class
Let:
We require:
For
For
Subtracting:
Therefore:
Substituting into:
we get:
Therefore:
Thus, the SVM decision function is:
Checking the Support Vectors
Support Vector
Therefore:
Correct.
Support Vector
Therefore:
Correct.
Checking the Hard-Margin Constraints
The constraint is:
Let's check every point.
| Point | Class | Constraint | ||
|---|---|---|---|---|
| -1 | -2 | 2 | ✓ | |
| -1 | -1 | 1 | ✓ | |
| +1 | 1 | 1 | ✓ | |
| +1 | 2 | 2 | ✓ |
All points satisfy:
Therefore, the data is correctly separated with a hard margin.
Margin Boundaries in the Example
The decision boundary is:
Therefore:
The positive margin boundary is:
Therefore:
The negative margin boundary is:
Therefore:
Thus:
Class -1 Decision Class +1 × × | ○ ○ ↑ ↑ ↑ x₁ = 2 x₁ = 3 x₁ = 4 Support Decision Support Vector Boundary Vector
Calculating the Margin
We have:
Therefore:
The total margin width is:
Therefore:
So the distance between the two margin boundaries is:
The decision boundary is exactly in the middle.
Therefore, the distance from the decision boundary to each support vector is:
Classifying New Points
The decision function is:
The classification rule is:
Example 1
Consider:
Then:
Since:
we predict:
Example 2
Consider:
Then:
Since:
we predict:
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
The complete idea of Hard-Margin Linear SVM can be summarized as follows:
Training Data
where:
Decision Function
Decision Boundary
Margin Boundaries
Classification Constraint
Margin Width
Optimization Problem
Example Problem:
Finding an SVM Classifier for the Given Data
Given data:
| Class | ||
|---|---|---|
| 2 | 2 | 0 |
| 4 | 5 | 1 |
| 7 | 4 | 1 |
We have two classes:
- Class :
- Class : ,
To use the standard hard-margin SVM formulation, let us convert the class labels:
Thus:
1. Identify the Support Vectors
Geometrically, the negative point is:
The two positive points are:
The closest positive point to is .
Distance between and :
Distance between and :
Since:
the closest points are:
These will be the support vectors.
2. Find the Maximum-Margin Decision Boundary
The maximum-margin boundary is:
- Perpendicular to the line joining the two support vectors.
- Passes through the midpoint of the support vectors.
Step 1: Find the midpoint
The support vectors are:
The midpoint is:
Therefore:
Step 2: Find a vector perpendicular to the decision boundary
The vector joining the support vectors is:
This vector is perpendicular (normal) to the decision boundary.
Therefore:
So the decision boundary has the form:
3. Find
The boundary passes through:
Substitute into:
Therefore:
So one form of the separating boundary is:
Multiplying by 2:
4. Convert to Standard SVM Form
For standard hard-margin SVM, the support vectors should satisfy:
and
Our current function is:
For :
For :
Therefore, divide everything by .
The standard SVM decision function is:
Thus:
and:
5. Final SVM Classifier
The classifier is:
The decision rule is:
Or equivalently, without fractions:
Therefore:
6. Verify All Training Points
Point
Negative, so:
✓ Correct.
Point
Positive, so:
✓ Correct.
Point
Positive, so:
✓ Correct.
Comments
Post a Comment