.. 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-18 .. 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) ot.ResourceMap.SetAsString("Contour-DefaultColorMap", "viridis") .. 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:: 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 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:: Python 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("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 49-52 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 54-56 Define the log-likelihood function ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 58-62 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 65-83 .. 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 84-86 With the draw method -------------------- .. GENERATED FROM PYTHON SOURCE LINES 88-90 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 92-94 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 96-102 .. 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 103-105 Customizing the number of levels -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 107-109 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 111-118 .. 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 119-120 We get the underlying `Contour` object as a `Drawable`. .. GENERATED FROM PYTHON SOURCE LINES 120-123 .. code-block:: Python contour = graphBasic.getDrawable(0) .. GENERATED FROM PYTHON SOURCE LINES 124-125 To be able to use specific `Contour` methods like `buildDefaultLevels`, we need to use `getImplementation`. .. GENERATED FROM PYTHON SOURCE LINES 125-133 .. 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 134-140 Using a rank-based normalization of the colors ---------------------------------------------- %% In the previous plots, there was little color variation for isolines corresponding to high 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 140-145 .. 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 146-148 Fill the contour graph ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 150-151 Areas between contour lines can be colored by requesting a filled outline. .. GENERATED FROM PYTHON SOURCE LINES 151-158 .. 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 159-161 Getting the level values ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 163-164 The level values can be retrieved with the `getLevels` method. .. GENERATED FROM PYTHON SOURCE LINES 166-170 .. 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 171-173 Monochrome contour plot ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 175-180 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 182-187 .. code-block:: Python contour = graphBasic.getDrawable(0) contour.setLevels(levels) contour.setDrawLabels(True) contour.setColor("red") .. GENERATED FROM PYTHON SOURCE LINES 188-191 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 191-195 .. code-block:: Python contour = contour.getImplementation() contour.setColorBarPosition("") .. GENERATED FROM PYTHON SOURCE LINES 196-197 Then we create a new graph. Finally, we use `setDrawables` to define this `Contour` object as the single Drawable. .. GENERATED FROM PYTHON SOURCE LINES 197-202 .. 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 203-205 Set multiple colors manually ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 207-210 The `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 `viewer.View` class. .. GENERATED FROM PYTHON SOURCE LINES 210-217 .. 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 218-219 Reset default settings .. GENERATED FROM PYTHON SOURCE LINES 219-220 .. 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 `