metrics#

Kernel functions and helper utilities for kernel-based calculations.

Most of the commonly-used kernel functions are already implemented in sklearn.metrics.pairwise. The RBF kernel and pairwise Euclidean distance function is re-implemented here as an alternative, in case sklearn is unavailable. Most, if not all of the kernel functions defined in sklearn.metrics.pairwise should be compatible with the functions defined here.

To use the sklearn functions, use them like so:

>>> from sklearn.metrics.pairwise import rbf_kernel
>>> from sklearn.metrics.pairwise import euclidean_distances
>>> X = np.arange(4).reshape((2, 2))
>>> Y = np.arange(6).reshape((3, 2))
>>> K = gym_socks.kernel.metrics.rbf_kernel(X, Y, distance_fn=euclidean_distances)
gym_socks.kernel.metrics.abel_kernel(X, Y=None, sigma=None, distance_fn=None)[source]#

Abel kernel function.

Parameters
  • X (numpy.ndarray) – The observations are oganized in ROWS.

  • Y (Optional[numpy.ndarray]) – The observations are oganized in ROWS.

  • sigma (Optional[float]) – Strictly positive real-valued kernel parameter.

  • distance_fn – Distance function to use in the kernel evaluation.

Returns

Gram matrix of pairwise evaluations of the kernel function.

Return type

np.ndarray

gym_socks.kernel.metrics.euclidean_distance(X, Y=None, squared=False)[source]#

Euclidean distance function.

The main difference between the way this function calculates euclidean distance over other implementations such as in sklearn.metrics.pairwise is that this implementation is largely agnostic to the dimensionality of the input data, and generally works well for high-dimensional vectors and dense data sets, such as state trajectories over a long time horizon.

Parameters
  • X (numpy.ndarray) – The observations are oganized in ROWS.

  • Y (Optional[numpy.ndarray]) – The observations are oganized in ROWS.

  • squared (bool) – Whether or not the result is the squared Euclidean distance.

Returns

Matrix of pairwise distances between points.

Return type

np.ndarray

gym_socks.kernel.metrics.rbf_kernel(X, Y=None, sigma=None, distance_fn=None)[source]#

RBF kernel function.

Computes the pairwise evaluation of the RBF kernel on each vector in X and Y. For instance, if X has n vecotrs, and Y has m vectors, then the result is an n x m matrix K where K_ij = k(xi, yj).

X = [[--- x1 ---],
     [--- x2 ---],
     ...
     [--- xn ---]]

Y = [[--- y1 ---],
     [--- y2 ---],
     ...
     [--- ym ---]]

The result is a matrix,

K = [[k(x1,y1), ..., k(x1,ym)],
     ...
     [k(xn,y1), ..., k(xn,ym)]]

The main difference between this implementation and the rbf_kernel in sklearn.metrics.pairwise is that this function optionally allows you to specify a different distance metric in the event the data is non-Euclidean.

Parameters
  • X (numpy.ndarray) – The observations are oganized in ROWS.

  • Y (Optional[numpy.ndarray]) – The observations are oganized in ROWS.

  • sigma (Optional[float]) – Strictly positive real-valued kernel parameter.

  • distance_fn – Distance function to use in the kernel evaluation.

Returns

Gram matrix of pairwise evaluations of the kernel function.

Return type

np.ndarray

gym_socks.kernel.metrics.regularized_inverse(X, Y=None, U=None, V=None, regularization_param=None, kernel_fn=None)[source]#

Regularized inverse.

Computes the regularized matrix inverse.

\[W = (K + \lambda M I)^{-1}, \quad K \in \mathbb{R}^{n \times n}, \quad K_{ij} = k(x_{i}, y_{j})\]

Example

>>> from gym_socks.kernel.metrics import regularized_inverse
>>> X = np.arange(4).reshape((2, 2))
>>> Y = np.arange(6).reshape((3, 2))
>>> K = regularized_inverse(X, Y)
Parameters
  • X (numpy.ndarray) – The observations are oganized in ROWS.

  • Y (Optional[numpy.ndarray]) – The observations are oganized in ROWS.

  • regularization_param (Optional[float]) – Regularization parameter, which is a strictly positive real value.

  • kernel_fn – The kernel function is a function that returns an ndarray where each element is the pairwise evaluation of a kernel function. See sklearn. metrics.pairwise for more info. The default is the RBF kernel.

  • U (Optional[numpy.ndarray]) –

  • V (Optional[numpy.ndarray]) –

Returns

Regularized matrix inverse.

Return type

numpy.ndarray