.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/distributions/plot_distribution_transformation.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_distribution_transformation.py: Transform a distribution ======================== .. GENERATED FROM PYTHON SOURCE LINES 6-7 In this example we are going to use distribution algebra and distribution transformation via functions. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: Python import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 16-17 We define some (classical) distributions : .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: Python distribution1 = ot.Uniform(0.0, 1.0) distribution2 = ot.Uniform(0.0, 2.0) distribution3 = ot.WeibullMin(1.5, 2.0) .. GENERATED FROM PYTHON SOURCE LINES 24-28 Sum & difference of distributions --------------------------------- It is easy to compute the sum of distributions. For example: .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: Python distribution = distribution1 + distribution2 print(distribution) graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_001.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Trapezoidal(a = 0, b = 1, c = 2, d = 3) .. GENERATED FROM PYTHON SOURCE LINES 36-37 We might also use subtraction even with scalar values: .. GENERATED FROM PYTHON SOURCE LINES 39-44 .. code-block:: Python distribution = 3.0 - distribution3 print(distribution) graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_002.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none RandomMixture(3 - WeibullMin(beta = 1.5, alpha = 2, gamma = 0)) .. GENERATED FROM PYTHON SOURCE LINES 45-49 Product & inverse ----------------- We might also compute the product of two (or more) distributions. For example: .. GENERATED FROM PYTHON SOURCE LINES 51-56 .. code-block:: Python distribution = distribution1 * distribution2 print(distribution) graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_003.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ProductDistribution(Uniform(a = 0, b = 1) * Uniform(a = 0, b = 2)) .. GENERATED FROM PYTHON SOURCE LINES 57-58 We could also inverse a distribution : .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: Python distribution = 1 / distribution1 print(distribution) graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_004.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none CompositeDistribution=f(Uniform(a = 0, b = 1)) with f=[x]->[1.0 / x] .. GENERATED FROM PYTHON SOURCE LINES 66-67 Or compute a ratio distribution : .. GENERATED FROM PYTHON SOURCE LINES 69-74 .. code-block:: Python ratio = distribution2 / distribution1 print(ratio) graph = ratio.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_005.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ProductDistribution(Uniform(a = 0, b = 2) * CompositeDistribution=f(Uniform(a = 0, b = 1)) with f=[x]->[1.0 / x]) .. GENERATED FROM PYTHON SOURCE LINES 75-102 Transformation using functions ------------------------------ The library provides methods to get the full distributions of `f(x)` where `f` can be equal to: - `sin`, - `asin`, - `cos`, - `acos`, - `tan`, - `atan`, - `sinh`, - `asinh`, - `cosh`, - `acosh`, - `tanh`, - `atanh`, - `sqr` (for square), - `inverse`, - `sqrt`, - `exp`, - `log`/`ln`, - `abs`, - `cbrt`. For example for the usual `log` transformation: .. GENERATED FROM PYTHON SOURCE LINES 104-107 .. code-block:: Python graph = distribution1.log().drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_006.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 108-109 And for the `log2` function : .. GENERATED FROM PYTHON SOURCE LINES 111-116 .. code-block:: Python f = ot.SymbolicFunction(["x"], ["log2(x)"]) f.setDescription(["X", "ln(X)"]) graph = ot.CompositeDistribution(f, distribution1).drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_007.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 117-118 If one wants a specific method, user might rely on the :class:`~openturns.CompositeDistribution` class. .. GENERATED FROM PYTHON SOURCE LINES 118-131 .. code-block:: Python # Create a composite distribution # ------------------------------- # # In this paragraph we create a distribution defined as the push-forward distribution of a scalar distribution by a transformation. # # If we note :math:`\mathcal{L}_0` a scalar distribution, :math:`f: \mathbb{R} \rightarrow \mathbb{R}` a mapping, # then it is possible to create the push-forward distribution :math:`\mathcal{L}` defined by # # .. math:: # \mathcal{L} = f(\mathcal{L}_0) # .. GENERATED FROM PYTHON SOURCE LINES 132-133 We create a 1D normal distribution .. GENERATED FROM PYTHON SOURCE LINES 133-135 .. code-block:: Python antecedent = ot.Normal() .. GENERATED FROM PYTHON SOURCE LINES 136-137 and a 1D transform : .. GENERATED FROM PYTHON SOURCE LINES 137-139 .. code-block:: Python f = ot.SymbolicFunction(["x"], ["sin(x)+cos(x)"]) .. GENERATED FROM PYTHON SOURCE LINES 140-141 We then create the composite distribution .. GENERATED FROM PYTHON SOURCE LINES 141-145 .. code-block:: Python distribution = ot.CompositeDistribution(f, antecedent) graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_008.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 146-147 We can also build a distribution with the simplified construction .. GENERATED FROM PYTHON SOURCE LINES 147-151 .. code-block:: Python distribution = antecedent.exp() graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_009.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_009.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 152-153 and by using chained operators: .. GENERATED FROM PYTHON SOURCE LINES 153-157 .. code-block:: Python distribution = antecedent.abs().sqrt() graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_010.png :alt: plot distribution transformation :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_010.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 158-159 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 159-160 .. code-block:: Python plt.show() .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_distribution_transformation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_distribution_transformation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_distribution_transformation.py `