.. 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_kriging_likelihood.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_meta_modeling_kriging_metamodel_plot_kriging_likelihood.py: Kriging : draw the likelihood ============================= .. GENERATED FROM PYTHON SOURCE LINES 6-11 Abstract -------- In this short example we draw the loglikelihood as a function of the scale parameter of a covariance kriging model. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: default import openturns as ot import openturns.viewer as otv from matplotlib import pylab as plt .. GENERATED FROM PYTHON SOURCE LINES 17-18 We define the exact model with a :class:`~openturns.SymbolicFunction` : .. GENERATED FROM PYTHON SOURCE LINES 18-20 .. code-block:: default f = ot.SymbolicFunction(['x'], ['x*sin(x)']) .. GENERATED FROM PYTHON SOURCE LINES 21-22 We use the following input and output training samples : .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: default inputSample = ot.Sample([[1.0], [3.0], [5.0], [6.0], [7.0], [8.0]]) outputSample = f(inputSample) .. GENERATED FROM PYTHON SOURCE LINES 26-27 We choose a constant basis for the trend of the metamodel : .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: default basis = ot.ConstantBasisFactory().build() covarianceModel = ot.SquaredExponential(1) .. GENERATED FROM PYTHON SOURCE LINES 31-32 For the covariance model, we use a Matern model with :math:`\nu = 1.5` : .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: default covarianceModel = ot.MaternModel([1.0], 1.5) .. GENERATED FROM PYTHON SOURCE LINES 35-36 We are now ready to build the kriging algorithm, run it and store the result : .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: default algo = ot.KrigingAlgorithm(inputSample, outputSample, covarianceModel, basis) algo.run() result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 41-43 We can retrieve the covariance model from the result object and then access the scale of the model : .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: default theta = result.getCovarianceModel().getScale() print("Scale of the covariance model : %.3e" % theta[0]) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Scale of the covariance model : 1.978e+00 .. GENERATED FROM PYTHON SOURCE LINES 47-48 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 48-52 .. code-block:: default ot.ResourceMap.SetAsBool( "GeneralLinearModelAlgorithm-UseAnalyticalAmplitudeEstimate", True) reducedLogLikelihoodFunction = algo.getReducedLogLikelihoodFunction() .. GENERATED FROM PYTHON SOURCE LINES 53-55 We draw the reduced loglikelihood :math:`\mathcal{L}(\theta)` as a function of the parameter :math:`\theta`. .. GENERATED FROM PYTHON SOURCE LINES 55-60 .. code-block:: default 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 61-62 We represent the estimated parameter as a point on the loglikelihood curve : .. GENERATED FROM PYTHON SOURCE LINES 62-69 .. code-block:: default 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 70-72 We verify on the previous graph that the estimate of :math:`\theta` maximizes the loglikelihood. .. GENERATED FROM PYTHON SOURCE LINES 74-75 Display figures .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: default view = otv.View(graph) otv.View.ShowAll() .. image-sg:: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_likelihood_001.png :alt: Log-likelihood as a function of $\theta$ :srcset: /auto_meta_modeling/kriging_metamodel/images/sphx_glr_plot_kriging_likelihood_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.120 seconds) .. _sphx_glr_download_auto_meta_modeling_kriging_metamodel_plot_kriging_likelihood.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_kriging_likelihood.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kriging_likelihood.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_