`     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
=======================================
In this example we are going to create a global approximation of a model response using a general linear model. We show how to use the `GeneralLinearModelAlgorithm` class, which estimates the parameters of the model. 
.. code-block:: default
    from __future__ import print_function
    import openturns as ot
    import openturns.viewer as viewer
    from matplotlib import pylab as plt
    ot.Log.Show(ot.Log.NONE)
We create a model and a sample from this model.
.. code-block:: default
    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)
We create a `GeneralLinearModelAlgorithm` based on a linear basis. The `run` method estimats the coefficients of the trend and the hyperparameters of the covariance model.
.. code-block:: default
    basis = ot.LinearBasisFactory(dimension).build()
    covarianceModel = ot.SquaredExponential([1]*dimension, [1.0])
    algo = ot.GeneralLinearModelAlgorithm(x, y, covarianceModel, basis)
    algo.run()
    result = algo.getResult()
We see that the trend coefficients have been estimated. 
.. code-block:: default
    result.getTrendCoefficients()
.. rst-class:: sphx-glr-script-out
 Out:
 .. code-block:: none
    [class=Point name=Unnamed dimension=3 values=[-0.22819,-0.0113566,-0.00958984]]
The parameters of the covariance models also have been estimated.
.. code-block:: default
    result.getCovarianceModel()
.. raw:: html
    SquaredExponential(scale=[1,1], amplitude=[0.323718])
    
    
The `getMetaModel` method returns the metamodel where the parameters have been estimated. 
.. code-block:: default
    responseSurface = result.getMetaModel()
Plot the output of our model depending on `x2` with `x1=0.5`.
.. code-block:: default
    xmin = -5.
    xmax = 5.
    x1value = 0.5
    parametricModelGraph = ot.ParametricFunction(model, [0], [x1value]).draw(xmin,xmax)
    parametricModelGraph.setColors(["red"])
    graphMetamodel = ot.ParametricFunction(responseSurface, [0], [x1value]).draw(xmin,xmax)
    graphMetamodel.setColors(["blue"])
    parametricModelGraph.add(graphMetamodel)
    parametricModelGraph.setLegends(["Model","Meta-Model"])
    parametricModelGraph.setLegendPosition("topright")
    view = viewer.View(parametricModelGraph)
    plt.show()
.. image:: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_general_linear_model_001.png
    :alt: y0 as a function of x2
    :class: sphx-glr-single-img
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 `GeneralLinearModelAlgorithm` class is the main building block of the `KrigingAlgorithm`. This is why most basic use cases are based on the `KrigingAlgorithm` instead of the `GeneralLinearModelAlgorithm`, because this allows to condition the gaussian process.
.. rst-class:: sphx-glr-timing
   **Total running time of the script:** ( 0 minutes  0.116 seconds)
.. _sphx_glr_download_auto_meta_modeling_general_purpose_metamodels_plot_general_linear_model.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_general_linear_model.py `
  .. container:: sphx-glr-download sphx-glr-download-jupyter
     :download:`Download Jupyter notebook: plot_general_linear_model.ipynb `
.. only:: html
 .. rst-class:: sphx-glr-signature
    `Gallery generated by Sphinx-Gallery `_