Kernels¶
online_cp.kernels.GaussianKernel
¶
Bases: Kernel
Gaussian (RBF) kernel: k(x, y) = exp(-||x - y||² / (2σ²)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma
|
float
|
Bandwidth parameter. |
required |
distance
|
str
|
Distance metric passed to scipy (default |
'sqeuclidean'
|
Source code in src/online_cp/kernels.py
online_cp.kernels.LinearKernel
¶
Bases: Kernel
Linear kernel: k(x, y) = x · y.
Source code in src/online_cp/kernels.py
online_cp.kernels.PolynomialKernel
¶
Bases: Kernel
Polynomial kernel: k(x, y) = (x · y + c)^d.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
int
|
Degree of the polynomial. |
required |
c
|
float
|
Offset constant. |
required |
Source code in src/online_cp/kernels.py
online_cp.kernels.PeriodicKernel
¶
Bases: Kernel
Periodic kernel: k(x, y) = exp(-2 sin²(π||x-y||) / s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
Period. |
required |
s
|
float
|
Smoothing parameter. |
required |
distance
|
str
|
Distance metric passed to scipy (default |
'euclidean'
|
Source code in src/online_cp/kernels.py
online_cp.kernels.LinearCombinationKernel
¶
Bases: Kernel
Positive combinations of kernels is still a kernel
k1 = LinearKernel() k2 = LinearKernel() kernels = [k1, k2] weights = [1 / 2, 1 / 2] k = LinearCombinationKernel(kernels, weights) X = np.array([[1, 2], [3, 4]]) np.allclose(k(X), (k1(X) + k2(X)) / 2) True
Source code in src/online_cp/kernels.py
online_cp.kernels.ProductKernel
¶
Bases: Kernel
TODO Implement. Need to understand what product is the relevant one. Inner? Matrix product?
Source code in src/online_cp/kernels.py
online_cp.kernels.kernel_induced_distance(kernel: Kernel) -> Callable[[NDArray[np.floating[Any]], NDArray[np.floating[Any]] | None], NDArray[np.floating[Any]]]
¶
Create a distance function from a kernel, compatible with distance_func.
The kernel-induced distance is:
.. math:: d_K(x, x') = \sqrt{K(x,x) - 2\,K(x,x') + K(x',x')}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel
|
Kernel
|
A kernel object with signature |
required |
Returns:
| Name | Type | Description |
|---|---|---|
distance_func |
callable
|
A function
Compatible with the |
Examples:
>>> from online_cp.kernels import GaussianKernel, LinearKernel, kernel_induced_distance
>>> import numpy as np
>>> dist_fn = kernel_induced_distance(LinearKernel())
>>> X = np.array([[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]])
>>> D = dist_fn(X)
>>> np.allclose(D, D.T)
True
>>> np.allclose(np.diag(D), 0.0)
True
Source code in src/online_cp/kernels.py
online_cp.kernels.kernel_matrix_to_distance_matrix(K: NDArray[np.floating[Any]]) -> NDArray[np.floating[Any]]
¶
Convert a kernel (Gram) matrix to a distance matrix.
.. math:: D_{ij} = \sqrt{K_{ii} - 2\,K_{ij} + K_{jj}}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
K
|
numpy.ndarray of shape (n, n)
|
A symmetric positive semi-definite kernel matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
D |
numpy.ndarray of shape (n, n)
|
The induced distance matrix. |
Examples:
>>> from online_cp.kernels import LinearKernel, kernel_matrix_to_distance_matrix
>>> import numpy as np
>>> X = np.array([[1.0, 0.0], [0.0, 1.0]])
>>> K = LinearKernel()(X)
>>> D = kernel_matrix_to_distance_matrix(K)
>>> np.allclose(D[0, 1], np.sqrt(2))
True