.. 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_distribution_transformation.py: Transform a distribution ======================== In this example we are going to use distribution algebra and distribution transformation via functions. .. 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) We define some (classical) distribution : .. code-block:: default distribution1 = ot.Uniform(0.0, 1.0) distribution2 = ot.Uniform(0.0, 2.0) distribution3 = ot.WeibullMin(1.5, 2.0) Sum & difference of distributions --------------------------------- It is easy to compute the sum of distributions. For example: .. code-block:: default distribution = distribution1 + distribution2 print(distribution) graph = distribution.drawPDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_001.png :alt: plot distribution transformation :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Trapezoidal(a = 0, b = 1, c = 2, d = 3) We might also use substraction even with scalar values: .. code-block:: default distribution = 3.0 - distribution3 print(distribution) graph = distribution.drawPDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_002.png :alt: plot distribution transformation :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none RandomMixture(3 - WeibullMin(beta = 1.5, alpha = 2, gamma = 0)) Product & inverse ----------------- We might also compute the product of two (or more) distributions. For example: .. code-block:: default distribution = distribution1 * distribution2 print(distribution) graph = distribution.drawPDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_003.png :alt: plot distribution transformation :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ProductDistribution(Uniform(a = 0, b = 1) * Uniform(a = 0, b = 2)) We could also inverse a distribution : .. code-block:: default distribution = 1 / distribution1 print(distribution) graph = distribution.drawPDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_004.png :alt: plot distribution transformation :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none CompositeDistribution=f(Uniform(a = 0, b = 1)) with f=[x]->[1.0 / x] Or compute a ratio distrobution: .. code-block:: default ratio = distribution2 / distribution1 print(ratio) graph = ratio.drawPDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_005.png :alt: plot distribution transformation :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ProductDistribution(Uniform(a = 0, b = 2) * CompositeDistribution=f(Uniform(a = 0, b = 1)) with f=[x]->[1.0 / x]) 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`. If one wants a specific method, user might rely on `CompositeDistribution`. For example for the usual `log` transformation: .. code-block:: default graph =distribution1.log().drawPDF() view = viewer.View(graph) .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_006.png :alt: plot distribution transformation :class: sphx-glr-single-img And for the `log2` function: .. code-block:: default f = ot.SymbolicFunction(['x'], ['log2(x)']) f.setDescription(["X","ln(X)"]) graph = ot.CompositeDistribution(f, distribution1).drawPDF() view = viewer.View(graph) plt.show() .. image:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_distribution_transformation_007.png :alt: plot distribution transformation :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.641 seconds) .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_distribution_transformation.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_distribution_transformation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_distribution_transformation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_