.. 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_probabilistic_modeling_distributions_plot_minimum_volume_level_set_2D.py:
Draw minimum volume level set in 2D
===================================
In this example, we compute the minimum volume level set of a bivariate distribution.
.. code-block:: default
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)
Create a gaussian
.. code-block:: default
corr = ot.CorrelationMatrix(2)
corr[0, 1] = 0.2
copula = ot.NormalCopula(corr)
x1 = ot.Normal(-1., 1)
x2 = ot.Normal(2, 1)
x_funk = ot.ComposedDistribution([x1, x2], copula)
# Create a second gaussian
x1 = ot.Normal(1.,1)
x2 = ot.Normal(-2,1)
x_punk = ot.ComposedDistribution([x1, x2], copula)
# Mix the distributions
mixture = ot.Mixture([x_funk, x_punk], [0.5,1.])
.. code-block:: default
graph = mixture.drawPDF()
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_minimum_volume_level_set_2D_001.png
:alt: [X0,X1] iso-PDF
:class: sphx-glr-single-img
For a multivariate distribution (with dimension greater than 1), the `computeMinimumVolumeLevelSetWithThreshold` uses Monte-Carlo sampling.
.. code-block:: default
ot.ResourceMap.SetAsUnsignedInteger("Distribution-MinimumVolumeLevelSetSamplingSize",1000)
We want to compute the minimum volume LevelSet which contains `alpha`=90% of the distribution. The `threshold` is the value of the PDF corresponding the `alpha`-probability: the points contained in the LevelSet have a PDF value lower or equal to this threshold.
.. code-block:: default
alpha = 0.9
levelSet, threshold = mixture.computeMinimumVolumeLevelSetWithThreshold(alpha)
threshold
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
0.009132175837384555
.. code-block:: default
def drawLevelSetContour2D(distribution, numberOfPointsInXAxis, alpha, threshold, sampleSize= 500):
'''
Compute the minimum volume LevelSet of measure equal to alpha and get the
corresponding density value (named threshold).
Generate a sample of the distribution and draw it.
Draw a contour plot for the distribution, where the PDF is equal to threshold.
'''
sample = distribution.getSample(sampleSize)
X1min = sample[:, 0].getMin()[0]
X1max = sample[:, 0].getMax()[0]
X2min = sample[:, 1].getMin()[0]
X2max = sample[:, 1].getMax()[0]
xx = ot.Box([numberOfPointsInXAxis],
ot.Interval([X1min], [X1max])).generate()
yy = ot.Box([numberOfPointsInXAxis],
ot.Interval([X2min], [X2max])).generate()
xy = ot.Box([numberOfPointsInXAxis, numberOfPointsInXAxis],
ot.Interval([X1min, X2min], [X1max, X2max])).generate()
data = distribution.computePDF(xy)
graph = ot.Graph('', 'X1', 'X2', True, 'topright')
labels = ["%.2f%%" % (100*alpha)]
contour = ot.Contour(xx, yy, data, ot.Point([threshold]), ot.Description(labels))
contour.setColor('black')
graph.setTitle("%.2f%% of the distribution, sample size = %d" % (100*alpha,sampleSize))
graph.add(contour)
cloud = ot.Cloud(sample)
graph.add(cloud)
return graph
The following plot shows that 90% of the sample is contained in the LevelSet.
.. code-block:: default
numberOfPointsInXAxis = 50
graph = drawLevelSetContour2D(mixture, numberOfPointsInXAxis, alpha, threshold)
view = viewer.View(graph)
plt.show()
.. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_minimum_volume_level_set_2D_002.png
:alt: 90.00% of the distribution, sample size = 500
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.222 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_minimum_volume_level_set_2D.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_minimum_volume_level_set_2D.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_minimum_volume_level_set_2D.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_