import openturns as ot
from matplotlib import pyplot as plt
from openturns.viewer import View

distX = ot.Normal(0.0, 10.0)
myFunc = ot.NumericalMathFunction('x', 'x+sin(x)')
distFin = ot.CompositeDistribution(myFunc, distX)

graphPDF = distFin.drawPDF(1024)
graphPDF.setXTitle('x')
graphPDF.setLegendPosition('')
graphCDF = distFin.drawCDF(1024)
graphCDF.setXTitle('x')
graphCDF.setLegendPosition('')

fig = plt.figure(figsize=(8, 4))
plt.suptitle(
    "CompositeDistribution: f(x)=x+sin(x); L=Normal(0.0, 10.0): pdf and cdf")
pdf_axis = fig.add_subplot(121)
cdf_axis = fig.add_subplot(122)
pdf_axis.set_xlim(auto=True)
cdf_axis.set_xlim(auto=True)

View(graphPDF, figure=fig, axes=[pdf_axis], add_legend=True)
View(graphCDF, figure=fig, axes=[cdf_axis], add_legend=True)
# %%
N = ot.Normal(0.0, 1.0)
N.setDescription(["Normal"])

# %%
# Secondly, we create a function.

# %%
f = ot.SymbolicFunction(['x'], ['exp(x)'])
f.setDescription(["X", "Exp(X)"])

# %%
# Finally, we create the distribution equal to the exponential of the gaussian random variable.

# %%
dist = ot.CompositeDistribution(f, N)

# %%
graph = dist.drawPDF()
graph.setTitle("Exponential of a gaussian random variable")
view = viewer.View(graph)

# %%
# In order to check the previous distribution, we compare it with the LogNormal distribution.

# %%
LN = ot.LogNormal()
LN.setDescription(["LogNormal"])
graph = LN.drawPDF()
view = viewer.View(graph)
# %%
# First, we import the useful librairies and we create the symbolic function :math:`G`.

# %%
import openturns as ot
from openturns.viewer import View

# %%
# Then, we create the :math:`G` function with :math:`\rho = 2.0`. To do this, we create a function which takes both :math:`y` and :math:`\rho` as inputs and returns :math:`G(u)`. Then the `g` function is defined as a `ParametricFunction` with a fixed value of :math:`\rho`.

# %%
gWithParameter = ot.SymbolicFunction(["u", "rho"], ["log(-log(u)) / log(rho)"])
rho = 2.0
g = ot.ParametricFunction(gWithParameter, [1], [rho])

# %%
# We define the distribution distF as the image through :math:`G` of the Uniform(0,1) distribution:

# %%
distF = ot.CompositeDistribution(g, ot.Uniform(0.0, 1.0))

# %%
# Now, we can draw its pdf, cdf, sample it,...

# %%
g = distF.drawPDF()
g.setTitle("A distribution based on the quantile function.")
g.setLegendPosition("")
view = View(g)
view.ShowAll()
Example #4
0
#  - `cbrt`.
#
#
# For example for the usual `log` transformation :

# %%
graph = distribution1.log().drawPDF()
view = viewer.View(graph)

# %%
# And for the `log2` function :

# %%
f = ot.SymbolicFunction(['x'], ['log2(x)'])
f.setDescription(["X", "ln(X)"])
graph = ot.CompositeDistribution(f, distribution1).drawPDF()
view = viewer.View(graph)

# %%
# If one wants a specific method, user might rely on the :class:`~openturns.CompositeDistribution` class.

# 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)
#
#  - `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:

# %%
graph =distribution1.log().drawPDF()
view = viewer.View(graph)

# %%
# And for the `log2` function:

# %%
f = ot.SymbolicFunction(['x'], ['log2(x)'])
f.setDescription(["X","ln(X)"])
graph = ot.CompositeDistribution(f, distribution1).drawPDF()
view = viewer.View(graph)
plt.show()
Example #6
0
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)

# %%
# create an 1-d distribution
antecedent = ot.Normal()

# %%
# Create an 1-d transformation
f = ot.SymbolicFunction(['x'], ['sin(x)+cos(x)'])

# %%
# Create the composite distribution
distribution = ot.CompositeDistribution(f, antecedent)
graph = distribution.drawPDF()
view = viewer.View(graph)

# %%
# Using the simplified construction
distribution = antecedent.exp()
graph = distribution.drawPDF()
view = viewer.View(graph)

# %%
# Using chained operators
distribution = antecedent.abs().sqrt()
graph = distribution.drawPDF()
view = viewer.View(graph)
plt.show()