.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_graphs/plot_graphs_loglikelihood_contour.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_graphs_plot_graphs_loglikelihood_contour.py: Plot the log-likelihood contours of a distribution ================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 In this example, we show how to plot the bidimensionnal log-likelihood contours of function given a sample. .. GENERATED FROM PYTHON SOURCE LINES 10-13 .. code-block:: Python import openturns.viewer as viewer import openturns as ot .. GENERATED FROM PYTHON SOURCE LINES 14-16 Generate a sample ----------------- .. GENERATED FROM PYTHON SOURCE LINES 18-19 We create a :class:`~openturns.TruncatedNormal` and generate a small sample. .. GENERATED FROM PYTHON SOURCE LINES 21-28 .. code-block:: Python a = -1 b = 2.5 mu = 2.0 sigma = 3.0 distribution = ot.TruncatedNormal(mu, sigma, a, b) sample = distribution.getSample(11) .. GENERATED FROM PYTHON SOURCE LINES 29-30 In order to see the distribution and the sample, we draw the PDF of the distribution and generate a cloud which `X` coordinates are the sample values. .. GENERATED FROM PYTHON SOURCE LINES 32-42 .. code-block:: Python graph = distribution.drawPDF() graph.setLegends(["TruncatedNormal"]) zeros = ot.Sample(sample.getSize(), 1) cloud = ot.Cloud(sample, zeros) cloud.setLegend("Sample") graph.add(cloud) graph.setLegendPosition("upper left") view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_001.svg :alt: plot graphs loglikelihood contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-46 The following function computes the log-likelihood of a :class:`~openturns.TruncatedNormal` which mean and standard deviations are given as input arguments. The lower and upper bounds of the distribution are computed as minimum and maximum of the sample. .. GENERATED FROM PYTHON SOURCE LINES 48-50 Define the log-likelihood function ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 52-56 The following function evaluates the log-likelihood function given a point :math:`X=(\mu,\sigma`). In order to evaluate the likelihood on the sample, we use a trick: we evaluate the `computeMean` method on the log-PDF sample, then multiply by the sample size. This is much faster than using a `for` loop. .. GENERATED FROM PYTHON SOURCE LINES 59-77 .. code-block:: Python def logLikelihood(X): """ Evaluate the log-likelihood of a TruncatedNormal on a sample. """ samplesize = sample.getSize() mu = X[0] sigma = X[1] a = sample.getMin()[0] b = sample.getMax()[0] delta = (b - a) / samplesize a -= delta b += delta distribution = ot.TruncatedNormal(mu, sigma, a, b) samplelogpdf = distribution.computeLogPDF(sample) loglikelihood = samplelogpdf.computeMean() * samplesize return loglikelihood .. GENERATED FROM PYTHON SOURCE LINES 78-80 With the draw method -------------------- .. GENERATED FROM PYTHON SOURCE LINES 82-84 In this section, we use the `draw` method which is available for any `Function` which has 1 or 2 input arguments. In our case, the log-likelihood function has two inputs: :math:`x_0=\mu` and :math:`x_1=\sigma`. .. GENERATED FROM PYTHON SOURCE LINES 86-88 Draw the log-likelihood function with the `draw` method: this is much faster than using a `for` loop. In order to print LaTeX `X` and `Y` labels, we use the `"r"` character in front of the string containing the LaTeX command. .. GENERATED FROM PYTHON SOURCE LINES 90-96 .. code-block:: Python logLikelihoodFunction = ot.PythonFunction(2, 1, logLikelihood) graphBasic = logLikelihoodFunction.draw([-3.0, 0.1], [5.0, 7.0], [50] * 2) graphBasic.setXTitle(r"$\mu$") graphBasic.setYTitle(r"$\sigma$") view = viewer.View(graphBasic) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_002.svg :alt: y0 as a function of (x0,x1) :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 97-99 Customizing the number of levels -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 101-103 The level values are computed from the quantiles of the data, so that the contours are equally spaced. We can configure the number of levels by setting the `Contour-DefaultLevelsNumber` key in the :class:`~openturns.ResourceMap`. .. GENERATED FROM PYTHON SOURCE LINES 105-112 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("Contour-DefaultLevelsNumber", 5) logLikelihoodFunction = ot.PythonFunction(2, 1, logLikelihood) graphBasic = logLikelihoodFunction.draw([-3.0, 0.1], [5.0, 7.0], [50] * 2) graphBasic.setXTitle(r"$\mu$") graphBasic.setYTitle(r"$\sigma$") view = viewer.View(graphBasic) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_003.svg :alt: y0 as a function of (x0,x1) :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 113-114 We get the underlying `Contour` object as a `Drawable`. .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. code-block:: Python contour = graphBasic.getDrawable(0) .. GENERATED FROM PYTHON SOURCE LINES 118-119 To be able to use specific `Contour` methods like `buildDefaultLevels`, we need to use the method named `getImplementation`. .. GENERATED FROM PYTHON SOURCE LINES 119-127 .. code-block:: Python contour = contour.getImplementation() contour.buildDefaultLevels(50) manyLevelGraph = ot.Graph() manyLevelGraph.add(contour) view = viewer.View(manyLevelGraph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_004.svg :alt: plot graphs loglikelihood contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_004.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 128-130 Using a rank-based normalization of the colors ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 132-135 In the previous plots, there was little color variation for isolines corresponding to large log-likelihood values. This is due to a steep cliff visible for low values of :math:`\sigma`. To make the color variation clearer around -13, we use a normalization based on the rank of the level curve and not on its value. .. GENERATED FROM PYTHON SOURCE LINES 135-140 .. code-block:: Python contour.setColorMapNorm("rank") rankGraph = ot.Graph() rankGraph.add(contour) view = viewer.View(rankGraph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_005.svg :alt: plot graphs loglikelihood contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_005.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 141-143 Fill the contour graph ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 145-146 Areas between contour lines can be colored by requesting a filled outline. .. GENERATED FROM PYTHON SOURCE LINES 146-153 .. code-block:: Python # sphinx_gallery_thumbnail_number = 6 contour.setIsFilled(True) filledGraph = ot.Graph() filledGraph.add(contour) view = viewer.View(filledGraph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_006.svg :alt: plot graphs loglikelihood contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_006.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 154-156 Getting the level values ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 158-159 The level values can be retrieved with the `getLevels` method. .. GENERATED FROM PYTHON SOURCE LINES 161-165 .. code-block:: Python drawables = graphBasic.getDrawables() levels = drawables[0].getLevels() levels .. raw:: html
class=Point name=Unnamed dimension=5 values=[-37.5422,-13.7386,-13.1559,-13.0795,-13.0552]


.. GENERATED FROM PYTHON SOURCE LINES 166-168 Monochrome contour plot ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 170-175 We first configure the contour plot. We use the `getDrawable` method to take the first contour as the only one with multiple levels. Then we use the `setLevels` method: we could have changed the levels. We use the `setColor` method to get a monochrome contour. In order to inline the level values labels, we use the `setDrawLabels` method. .. GENERATED FROM PYTHON SOURCE LINES 177-182 .. code-block:: Python contour = graphBasic.getDrawable(0) contour.setLevels(levels) contour.setDrawLabels(True) contour.setColor("red") .. GENERATED FROM PYTHON SOURCE LINES 183-186 Hide the color bar. The method to do this is not available to generic Drawables, so we need to get the actual `Contour` object. .. GENERATED FROM PYTHON SOURCE LINES 186-190 .. code-block:: Python contour = contour.getImplementation() contour.setColorBarPosition("") .. GENERATED FROM PYTHON SOURCE LINES 191-192 Then we create a new graph. Finally, we use `setDrawables` to define this `Contour` object as the single Drawable. .. GENERATED FROM PYTHON SOURCE LINES 192-197 .. code-block:: Python graphFineTune = ot.Graph("Log-Likelihood", r"$\mu$", r"$\sigma$", True, "") graphFineTune.setDrawables([contour]) view = viewer.View(graphFineTune) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_007.svg :alt: Log-Likelihood :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_007.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 198-200 Set multiple colors manually ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 202-205 The :class:`~openturns.Contour` class does not allow us to manually set multiple colors. Here we show how to assign explicit colors to the different contour lines by passing keyword arguments to the class:`~openturns.viewer.View` class. .. GENERATED FROM PYTHON SOURCE LINES 205-210 .. code-block:: Python # Build a range of colors corresponding to the Tableau palette palette = ot.Drawable.BuildTableauPalette(len(levels)) view = viewer.View(graphFineTune, contour_kw={"colors": palette, "cmap": None}) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_008.svg :alt: Log-Likelihood :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_008.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 211-212 .. code-block:: Python viewer.View.ShowAll() .. _sphx_glr_download_auto_graphs_plot_graphs_loglikelihood_contour.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_graphs_loglikelihood_contour.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_graphs_loglikelihood_contour.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_graphs_loglikelihood_contour.zip `