File: plot_box_cox_transform.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (99 lines) | stat: -rw-r--r-- 3,274 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Use the Box-Cox transformation
==============================
"""

# %%
#
# The objective of this Use Case is to estimate a Box Cox transformation
# from a field which all values are positive (eventually after a shift
# to satisfy the positiveness) and to apply it on the field.
# The object :class:`~openturns.BoxCoxFactory` enables to create a factory of
# Box Cox transformation.
# Then, we estimate the Box Cox transformation
# :math:`h_{\vect{\lambda}}` from the initial field values
# :math:`(\vect{x}_0, \dots, \vect{x}_{N-1})` thanks to the method
# `build` of the object :class:`~openturns.BoxCoxFactory`, which produces an object of
# type :class:`~openturns.BoxCoxTransform`.
# If the field values :math:`(\vect{x}_0, \dots, \vect{x}_{N-1})` have
# some negative values, it is possible to translate the values with
# respect a given shift :math:`\vect{\alpha}` which has to be mentioned
# either at the creation of the object :class:`~openturns.BoxCoxFactory` or when using the
# method `build`.
# Then the Box Cox transformation is the composition of
# :math:`h_{\vect{\lambda}}` and this translation.
#
# The object :class:`~openturns.BoxCoxTransform` enables to:
#
# -  transform the field values
#    :math:`(\vect{x}_{0}, \dots,\vect{x}_{N-1})` of dimension :math:`d`
#    into the values :math:`(\vect{y}_{0}, \dots, \vect{y}_{N-1})` with
#    stabilized variance, such that for each vertex :math:`\vect{t}_i` we
#    have:
#
#    :math:`\vect{y}_{i} = h_{\vect{\lambda}}(\vect{x}_{i})`
#
#     or
#
#    :math:`\vect{y}_{i} = h_{\vect{\lambda}}(\vect{x}_{i} + \vect{\alpha})`
#
#    thanks to the operand (). The field based on the values
#    :math:`\vect{y}_{i}` shares the same mesh than the initial field.
#
# -  create the inverse Box Cox transformation such that :
#
#    :math:`\vect{x}_{i}= h^{-1}_{\vect{\lambda}}(\vect{y}_{i})`
#
#     or
#
#    :math:`\vect{x}_{i} = h^{-1}_{\vect{\lambda}}(\vect{y}_{i}) - \vect{\alpha}`
#
#    thanks to the method `getInverse()` which produces an object of type
#    :class:`~openturns.InverseBoxCoxTransform` that can be evaluated on a field.
#    The new field based shares the same mesh than the initial field.
#

# %%
import openturns as ot
import openturns.viewer as otv

# %%
# Define a process
myIndices = [10, 5]
myMesher = ot.IntervalMesher(myIndices)
myInterval = ot.Interval([0.0, 0.0], [2.0, 1.0])
myMesh = myMesher.build(myInterval)
amplitude = [1.0]
scale = [0.2, 0.2]
myCovModel = ot.ExponentialModel(scale, amplitude)
myXproc = ot.GaussianProcess(myCovModel, myMesh)
g = ot.SymbolicFunction(["x1"], ["exp(x1)"])
myDynTransform = ot.ValueFunction(g, myMesh)
myXtProcess = ot.CompositeProcess(myDynTransform, myXproc)

# %%
# Draw a field
field = myXtProcess.getRealization()
graph = field.drawMarginal(0)
view = otv.View(graph)

# %%
# Draw values
marginal = ot.HistogramFactory().build(field.getValues())
graph = marginal.drawPDF()
view = otv.View(graph)

# %%
# Build the transformed field through Box-Cox
myModelTransform = ot.BoxCoxFactory().build(field)
myStabilizedField = myModelTransform(field)

# %%
# Draw values
marginal = ot.HistogramFactory().build(myStabilizedField.getValues())
graph = marginal.drawPDF()
view = otv.View(graph)

# %%
# Display all figures
otv.View.ShowAll()