.. 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 Click :ref:`here ` 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 5-6 .. code-block:: default # sphinx_gallery_thumbnail_number = 5 .. GENERATED FROM PYTHON SOURCE LINES 9-10 In this example, we show how to plot the bidimensionnal log-likelihood contours of function given a sample. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: default from matplotlib import pylab as plt import openturns.viewer as viewer import openturns as ot ot.RandomGenerator.SetSeed(0) ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 19-21 Generate a sample ----------------- .. GENERATED FROM PYTHON SOURCE LINES 23-24 We create a `TruncatedNormal` and generate a small sample. .. GENERATED FROM PYTHON SOURCE LINES 26-33 .. code-block:: default 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 34-35 In order to see the distribution and the sample, we draw the PDF of the distribution and generate a clouds which X coordinates are the sample values. .. GENERATED FROM PYTHON SOURCE LINES 37-48 .. code-block:: default graph = distribution.drawPDF() graph.setLegends(["TruncatedNormal"]) graph.setColors(["red"]) zeros = ot.Sample(sample.getSize(), 1) cloud = ot.Cloud(sample, zeros) cloud.setLegend("Sample") graph.add(cloud) graph.setLegendPosition("topleft") view = viewer.View(graph) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_001.png :alt: plot graphs loglikelihood contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-50 The following function computes the log-likelihood of a `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 52-54 Define the log-likelihood function ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-57 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:: default 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-83 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 85-86 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 88-94 .. code-block:: default 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.png :alt: y0 as a function of (x0,x1) :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 95-97 Customizing the number of levels -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 99-100 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 `ResourceMap`. .. GENERATED FROM PYTHON SOURCE LINES 102-109 .. code-block:: default 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.png :alt: y0 as a function of (x0,x1) :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-111 A part of the graphics is hidden by the legends. This is why we fine tune the graphics in the next examples. .. GENERATED FROM PYTHON SOURCE LINES 113-115 Getting the level values ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 117-118 The level values can be retrieved with the `getLevels` method. .. GENERATED FROM PYTHON SOURCE LINES 120-127 .. code-block:: default drawables = graphBasic.getDrawables() levels = [] for i in range(len(drawables)): contours = drawables[i] levels.append(contours.getLevels()[0]) levels .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [-40.84428512570853, -13.765651599444114, -13.159190501082549, -13.080032173821092, -13.055840526560251] .. GENERATED FROM PYTHON SOURCE LINES 128-130 Monochrome contour plot ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 132-133 We first configure the contour plot. By default each level is a dedicated contour in order to have one color per contour, but they all share the same grid and data. We use the `getDrawable` method to take the first contour as the only one with multiple levels. Then we use the `setLevels` method: we ask for many iso-values in the same data so the color will be the same for all curves. In order to inline the level values labels, we use the `setDrawLabels` method. .. GENERATED FROM PYTHON SOURCE LINES 135-139 .. code-block:: default contours = graphBasic.getDrawable(0) contours.setLevels(levels) contours.setDrawLabels(True) .. GENERATED FROM PYTHON SOURCE LINES 140-141 Then we create a new graph. Finally, we use the `setDrawables` to substitute the collection of drawables by a collection reduced to this unique contour. .. GENERATED FROM PYTHON SOURCE LINES 143-148 .. code-block:: default graphFineTune = ot.Graph("Log-Likelihood", r"$\mu$", r"$\sigma$", True, '') graphFineTune.setDrawables([contours]) graphFineTune.setLegendPosition("") # Remove the legend view = viewer.View(graphFineTune) .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_004.png :alt: Log-Likelihood :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 149-151 Multicolor contour plot ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 153-156 The previous contour plot is fine, but lacks of colors. It is not obvious that the colors make the plot clearer given that the values in the contour plot are so different: some adjacent contours have close levels, while others are very different. Anyway, it is obviously nicer to get a colored graphics. The following script first creates a palette of colors with the `BuildDefaultPalette` class. Before doing so, we configure the `Drawable-DefaultPalettePhase` `ResourceMap` key so that the number of generated colors corresponds to the number of levels. Then we create the `drawables` list, where each item is a single contour with its own level and color. .. GENERATED FROM PYTHON SOURCE LINES 158-159 Take the first contour as the only one with multiple levels .. GENERATED FROM PYTHON SOURCE LINES 159-173 .. code-block:: default contour = graphBasic.getDrawable(0) # Build a range of colors ot.ResourceMap.SetAsUnsignedInteger( 'Drawable-DefaultPalettePhase', len(levels)) palette = ot.Drawable.BuildDefaultPalette(len(levels)) # Create the drawables list, appending each contour with its own color drawables = list() for i in range(len(levels)): contour.setLevels([levels[i]]) # Inline the level values contour.setDrawLabels(True) # We have to copy the drawable because a Python list stores only pointers drawables.append(ot.Drawable(contour)) .. GENERATED FROM PYTHON SOURCE LINES 174-180 .. code-block:: default graphFineTune = ot.Graph("Log-Likelihood", r"$\mu$", r"$\sigma$", True, '') graphFineTune.setDrawables(drawables) # Replace the drawables graphFineTune.setLegendPosition("") # Remove the legend graphFineTune.setColors(palette) # Add colors view = viewer.View(graphFineTune) plt.show() .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_005.png :alt: Log-Likelihood :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.598 seconds) .. _sphx_glr_download_auto_graphs_plot_graphs_loglikelihood_contour.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_graphs_loglikelihood_contour.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_graphs_loglikelihood_contour.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_