.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_meta_modeling/general_purpose_metamodels/plot_general_linear_model.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_general_purpose_metamodels_plot_general_linear_model.py: Create a general linear model metamodel ======================================= .. GENERATED FROM PYTHON SOURCE LINES 7-9 In this example we create a global approximation of a model response using a general linear model. We show how to use the :class:`~openturns.GeneralLinearModelAlgorithm` class, which estimates the parameters of the model. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import openturns as ot import openturns.viewer as viewer ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 17-18 We create a model and a sample from this model. .. GENERATED FROM PYTHON SOURCE LINES 20-29 .. code-block:: Python ot.RandomGenerator.SetSeed(0) dimension = 2 input_names = ["x1", "x2"] formulas = ["cos(x1 + x2)"] model = ot.SymbolicFunction(input_names, formulas) distribution = ot.Normal(dimension) x = distribution.getSample(100) y = model(x) .. GENERATED FROM PYTHON SOURCE LINES 30-32 We create a :class:`~openturns.GeneralLinearModelAlgorithm` based on a linear basis. The `run` method estimates the coefficients of the trend and the hyperparameters of the covariance model. .. GENERATED FROM PYTHON SOURCE LINES 34-40 .. code-block:: Python basis = ot.LinearBasisFactory(dimension).build() covarianceModel = ot.SquaredExponential([1] * dimension, [1.0]) algo = ot.GeneralLinearModelAlgorithm(x, y, covarianceModel, basis) algo.run() result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 41-42 We see that the trend coefficients have been estimated. .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python result.getTrendCoefficients() .. raw:: html
class=Point name=Unnamed dimension=3 values=[-0.22819,-0.0113566,-0.00958984]


.. GENERATED FROM PYTHON SOURCE LINES 47-48 The parameters of the covariance models also have been estimated. .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python result.getCovarianceModel() .. raw:: html

SquaredExponential(scale=[1,1], amplitude=[0.323718])



.. GENERATED FROM PYTHON SOURCE LINES 53-54 The `getMetaModel` method returns the metamodel where the parameters have been estimated. .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python responseSurface = result.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 59-60 Plot the output of our model depending on :math:`x_2` with :math:`x_1=0.5`. .. GENERATED FROM PYTHON SOURCE LINES 62-72 .. code-block:: Python xmin = -5.0 xmax = 5.0 x1value = 0.5 parametricModelGraph = ot.ParametricFunction(model, [0], [x1value]).draw(xmin, xmax) graphMetamodel = ot.ParametricFunction(responseSurface, [0], [x1value]).draw(xmin, xmax) parametricModelGraph.add(graphMetamodel) parametricModelGraph.setLegends(["Model", "Meta-Model"]) parametricModelGraph.setLegendPosition("upper right") view = viewer.View(parametricModelGraph) .. image-sg:: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_general_linear_model_001.png :alt: y0 as a function of x2 :srcset: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_general_linear_model_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python view.ShowAll() .. GENERATED FROM PYTHON SOURCE LINES 76-82 We see that the metamodel is equal to the trend because it takes into account the fact that the mean of the Gaussian process is zero. This :class:`~openturns.GeneralLinearModelAlgorithm` class is the main building block of the :class:`~openturns.KrigingAlgorithm`. This is why most basic use cases are based on the :class:`~openturns.KrigingAlgorithm` instead of the :class:`~openturns.GeneralLinearModelAlgorithm`, because this allows one to condition the Gaussian process. .. _sphx_glr_download_auto_meta_modeling_general_purpose_metamodels_plot_general_linear_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_general_linear_model.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_general_linear_model.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_general_linear_model.zip `