.. 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_create_linear_least_squares_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_create_linear_least_squares_model.py: Create a linear least squares model =================================== .. GENERATED FROM PYTHON SOURCE LINES 6-51 In this example we are going to create a global approximation of a model response based on a linear function using the :class:`~openturns.LinearLeastSquares` class. We consider the function :math:`h : \Rset^2 \rightarrow \Rset^2` is defined by: .. math:: h(x_1, x_2) = \left(\cos(x_1 + x_2), (x_2 + 1) e^{x_1 - 2 x_2} \right) for any :math:`\vect{x} \in \Rset^2`. Since the output is a dimension 2 vector, the model has vector coefficients. We use the linear model: .. math:: \vect{y} \, \approx \, \widehat{\vect{h}}(\vect{x}) \, = \, \sum_{k=0}^2 \; \vect{a}_k \; \psi_k(\vect{x}) for any :math:`\vect{x} \in \Rset^2` where :math:`\left\{\vect{a}_k \in \Rset^2\right\}_{k = 0,..., 2}` are the vector coefficients and :math:`\left\{\psi_k : \Rset^2 \rightarrow \Rset\right\}_{k = 0, ..., 2}` are the basis functions. This implies that each marginal output :math:`\widehat{h}_i(\vect{x})` is approximated by the linear model: .. math:: \widehat{h}_i(\vect{x}) \, = \, \sum_{k=0}^2 \; a_{ki} \; \psi_k(\vect{x}) for any :math:`\vect{x} \in \Rset^2` and any :math:`i = 1, 2` where :math:`a_{ki}` is the :math:`k`-th coefficient of the :math:`i`-th output marginal: .. math:: \vect{a}_k = \begin{pmatrix}a_{k1} \\ a_{k2} \end{pmatrix} for :math:`k = 0, 1, 2`. We consider the basis functions: .. math:: \psi_0(\vect{x}) & = 1 \\ \psi_1(\vect{x}) & = x_1 \\ \psi_2(\vect{x}) & = x_2 \\ for any :math:`\vect{x} \in \Rset^2`. .. GENERATED FROM PYTHON SOURCE LINES 54-60 .. 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 61-63 Define the model ~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 65-67 Prepare an input sample. Each point is a pair of coordinates :math:`(x_1, x_2)`. .. GENERATED FROM PYTHON SOURCE LINES 69-75 .. code-block:: Python inputTrain = [[0.5, 0.5], [-0.5, -0.5], [-0.5, 0.5], [0.5, -0.5]] inputTrain += [[0.25, 0.25], [-0.25, -0.25], [-0.25, 0.25], [0.25, -0.25]] inputTrain = ot.Sample(inputTrain) inputTrain.setDescription(["x1", "x2"]) inputTrain .. raw:: html
x1x2
00.50.5
1-0.5-0.5
2-0.50.5
30.5-0.5
40.250.25
5-0.25-0.25
6-0.250.25
70.25-0.25


.. GENERATED FROM PYTHON SOURCE LINES 76-77 Compute the output sample from the input sample and a function. .. GENERATED FROM PYTHON SOURCE LINES 79-85 .. code-block:: Python formulas = ["cos(x1 + x2)", "(x2 + 1) * exp(x1 - 2 * x2)"] model = ot.SymbolicFunction(["x1", "x2"], formulas) model.setOutputDescription(["y1", "y2"]) outputTrain = model(inputTrain) outputTrain .. raw:: html
y1y2
00.54030230.909796
10.54030230.8243606
210.3346952
312.240845
40.87758260.973501
50.87758260.9630191
610.5904582
711.58775


.. GENERATED FROM PYTHON SOURCE LINES 86-88 Linear least squares ~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 90-91 Create a linear least squares model. .. GENERATED FROM PYTHON SOURCE LINES 93-96 .. code-block:: Python algo = ot.LinearLeastSquares(inputTrain, outputTrain) algo.run() .. GENERATED FROM PYTHON SOURCE LINES 97-98 Get the linear term. .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: Python algo.getLinear() .. raw:: html

[[ 9.93014e-17 0.998189 ]
[ 4.96507e-17 -0.925648 ]]



.. GENERATED FROM PYTHON SOURCE LINES 103-104 Get the constant term. .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: Python algo.getConstant() .. raw:: html
class=Point name=Unnamed dimension=2 values=[0.854471,1.05305]


.. GENERATED FROM PYTHON SOURCE LINES 109-110 Get the metamodel. .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python responseSurface = algo.getMetaModel() .. GENERATED FROM PYTHON SOURCE LINES 113-114 Plot the second output of our model with :math:`x_1=0.5`. .. GENERATED FROM PYTHON SOURCE LINES 116-131 .. code-block:: Python graph = ot.ParametricFunction(model, [0], [0.5]).getMarginal(1).draw(-0.5, 0.5) graph.setLegends(["Model"]) curve = ( ot.ParametricFunction(responseSurface, [0], [0.5]) .getMarginal(1) .draw(-0.5, 0.5) .getDrawable(0) ) curve.setLineStyle("dashed") curve.setLegend("Linear L.S.") graph.add(curve) graph.setLegendPosition("upper right") graph.setColors(ot.Drawable.BuildDefaultPalette(2)) view = viewer.View(graph) plt.show() .. image-sg:: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_create_linear_least_squares_model_001.png :alt: y2 as a function of x2 :srcset: /auto_meta_modeling/general_purpose_metamodels/images/sphx_glr_plot_create_linear_least_squares_model_001.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_meta_modeling_general_purpose_metamodels_plot_create_linear_least_squares_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_create_linear_least_squares_model.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_create_linear_least_squares_model.py `