.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_surrogate_modeling/gaussian_process_regression/plot_kriging_branin_function.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_surrogate_modeling_gaussian_process_regression_plot_kriging_branin_function.py: Gaussian Process Regression: metamodel of the Branin-Hoo function ================================================================= .. GENERATED FROM PYTHON SOURCE LINES 7-10 As a popular use case in optimization we briefly present the construction of a metamodel of the Branin (also referred to as Branin-Hoo) function. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python from numpy import sqrt import openturns as ot import openturns.viewer as otv from openturns.usecases import branin_function .. GENERATED FROM PYTHON SOURCE LINES 19-20 We load the Branin-Hoo model from the usecases module and use the model (stored in `objectiveFunction`) .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python bm = branin_function.BraninModel() model = bm.objectiveFunction .. GENERATED FROM PYTHON SOURCE LINES 24-26 We shall represent this 2-d function with isolines. We set the number of isolines to a maximum of 10 thanks to the following `ResourceMap` key : .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("Contour-DefaultLevelsNumber", 10) graphBasic = model.draw([0.0, 0.0], [1.0, 1.0], [100] * 2) view = otv.View(graphBasic) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_kriging_branin_function_001.svg :alt: y0 as a function of (u1,u2) :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_kriging_branin_function_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 31-32 We get the values of all isolines : .. GENERATED FROM PYTHON SOURCE LINES 32-46 .. code-block:: Python levels = graphBasic.getDrawables()[0].getLevels() # # %% # # We now build fancy isolines : # # Build a range of colors # ot.ResourceMap.SetAsUnsignedInteger("Drawable-DefaultPalettePhase", len(levels)) # palette = ot.Drawable.BuildDefaultPalette(len(levels)) graphFineTune = ot.Graph("The exact Branin model", r"$x_1$", r"$x_2$") graphFineTune.setDrawables([graphBasic.getDrawable(0)]) # graphFineTune.setLegendPosition("") # Remove the legend .. GENERATED FROM PYTHON SOURCE LINES 47-48 We also represent the three minima of the Branin function with orange diamonds : .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python sample1 = ot.Sample([bm.xexact1, bm.xexact2, bm.xexact3]) cloud1 = ot.Cloud(sample1, "orange", "diamond", "First Cloud") graphFineTune.add(cloud1) # Draw the graph with the palette assigned to the contour view = otv.View(graphFineTune) # # The values of the exact model at these points are : print(bm.objectiveFunction(sample1)) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_kriging_branin_function_002.svg :alt: The exact Branin model :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_kriging_branin_function_002.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ y0 ] 0 : [ -1.04741 ] 1 : [ -1.04741 ] 2 : [ -1.04741 ] .. GENERATED FROM PYTHON SOURCE LINES 59-61 The Branin function has a global minimum attained at three different points. We shall build a metamodel of this function that presents the same behaviour. .. GENERATED FROM PYTHON SOURCE LINES 64-69 Optimization of GP hyperparameters and conditioning to data ----------------------------------------------------------- We use the :class:`~openturns.GaussianProcessFitter` class to perform the GP fitter analysis. We first generate a design of experiments with LHS and store the input training points in `xdata` .. GENERATED FROM PYTHON SOURCE LINES 69-77 .. code-block:: Python experiment = ot.LHSExperiment( ot.JointDistribution([ot.Uniform(0.0, 1.0), ot.Uniform(0.0, 1.0)]), 28, False, True, ) xdata = experiment.generate() .. GENERATED FROM PYTHON SOURCE LINES 78-79 We also get the output training values : .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: Python ydata = bm.objectiveFunction(xdata) .. GENERATED FROM PYTHON SOURCE LINES 83-84 This use case is defined in dimension 2 and we use a constant basis for the trend estimation : .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: Python dimension = bm.dim basis = ot.ConstantBasisFactory(dimension).build() .. GENERATED FROM PYTHON SOURCE LINES 88-89 We choose a squared exponential covariance model in dimension 2 : .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python covarianceModel = ot.SquaredExponential([0.1] * dimension, [1.0]) .. GENERATED FROM PYTHON SOURCE LINES 92-93 We have all the components to build a GP fitter algorithm and run it : .. GENERATED FROM PYTHON SOURCE LINES 93-102 .. code-block:: Python fitter_algo = ot.GaussianProcessFitter(xdata, ydata, covarianceModel, basis) fitter_algo.setOptimizeParameters(True) fitter_algo.run() fitter_result = fitter_algo.getResult() gpr_algo = ot.GaussianProcessRegression(fitter_result) gpr_algo.run() gpr_result = gpr_algo.getResult() print(gpr_result) .. rst-class:: sphx-glr-script-out .. code-block:: none GaussianProcessRegressionResult(covariance models=SquaredExponential(scale=[0.277772,1.36174], amplitude=[6.30176]), covariance coefficients= 0 : [ 62.1601 ] 1 : [ 287.629 ] 2 : [ 3.3699 ] 3 : [ 31.4885 ] 4 : [ 10.8835 ] 5 : [ 431.877 ] 6 : [ -107.214 ] 7 : [ -222.362 ] 8 : [ 527.483 ] 9 : [ 455.441 ] 10 : [ -19.3105 ] 11 : [ -156.261 ] 12 : [ -454.502 ] 13 : [ -244.102 ] 14 : [ 11.9605 ] 15 : [ 50.7823 ] 16 : [ 139.399 ] 17 : [ -309.371 ] 18 : [ 161.694 ] 19 : [ -275.252 ] 20 : [ -27.8661 ] 21 : [ -21.9939 ] 22 : [ -31.0059 ] 23 : [ -322.299 ] 24 : [ 128.301 ] 25 : [ -67.2894 ] 26 : [ 170.952 ] 27 : [ -214.593 ], basis=Basis( [[x0,x1]->[1]] ), trend coefficients=[7.17468]) .. GENERATED FROM PYTHON SOURCE LINES 103-108 Metamodel visualization ----------------------- We get the metamodel (mean of the conditioned Gaussian Process) and the variance estimation. We draw the GP metamodel of the Branin function. .. GENERATED FROM PYTHON SOURCE LINES 108-127 .. code-block:: Python gprMetamodel = gpr_result.getMetaModel() gccc = ot.GaussianProcessConditionalCovariance(gpr_result) graphBasic = gprMetamodel.draw([0.0, 0.0], [1.0, 1.0], [100] * 2) # Take the first drawable as the only contour with multiple levels contours = graphBasic.getDrawable(0).getImplementation() contours.setColorBarPosition("") # Hide the color bar contours.setDrawLabels(True) # Draw the labels levels = contours.getLevels() # Build a range of colors ot.ResourceMap.SetAsUnsignedInteger("Drawable-DefaultPalettePhase", len(levels)) palette = ot.Drawable.BuildDefaultPalette(len(levels)) graphFineTune = ot.Graph("Branin metamodel (mean)", r"$x_1$", r"$x_2$") graphFineTune.setDrawables([contours]) graphFineTune.setLegendPosition("") .. GENERATED FROM PYTHON SOURCE LINES 128-129 We also represent the location of the minima of the Branin function : .. GENERATED FROM PYTHON SOURCE LINES 129-134 .. code-block:: Python sample1 = ot.Sample([bm.xexact1, bm.xexact2, bm.xexact3]) cloud1 = ot.Cloud(sample1, "orange", "diamond", "First Cloud") graphFineTune.add(cloud1) view = otv.View(graphFineTune) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_kriging_branin_function_003.svg :alt: Branin metamodel (mean) :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_kriging_branin_function_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 135-136 We evaluate the metamodel at the minima locations : .. GENERATED FROM PYTHON SOURCE LINES 136-138 .. code-block:: Python print(gprMetamodel(sample1)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 : [ -1.03163 ] 1 : [ -1.04646 ] 2 : [ -1.05144 ] .. GENERATED FROM PYTHON SOURCE LINES 139-142 Graphically, both the metamodel and the exact function look the same. The metamodel also has three basins around the minima and the value of the metamodel at each minimum location is comparable to the exact value of -0.979476. We have roughly two correct digits for each isoline. .. GENERATED FROM PYTHON SOURCE LINES 145-150 Standard deviation ------------------ We finally take a look at the standard deviation in the :math:`[0,1] \times [0,1]` domain. It may be seen as a measure of the error of the metamodel. .. GENERATED FROM PYTHON SOURCE LINES 152-153 We discretize the domain with 22 points (`N` inside points and 2 endpoints) : .. GENERATED FROM PYTHON SOURCE LINES 153-156 .. code-block:: Python N = 20 inputData = ot.Box([N, N]).generate() .. GENERATED FROM PYTHON SOURCE LINES 157-158 We compute the conditional variance of the model and take the square root to get the deviation : .. GENERATED FROM PYTHON SOURCE LINES 158-161 .. code-block:: Python condCov = gccc.getConditionalMarginalVariance(inputData, 0) condCovSd = sqrt(condCov) .. GENERATED FROM PYTHON SOURCE LINES 162-163 As we have previously done we build contours with the following levels ans labels : .. GENERATED FROM PYTHON SOURCE LINES 163-170 .. code-block:: Python levels = [0.01, 0.025, 0.050, 0.075, 0.1, 0.125, 0.150, 0.175] contour = ot.Contour(N + 2, N + 2, condCovSd) contour.setLevels(levels) graphFineTune = ot.Graph("Standard deviation", r"$x_1$", r"$x_2$") graphFineTune.setDrawables([contour]) graphFineTune.setLegendPosition("") .. GENERATED FROM PYTHON SOURCE LINES 171-172 We superimpose the training sample : .. GENERATED FROM PYTHON SOURCE LINES 172-178 .. code-block:: Python cloud = ot.Cloud(xdata) cloud.setPointStyle("fcircle") cloud.setColor("red") graphFineTune.add(cloud) view = otv.View(graphFineTune) .. image-sg:: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_kriging_branin_function_004.svg :alt: Standard deviation :srcset: /auto_surrogate_modeling/gaussian_process_regression/images/sphx_glr_plot_kriging_branin_function_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 179-182 We observe that the standard deviation is small in the center of the domain where we have enough data to learn the model. We can print the value of the variance at the first 5 training points (they all behave similarly) : .. GENERATED FROM PYTHON SOURCE LINES 182-184 .. code-block:: Python print(gccc.getConditionalMarginalVariance(xdata, 0)[0:5]) .. rst-class:: sphx-glr-script-out .. code-block:: none [ v0 ] 0 : [ 2.84217e-14 ] 1 : [ 2.13163e-14 ] 2 : [ 2.84217e-14 ] 3 : [ 2.84217e-14 ] 4 : [ 2.84217e-14 ] .. GENERATED FROM PYTHON SOURCE LINES 185-187 These values are nearly zero which is expected as the metamodel interpolates data. The value being known it is not random anymore and the variance ought to be zero. .. GENERATED FROM PYTHON SOURCE LINES 189-190 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 190-191 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_surrogate_modeling_gaussian_process_regression_plot_kriging_branin_function.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kriging_branin_function.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kriging_branin_function.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_kriging_branin_function.zip `