2. Generic kernel classes¶
All kernels in lsqfitgp
are subclasses of Kernel
,
StationaryKernel
or IsotropicKernel
(which are themselves
subclasses of Kernel
).
The three general classes can be used directly by instantiating them with a
callable which will do the actual computation. However, this can be done in a
simpler and more functional way using the decorators kernel()
,
stationarykernel()
and isotropickernel()
.
- class lsqfitgp.Kernel(kernel, *, dim=None, loc=None, scale=None, forcebroadcast=False, forcekron=False, derivable=None, saveargs=False, **kw)¶
Base class for objects representing covariance kernels.
A Kernel object is callable, the signature is obj(x, y). Kernel objects can be summed and multiplied between them and with scalars, or raised to power with a scalar exponent.
- Parameters:
- kernelcallable
A function with signature
kernel(x, y)
, where x and y are two broadcastable numpy arrays, which computes the covariance of f(x) with f(y) where f is the Gaussian process.- dimNone or str
When the input arrays are structured arrays, if dim is None the kernel will operate on all fields, i.e., it will be passed the whole arrays. If dim is a string, kernel will see only the arrays for the field named dim. If dim is a string and the array is not structured, an exception is raised. If the field for name dim has a nontrivial shape, the array passed to kernel is still structured but has only field dim.
- loc, scalescalar
The inputs to kernel are transformed as (x - loc) / scale.
- forcebroadcastbool
If True, the inputs to kernel will always have the same shape. Default is False.
- forcekronbool
If True, when calling kernel, if x and y are structured arrays, i.e., if they represent multidimensional input, kernel is invoked separately for each dimension, and the result is the product. Default False. The selection indicated by dim limits the dimensions over which forcekron applies.
- derivablebool, int, None, or callable
Specifies how many times the kernel can be derived, only for error checking purposes. True means infinitely many times derivable. If callable, it is called with the same keyword arguments of kernel. If None (default) it means that the degree of derivability is unknown.
- saveargsbool
If True, save the all the initialization arguments in a dictionary under the attribute initargs. Default False.
- **kw
Additional keyword arguments are passed to kernel.
- Attributes:
- derivableint or None
How many times the process represented by the kernel is derivable.
sys.maxsize
if it is smooth.None
if the derivability is unknown.
Methods
rescale
(xfun, yfun)Multiply the kernel by functions of its arguments.
diff
(xderiv, yderiv)Return a Kernel-like object that computes the derivatives of this kernel.
xtransf
(xfun, yfun)Transform the inputs of the kernel.
fourier
(dox, doy)Compute the Fourier series of the kernel.
taylor
(dox, doy)Compute the Taylor series of the kernel.
- diff(xderiv, yderiv)¶
Return a Kernel-like object that computes the derivatives of this kernel. The derivatives are computed automatically with autograd. If xderiv and yderiv are trivial, this is a no-op.
\[h(x, y) = \frac{\partial^n}{\partial x^n} \frac{\partial^m}{\partial y^m} k(x, y)\]- Parameters:
- xderiv, yderivDeriv-like
A
Deriv
object or something that can be converted to a Deriv object.
- Returns:
- hKernel-like
An object representing the derivatives of this one. If
xderiv == yderiv
, it is actually another Kernel.
- Raises:
- RuntimeError
The derivative orders are greater than the derivative attribute.
- fourier(dox, doy)¶
Compute the Fourier series of the kernel.
\[\begin{split}h(k, y) = \begin{cases} \frac2T \int_0^T \mathrm dx\, k(x, y) \cos\left(\frac{2\pi}T \frac k2 x\right) & \text{if $k$ is even} \\ \frac2T \int_0^T \mathrm dx\, k(x, y) \sin\left(\frac{2\pi}T \frac{k+1}2 x\right) & \text{if $k$ is odd} \end{cases}\end{split}\]The period T is implicit in the definition of the kernel.
- Parameters:
- dox, doybool
Specify if to compute the series w.r.t. x, y or both. If both are False, this is a no-op.
- Returns:
- hKernel-like
A Kernel-like object computing the Fourier series. If dox and doy are equal, it is a Kernel.
- rescale(xfun, yfun)¶
Multiply the kernel by functions of its arguments.
\[h(x, y) = f(x) k(x, y) g(y)\]- Parameters:
- xfun, yfuncallable or None
Functions from the type of the arguments of the kernel to scalar. If both are None, this is a no-op.
- Returns:
- hKernel-like
The rescaled kernel. If
xfun is yfun
, it is a Kernel object, otherwise a Kernel-like one.
- taylor(dox, doy)¶
Compute the Taylor series of the kernel.
\[h(k, y) = \left. \frac{\partial^k}{\partial x^k} k(x, y) \right|_{x_0}\]The expansion point x0 is implicit in the definition of the kernel.
- Parameters:
- dox, doybool
Specify if to compute the series w.r.t. x, y or both. If both are False, this is a no-op.
- Returns:
- hKernel-like
A Kernel-like object computing the Taylor series. If dox and doy are equal, it is a Kernel.
- xtransf(xfun, yfun)¶
Transform the inputs of the kernel.
\[h(x, y) = k(f(x), g(y))\]- Parameters:
- xfun, yfuncallable or None
Functions mapping a new kind of input to the kind of input accepted by the kernel. If both are None, this is a no-op.
- Returns:
- hKernel-like
The transformed kernel. If
xfun is yfun
, it is a Kernel object, otherwise a Kernel-like one.
- class lsqfitgp.StationaryKernel(kernel, *, input='signed', scale=None, **kw)¶
Subclass of
Kernel
for isotropic kernels.- Parameters:
- kernelcallable
A function taking one argument delta which is the difference between x and y, plus optionally keyword arguments.
- input{‘signed’, ‘soft’}
If ‘signed’ (default), kernel is passed the bare difference. If ‘soft’, kernel is passed the absolute value of the difference, and the difference of equal points is a small number instead of zero.
- scalescalar
The difference is divided by scale.
- **kw
Additional keyword arguments are passed to the
Kernel
init.
- class lsqfitgp.IsotropicKernel(kernel, *, input='squared', scale=None, **kw)¶
Subclass of
Kernel
for isotropic kernels.- Parameters:
- kernelcallable
A function taking one argument r2 which is the squared distance between x and y, plus optionally keyword arguments.
- input{‘squared’, ‘soft’}
If ‘squared’ (default), kernel is passed the squared distance. If ‘soft’, kernel is passed the distance, and the distance of equal points is a small number instead of zero.
- scalescalar
The distance is divided by scale.
- **kw
Additional keyword arguments are passed to the
Kernel
init.