Create and draw scalar distributions

import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt

ot.Log.Show(ot.Log.NONE)

A continuous distribution

We build a Normal distribution with parameters:

\mu = 2.2, \sigma = 0.6

distribution = ot.Normal(2.2, 0.6)
print(distribution)
Normal(mu = 2.2, sigma = 0.6)

We can draw a sample following this distribution with the getSample method :

size = 10
sample = distribution.getSample(size)
print(sample)
    [ X0      ]
0 : [ 3.14485 ]
1 : [ 2.31703 ]
2 : [ 2.35843 ]
3 : [ 1.81332 ]
4 : [ 2.05531 ]
5 : [ 2.20004 ]
6 : [ 2.86632 ]
7 : [ 1.04108 ]
8 : [ 1.74157 ]
9 : [ 1.68863 ]

We draw its PDF and CDF :

graphPDF = distribution.drawPDF()
graphPDF.setTitle(
    r"PDF of a normal distribution with parameters $\mu = 2.2$ and $\sigma = 0.6$"
)
view = viewer.View(graphPDF)
PDF of a normal distribution with parameters $\mu = 2.2$ and $\sigma = 0.6$
graphCDF = distribution.drawCDF()
graphCDF.setTitle(
    r"CDF of a normal distribution with parameters $\mu = 2.2$ and $\sigma = 0.6$"
)
view = viewer.View(graphCDF)
CDF of a normal distribution with parameters $\mu = 2.2$ and $\sigma = 0.6$

A discrete distribution

We define a geometric distribution with parameter p = 0.7.

p = 0.7
distribution = ot.Geometric(p)
print(distribution)
Geometric(p = 0.7)

We draw a sample of it :

size = 10
sample = distribution.getSample(size)
print(sample)
    [ X0 ]
0 : [ 3  ]
1 : [ 2  ]
2 : [ 1  ]
3 : [ 1  ]
4 : [ 1  ]
5 : [ 1  ]
6 : [ 1  ]
7 : [ 2  ]
8 : [ 1  ]
9 : [ 1  ]

We draw its PDF and its CDF :

graphPDF = distribution.drawPDF()
graphPDF.setTitle(r"PDF of a geometric distribution with parameter $p = 0.7$")
view = viewer.View(graphPDF)
PDF of a geometric distribution with parameter $p = 0.7$
graphCDF = distribution.drawCDF()
graphCDF.setTitle(r"CDF of a geometric distribution with parameter $p = 0.7$")
view = viewer.View(graphCDF)
CDF of a geometric distribution with parameter $p = 0.7$

Conclusion

The two previous examples look very similar despite their continuous and discrete nature. In the library there is no distinction between continuous and discrete distributions.

Display all figures

plt.show()