.. 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 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: 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-32 .. code-block:: default 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 33-34 truncate on the left .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: default 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-46 .. code-block:: default 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 47-48 truncated on both bounds .. GENERATED FROM PYTHON SOURCE LINES 48-52 .. code-block:: default 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 53-54 Define a multivariate distribution .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. code-block:: default dimension = 2 size = 70 sample = ot.Normal(dimension).getSample(size) ks = ot.KernelSmoothing().build(sample) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Truncate it between (-2;2)^n .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: default bounds = ot.Interval([-2.0] * dimension, [2.0] * dimension) truncatedKS = ot.Distribution(ot.TruncatedDistribution(ks, bounds)) .. GENERATED FROM PYTHON SOURCE LINES 65-66 Draw its PDF .. GENERATED FROM PYTHON SOURCE LINES 66-72 .. 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-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 .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/devel/project/build/python/src/site-packages/openturns/viewer.py:442: 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.388 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 `_