.. 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_simulate.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_meta_modeling_kriging_metamodel_plot_kriging_simulate.py: Kriging : generate trajectories from a metamodel ================================================ .. GENERATED FROM PYTHON SOURCE LINES 7-8 The main goal of this example is to show how to simulate new trajectories from a Kriging metamodel. .. GENERATED FROM PYTHON SOURCE LINES 10-40 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 42-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:: Python 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:: Python g = ot.SymbolicFunction(["x"], ["sin(x)"]) .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: Python x_train = [[x] for x in [1.0, 3.0, 4.0, 6.0, 7.9, 11.0, 11.5]] y_train = g(x_train) n_train = len(x_train) n_train .. rst-class:: sphx-glr-script-out .. code-block:: none 7 .. GENERATED FROM PYTHON SOURCE LINES 67-70 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 :class:`~openturns.Sample` and we compute the outputs of the function on this sample. .. GENERATED FROM PYTHON SOURCE LINES 72-81 .. code-block:: Python xmin = 0.0 xmax = 12.0 n_test = 101 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 82-83 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 86-94 .. code-block:: Python 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 95-104 .. code-block:: Python 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 105-114 .. code-block:: Python graph = ot.Graph() 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("upper right") view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_simulate_001.png :alt: plot kriging simulate :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_simulate_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 115-117 We use the :class:`~openturns.ConstantBasisFactory` class to define the trend and the :class:`~openturns.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 119-127 .. code-block:: Python dimension = 1 basis = ot.ConstantBasisFactory(dimension).build() covarianceModel = ot.MaternModel([1.0] * dimension, 1.5) algo = ot.KrigingAlgorithm(x_train, y_train, covarianceModel, basis) algo.run() krigingResult = algo.getResult() krigingResult .. raw:: html
class=KrigingResult, basis=class=Basis coll=[class=Function name=Unnamed implementation=class=FunctionImplementation name=Unnamed description=[x0,y0] evaluationImplementation=class=LinearEvaluation name=Unnamed center=class=Point name=Unnamed dimension=1 values=[0] constant=class=Point name=Unnamed dimension=1 values=[1] linear=class=Matrix implementation=class=MatrixImplementation name=Unnamed rows=1 columns=1 values=[0] gradientImplementation=class=ConstantGradient name=Unnamed constant=class=Matrix implementation=class=MatrixImplementation name=Unnamed rows=1 columns=1 values=[0] hessianImplementation=class=NullHessian name=Unnamed inputDimension=1 outputDimension=1], trend coefficients=class=Point name=Unnamed dimension=1 values=[0.00736751], covariance models=class=MaternModel scale=class=Point name=Unnamed dimension=1 values=[1.27453] amplitude=class=Point name=Unnamed dimension=1 values=[0.822263] nu=1.5, covariance coefficients=class=Sample name=Unnamed implementation=class=SampleImplementation name=Unnamed size=7 dimension=1 data=[[1.13904],[1.01762],[-1.76279],[-0.559148],[1.78757],[-1.61946],[-0.00283259]]


.. GENERATED FROM PYTHON SOURCE LINES 128-130 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 132-136 .. code-block:: Python krigeageMM = krigingResult.getMetaModel() y_test_MM = krigeageMM(x_test) .. GENERATED FROM PYTHON SOURCE LINES 137-138 The following function plots the Kriging data. .. GENERATED FROM PYTHON SOURCE LINES 141-149 .. code-block:: Python 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 150-160 .. code-block:: Python graph = ot.Graph() 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("upper right") view = viewer.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_simulate_002.png :alt: plot kriging simulate :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_simulate_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 161-171 Simulate new trajectories ------------------------- In order to generate new trajectories of the conditioned Gaussian process, we could technically use the :class:`~openturns.KrigingRandomVector` class, because it provides the `getSample` method that we need. However, the :class:`~openturns.KrigingRandomVector` class was more specifically designed to create a :class:`~openturns.RandomVector` so that it can feed, for example, a function which has a field as input argument. This is why we use the :class:`~openturns.ConditionedGaussianProcess`, which provides a :class:`~openturns.Process`. .. GENERATED FROM PYTHON SOURCE LINES 173-175 .. code-block:: Python process = ot.ConditionedGaussianProcess(krigingResult, myRegularGrid) .. GENERATED FROM PYTHON SOURCE LINES 176-179 .. code-block:: Python trajectories = process.getSample(10) type(trajectories) .. GENERATED FROM PYTHON SOURCE LINES 180-182 The `getSample` method returns a :class:`~openturns.ProcessSample`. By comparison, the `getSample` method of a :class:`~openturns.KrigingRandomVector` would return a :class:`~openturns.Sample`. .. GENERATED FROM PYTHON SOURCE LINES 184-196 .. code-block:: Python graph = trajectories.drawMarginal() 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("upper right") graph.setTitle("10 simulated trajectories") view = viewer.View(graph) plt.show() # sphinx_gallery_thumbnail_number = 3 .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_simulate_003.png :alt: 10 simulated trajectories :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_simulate_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 197-202 References ---------- * Metamodeling with Gaussian processes, Bertrand Iooss, EDF R&D, 2014, www.gdr-mascotnum.fr/media/sssamo14_iooss.pdf .. _sphx_glr_download_auto_meta_modeling_kriging_metamodel_plot_kriging_simulate.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_simulate.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_kriging_simulate.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_kriging_simulate.zip `