` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_reliability_sensitivity_central_dispersion_plot_estimate_moments_taylor.py:
Estimate moments from Taylor expansions
=======================================
In this example we are going to estimate mean and standard deviation of an output variable of interest thanks to the Taylor variance decomposition method of order 1 or 2.
.. 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)
Create a composite random vector
.. code-block:: default
ot.RandomGenerator.SetSeed(0)
input_names = ['x1', 'x2', 'x3', 'x4']
myFunc = ot.SymbolicFunction(input_names,
['cos(x2*x2+x4)/(x1*x1+1+x3^4)'])
R = ot.CorrelationMatrix(4)
for i in range(4):
R[i, i - 1] = 0.25
distribution = ot.Normal([0.2]*4, [0.1, 0.2, 0.3, 0.4], R)
distribution.setDescription(input_names)
# We create a distribution-based RandomVector
X = ot.RandomVector(distribution)
# We create a composite RandomVector Y from X and myFunc
Y = ot.CompositeRandomVector(myFunc, X)
We create a Taylor expansion method to approximate moments
.. code-block:: default
taylor = ot.TaylorExpansionMoments(Y)
get 1st order mean
.. code-block:: default
print(taylor.getMeanFirstOrder())
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[0.932544]
get 2nd order mean
.. code-block:: default
print(taylor.getMeanSecondOrder())
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[0.820295]
get covariance
.. code-block:: default
print(taylor.getCovariance())
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[[ 0.0124546 ]]
draw importance factors
.. code-block:: default
taylor.getImportanceFactors()
.. raw:: html
[x1 : 0.181718, x2 : 0.0430356, x3 : 0.0248297, x4 : 0.750417]
draw importance factors
.. code-block:: default
graph = taylor.drawImportanceFactors()
view = viewer.View(graph)
.. image:: /auto_reliability_sensitivity/central_dispersion/images/sphx_glr_plot_estimate_moments_taylor_001.png
:alt: Importance Factors from Taylor expansions - y0
:class: sphx-glr-single-img
Get the value of the output at the mean point
.. code-block:: default
taylor.getValueAtMean()
.. raw:: html
[0.932544]
Get the gradient value of the output at the mean point
.. code-block:: default
taylor.getGradientAtMean()
.. raw:: html
[[ -0.35812 ]
[ -0.0912837 ]
[ -0.0286496 ]
[ -0.228209 ]]
Get the hessian value of the output at the mean point
.. code-block:: default
taylor.getHessianAtMean()
plt.show()
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.047 seconds)
.. _sphx_glr_download_auto_reliability_sensitivity_central_dispersion_plot_estimate_moments_taylor.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_estimate_moments_taylor.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_estimate_moments_taylor.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_