.. 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_gpr_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_gpr_simulate.py: Gaussian Process Regression : generate trajectories from the metamodel ====================================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 The main goal of this example is to show how to simulate new trajectories from a Gaussian Process Regression metamodel. .. GENERATED FROM PYTHON SOURCE LINES 10-45 Introduction ------------ We consider the sine function: .. math:: \model(x) = \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 = \model(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 Gaussian Process Regression metamodel with: * a constant trend, * a Matern covariance model. In the :doc:`/auto_meta_modeling/kriging_metamodel/plot_gpr_1d` example, we detail the estimation of this metamodel. Refer to it for further details: we only focus here on the simulation of new trajectories. .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: Python import openturns as ot import openturns.viewer as otv import openturns.experimental as otexp .. GENERATED FROM PYTHON SOURCE LINES 51-55 Creation of the metamodel ------------------------- We define the function `g`, the training sample `(x_train, y_train)` and the test sample `(x_test, y_tst)`. .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: Python g = ot.SymbolicFunction(["x"], ["sin(x)"]) .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: Python x_train = ot.Sample([[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 = x_train.getSize() n_train .. rst-class:: sphx-glr-script-out .. code-block:: none 7 .. GENERATED FROM PYTHON SOURCE LINES 66-75 .. 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 76-77 In order to observe the function and the location of the points in the input design of experiments, we define the following function which plots the data. .. GENERATED FROM PYTHON SOURCE LINES 80-97 .. code-block:: Python def plot_1d_data(x_data, y_data, type="Curve", legend=None, color=None, linestyle=None): """Plot the data (x_data,y_data) as a Cloud/Curve""" if type == "Curve": graphF = ot.Curve(x_data, y_data) else: graphF = ot.Cloud(x_data, y_data) if legend is not None: graphF.setLegend(legend) if color is not None: graphF.setColor(color) if linestyle is not None: graphF.setLineStyle(linestyle) return graphF .. GENERATED FROM PYTHON SOURCE LINES 98-99 Here, we draw the model and the train sample. .. GENERATED FROM PYTHON SOURCE LINES 99-111 .. code-block:: Python graph = ot.Graph("Model and Train sample", "X", "Y", True, "") graph.add( plot_1d_data( x_test, y_test, type="Curve", legend="model", color="black", linestyle="dashed" ) ) graph.add( plot_1d_data(x_train, y_train, type="Cloud", legend="train sample", color="red") ) graph.setLegendPosition("upper right") view = otv.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_gpr_simulate_001.svg :alt: Model and Train sample :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_gpr_simulate_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-114 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 116-120 .. code-block:: Python dimension = 1 basis = ot.ConstantBasisFactory(dimension).build() covarianceModel = ot.MaternModel([1.0] * dimension, 1.5) .. GENERATED FROM PYTHON SOURCE LINES 121-122 We estimate the Gaussian process :math:`Y` with the class :class:`~openturns.experimental.GaussianProcessFitter`. .. GENERATED FROM PYTHON SOURCE LINES 122-127 .. code-block:: Python fitter_algo = otexp.GaussianProcessFitter(x_train, y_train, covarianceModel, basis) fitter_algo.run() fitter_result = fitter_algo.getResult() print(fitter_result) .. rst-class:: sphx-glr-script-out .. code-block:: none GaussianProcessFitterResult(covariance model=MaternModel(scale=[1.27453], amplitude=[0.822263], nu=1.5), basis=Basis( [[x0]->[1]] ), trend coefficients=[0.00736753]) .. GENERATED FROM PYTHON SOURCE LINES 128-134 We observe that the `scale` and `amplitude` hyper-parameters have been optimized by the :meth:`~openturns.experimental.GaussianProcessFitter.run` method, while the :math:`\nu` parameter has remained unchanged, as expected. Then, we condition the gaussian process to make it interpolate the data set using the class :class:`~openturns.experimental.GaussianProcessRegression`. .. GENERATED FROM PYTHON SOURCE LINES 134-139 .. code-block:: Python gpr_algo = otexp.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=MaternModel(scale=[1.27453], amplitude=[0.822263], nu=1.5), covariance coefficients=0 : [ 1.13904 ] 1 : [ 1.01762 ] 2 : [ -1.76279 ] 3 : [ -0.559148 ] 4 : [ 1.78757 ] 5 : [ -1.61946 ] 6 : [ -0.00283147 ], basis=Basis( [[x0]->[1]] ), trend coefficients=[0.00736753]) .. GENERATED FROM PYTHON SOURCE LINES 140-141 We get the metamodel and the predictions on the test sample. .. GENERATED FROM PYTHON SOURCE LINES 141-144 .. code-block:: Python gpr_metamodel = gpr_result.getMetaModel() y_test_MM = gpr_metamodel(x_test) .. GENERATED FROM PYTHON SOURCE LINES 145-146 The following function plots the Gaussian Process Regression predictions on the test sample. .. GENERATED FROM PYTHON SOURCE LINES 146-163 .. code-block:: Python graph = ot.Graph("Gaussian process regression metamodel", "X", "Y", True, "") graph.add( plot_1d_data( x_test, y_test, type="Curve", legend="model", color="black", linestyle="dashed" ) ) graph.add( plot_1d_data(x_train, y_train, type="Cloud", legend="train sample", color="red") ) graph.add( plot_1d_data( x_test, y_test_MM, type="Curve", legend="GPR", color="blue", linestyle="solid" ) ) graph.setLegendPosition("upper right") view = otv.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_gpr_simulate_002.svg :alt: Gaussian process regression metamodel :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_gpr_simulate_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 164-170 Simulate new trajectories ------------------------- In order to generate new trajectories of the conditioned Gaussian process, we use the class :class:`~openturns.experimental.ConditionedGaussianProcess`, which provides a :class:`~openturns.Process`. It is created from the result of the Gaussian Process Regression algorithm. .. GENERATED FROM PYTHON SOURCE LINES 170-172 .. code-block:: Python process = otexp.ConditionedGaussianProcess(gpr_result, myRegularGrid) .. GENERATED FROM PYTHON SOURCE LINES 173-174 The method :meth:`~openturns.Process.getSample` method returns a :class:`~openturns.ProcessSample`. .. GENERATED FROM PYTHON SOURCE LINES 176-177 sphinx_gallery_thumbnail_number = 3 .. GENERATED FROM PYTHON SOURCE LINES 177-199 .. code-block:: Python trajectories = process.getSample(10) type(trajectories) graph = trajectories.drawMarginal() graph.add( plot_1d_data( x_test, y_test, type="Curve", legend="model", color="black", linestyle="dashed", ) ) graph.add( plot_1d_data(x_train, y_train, type="Cloud", legend="train sample", color="red") ) graph.setXTitle("X") graph.setYTitle("Y") graph.setLegendPosition("upper right") graph.setTitle("10 simulated trajectories") view = otv.View(graph) .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_gpr_simulate_003.svg :alt: 10 simulated trajectories :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_gpr_simulate_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 200-201 Display all figures. .. GENERATED FROM PYTHON SOURCE LINES 201-202 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_meta_modeling_kriging_metamodel_plot_gpr_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_gpr_simulate.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gpr_simulate.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gpr_simulate.zip `