.. 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_1d.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_1d.py: Kriging : quick-start ===================== .. GENERATED FROM PYTHON SOURCE LINES 6-10 Abstract -------- In this example, we create a kriging metamodel for a function which has scalar real inputs and outputs. We show how to create the learning and the validation samples. We show how to create the kriging metamodel by chosing a trend and a covariance model. Finally, we compute the predicted kriging confidence interval using the conditional variance. .. GENERATED FROM PYTHON SOURCE LINES 12-43 Introduction ------------ We consider the sine function: .. math:: y = \sin(x) for any :math:`x\in[0,12]`. We want to create a metamodel of this function. This is why we create a sample of :math:`n` observations of the function: .. math:: y_i=\sin(x_i) for :math:`i=1,...,7`, where :math:`x_i` is the i-th input and :math:`y_i` is the corresponding output. We consider the seven following inputs : ============ === === === === ===== ==== ====== :math:`i` 1 2 3 4 5 6 7 ============ === === === === ===== ==== ====== :math:`x_i` 1 3 4 6 7.9 11 11.5 ============ === === === === ===== ==== ====== We are going to consider a kriging metamodel with: * a constant trend, * a Matern covariance model. .. GENERATED FROM PYTHON SOURCE LINES 45-49 Creation of the metamodel ------------------------- We begin by defining the function `g` as a symbolic function. Then we define the `x_train` variable which contains the inputs of the design of experiments of the training step. Then we compute the `y_train` corresponding outputs. The variable `n_train` is the size of the training design of experiments. .. GENERATED FROM PYTHON SOURCE LINES 51-57 .. code-block:: default import numpy as np import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: default g = ot.SymbolicFunction(['x'], ['sin(x)']) .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: default x_train = ot.Sample([[x] for x in [1., 3., 4., 6., 7.9, 11., 11.5]]) y_train = g(x_train) n_train = x_train.getSize() n_train .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 7 .. GENERATED FROM PYTHON SOURCE LINES 67-68 In order to compare the function and its metamodel, we use a test (i.e. validation) design of experiments made of a regular grid of 100 points from 0 to 12. Then we convert this grid into a `Sample` and we compute the outputs of the function on this sample. .. GENERATED FROM PYTHON SOURCE LINES 70-79 .. code-block:: default xmin = 0. xmax = 12. n_test = 100 step = (xmax-xmin)/(n_test-1) myRegularGrid = ot.RegularGrid(xmin, step, n_test) x_test = myRegularGrid.getVertices() y_test = g(x_test) .. GENERATED FROM PYTHON SOURCE LINES 80-81 In order to observe the function and the location of the points in the input design of experiments, we define the following functions which plots the data. .. GENERATED FROM PYTHON SOURCE LINES 83-91 .. code-block:: default def plot_data_train(x_train, y_train): """Plot the data (x_train,y_train) as a Cloud, in red""" graph_train = ot.Cloud(x_train, y_train) graph_train.setColor("red") graph_train.setLegend("Data") return graph_train .. GENERATED FROM PYTHON SOURCE LINES 92-101 .. code-block:: default def plot_data_test(x_test, y_test): """Plot the data (x_test,y_test) as a Curve, in dashed black""" graphF = ot.Curve(x_test, y_test) graphF.setLegend("Exact") graphF.setColor("black") graphF.setLineStyle("dashed") return graphF .. GENERATED FROM PYTHON SOURCE LINES 102-111 .. code-block:: default graph = ot.Graph('test and train', '', '', True, '') graph.add(plot_data_test(x_test, y_test)) graph.add(plot_data_train(x_train, y_train)) graph.setAxes(True) graph.setXTitle("X") graph.setYTitle("Y") graph.setLegendPosition("topright") view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_1d_001.png :alt: test and train :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_1d_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-113 We use the `ConstantBasisFactory` class to define the trend and the `MaternModel` class to define the covariance model. This Matérn model is based on the regularity parameter :math:`\nu=3/2`. .. GENERATED FROM PYTHON SOURCE LINES 115-125 .. code-block:: default dimension = 1 basis = ot.ConstantBasisFactory(dimension).build() #basis = ot.LinearBasisFactory(dimension).build() basis = ot.QuadraticBasisFactory(dimension).build() covarianceModel = ot.MaternModel([1.]*dimension, 1.5) algo = ot.KrigingAlgorithm(x_train, y_train, covarianceModel, basis) algo.run() result = algo.getResult() print(result) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none KrigingResult(covariance models=MaternModel(scale=[1.05676], amplitude=[0.872729], nu=1.5), covariance coefficients=0 : [ 0.369752 ] 1 : [ 0.473044 ] 2 : [ -1.4787 ] 3 : [ -0.453386 ] 4 : [ 1.71229 ] 5 : [ -0.967219 ] 6 : [ 0.344222 ], basis=[Basis( [class=LinearEvaluation name=Unnamed center=[0] constant=[1] linear=[[ 0 ]],class=LinearEvaluation name=Unnamed center=[0] constant=[0] linear=[[ 1 ]],QuadraticEvaluation center : [0] constant : [0] linear : [[ 0 ]] quadratic : sheet #0 [[ 2 ]] ] )], trend coefficients=[[0.667389,-0.117007,0.000809415]]) .. GENERATED FROM PYTHON SOURCE LINES 126-127 We observe that the `scale` and `amplitude` hyper-parameters have been optimized by the `run` method. Then we get the metamodel with `getMetaModel` and evaluate the outputs of the metamodel on the test design of experiments. .. GENERATED FROM PYTHON SOURCE LINES 129-133 .. code-block:: default krigeageMM = result.getMetaModel() y_test_MM = krigeageMM(x_test) .. GENERATED FROM PYTHON SOURCE LINES 134-135 The following function plots the kriging data. .. GENERATED FROM PYTHON SOURCE LINES 137-145 .. code-block:: default def plot_data_kriging(x_test, y_test_MM): """Plots (x_test,y_test_MM) from the metamodel as a Curve, in blue""" graphK = ot.Curve(x_test, y_test_MM) graphK.setColor("blue") graphK.setLegend("Kriging") return graphK .. GENERATED FROM PYTHON SOURCE LINES 146-156 .. code-block:: default graph = ot.Graph('', '', '', True, '') graph.add(plot_data_test(x_test, y_test)) graph.add(plot_data_train(x_train, y_train)) graph.add(plot_data_kriging(x_test, y_test_MM)) graph.setAxes(True) graph.setXTitle("X") graph.setYTitle("Y") graph.setLegendPosition("topright") view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_1d_002.png :alt: plot kriging 1d :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_1d_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 157-160 We see that the kriging metamodel is interpolating. This is what is meant by *conditioning* a gaussian process. We see that, when the sine function has a strong curvature between two points which are separated by a large distance (e.g. between :math:`x=4` and :math:`x=6`), then the kriging metamodel is not close to the function :math:`g`. However, when the training points are close (e.g. between :math:`x=11` and :math:`x=11.5`) or when the function is nearly linear (e.g. between :math:`x=8` and :math:`x=11`), then the kriging metamodel is quite accurate. .. GENERATED FROM PYTHON SOURCE LINES 162-164 Compute confidence bounds ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 166-174 In order to assess the quality of the metamodel, we can estimate the kriging variance and compute a 95% confidence interval associated with the conditioned gaussian process. We begin by defining the `alpha` variable containing the complementary of the confidence level than we want to compute. Then we compute the quantile of the gaussian distribution corresponding to `1-alpha/2`. Therefore, the confidence interval is : .. math:: P\in\left(X\in\left[q_{\alpha/2},q_{1-\alpha/2}\right]\right)=1-\alpha. .. GENERATED FROM PYTHON SOURCE LINES 176-188 .. code-block:: default alpha = 0.05 def computeQuantileAlpha(alpha): bilateralCI = ot.Normal().computeBilateralConfidenceInterval(1-alpha) return bilateralCI.getUpperBound()[0] quantileAlpha = computeQuantileAlpha(alpha) print("alpha=%f" % (alpha)) print("Quantile alpha=%f" % (quantileAlpha)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none alpha=0.050000 Quantile alpha=1.959964 .. GENERATED FROM PYTHON SOURCE LINES 189-190 In order to compute the kriging error, we can consider the conditional variance. The `getConditionalCovariance` method returns the covariance matrix `covGrid` evaluated at each points in the given sample. Then we can use the diagonal coefficients in order to get the marginal conditional kriging variance. Since this is a variance, we use the square root in order to compute the standard deviation. However, some coefficients in the diagonal are very close to zero and nonpositive, which leads to an exception of the sqrt function. This is why we add an epsilon on the diagonal (nugget factor), which prevents this issue. .. GENERATED FROM PYTHON SOURCE LINES 192-197 .. code-block:: default sqrt = ot.SymbolicFunction(["x"], ["sqrt(x)"]) epsilon = ot.Sample(n_test, [1.e-8]) conditionalVariance = result.getConditionalMarginalVariance(x_test)+epsilon conditionalSigma = sqrt(conditionalVariance) .. GENERATED FROM PYTHON SOURCE LINES 198-199 The following figure presents the conditional standard deviation depending on :math:`x`. .. GENERATED FROM PYTHON SOURCE LINES 201-208 .. code-block:: default graph = ot.Graph('Conditional standard deviation', 'x', 'Conditional standard deviation', True, '') curve = ot.Curve(x_test, conditionalSigma) graph.add(curve) view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_1d_003.png :alt: Conditional standard deviation :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_1d_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 209-211 We now compute the bounds of the confidence interval. For this purpose we define a small function `computeBoundsConfidenceInterval` : .. GENERATED FROM PYTHON SOURCE LINES 213-222 .. code-block:: default def computeBoundsConfidenceInterval(quantileAlpha): dataLower = [[y_test_MM[i, 0] - quantileAlpha * conditionalSigma[i, 0]] for i in range(n_test)] dataUpper = [[y_test_MM[i, 0] + quantileAlpha * conditionalSigma[i, 0]] for i in range(n_test)] dataLower = ot.Sample(dataLower) dataUpper = ot.Sample(dataUpper) return dataLower, dataUpper .. GENERATED FROM PYTHON SOURCE LINES 223-224 In order to create the graphics containing the bounds of the confidence interval, we use the `Polygon`. This will create a colored surface associated to the confidence interval. In order to do this, we create the nodes of the polygons at the lower level `vLow` and at the upper level `vUp`. Then we assemble these nodes to create the polygons. That is what we do inside the `plot_kriging_bounds` function. .. GENERATED FROM PYTHON SOURCE LINES 226-245 .. code-block:: default def plot_kriging_bounds(dataLower, dataUpper, n_test, color=[120, 1.0, 1.0]): """ From two lists containing the lower and upper bounds of the region, create a PolygonArray. Default color is green given by HSV values in color list. """ vLow = [[x_test[i, 0], dataLower[i, 0]] for i in range(n_test)] vUp = [[x_test[i, 0], dataUpper[i, 0]] for i in range(n_test)] myHSVColor = ot.Polygon.ConvertFromHSV(color[0], color[1], color[2]) polyData = [[vLow[i], vLow[i+1], vUp[i+1], vUp[i]] for i in range(n_test-1)] polygonList = [ot.Polygon(polyData[i], myHSVColor, myHSVColor) for i in range(n_test-1)] boundsPoly = ot.PolygonArray(polygonList) return boundsPoly .. GENERATED FROM PYTHON SOURCE LINES 246-247 We define two small lists to draw three different confidence intervals (defined by the alpha value) : .. GENERATED FROM PYTHON SOURCE LINES 247-251 .. code-block:: default alphas = [0.05, 0.1, 0.2] # three different green colors defined by HSV values mycolors = [[120, 1.0, 1.0], [120, 1.0, 0.75], [120, 1.0, 0.5]] .. GENERATED FROM PYTHON SOURCE LINES 252-253 We are ready to display all the previous information and the three confidence intervals we want. .. GENERATED FROM PYTHON SOURCE LINES 255-256 sphinx_gallery_thumbnail_number = 4 .. GENERATED FROM PYTHON SOURCE LINES 256-275 .. code-block:: default graph = ot.Graph('', '', '', True, '') graph.add(plot_data_test(x_test, y_test)) graph.add(plot_data_train(x_train, y_train)) graph.add(plot_data_kriging(x_test, y_test_MM)) # Now we loop over the different values : for idx, v in enumerate(alphas): quantileAlpha = computeQuantileAlpha(v) vLow, vUp = computeBoundsConfidenceInterval(quantileAlpha) boundsPoly = plot_kriging_bounds(vLow, vUp, n_test, mycolors[idx]) boundsPoly.setLegend(" %d%% bounds" % ((1.0-v)*100)) graph.add(boundsPoly) graph.setAxes(True) graph.setXTitle("X") graph.setYTitle("Y") graph.setLegendPosition("topright") view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_1d_004.png :alt: plot kriging 1d :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_1d_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 276-277 We see that the confidence intervals are small in the regions where two consecutive training points are close to each other (e.g. between :math:`x=11` and :math:`x=11.5`) and large when the two points are not (e.g. between :math:`x=8.` and :math:`x=11`) or when the curvature of the function is large (between :math:`x=4` and :math:`x=6`). .. GENERATED FROM PYTHON SOURCE LINES 277-280 .. code-block:: default plt.show() .. GENERATED FROM PYTHON SOURCE LINES 281-285 References ---------- * Metamodeling with Gaussian processes, Bertrand Iooss, EDF R&D, 2014, www.gdr-mascotnum.fr/media/sssamo14_iooss.pdf .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.557 seconds) .. _sphx_glr_download_auto_meta_modeling_kriging_metamodel_plot_kriging_1d.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_1d.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kriging_1d.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_