.. 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_truncated_distribution.py:
Create a truncated distribution
===============================
In this example we are going to define truncated distributions.
It is possible to truncate a distribution in its lower area, or its upper area or in both lower and upper areas.
In 1-d, assuming a and b bounds, its probability density function is defined as:
.. math::
\forall y \in \mathbb{R}, p_Y(y) =
\begin{array}{|ll}
0 & \mbox{for } y \geq b \mbox{ or } y \leq a\\
\displaystyle \frac{1}{F_X(b) - F_X(a)}\, p_X(y) & \mbox{for } y\in[a,b]
\end{array}
Is is also possible to truncate a multivariate distribution.
.. code-block:: default
from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)
# the original distribution
distribution = ot.Gumbel(0.45, 0.6)
graph = distribution.drawPDF()
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_001.png
:alt: plot truncated distribution
:class: sphx-glr-single-img
truncate on the left
.. code-block:: default
truncated = ot.TruncatedDistribution(distribution, 0.2, ot.TruncatedDistribution.LOWER)
graph = truncated.drawPDF()
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_002.png
:alt: plot truncated distribution
:class: sphx-glr-single-img
truncate on the right
.. code-block:: default
truncated = ot.TruncatedDistribution(distribution, 1.5, ot.TruncatedDistribution.UPPER)
graph = truncated.drawPDF()
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_003.png
:alt: plot truncated distribution
:class: sphx-glr-single-img
truncated on both bounds
.. code-block:: default
truncated = ot.TruncatedDistribution(distribution, 0.2, 1.5)
graph = truncated.drawPDF()
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_004.png
:alt: plot truncated distribution
:class: sphx-glr-single-img
Define a multivariate distribution
.. code-block:: default
dimension = 2
size = 70
sample = ot.Normal(dimension).getSample(size)
ks = ot.KernelSmoothing().build(sample)
Truncate it between (-2;2)^n
.. code-block:: default
bounds = ot.Interval([-2.0] * dimension, [2.0] * dimension)
truncatedKS = ot.Distribution(ot.TruncatedDistribution(ks, bounds))
Draw its PDF
.. code-block:: default
graph = truncatedKS.drawPDF([-2.5] * dimension, [2.5] * dimension, [256] * dimension)
graph.add(ot.Cloud(truncatedKS.getSample(200)))
graph.setColors(["blue", "red"])
view = viewer.View(graph)
plt.show()
.. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_005.png
:alt: [X0,X1] iso-PDF
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
/home/devel/project/build/python/src/site-packages/openturns/viewer.py:432: UserWarning: No contour levels were found within the data range.
contourset = self._ax[0].contour(X, Y, Z, **contour_kw)
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.410 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_truncated_distribution.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_truncated_distribution.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_truncated_distribution.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_