In OpenTURNS, the KrigingAlgorithm
class can estimate the hyperparameters of a Gaussian process model based on the known output values at specific input points. The getMetamodel
method of KrigingAlgorithm
, then, returns a function which interpolates the data.
First, we need to convert the Numpy arrays coordinates
and observations
to OpenTURNS Sample
objects:
import openturns as ot
input_train = ot.Sample(coordinates)
output_train = ot.Sample(observations, 1)
The array coordinates
has shape (6, 2)
, so it is turned into a Sample
of size 6 and dimension 2. The array observations
has shape (6,)
, which is ambiguous: Is it going to be a Sample
of size 6 and dimension 1, or a Sample
of size 1 and dimension 6? To clarify this, we specify the dimension (1) in the call to the Sample
class constructor.
In the following, we define a Gaussian process model with constant trend function and squared exponential covariance kernel:
inputDimension = 2
basis = ot.ConstantBasisFactory(inputDimension).build()
covariance_kernel = ot.SquaredExponential([1.0]*inputDimension, [1.0])
algo = ot.KrigingAlgorithm(input_train, output_train,
covariance_kernel, basis)
We then fit the value of the trend and the parameters of the covariance kernel (amplitude parameter and scale parameters) and obtain a metamodel:
# Fit
algo.run()
result = algo.getResult()
krigingMetamodel = result.getMetaModel()
The resulting krigingMetamodel
is a Function
which takes a 2D Point
as input and returns a 1D Point
. It predicts the quantity of interest. To illustrate this, let us build the 2D domain [0,1]×[0,1] and discretize it with a regular grid:
# Create the 2D domain
myInterval = ot.Interval([0.0, 0.0], [1.0, 1.0])
# Define the number of interval in each direction of the box
nx = 20
ny = 10
myIndices = [nx - 1, ny - 1]
myMesher = ot.IntervalMesher(myIndices)
myMeshBox = myMesher.build(myInterval)
Using our krigingMetamodel
to predict the values taken by the quantity of interest on this mesh can be done with the following statements. We first get the vertices
of the mesh as a Sample
, and then evaluate the predictions with a single call to the metamodel (there is no need for a for
loop here):
# Predict
vertices = myMeshBox.getVertices()
predictions = krigingMetamodel(vertices)
In order to see the result with Matplotlib, we first have to create the data required by the pcolor
function:
# Format for plot
X = np.array(vertices[:, 0]).reshape((ny, nx))
Y = np.array(vertices[:, 1]).reshape((ny, nx))
predictions_array = np.array(predictions).reshape((ny,nx))
The following script produces the plot:
# Plot
import matplotlib.pyplot as plt
fig = plt.figure()
plt.pcolor(X, Y, predictions_array)
plt.colorbar()
plt.show()

We see that the predictions of the metamodel are equal to the observations at the observed input points.
This metamodel is a smooth function of the coordinates: its smoothness increases with covariance kernel smoothness and squared exponential covariance kernels happen to be smooth.