.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_probabilistic_modeling/distributions/plot_overview_univariate_distributions.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_overview_univariate_distributions.py: Overview of univariate distribution management ============================================== .. GENERATED FROM PYTHON SOURCE LINES 6-15 Abstract -------- In this example, we present the following topics: - the distributions with several parametrizations (particularly with the `Beta` distribution), - the arithmetic of distributions and functions of distributions, - the `CompositeDistribution` for more general functions, - how to define our customized distribution with `PythonDistribution` if the distribution do not exist. .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. 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 24-26 Distributions with several parametrizations ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-52 By default, any univariate distribution uses its native parameters. For some few distributions, alternative parameters might be used to define the distribution. For example, the `Beta` distribution has several parametrizations. The native parametrization uses the following parameters: - :math:`\alpha` : the first shape parameter, :math:`\alpha>0`, - :math:`\beta` : the second shape parameter, :math:`\beta> 0`, - :math:`a` : the lower bound, - :math:`b` : the upper bound with :math:`a0`, the beta function is: .. math:: B(y,z) = \int_0^1 t^{y-1} (1-t)^{z-1} dt. .. GENERATED FROM PYTHON SOURCE LINES 54-55 The `Beta` class uses the native parametrization. .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python distribution = ot.Beta(2.5, 2.5, -1, 2) graph = distribution.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_001.png :alt: plot overview univariate distributions :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 62-86 The `BetaMuSigma` class provides another parametrization, based on the expectation :math:`\mu` and the standard deviation :math:`\sigma` of the random variable: .. math:: \mu = a + \frac{(b-a)\alpha}{\alpha+\beta} and .. math:: \sigma^2 = \left(\frac{b-a}{\alpha+\beta}\right)^2 \frac{\alpha\beta}{\alpha+\beta+1}. Inverting the equations, we get: .. math:: \alpha = \left(\dfrac{\mu-a}{b-a}\right) \left( \dfrac{(b-\mu)(\mu-a)}{\sigma^2}-1\right) \\ and .. math:: \beta = \left( \dfrac{b-\mu}{\mu-a}\right) \alpha .. GENERATED FROM PYTHON SOURCE LINES 88-89 The following session creates a beta random variable with parameters :math:`\mu=0.2`, :math:`\sigma=0.6`, :math:`a=-1` et :math:`b=2`. .. GENERATED FROM PYTHON SOURCE LINES 91-94 .. code-block:: Python parameters = ot.BetaMuSigma(0.2, 0.6, -1, 2) parameters.evaluate() .. raw:: html
class=Point name=Unnamed dimension=4 values=[2,3,-1,2]


.. GENERATED FROM PYTHON SOURCE LINES 95-96 The `ParametrizedDistribution` creates a distribution from a parametrization. .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: Python param_dist = ot.ParametrizedDistribution(parameters) param_dist .. raw:: html
ParametrizedDistribution
  • name=Unnamed
  • dimension=1
  • weight=1
  • range=[-1, 2]
  • description=[X0]
  • isParallel=true
  • isCopula=false


