Total Pageviews

Thursday, October 1, 2026

🧠 SVM Kernel Mathematics & Kernel Matrices

🧠 SVM Kernel Mathematics & Kernel Matrices

A Support Vector Machine (SVM) uses a kernel function to calculate the similarity between two observations. Instead of explicitly transforming the data into a higher-dimensional feature space, the kernel computes:

K(x,z) = φ(x)ᵀφ(z)

For n observations, the kernel calculations produce an n × n Gram matrix.

1️⃣ Five-Point Dataset

Point x₁ x₂ Class y
A12-1
B21-1
C45+1
D54+1
E66+1

Example Points

We will frequently calculate the kernel between:

A = (1,2)
C = (4,5)

Dot product:

A · C = (1)(4) + (2)(5)
= 4 + 10
= 14

Squared Euclidean distance:

||A-C||² = (1-4)² + (2-5)²
= 9 + 9
= 18

L1 distance:

||A-C||₁ = |1-4| + |2-5|
= 3 + 3
= 6

2️⃣ Linear Kernel

K(x,z) = xᵀz
For A and C:

K(A,C) = (1)(4) + (2)(5)
= 4 + 10
= 14

5 × 5 Linear Kernel Matrix

[
54141318
45131418
1413414054
1314404154
1818545472
]

3️⃣ Polynomial Kernel

K(x,z) = (γxᵀz + r)ᵈ

Take:

γ = 1    r = 1    d = 2
K(A,C)
= (1 × 14 + 1)²
= 15²
= 225

Polynomial Degree 2 Matrix

[
3625225196361
2536196225361
225196176416813025
196225168117643025
361361302530255329
]

4️⃣ RBF / Gaussian Kernel

K(x,z) = exp(-γ||x-z||²)

Take:

γ = 0.1
For A and C:

||A-C||² = 18
K(A,C) = e-0.1(18)
= e-1.8
≈ 0.1653

RBF Kernel Matrix

[
1.00000.81870.16530.13530.0166
0.81871.00000.13530.16530.0166
0.16530.13531.00000.81870.6065
0.13530.16530.81871.00000.6065
0.01660.01660.60650.60651.0000
]

5️⃣ Sigmoid Kernel

K(x,z) = tanh(γxᵀz + r)

Take:

γ = 0.1    r = 0
K(A,C)
= tanh(0.1 × 14)
= tanh(1.4)
≈ 0.8854

Sigmoid Kernel Matrix

[
0.46210.37990.88540.86170.9468
0.37990.46210.86170.88540.9468
0.88540.86170.99950.99931.0000
0.86170.88540.99930.99951.0000
0.94680.94681.00001.00001.0000
]

6️⃣ Laplacian Kernel

K(x,z) = exp(-γ||x-z||₁)
For A and C:

||A-C||₁ = |1-4| + |2-5|
= 3 + 3
= 6

K(A,C) = e-0.1(6)
= e-0.6
≈ 0.5488

Laplacian Kernel Matrix

[
1.00000.81870.54880.54880.4066
0.81871.00000.54880.54880.4066
0.54880.54881.00000.81870.7408
0.54880.54880.81871.00000.7408
0.40660.40660.74080.74081.0000
]

7️⃣ ANOVA Kernel

K(x,z) = [ Σᵢ exp(-γ(xᵢ-zᵢ)²) ]ᵈ

Take:

γ = 0.1    d = 2
For A = (1,2) and C = (4,5):

K(A,C) = [e-0.1(1-4)² + e-0.1(2-5)²]²
= [e-0.9 + e-0.9]²
= [2(0.40657)]²
≈ 0.6612

ANOVA Kernel Matrix

[
4.00003.27490.66120.76080.0806
3.27494.00000.76080.66120.0806
0.66120.76084.00003.27492.4811
0.76080.66123.27494.00002.4811
0.08060.08062.48112.48114.0000
]

8️⃣ Chi-Square Kernel

K(x,z) = exp[ -γ Σᵢ (xᵢ-zᵢ)²/(xᵢ+zᵢ) ]
For A and C:

Term 1:
(1-4)²/(1+4) = 9/5 = 1.8

Term 2:
(2-5)²/(2+5) = 9/7 ≈ 1.2857

Total:
1.8 + 1.2857 = 3.0857

K(A,C) = e-0.1(3.0857)
≈ 0.7345

Chi-Square Kernel Matrix

[
1.00000.93550.73450.71650.5728
0.93551.00000.71650.73450.5728
0.73450.71651.00000.97800.9521
0.71650.73450.97801.00000.9521
0.57280.57280.95210.95211.0000
]

9️⃣ Histogram Intersection Kernel

K(x,z) = Σᵢ min(xᵢ,zᵢ)
For A and C:

K(A,C) = min(1,4) + min(2,5)
= 1 + 2
= 3

Histogram Intersection Matrix

[
32333
23333
33989
33899
339912
]

🔟 Kernel Matrix Concept

For five observations:

X = {x₁,x₂,x₃,x₄,x₅}

the kernel matrix is:

K = [ K(x₁,x₁)   K(x₁,x₂)   ...   K(x₁,x₅)
K(x₂,x₁)   K(x₂,x₂)   ...   K(x₂,x₅)
⋮            ⋱          ⋮
K(x₅,x₁)   K(x₅,x₂)   ...   K(x₅,x₅) ]

Each entry represents the similarity between two observations. For example:

K₂,₄ = K(B,D)

The diagonal contains self-similarity:

K(xᵢ,xᵢ)

📊 Kernel Comparison

Kernel Formula K(A,C)
Linear xᵀz 14
Polynomial d=2 (xᵀz+1)² 225
Polynomial d=3 (xᵀz+1)³ 3375
RBF e-0.1||x-z||² 0.1653
Sigmoid tanh(0.1xᵀz) 0.8854
Laplacian e-0.1||x-z||₁ 0.5488
ANOVA [Σe-0.1(xᵢ-zᵢ)²]² 0.6612
Chi-Square exp[-γΣ((xᵢ-zᵢ)²/(xᵢ+zᵢ))] 0.7345
Histogram Intersection Σmin(xᵢ,zᵢ) 3
📌 Important:

The kernel matrix is also called the Gram matrix. For five observations, every kernel produces a 5 × 5 matrix.

For the SVM, these kernel values are used inside the dual optimization formulation:

max α: Σᵢ αᵢ − 1/2 ΣᵢΣⱼ αᵢαⱼ yᵢyⱼ K(xᵢ,xⱼ)

subject to:

αᵢ ≥ 0

Σᵢ αᵢyᵢ = 0

No comments:

Post a Comment