.. 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-17 .. code-block:: Python 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 18-20 Generate a sample ----------------- .. GENERATED FROM PYTHON SOURCE LINES 22-23 We create a :class:`~openturns.TruncatedNormal` and generate a small sample. .. GENERATED FROM PYTHON SOURCE LINES 25-32 .. 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 33-34 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 36-46 .. 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.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 47-50 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 52-54 Define the log-likelihood function ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-60 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 63-81 .. 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 82-84 With the draw method -------------------- .. GENERATED FROM PYTHON SOURCE LINES 86-88 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 90-92 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 94-100 .. 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.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 101-103 Customizing the number of levels -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 105-107 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 109-116 .. 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.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 117-118 We get the underlying `Contour` object as a `Drawable`. .. GENERATED FROM PYTHON SOURCE LINES 118-121 .. code-block:: Python contour = graphBasic.getDrawable(0) .. GENERATED FROM PYTHON SOURCE LINES 122-123 To be able to use specific `Contour` methods like `buildDefaultLevels`, we need to use the method named `getImplementation`. .. GENERATED FROM PYTHON SOURCE LINES 123-131 .. 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.png :alt: plot graphs loglikelihood contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 132-134 Using a rank-based normalization of the colors ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 136-139 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 139-144 .. 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.png :alt: plot graphs loglikelihood contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 145-147 Fill the contour graph ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 149-150 Areas between contour lines can be colored by requesting a filled outline. .. GENERATED FROM PYTHON SOURCE LINES 150-157 .. 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.png :alt: plot graphs loglikelihood contour :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 158-160 Getting the level values ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 162-163 The level values can be retrieved with the `getLevels` method. .. GENERATED FROM PYTHON SOURCE LINES 165-169 .. 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 170-172 Monochrome contour plot ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 174-179 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 181-186 .. code-block:: Python contour = graphBasic.getDrawable(0) contour.setLevels(levels) contour.setDrawLabels(True) contour.setColor("red") .. GENERATED FROM PYTHON SOURCE LINES 187-190 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 190-194 .. code-block:: Python contour = contour.getImplementation() contour.setColorBarPosition("") .. GENERATED FROM PYTHON SOURCE LINES 195-196 Then we create a new graph. Finally, we use `setDrawables` to define this `Contour` object as the single Drawable. .. GENERATED FROM PYTHON SOURCE LINES 196-201 .. 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.png :alt: Log-Likelihood :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 202-204 Set multiple colors manually ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 206-209 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 209-216 .. 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}) plt.show() .. image-sg:: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_008.png :alt: Log-Likelihood :srcset: /auto_graphs/images/sphx_glr_plot_graphs_loglikelihood_contour_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 217-218 Reset default settings .. GENERATED FROM PYTHON SOURCE LINES 218-219 .. code-block:: Python ot.ResourceMap.Reload() .. _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 `