.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_probabilistic_modeling_stochastic_processes_plot_box_cox_transform.py:
Apply a Box-Cox transformation to a Field
=========================================
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 *BoxCoxFactory* enables to create a factory of
Box Cox transformation.
Then, we estimate the Box Cox transformation
:math:`h_{\underline{\lambda}}` from the initial field values
:math:`(\underline{x}_0, \dots, \underline{x}_{N-1})` thanks to the method
*build* of the object *BoxCoxFactory*, which produces an object of
type *BoxCoxTransform*.
If the field values :math:`(\underline{x}_0, \dots, \underline{x}_{N-1})` have
some negative values, it is possible to translate the values with
respect a given shift :math:`\underline{\alpha}` which has to be mentioned
either at the creation of the object *BoxCoxFactory* or when using the
method *build*.
Then the Box Cox transformation is the composition of
:math:`h_{\underline{\lambda}}` and this translation.
The object *BoxCoxTransform* enables to:
- transform the field values
:math:`(\underline{x}_{0}, \dots,\underline{x}_{N-1})` of dimension :math:`d`
into the values :math:`(\underline{y}_{0}, \dots, \underline{y}_{N-1})` with
stabilized variance, such that for each vertex :math:`\underline{t}_i` we
have:
:math:`\underline{y}_{i} = h_{\underline{\lambda}}(\underline{x}_{i})`
or
:math:`\underline{y}_{i} = h_{\underline{\lambda}}(\underline{x}_{i} + \underline{\alpha})`
thanks to the operand *()*. The field based on the values
:math:`\underline{y}_{i}` shares the same mesh than the initial field.
- create the inverse Box Cox transformation such that :
:math:`\underline{x}_{i}= h^{-1}_{\underline{\lambda}}(\underline{y}_{i})`
or
:math:`\underline{x}_{i} = h^{-1}_{\underline{\lambda}}(\underline{y}_{i}) - \underline{\alpha}`
thanks to the method *getInverse()* which produces an object of type
*InverseBoxCoxTransform* that can be evaluated on a field.
The new field based shares the same mesh than the initial field.
.. code-block:: default
from __future__ import print_function
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt
ot.Log.Show(ot.Log.NONE)
Define a process
.. code-block:: default
myIndices= ot.Indices([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
.. code-block:: default
field = myXtProcess.getRealization()
graph = field.drawMarginal(0)
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_box_cox_transform_001.png
:alt: Unnamed - 0 marginal
:class: sphx-glr-single-img
Draw values
.. code-block:: default
marginal = ot.HistogramFactory().build(field.getValues())
graph = marginal.drawPDF()
view = viewer.View(graph)
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_box_cox_transform_002.png
:alt: y0 PDF
:class: sphx-glr-single-img
Build the transformed field through Box-Cox
.. code-block:: default
myModelTransform = ot.BoxCoxFactory().build(field)
myStabilizedField = myModelTransform(field)
Draw values
.. code-block:: default
marginal = ot.HistogramFactory().build(myStabilizedField.getValues())
graph = marginal.drawPDF()
view = viewer.View(graph)
plt.show()
.. image:: /auto_probabilistic_modeling/stochastic_processes/images/sphx_glr_plot_box_cox_transform_003.png
:alt: y0 PDF
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 1.174 seconds)
.. _sphx_glr_download_auto_probabilistic_modeling_stochastic_processes_plot_box_cox_transform.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_box_cox_transform.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_box_cox_transform.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_