File: plot_bayes_distribution.py

package info (click to toggle)
openturns 1.24-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,204 kB
  • sloc: cpp: 256,662; python: 63,381; ansic: 4,414; javascript: 406; sh: 180; xml: 164; yacc: 123; makefile: 98; lex: 55
file content (57 lines) | stat: -rw-r--r-- 1,143 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Create a Bayes distribution
===========================
"""

# %%
# In this example we are going to build the distribution of the random vector
#
# .. math::
#    (Y, \vect{X}|\vect{\Theta})
#
# with :math:`\vect{X}` conditioned by the random vector :math:`\vect{\Theta}` obtained with the random variable :math:`Y` through a function :math:`f`
#
# .. math::
#    \vect{\Theta}=f(Y)
#

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

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

# %%
# Create the :math:`Y` distribution

YDist = ot.Normal(0.0, 1.0)

# %%
# Create :math:`\vect{\Theta}=f(Y)`
f = ot.SymbolicFunction(["y"], ["y", "0.1 + y^2"])

# %%
# Create the :math:`\vect{X}|\vect{\Theta}` distribution

XgivenThetaDist = ot.Normal()

# %%
# create the distribution
XDist = ot.JointByConditioningDistribution(XgivenThetaDist, YDist, f)
XDist.setDescription(["X|Theta=f(y)", "y"])
XDist

# %%
# Get a sample
sample = XDist.getSample(100)

# %%
# Draw PDF
graph = XDist.drawPDF()
cloud = ot.Cloud(sample)
cloud.setColor("red")
cloud.setLegend("sample")
graph.add(cloud)
view = viewer.View(graph)
plt.show()