kernels

Kernels!

SwitchError
lazy_kernel(xdata[, ydata, kernel_type, dtype]) Generate kernels from cache
gaussian_kernel(xdata[, ydata, sigma]) Compute the gaussian kernel along the first dimension This uses the vector_norm_sq() to compute the norms across the vectors in the matrices and then re-scales it
homogeneous_polykern(data[, ydata, powa]) Compute the homogeneous polynomial kernel.
inhomogeneous_polykern(data[, ydata, powa]) Compute the homogeneous polynomial kernel.
linear_kernel(xdata[, ydata]) Compute a linear kernel
multiquad_kernel(xdata[, ydata, c]) Compute the multi-quadratic kernel.
vector_norm_sq(xdata[, ydata]) Compute the squared vector norm.
volterra_temporal(X[, delays, degree]) Volterra series.

SwitchError

class tikreg.kernels.SwitchError

Bases: exceptions.Exception

__init__()

x.__init__(…) initializes x; see help(type(x)) for signature

lazy_kernel

class tikreg.kernels.lazy_kernel(xdata, ydata=None, kernel_type=None, dtype=<type 'numpy.float64'>)

Bases: object

Generate kernels from cache

Some kernels operate on the inner-product space, others operate on vector norms. This class caches these matrices and lets the user construct the kernel with a given parameter from the cache. This is fast because the inner-products don’t need to be recomputed every time a new parameter is passed. This is useful when cross-validating kernel parameters (e.g. for a gaussian kernel) or when switching between different kernels types (e.g. from gaussian to multi-quadratic).

__init__(self, xdata, ydata=None, kernel_type=None, dtype=<type 'numpy.float64'>)
Parameters:
xdata : 2D np.ndarray (nx, p)

The feature matrix X

ydata : 2D np.ndarray (ny, p)

If None, ydata = xdata.

kernel_type (str)
  • ‘linear’: Linear kernel (default)
  • ‘ihpolykern’ : Inhomogeneous polynomial kernel
  • ‘hpolykern’: Homogeneous polynomial kernel
  • ‘gaussian’: Gaussian kernel
  • ‘multiquad’: Multiquadratic kernel
update(self, kernel_parameter, kernel_type=None, verbose=False)

Update the kernel with the parameter given.

Parameters:
kernel_parameter (float-like)

The parameter for the particular kernel

kernel_type (str)

The kernel to use. If none is given, assume we are using the kernel with which the class was instantiated. One can switch kernels in these directions:

  • multiquad <-> gaussian
  • linear <-> ihpolykern <-> hpolykern
Returns:
lazy_kernel.kernel : np.ndarray (nx, ny)

Sets the kernel property with the computed kernel.

gaussian_kernel

tikreg.kernels.gaussian_kernel(xdata, ydata=None, sigma=1.0)

Compute the gaussian kernel along the first dimension This uses the vector_norm_sq() to compute the norms across the vectors in the matrices and then re-scales it

Parameters:
xdata : 2D np.ndarray (nx, p)
ydata : 2D np.ndarray (ny by p) or None

If None, ydata = xdata.

sigma (float):

The width of the gaussian.

Returns:
gaussian_kern : 2D np.ndarray (nx, ny)

The gaussian kernel

homogeneous_polykern

tikreg.kernels.homogeneous_polykern(data, ydata=None, powa=2)

Compute the homogeneous polynomial kernel.

The polynomial expansion does not include interactions.

Parameters:
xdata : 2D np.ndarray (nx, p)
ydata : 2D np.ndarray (ny, p), or None

If None, ydata = xdata.

powa : scalar

Degree of polynomial expansion. Defaults to 2.

Returns:
hpoly_kernel : 2D np.ndarray (nx, ny)

inhomogeneous_polykern

tikreg.kernels.inhomogeneous_polykern(data, ydata=None, powa=2)

Compute the homogeneous polynomial kernel.

The polynomial expansion includes interaction terms.

Parameters:
xdata : 2D np.ndarray (nx, p)
ydata : 2D np.ndarray (ny, p), or None

If None, ydata = xdata.

powa : scalar

Degree of polynomial expansion. Defaults to 2.

Returns:
ihpoly_kernel : 2D np.ndarray (nx, ny)

linear_kernel

tikreg.kernels.linear_kernel(xdata, ydata=None)

Compute a linear kernel

Parameters:
xdata : 2D np.ndarray (nx, p)
ydata : 2D np.ndarray (ny, p), or None

If None, ydata = xdata.

Returns:
linear_kernel : 2D np.ndarray (nx, ny)

multiquad_kernel

tikreg.kernels.multiquad_kernel(xdata, ydata=None, c=1.0)

Compute the multi-quadratic kernel.

Parameters:
xdata : 2D np.ndarray (nx, p)
ydata : 2D np.ndarray (ny, p), or None

If None, ydata = xdata.

c : scalar

Defaults to 1.

Returns:
multiquad_kernel : 2D np.ndarray (nx, ny)

vector_norm_sq

tikreg.kernels.vector_norm_sq(xdata, ydata=None)

Compute the squared vector norm.

This assumes the vectors are in the first dimension.

Parameters:
xdata : 2D np.ndarray (nx by p)
ydata : 2D np.ndarray (ny by p) or None

If None, ydata = xdata

Returns:
Q : 2D np.ndarray (nx, ny)

The vectorm norms

Examples

>>> xdata = np.arange(3*10).reshape(3,10)
>>> print(vector_norm_sq(xdata))
[[   0 1000 4000]
 [1000    0 1000]
 [4000 1000    0]]

volterra_temporal

tikreg.kernels.volterra_temporal(X, delays=[0, 1, 2], degree=2.0)

Volterra series.

Implemented as a wrapper around inhomogenous poly