.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_meta_modeling/kriging_metamodel/plot_kriging_branin_function.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_meta_modeling_kriging_metamodel_plot_kriging_branin_function.py: Kriging: metamodel of the Branin-Hoo function ============================================== .. GENERATED FROM PYTHON SOURCE LINES 6-9 As a popular use case in optimization we briefly present the construction of a metamodel of the Branin (also refered to as Branin-Hoo) function. .. GENERATED FROM PYTHON SOURCE LINES 11-19 .. code-block:: default from numpy import sqrt import openturns as ot import openturns.viewer as viewer import openturns.viewer as otv from openturns.usecases import branin_function as branin_function from matplotlib import pylab as plt .. GENERATED FROM PYTHON SOURCE LINES 20-21 We load the Branin-Hoo model from the usecases module and use the model (stored in `objectiveFunction`) .. GENERATED FROM PYTHON SOURCE LINES 21-24 .. code-block:: default bm = branin_function.BraninModel() model = bm.objectiveFunction .. GENERATED FROM PYTHON SOURCE LINES 25-27 We shall represent this 2D 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 27-31 .. code-block:: default ot.ResourceMap.SetAsUnsignedInteger("Contour-DefaultLevelsNumber", 10) graphBasic = model.draw([0.0, 0.0], [1.0, 1.0], [100]*2) .. GENERATED FROM PYTHON SOURCE LINES 32-33 We get the values of all isolines : .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: default drawables = graphBasic.getDrawables() levels = [] for contours in drawables: levels.append(contours.getLevels()[0]) .. GENERATED FROM PYTHON SOURCE LINES 39-40 We now build fancy isolines : .. GENERATED FROM PYTHON SOURCE LINES 40-63 .. code-block:: default # Take the first contour as the only one with multiple levels contour = graphBasic.getDrawable(0) # Build a range of colors ot.ResourceMap.SetAsUnsignedInteger( 'Drawable-DefaultPalettePhase', len(levels)) palette = ot.Drawable.BuildDefaultPalette(len(levels)) # Create the drawables list, appending each contour with its own color drawables = list() for i in range(len(levels)): contour.setLevels([levels[i]]) # Inline the level values contour.setDrawLabels(True) # We have to copy the drawable because a Python list stores only pointers drawables.append(ot.Drawable(contour)) graphFineTune = ot.Graph("The exact Branin model", r"$x_1$", r"$x_2$", True, '') graphFineTune.setDrawables(drawables) # Replace the drawables graphFineTune.setLegendPosition("") # Remove the legend graphFineTune.setColors(palette) # Add colors .. GENERATED FROM PYTHON SOURCE LINES 64-65 We also represent the three minima of the Branin function with orange diamonds : .. GENERATED FROM PYTHON SOURCE LINES 65-74 .. code-block:: default sample1 = ot.Sample([bm.xexact1, bm.xexact2, bm.xexact3]) cloud1 = ot.Cloud(sample1, 'orange', 'diamond', 'First Cloud') graphFineTune.add(cloud1) view = otv.View(graphFineTune) # # The values of the exact model at these points are : print(bm.objectiveFunction(sample1)) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_branin_function_001.png :alt: The exact Branin model :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_branin_function_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [ y0 ] 0 : [ -1.04741 ] 1 : [ -1.04741 ] 2 : [ -1.04741 ] .. GENERATED FROM PYTHON SOURCE LINES 75-77 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 80-85 Definition of the Kriging metamodel ----------------------------------- We use the :class:`~openturns.KrigingAlgorithm` class to perform the kriging analysis. We first generate a design of experiments with LHS and store the input trainig points in `xdata` .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. code-block:: default experiment = ot.LHSExperiment(ot.ComposedDistribution( [ot.Uniform(0.0, 1.0), ot.Uniform(0.0, 1.0)]), 28, False, True) xdata = experiment.generate() .. GENERATED FROM PYTHON SOURCE LINES 90-91 We also get the output training values : .. GENERATED FROM PYTHON SOURCE LINES 91-94 .. code-block:: default ydata = bm.objectiveFunction(xdata) .. GENERATED FROM PYTHON SOURCE LINES 95-96 This use case is defined in dimension 2 and we use a constant basis for the trend estimation : .. GENERATED FROM PYTHON SOURCE LINES 96-99 .. code-block:: default dimension = bm.dim basis = ot.ConstantBasisFactory(dimension).build() .. GENERATED FROM PYTHON SOURCE LINES 100-101 We choose a squared exponential covariance model in dimension 2 : .. GENERATED FROM PYTHON SOURCE LINES 101-103 .. code-block:: default covarianceModel = ot.SquaredExponential([0.1]*dimension, [1.0]) .. GENERATED FROM PYTHON SOURCE LINES 104-105 We have all the components to build a kriging algorithm and run it : .. GENERATED FROM PYTHON SOURCE LINES 105-108 .. code-block:: default algo = ot.KrigingAlgorithm(xdata, ydata, covarianceModel, basis) algo.run() .. GENERATED FROM PYTHON SOURCE LINES 109-110 We get the result of the kriging analysis with : .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: default result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 113-117 Metamodel visualization ----------------------- We draw the kriging metamodel of the Branin function. It is the mean of the random process. .. GENERATED FROM PYTHON SOURCE LINES 117-148 .. code-block:: default metamodel = result.getMetaModel() graphBasic = metamodel.draw([0.0, 0.0], [1.0, 1.0], [100]*2) drawables = graphBasic.getDrawables() levels = [] for i in range(len(drawables)): contours = drawables[i] levels.append(contours.getLevels()[0]) # Take the first contour as the only one with multiple levels contour = graphBasic.getDrawable(0) # Build a range of colors ot.ResourceMap.SetAsUnsignedInteger( 'Drawable-DefaultPalettePhase', len(levels)) palette = ot.Drawable.BuildDefaultPalette(len(levels)) # Create the drawables list, appending each contour with its own color drawables = list() for i in range(len(levels)): contour.setLevels([levels[i]]) # Inline the level values contour.setDrawLabels(True) # We have to copy the drawable because a Python list stores only pointers drawables.append(ot.Drawable(contour)) graphFineTune = ot.Graph("Branin metamodel (mean)", r"$x_1$", r"$x_2$", True, '') graphFineTune.setDrawables(drawables) graphFineTune.setLegendPosition("") graphFineTune.setColors(palette) .. GENERATED FROM PYTHON SOURCE LINES 149-150 We also represent the location of the minima of the Branin function : .. GENERATED FROM PYTHON SOURCE LINES 150-155 .. code-block:: default 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_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_branin_function_002.png :alt: Branin metamodel (mean) :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_branin_function_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 156-157 We evaluate the metamodel at the minima locations : .. GENERATED FROM PYTHON SOURCE LINES 157-159 .. code-block:: default print(metamodel(sample1)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 0 : [ -1.05296 ] 1 : [ -1.04014 ] 2 : [ -1.1114 ] .. GENERATED FROM PYTHON SOURCE LINES 160-163 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 166-171 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 173-174 We discretize the domain with 22 points (N inside points and 2 endpoints) : .. GENERATED FROM PYTHON SOURCE LINES 174-177 .. code-block:: default N = 20 inputData = ot.Box([N, N]).generate() .. GENERATED FROM PYTHON SOURCE LINES 178-179 We compute the conditional variance of the model and take the square root to get the deviation : .. GENERATED FROM PYTHON SOURCE LINES 179-182 .. code-block:: default condCov = result.getConditionalMarginalVariance(inputData, 0) condCovSd = sqrt(condCov) .. GENERATED FROM PYTHON SOURCE LINES 183-184 As we have previously done we build contours with the following levels ans labels : .. GENERATED FROM PYTHON SOURCE LINES 184-191 .. code-block:: default levels = [0.01, 0.025, 0.050, 0.075, 0.1, 0.125, 0.150, 0.175] labels = ['0.01', '0.025', '0.050', '0.075', '0.1', '0.125', '0.150', '0.175'] contour = ot.Contour(N+2, N+2, condCovSd) graph = ot.Graph('', 'x', 'y', True, '') graph.add(contour) .. GENERATED FROM PYTHON SOURCE LINES 192-193 We use fancy colored isolines for the contour plot : .. GENERATED FROM PYTHON SOURCE LINES 193-208 .. code-block:: default contour = graph.getDrawable(0) ot.ResourceMap.SetAsUnsignedInteger( 'Drawable-DefaultPalettePhase', len(levels)) palette = ot.Drawable.BuildDefaultPalette(len(levels)) drawables = list() for i in range(len(levels)): contour.setLevels([levels[i]]) contour.setDrawLabels(True) drawables.append(ot.Drawable(contour)) graphFineTune = ot.Graph("Standard deviation", r"$x_1$", r"$x_2$", True, '') graphFineTune.setDrawables(drawables) graphFineTune.setLegendPosition("") graphFineTune.setColors(palette) .. GENERATED FROM PYTHON SOURCE LINES 209-210 We superimpose the training sample : .. GENERATED FROM PYTHON SOURCE LINES 210-216 .. code-block:: default cloud = ot.Cloud(xdata) cloud.setPointStyle("fcircle") cloud.setColor("red") graphFineTune.add(cloud) view = otv.View(graphFineTune) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_branin_function_003.png :alt: Standard deviation :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_branin_function_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 217-220 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 220-222 .. code-block:: default print(result.getConditionalMarginalVariance(xdata, 0)[0:5]) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [ v0 ] 0 : [ 0.00106993 ] 1 : [ 0.000674665 ] 2 : [ 0.000840553 ] 3 : [ 0.000694642 ] 4 : [ 0.00108795 ] .. GENERATED FROM PYTHON SOURCE LINES 223-225 These values are nearly zero which is expected as the kriging interpolates data. The value being known it is not random anymore and the variance ought to be zero. .. GENERATED FROM PYTHON SOURCE LINES 227-228 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 228-229 .. code-block:: default plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.663 seconds) .. _sphx_glr_download_auto_meta_modeling_kriging_metamodel_plot_kriging_branin_function.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. 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-jupyter :download:`Download Jupyter notebook: plot_kriging_branin_function.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_