This example showcases how to perform Kriging metamodeling for a simple one-dimensional function, using various hyperparameter estimation and optimization methods.
from uqpylab import sessions, display_util
import numpy as np
import matplotlib.pyplot as plt
# Initialize common plotting parameters
display_util.load_plt_defaults()
uq_colors = display_util.get_uq_color_order()
# Start the session
mySession = sessions.cloud()
# (Optional) Get a convenient handle to the command line interface
uq = mySession.cli
# Reset the session
mySession.reset()
uqpylab.sessions :: INFO :: A new session (4c3ccb4810574e4ab022ef18267c50cb) started. uqpylab.sessions :: INFO :: Reset successful.
uq.rng(0,'twister');
The computational model is a simple analytical function defined by:
$$ y = x \sin(x), \; x \in [0,15] $$Specify this model in UQCloud using a string with vectorized operation:
ModelOpts = {
'Type': 'Model',
'Name': 'XsinX',
'ModelFun': 'xsinx.model'
}
myModel = uq.createModel(ModelOpts)
The probabilistic input model consists of one uniform random variable: $X \sim \mathcal{U}(0, 15)$
Specify the marginal and create an INPUT object:
InputOpts = {
'Marginals': {
'Type': 'Uniform',
'Parameters': [0, 15]
}
}
myInput = uq.createInput(InputOpts)
Generate $10$ sample points of $X$ using the Sobol' sequence sampling:
X = uq.getSample(myInput, 10, 'Sobol')
Evaluate the corresponding model responses:
Y = uq.evalModel(myModel,X)
Select the metamodeling tool and the Kriging module:
MetaOpts = {
'Type': 'Metamodel',
'MetaType': 'Kriging'
}
Assign the experimental design and the corresponding model responses:
MetaOpts['ExpDesign'] = {
'X': X.tolist(),
'Y': Y.tolist()
}
Perform ordinary Kriging (i.e., Kriging with a constant but unknown trend):
MetaOpts['Trend'] = {
'Type': 'ordinary'
}
Set the correlation family to Matérn 5/2:
MetaOpts['Corr'] = {
'Family': 'matern-5_2'
}
The previous options are fixed, i.e., all the Kriging metamodels that will be created will use them. The selected optimization and hyperparameter estimation method, however, varies for each Krigin metamodel that is created next:
MetaOpts['EstimMethod'] = 'ML'
MetaOpts['Optim'] = {
'Method': 'BFGS'
}
myKriging_ML_BFGS = uq.createModel(MetaOpts)
MetaOpts['Optim'] = {
'Method': 'HGA'
}
myKriging_ML_HGA = uq.createModel(MetaOpts)
MetaOpts['EstimMethod'] = 'CV'
MetaOpts['Optim'] = {
'Method': 'BFGS'
}
myKriging_CV_BFGS = uq.createModel(MetaOpts)
MetaOpts['EstimMethod'] = 'CV'
MetaOpts['Optim'] = {
'Method': 'HGA'
}
myKriging_CV_HGA = uq.createModel(MetaOpts)
Create a validation set of size $10^3$ over a regular grid:
Xval = uq.getSample(myInput, 10**3, 'grid')
Evaluate the true model response at the validation sample points:
Yval = uq.evalModel(myModel,Xval)
Evaluate the Kriging surrogate predictions on the validation set. In each case, both the mean and the variance of the Kriging predictor are calculated:
Ymu_ML_BFGS, Yvar_ML_BFGS = uq.evalModel(myKriging_ML_BFGS, Xval, nargout=2)
Ymu_ML_HGA, Yvar_ML_HGA = uq.evalModel(myKriging_ML_HGA, Xval, nargout=2)
Ymu_CV_BFGS, Yvar_CV_BFGS = uq.evalModel(myKriging_CV_BFGS, Xval, nargout=2)
Ymu_CV_HGA, Yvar_CV_HGA = uq.evalModel(myKriging_CV_HGA, Xval, nargout=2)
Lastly, comparison plots for the mean and the variance of the Kriging predictors are generated. They are divided into two groups based on the hyperparameter estimation method.
plt.plot(Xval, Yval, '-k',linewidth=1.0)
plt.plot(Xval, Ymu_ML_BFGS,'-',c=uq_colors[1])
plt.plot(Xval, Ymu_ML_HGA, '--',c=uq_colors[0])
plt.plot(X, Y, 'ko',markersize=2)
plt.xlim([0, 15])
plt.ylim([-15, 20])
plt.legend(['True model', 'Kriging, optim.method: BFGS',
'Kriging, optim. method: HGA', 'Observations'],
loc='upper left')
plt.xlabel('$\mathrm{X}$')
plt.ylabel('$\mathrm{Y}$')
plt.title('Maximum likelihood - Mean');
plt.show()
plt.plot(X, np.zeros(X.shape), 'k', linewidth=1)
plt.plot(Xval, Yvar_ML_BFGS, '-', c=uq_colors[1])
plt.plot(Xval, Yvar_ML_HGA, '--', c=uq_colors[0])
plt.plot(X, np.zeros(X.shape), 'ko', markersize=2)
plt.xlim([0, 15])
plt.ylim([0, 25])
plt.legend(['True model', 'Kriging, optim.method: BFGS',
'Kriging, optim. method: HGA', 'Observations'],
loc='upper center')
plt.xlabel('$\mathrm{X}$')
plt.ylabel('$\mathrm{\sigma^2_{\widehat{Y}}}$')
plt.title('Maximum likelihood - Variance');
plt.show()
plt.plot(Xval, Yval, 'k', linewidth=1)
plt.plot(Xval, Ymu_CV_BFGS, '-',c=uq_colors[1])
plt.plot(Xval, Ymu_CV_HGA, '--',c=uq_colors[0])
plt.plot(X, Y, 'ko', markersize=3)
plt.xlim([0, 15])
plt.ylim([-15, 25])
plt.legend(['True model', 'Kriging, optim.method: BFGS',
'Kriging, optim. method: HGA', 'Observations'],
loc='upper center')
plt.xlabel('$\mathrm{X}$')
plt.ylabel('$\mathrm{Y}$')
plt.title('Cross-validation - Mean');
plt.show()
plt.plot(Xval, np.zeros(Yval.shape), 'k', linewidth=1)
plt.plot(Xval, Yvar_CV_BFGS, '-.',c=uq_colors[1])
plt.plot(Xval, Yvar_CV_HGA, '--',c=uq_colors[0])
plt.plot(X, np.zeros(X.shape), 'ko')
plt.xlim([0, 15])
plt.ylim([0, 25])
plt.legend(['True model', 'Kriging, optim.method: BFGS',
'Kriging, optim. method: HGA', 'Observations'],
loc='upper center')
plt.xlabel('$\mathrm{X}$')
plt.ylabel('$\mathrm{\sigma^2_{\widehat{Y}}}$')
plt.title('Cross-validation - Variance');
plt.show()
mySession.quit()