.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/distributions/plot_truncated_distribution.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_probabilistic_modeling_distributions_plot_truncated_distribution.py: Truncate a distribution ======================== .. GENERATED FROM PYTHON SOURCE LINES 6-20 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. .. GENERATED FROM PYTHON SOURCE LINES 22-33 .. code-block:: Python 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-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_001.png :alt: plot truncated distribution :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 34-35 truncate on the left .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python truncated = ot.TruncatedDistribution(distribution, 0.2, ot.TruncatedDistribution.LOWER) graph = truncated.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_002.png :alt: plot truncated distribution :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 40-41 truncate on the right .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python truncated = ot.TruncatedDistribution(distribution, 1.5, ot.TruncatedDistribution.UPPER) graph = truncated.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_003.png :alt: plot truncated distribution :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-47 truncated on both bounds .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: Python truncated = ot.TruncatedDistribution(distribution, 0.2, 1.5) graph = truncated.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_004.png :alt: plot truncated distribution :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 52-53 Define a multivariate distribution .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: Python dimension = 2 size = 70 sample = ot.Normal(dimension).getSample(size) ks = ot.KernelSmoothing().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 59-60 Truncate it between (-2;2)^n .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python bounds = ot.Interval([-2.0] * dimension, [2.0] * dimension) truncatedKS = ot.Distribution(ot.TruncatedDistribution(ks, bounds)) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Draw its PDF .. GENERATED FROM PYTHON SOURCE LINES 65-70 .. code-block:: Python 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-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_005.png :alt: [X0,X1] iso-PDF :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_truncated_distribution_005.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_truncated_distribution.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_truncated_distribution.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_truncated_distribution.py `