2. Generic kernel classes¶
All kernels in lsqfitgp
are subclasses of Kernel
,
StationaryKernel
or IsotropicKernel
(the latter two are
themselves subclasses of Kernel
).
Kernel
itself is a subclass of CrossKernel
, which represents
the covariance function between two different processes. In symbols, the kernel
of a process \(f\) is
while the cross kernel between processes \(f\) and \(g\) is
However you will probably never need to deal directly with a cross kernel, they
are generated automatically behind the scenes by GP
.
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, forcekron=False, derivable=None, saveargs=False, maxdim=sys.maxsize, batchbytes=None, **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)
, wherex
andy
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. Ifdim
is a string,kernel
will see only the arrays for the field nameddim
. Ifdim
is a string and the array is not structured, an exception is raised. If the field for namedim
has a nontrivial shape, the array passed tokernel
is still structured but has only fielddim
.- loc, scalescalar
The inputs to
kernel
are transformed as (x - loc) / scale.- forcekronbool
If True, when calling
kernel
, ifx
andy
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 bydim
limits the dimensions over whichforcekron
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 as
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.- maxdimint, callable or None
The maximum input dimensionality accepted by the kernel. If callable, it is called with the same keyword arguments as the kernel. Default sys.maxsize.
- batchbytesnumber, optional
If specified, apply
batch(batchbytes)
to the kernel.- **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.- maxdimint or None
Maximum input dimensionality. None means 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.
batch
(maxnbytes)Return a batched version of the kernel.
- batch(maxnbytes)¶
Return a batched version of the kernel.
The batched kernel processes its inputs in chunks to try to limit memory usage.
- Parameters:
- maxnbytesnumber
The maximum number of input bytes per chunk, counted after broadcasting the inputs (actual broadcasting may not occur if not induced by the operations in the kernel).
- Returns:
- batched_kernelCrossKernel
The same kernel but with batched computations.
- diff(xderiv, yderiv)¶
Return a Kernel-like object that computes the derivatives of this kernel. The derivatives are computed automatically with JAX. If
xderiv
andyderiv
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’, ‘hard’}
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. If ‘hard’, the absolute value.- 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’, ‘hard’, ‘soft’, ‘raw’}
If ‘squared’ (default),
kernel
is passed the squared distance. If ‘hard’, it is passed the distance (not squared). If ‘soft’, it is passed the distance, and the distance of equal points is a small number instead of zero. If ‘raw’, the kernel is passed both points separately like non-stationary kernels.- scalescalar
The distance is divided by
scale
.- **kw
Additional keyword arguments are passed to the
Kernel
init.
Notes
The ‘soft’ option will cause problems with second derivatives in more than one dimension.