The Kernel
Class¶
Kernel module, implements a few standard stationary kernels for use with the
GaussianProcess
class. At present, kernels can only be selected manually by setting
the kernel
attribute of the GP. The default is to use the SquaredExponential
kernel, but this can be changed once the GaussianProcess
instance is created.
-
class
mogp_emulator.Kernel.
Kernel
¶ Generic class representing a stationary kernel
This base class implements the necessary scaffolding for defining a stationary kernel. Stationary kernels are only dependent on a distance measure between any two points, so the base class holds all the necessary information for doing the distance computation. Individual subclasses will implement the functional dependence of the kernel on the distance, plus first and second derivatives (if desired) to compute the gradient or Hessian of the kernel with respect to the hyperparameters.
This implementation uses a scaled euclidean distance metric. Each individual parameter has a hyperparameter scale associated with it that is used in the distance computation. If a different metric is to be defined, a new base class needs to be defined that implements the
calc_r
, and optionallycalc_drdtheta
andcalc_d2rdtheta2
methods if gradient or Hessian computation is desired. The methodskernel_f
,kernel_gradient
, andkernel_hessian
can then be used to compute the appropriate quantities with no further modification.Note that the Kernel object just collates all of the methods together; the class itself does not hold any information on the data point or hyperparamters, which are passed directly to the appropriate methods. Thus, no information needs to be provided when creating a new
Kernal
instance.-
calc_K
(r)¶ Calculate kernel as a function of distance
This method implements the kernel function as a function of distance. Given an array of distances, this function evaluates the kernel function of those values, returning an array of the same shape. Note that this is not implemented for the base class, as this must be defined for a specific kernel.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel evaluations, with the same shape as the input r
Return type: ndarray
-
calc_d2Kdr2
(r)¶ Calculate second derivative of kernel as a function of distance
This method implements the second derivative of the kernel function as a function of distance. Given an array of distances, this function evaluates the second derivative function of those values, returning an array of the same shape. Note that this is not implemented for the base class, as this must be defined for a specific kernel.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel second derivatives, with the same shape as the input r
Return type: ndarray
-
calc_d2rdtheta2
(x1, x2, params)¶ Calculate all second derivatives of the distance between all pairs of points with respect to the hyperparameters
This method computes all second derivatives of the scaled Euclidean distance between all pairs of points in
x1
andx2
with respect to the hyperparameters. The gradient is held in an array with shape(D, D, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
, andn2
is the length of the first axis ofx2
. This is used in the computation of the gradient and Hessian of the kernel. The first two indices represents the different derivatives with respect to each hyperparameter.Parameters: - x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
x2
and one less than the length ofparams
.x1
may be 1-D if either each point consists of a single parameter (andparams
has length 2) or the array only contains a single point (in which case, the array will be reshaped to(1, D - 1)
). - x2 (array-like) – Second input array. The same restrictions that apply to
x1
also apply here. - params (array-like) – Hyperparameter array. Must be 1-D with length one greater than
the last dimension of
x1
andx2
.
Returns: Array holding the second derivatives of the pair-wise distances between points in arrays
x1
andx2
with respect to the hyperparameters. Will be an array with shape(D, D, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
andn2
is the length of the first axis ofx2
. The first two axes indicates the different derivative components (i.e. the second derivative with respect to the first parameter is [0,0,:,:], the mixed partial with respect to the first and second parameters is [0,1,:,:] or [1,0,:,:], etc.)Return type: ndarray
- x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
-
calc_dKdr
(r)¶ Calculate first derivative of kernel as a function of distance
This method implements the first derivative of the kernel function as a function of distance. Given an array of distances, this function evaluates the derivative function of those values, returning an array of the same shape. Note that this is not implemented for the base class, as this must be defined for a specific kernel.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel derivatives, with the same shape as the input r
Return type: ndarray
-
calc_drdtheta
(x1, x2, params)¶ Calculate the first derivative of the distance between all pairs of points with respect to the hyperparameters
This method computes the derivative of the scaled Euclidean distance between all pairs of points in
x1
andx2
with respect to the hyperparameters. The gradient is held in an array with shape(D, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
, andn2
is the length of the first axis ofx2
. This is used in the computation of the gradient and Hessian of the kernel. The first index represents the different derivatives with respect to each hyperparameter.Parameters: - x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
x2
and one less than the length ofparams
.x1
may be 1-D if either each point consists of a single parameter (andparams
has length 2) or the array only contains a single point (in which case, the array will be reshaped to(1, D - 1)
). - x2 (array-like) – Second input array. The same restrictions that apply to
x1
also apply here. - params (array-like) – Hyperparameter array. Must be 1-D with length one greater than
the last dimension of
x1
andx2
.
Returns: Array holding the derivative of the pair-wise distances between points in arrays
x1
andx2
with respect to the hyperparameters. Will be an array with shape(D, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
andn2
is the length of the first axis ofx2
. The first axis indicates the different derivative components (i.e. the derivative with respect to the first parameter is [0,:,:], etc.)Return type: ndarray
- x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
-
calc_drdx
(x1, x2, params)¶ Calculate the first derivative of the distance between all pairs of points with respect to the first set of inputs
This method computes the derivative of the scaled Euclidean distance between all pairs of points in
x1
andx2
with respect to the first inputx1
. The gradient is held in an array with shape(D - 1, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
, andn2
is the length of the first axis ofx2
. This is used in the computation of the derivative of the kernel with respect to the inputs. The first index represents the different derivatives with respect to each input dimension.Parameters: - x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
x2
and one less than the length ofparams
.x1
may be 1-D if either each point consists of a single parameter (andparams
has length 2) or the array only contains a single point (in which case, the array will be reshaped to(1, D - 1)
). - x2 (array-like) – Second input array. The same restrictions that apply to
x1
also apply here. - params (array-like) – Hyperparameter array. Must be 1-D with length one greater than
the last dimension of
x1
andx2
.
Returns: Array holding the derivative of the pair-wise distances between points in arrays
x1
andx2
with respect tox1
. Will be an array with shape(D, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
andn2
is the length of the first axis ofx2
. The first axis indicates the different derivative components (i.e. the derivative with respect to the first input parameter is [0,:,:], etc.)Return type: ndarray
- x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
-
calc_r
(x1, x2, params)¶ Calculate distance between all pairs of points
This method computes the scaled Euclidean distance between all pairs of points in
x1
andx2
. Each component distance is multiplied by the corresponding hyperparameter prior to summing and taking the square root. For example, ifx1 = [1.]
,x2
= [2.], andparams = [2., 2.]
thencalc_r
would return \({\sqrt{2(1 - 2)^2}=\sqrt{2}}\) as an array with shape(1,1)
.Parameters: - x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
x2
and one less than the length ofparams
.x1
may be 1-D if either each point consists of a single parameter (andparams
has length 2) or the array only contains a single point (in which case, the array will be reshaped to(1, D - 1)
). - x2 (array-like) – Second input array. The same restrictions that apply to
x1
also apply here. - params (array-like) – Hyperparameter array. Must be 1-D with length one greater than
the last dimension of
x1
andx2
.
Returns: Array holding all pair-wise distances between points in arrays
x1
andx2
. Will be an array with shape(n1, n2)
, wheren1
is the length of the first axis ofx1
andn2
is the length of the first axis ofx2
.Return type: ndarray
- x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
-
kernel_deriv
(x1, x2, params)¶ Compute kernel gradient for a set of inputs
Returns the value of the kernel gradient for two sets of input points and a choice of hyperparameters. This function should not need to be modified for different choices of the kernel function or distance metric, as after checking the inputs it simply calls the routine to compute the distance metric, kernel function, and the appropriate derivative functions of the distance and kernel functions.
Parameters: - x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
x2
and one less than the length ofparams
.x1
may be 1-D if either each point consists of a single parameter (andparams
has length 2) or the array only contains a single point (in which case, the array will be reshaped to(1, D - 1)
). - x2 (array-like) – Second input array. The same restrictions that apply to
x1
also apply here. - params (array-like) – Hyperparameter array. Must be 1-D with length one greater than
the last dimension of
x1
andx2
.
Returns: Array holding the gradient of the kernel function between points in arrays
x1
andx2
with respect to the hyperparameters. Will be an array with shape(D, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
andn2
is the length of the first axis ofx2
. The first axis indicates the different derivative components (i.e. the derivative with respect to the first parameter is [0,:,:], etc.)Return type: ndarray
- x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
-
kernel_f
(x1, x2, params)¶ Compute kernel values for a set of inputs
Returns the value of the kernel for two sets of input points and a choice of hyperparameters. This function should not need to be modified for different choices of the kernel function or distance metric, as after checking the inputs it simply calls the routine to compute the distance metric and then evaluates the kernel function for those distances.
Parameters: - x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
x2
and one less than the length ofparams
.x1
may be 1-D if either each point consists of a single parameter (andparams
has length 2) or the array only contains a single point (in which case, the array will be reshaped to(1, D - 1)
). - x2 (array-like) – Second input array. The same restrictions that apply to
x1
also apply here. - params (array-like) – Hyperparameter array. Must be 1-D with length one greater than
the last dimension of
x1
andx2
.
Returns: Array holding all kernel values between points in arrays
x1
andx2
. Will be an array with shape(n1, n2)
, wheren1
is the length of the first axis ofx1
andn2
is the length of the first axis ofx2
.Return type: ndarray
- x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
-
kernel_hessian
(x1, x2, params)¶ Calculate the Hessian of the kernel evaluated for all pairs of points with respect to the hyperparameters
Returns the value of the kernel Hessian for two sets of input points and a choice of hyperparameters. This function should not need to be modified for different choices of the kernel function or distance metric, as after checking the inputs it simply calls the routine to compute the distance metric, kernel function, and the appropriate derivative functions of the distance and kernel functions.
Parameters: - x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
x2
and one less than the length ofparams
.x1
may be 1-D if either each point consists of a single parameter (andparams
has length 2) or the array only contains a single point (in which case, the array will be reshaped to(1, D - 1)
). - x2 (array-like) – Second input array. The same restrictions that apply to
x1
also apply here. - params (array-like) – Hyperparameter array. Must be 1-D with length one greater than
the last dimension of
x1
andx2
.
Returns: Array holding the Hessian of the pair-wise distances between points in arrays
x1
andx2
with respect to the hyperparameters. Will be an array with shape(D, D, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
andn2
is the length of the first axis ofx2
. The first two axes indicates the different derivative components (i.e. the second derivative with respect to the first parameter is [0,0,:,:], the mixed partial with respect to the first and second parameters is [0,1,:,:] or [1,0,:,:], etc.)Return type: ndarray
- x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
-
kernel_inputderiv
(x1, x2, params)¶ Compute derivative of Kernel with respect to inputs x1
Returns the value of the kernel derivative with respect to the first set of input points given inputs and a choice of hyperparameters. This function should not need to be modified for different choices of the kernel function or distance metric, as after checking the inputs it simply calls the routine to compute the distance metric, kernel function, and the appropriate derivative functions of the distance and kernel functions.
Parameters: - x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
x2
and one less than the length ofparams
.x1
may be 1-D if either each point consists of a single parameter (andparams
has length 2) or the array only contains a single point (in which case, the array will be reshaped to(1, D - 1)
). - x2 (array-like) – Second input array. The same restrictions that apply to
x1
also apply here. - params (array-like) – Hyperparameter array. Must be 1-D with length one greater than
the last dimension of
x1
andx2
.
Returns: Array holding the derivative of the kernel function between points in arrays
x1
andx2
with respect to the first inputsx1
. Will be an array with shape(D, n1, n2)
, whereD
is the length ofparams
,n1
is the length of the first axis ofx1
andn2
is the length of the first axis ofx2
. The first axis indicates the different derivative components (i.e. the derivative with respect to the first input dimension is [0,:,:], etc.)Return type: ndarray
- x1 (array-like) – First input array. Must be a 1-D or 2-D array, with the length of
the last dimension matching the last dimension of
-
-
class
mogp_emulator.Kernel.
SquaredExponential
¶ Implementation of the squared exponential kernel
Class representing a squared exponential kernel. It derives from the base class for a stationary kernel, using the scaled Euclidean distance metric. The subclass then just defines the kernel function and its derivatives.
-
calc_K
(r)¶ Compute K(r) for the squared exponential kernel
This method implements the squared exponential kernel function as a function of distance. Given an array of distances, this function evaluates the kernel function of those values, returning an array of the same shape.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel evaluations, with the same shape as the input r
Return type: ndarray
-
calc_d2Kdr2
(r)¶ Calculate second derivative of the squared exponential kernel as a function of distance
This method implements the second derivative of the squared exponential kernel function as a function of distance. Given an array of distances, this function evaluates the second derivative function of those values, returning an array of the same shape.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel second derivatives, with the same shape as the input r
Return type: ndarray
-
calc_dKdr
(r)¶ Calculate first derivative of the squared exponential kernel as a function of distance
This method implements the first derivative of the squared exponential kernel function as a function of distance. Given an array of distances, this function evaluates the derivative function of those values, returning an array of the same shape.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel derivatives, with the same shape as the input r
Return type: ndarray
-
-
class
mogp_emulator.Kernel.
Matern52
¶ Implementation of the Matern 5/2 kernel
Class representing the Matern 5/2 kernel. It derives from the base class for a stationary kernel, using the scaled Euclidean distance metric. The subclass then just defines the kernel function and its derivatives.
-
calc_K
(r)¶ Compute K(r) for the Matern 5/2 kernel
This method implements the Matern 5/2 kernel function as a function of distance. Given an array of distances, this function evaluates the kernel function of those values, returning an array of the same shape.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel evaluations, with the same shape as the input r
Return type: ndarray
-
calc_d2Kdr2
(r)¶ Calculate second derivative of the squared exponential kernel as a function of distance
This method implements the second derivative of the squared exponential kernel function as a function of distance. Given an array of distances, this function evaluates the second derivative function of those values, returning an array of the same shape.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel second derivatives, with the same shape as the input r
Return type: ndarray
-
calc_dKdr
(r)¶ Calculate first derivative of the Matern 5/2 kernel as a function of distance
This method implements the first derivative of the Matern 5/2 kernel function as a function of distance. Given an array of distances, this function evaluates the derivative function of those values, returning an array of the same shape.
Parameters: r (array-like) – Array holding distances between all points. All values in this array must be non-negative. Returns: Array holding kernel derivatives, with the same shape as the input r
Return type: ndarray
-