The Kernel
Module¶
-
class
mogp_emulator.Kernel.
SquaredExponential
¶ Squared Exponential Kernel
Inherits from
SqExpBase
andStationaryKernel
, so this will be a stationary kernel with one correlation length per input dimension and a squared exponential fall-off with distance.
-
class
mogp_emulator.Kernel.
Matern52
¶ Matern 5/2 Kernel
Inherits from
Mat52Base
andStationaryKernel
, so this will be a stationary kernel with one correlation length per input dimension and a Matern 5/2 fall-off with distance.
-
class
mogp_emulator.Kernel.
ProductMat52
¶ Product Matern 5/2 Kernel
Inherits from
Mat52Base
andProductKernel
, so this will be a kernel with one correlation length per input dimension. The Matern 5/2 fall-off function is applied to each dimension before taking the product of all dimensions in this case. Generally results in a slighly smoother kernel than the stationary version of the Matern 5/2 kernel.
-
class
mogp_emulator.Kernel.
UniformSqExp
¶ Uniform Squared Exponential Kernel
Inherits from
SqExpBase
andUniformKernel
, so this will be a uniform kernel with one correlation length (independent of the number of dimensions) and a squared exponential fall-off with distance.
-
class
mogp_emulator.Kernel.
UniformMat52
¶ Uniform Matern 5/2 Kernel
Inherits from
Mat52Base
andUniformKernel
, so this will be a uniform kernel with one correlation length (independent of the number of dimensions) and a Matern 5/2 fall-off with distance.
-
class
mogp_emulator.Kernel.
KernelBase
¶ Base Kernel
-
get_n_params
(inputs)¶ Determine number of correlation length parameters based on inputs
Determines the number of parameters required for a given set of inputs. Returns the number of parameters as an integer.
Parameters: inputs (ndarray) – Set of inputs for which the number of correlation length parameters is desired. Returns: Number of correlation length parameters Return type: int
-
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
-
-
class
mogp_emulator.Kernel.
UniformKernel
¶ Kernel with a single correlation length
-
calc_d2r2dtheta2
(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_dr2dtheta
(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_r2
(x1, x2, params)¶ Calculate squared distance between all pairs of points
This method computes the scaled Euclidean distance between all pairs of points in
x1
andx2
. For example, ifx1 = [1.]
,x2
= [2.], andparams = [2.]
thencalc_r
would return \({\sqrt{exp(2)*(1 - 2)^2}=\sqrt{exp(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 squared 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
-
get_n_params
(inputs)¶ Determine number of correlation length parameters based on inputs
Determines the number of parameters required for a given set of inputs. Returns the number of parameters as an integer.
Parameters: inputs (ndarray) – Set of inputs for which the number of correlation length parameters is desired. Returns: Number of correlation length parameters Return type: int
-
-
class
mogp_emulator.Kernel.
StationaryKernel
¶ 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
Kernel
instance.-
calc_d2r2dtheta2
(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_dr2dtheta
(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_r2
(x1, x2, params)¶ Calculate squared 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 exponential of 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{exp(2)*(1 - 2)^2}=\sqrt{exp(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 squared 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
-
-
class
mogp_emulator.Kernel.
ProductKernel
¶ Product form of kernel
-
calc_r2
(x1, x2, params)¶ Calculate squared distance between all pairs of points
This method computes the scaled Euclidean distance between all pairs of points in
x1
andx2
along each axis. Each component distance is multiplied by the exponential of the corresponding hyperparameter. For example, ifx1 = [[1., 2.]]
,x2
= [[2., 4.]], andparams = [2., 2.]
thencalc_r
would return the array \({[exp(2)*(1 - 2)^2, exp(2)*(2 - 4)^2]}\) as an array with shape(2, 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)
). - 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 squared distances between points in arrays
x1
andx2
. Will be an array with shape(D, n1, n2)
, whereD
is the number of dimensions,n1
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
-