.. 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_likelihood.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_likelihood.py: Gaussian process regression: draw the likelihood ================================================ .. GENERATED FROM PYTHON SOURCE LINES 7-12 Abstract -------- In this short example we draw the log-likelihood as a function of the scale parameter of a covariance model. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import openturns as ot import openturns.experimental as otexp import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 18-19 We define the exact model with a :class:`~openturns.SymbolicFunction` : .. GENERATED FROM PYTHON SOURCE LINES 19-21 .. code-block:: Python f = ot.SymbolicFunction(["x"], ["x*sin(x)"]) .. GENERATED FROM PYTHON SOURCE LINES 22-23 We use the following input and output training samples : .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: Python inputSample = ot.Sample([[1.0], [3.0], [5.0], [6.0], [7.0], [8.0]]) outputSample = f(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 27-28 We choose a constant basis for the trend of the metamodel : .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: Python basis = ot.ConstantBasisFactory().build() covarianceModel = ot.SquaredExponential(1) .. GENERATED FROM PYTHON SOURCE LINES 32-33 For the covariance model, we use a Matern model with :math:`\nu = 1.5` : .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: Python covarianceModel = ot.MaternModel([1.0], 1.5) .. GENERATED FROM PYTHON SOURCE LINES 36-37 We are now ready to fit the Gaussian Process parameters and store the result : .. GENERATED FROM PYTHON SOURCE LINES 37-42 .. code-block:: Python fitter = otexp.GaussianProcessFitter(inputSample, outputSample, covarianceModel, basis) fitter.run() fitter_result = fitter.getResult() .. GENERATED FROM PYTHON SOURCE LINES 43-45 We can retrieve the covariance model from the result object and then access the scale of the model : .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python theta = fitter_result.getCovarianceModel().getScale() print("Scale of the covariance model : %.3e" % theta[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none Scale of the covariance model : 1.978e+00 .. GENERATED FROM PYTHON SOURCE LINES 49-50 This hyperparameter is calibrated thanks to a maximization of the log-likelihood. We get this log-likehood as a function of :math:`\theta` : .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python ot.ResourceMap.SetAsBool("GaussianProcessFitter-UseAnalyticalAmplitudeEstimate", True) reducedLogLikelihoodFunction = fitter.getReducedLogLikelihoodFunction() .. GENERATED FROM PYTHON SOURCE LINES 54-56 We draw the reduced log-likelihood :math:`\mathcal{L}(\theta)` as a function of the parameter :math:`\theta`. .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. code-block:: Python graph = reducedLogLikelihoodFunction.draw(0.1, 10.0, 100) graph.setXTitle(r"$\theta$") graph.setYTitle(r"$\mathcal{L}(\theta)$") graph.setTitle(r"Log-likelihood as a function of $\theta$") .. GENERATED FROM PYTHON SOURCE LINES 62-63 We represent the estimated parameter as a point on the log-likelihood curve : .. GENERATED FROM PYTHON SOURCE LINES 63-70 .. code-block:: Python L_theta = reducedLogLikelihoodFunction(theta) cloud = ot.Cloud(theta, L_theta) cloud.setColor("red") cloud.setPointStyle("fsquare") graph.add(cloud) graph.setLegends([r"Matern $\nu = 1.5$", r"$\theta$ estimate"]) .. GENERATED FROM PYTHON SOURCE LINES 71-73 We verify on the previous graph that the estimate of :math:`\theta` maximizes the log-likelihood. .. GENERATED FROM PYTHON SOURCE LINES 75-76 Display figures .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python view = otv.View(graph) otv.View.ShowAll() .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_gpr_likelihood_001.svg :alt: Log-likelihood as a function of $\theta$ :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_gpr_likelihood_001.svg :class: sphx-glr-single-img .. _sphx_glr_download_auto_meta_modeling_kriging_metamodel_plot_gpr_likelihood.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_likelihood.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gpr_likelihood.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gpr_likelihood.zip `