.. GENERATED FROM PYTHON SOURCE LINES 102-104 Functions of distributions -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 106-120 The library provides algebra of univariate distributions: - `+, -` - `*, /` It also provides methods to get the full distributions of `f(x)` where `f` can be equal to : - `sin`, - `cos`, - `acos`, - `asin` - `square`, - `inverse`, - `sqrt`. .. GENERATED FROM PYTHON SOURCE LINES 122-123 In the following example, we create a beta and an exponential variable. Then we create the random variable equal to the sum of the two previous variables. .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: Python B = ot.Beta(5, 2, 9, 10) .. GENERATED FROM PYTHON SOURCE LINES 128-130 .. code-block:: Python E = ot.Exponential(3) .. GENERATED FROM PYTHON SOURCE LINES 131-133 .. code-block:: Python S = B + E .. GENERATED FROM PYTHON SOURCE LINES 134-136 The `S` variable is equipped with the methods of any distribution: we can for example compute the PDF or the CDF at any point and compute its quantile. For example, we can simply draw the PDF with the `drawPDF` class. .. GENERATED FROM PYTHON SOURCE LINES 138-142 .. code-block:: Python graph = S.drawPDF() graph.setTitle("Sum of a beta and an exponential distribution") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_002.png :alt: Sum of a beta and an exponential distribution :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 143-144 The exponential function of this distribution can be computed with the `exp` method. .. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: Python sumexp = S.exp() .. GENERATED FROM PYTHON SOURCE LINES 149-153 .. code-block:: Python graph = sumexp.drawPDF() graph.setTitle("Exponential of a sum of a beta and an exponential") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_003.png :alt: Exponential of a sum of a beta and an exponential :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 154-160 The `CompositeDistribution` class for more general functions ------------------------------------------------------------ More complex functions can be created thanks to the `CompositeDistribution` class, but it requires an `f` function. In the following example, we create the distribution of a random variable equal to the exponential of a gaussian variable. Obviously, this is equivalent to the `LogNormal` distribution but this shows how such a distribution could be created. .. GENERATED FROM PYTHON SOURCE LINES 162-163 First, we create a distribution. .. GENERATED FROM PYTHON SOURCE LINES 165-168 .. code-block:: Python N = ot.Normal(0.0, 1.0) N.setDescription(["Normal"]) .. GENERATED FROM PYTHON SOURCE LINES 169-170 Secondly, we create a function. .. GENERATED FROM PYTHON SOURCE LINES 172-175 .. code-block:: Python f = ot.SymbolicFunction(["x"], ["exp(x)"]) f.setDescription(["X", "Exp(X)"]) .. GENERATED FROM PYTHON SOURCE LINES 176-177 Finally, we create the distribution equal to the exponential of the gaussian random variable. .. GENERATED FROM PYTHON SOURCE LINES 179-181 .. code-block:: Python dist = ot.CompositeDistribution(f, N) .. GENERATED FROM PYTHON SOURCE LINES 182-186 .. code-block:: Python graph = dist.drawPDF() graph.setTitle("Exponential of a gaussian random variable") view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_004.png :alt: Exponential of a gaussian random variable :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 187-188 In order to check the previous distribution, we compare it with the LogNormal distribution. .. GENERATED FROM PYTHON SOURCE LINES 190-196 .. code-block:: Python LN = ot.LogNormal() LN.setDescription(["LogNormal"]) graph = LN.drawPDF() view = viewer.View(graph) .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_005.png :alt: plot overview univariate distributions :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 197-231 The `PythonDistribution` class ------------------------------ Another possibility is to define our own `distribution`. For example let us implement the `Quartic` kernel (also known as the `Biweight` kernel, see `here `_), which is sometimes used in the context of kernel smoothing. The PDF of the kernel is defined by: .. math:: f(u) = \frac{15}{16} (1 - u^2)^2 for any :math:`u\in[-1,1]` and :math:`f(u)=0` otherwise. Expanding the previous square, we find: .. math:: f(u) = \frac{15}{16} (1 - 2 u^2 + u^4) for any :math:`u\in[-1,1]`. Integrating the previous equation leads to the CDF: .. math:: F(u) = \frac{1}{2} + \frac{15}{16} u - \frac{5}{8} u^3 + \frac{3}{16} u^5 for any :math:`u\in[-1,1]` and :math:`F(u)=0` otherwise. The only required method is `computeCDF`. Since the PDF is easy to define in our example, we implement it as well. Here, the distribution is defined on the interval :math:`[-1,1]`, so that we define the `getRange` method. .. GENERATED FROM PYTHON SOURCE LINES 234-266 .. code-block:: Python class Quartic(ot.PythonDistribution): """ Quartic (biweight) kernel See https://en.wikipedia.org/wiki/Kernel_(statistics)#Kernel_functions_in_common_use for more details """ def __init__(self): super(Quartic, self).__init__(1) self.c = 15.0 / 16 def computeCDF(self, x): u = x[0] if u <= -1: p = 0.0 elif u >= 1: p = 1.0 else: p = 0.5 + 15.0 / 16 * u - 5.0 / 8 * pow(u, 3) + 3.0 / 16 * pow(u, 5) return p def computePDF(self, x): u = x[0] if u < -1 or u > 1: y = 0.0 else: y = self.c * (1 - u**2) ** 2 return y def getRange(self): return ot.Interval(-1, 1) .. GENERATED FROM PYTHON SOURCE LINES 267-270 .. code-block:: Python Q = ot.Distribution(Quartic()) Q.setDescription(["Quartic Kernel"]) .. GENERATED FROM PYTHON SOURCE LINES 271-274 .. code-block:: Python graph = Q.drawPDF() view = viewer.View(graph) plt.show() .. image-sg:: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_006.png :alt: plot overview univariate distributions :srcset: /auto_probabilistic_modeling/distributions/images/sphx_glr_plot_overview_univariate_distributions_006.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_probabilistic_modeling_distributions_plot_overview_univariate_distributions.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_overview_univariate_distributions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_overview_univariate_distributions.py `