.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_reliability_sensitivity/central_dispersion/plot_estimate_moments_taylor.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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. .. GENERATED FROM PYTHON SOURCE LINES 8-10 Model definition ---------------- .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python import openturns as ot import openturns.viewer as viewer import numpy as np from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 20-21 Create a composite random vector .. GENERATED FROM PYTHON SOURCE LINES 21-34 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 35-37 Taylor expansion based estimation of the moments ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 39-40 We create a Taylor expansion method to approximate moments .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python taylor = ot.TaylorExpansionMoments(Y) .. GENERATED FROM PYTHON SOURCE LINES 43-45 Analysis of the results ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 47-48 get 1st order mean .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python print(taylor.getMeanFirstOrder()) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.932544] .. GENERATED FROM PYTHON SOURCE LINES 51-52 get 2nd order mean .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python print(taylor.getMeanSecondOrder()) .. rst-class:: sphx-glr-script-out .. code-block:: none [0.820295] .. GENERATED FROM PYTHON SOURCE LINES 55-56 get covariance .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python print(taylor.getCovariance()) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 0.0124546 ]] .. GENERATED FROM PYTHON SOURCE LINES 59-60 draw importance factors .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python taylor.getImportanceFactors() .. raw:: html
class=PointWithDescription name=Unnamed dimension=4 description=[x1,x2,x3,x4] values=[0.181718,0.0430356,0.0248297,0.750417]


.. GENERATED FROM PYTHON SOURCE LINES 63-64 draw importance factors .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python graph = taylor.drawImportanceFactors() view = viewer.View(graph) .. image-sg:: /auto_reliability_sensitivity/central_dispersion/images/sphx_glr_plot_estimate_moments_taylor_001.png :alt: Importance Factors from Taylor expansions - y0 :srcset: /auto_reliability_sensitivity/central_dispersion/images/sphx_glr_plot_estimate_moments_taylor_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-69 Get the value of the output at the mean point .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python taylor.getValueAtMean() .. raw:: html
class=Point name=Unnamed dimension=1 values=[0.932544]


.. GENERATED FROM PYTHON SOURCE LINES 72-73 Get the gradient value of the output at the mean point .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python taylor.getGradientAtMean() .. raw:: html

[[ -0.35812 ]
[ -0.0912837 ]
[ -0.0286496 ]
[ -0.228209 ]]



.. GENERATED FROM PYTHON SOURCE LINES 76-77 Get the hessian value of the output at the mean point .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python taylor.getHessianAtMean() plt.show() .. GENERATED FROM PYTHON SOURCE LINES 81-83 Using finite difference gradients --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 85-88 When no gradient and/or functions are provided for the model, a finite difference approach is relied on automatically. However, it is possible to manually specify the characteristic of the considered difference steps. .. GENERATED FROM PYTHON SOURCE LINES 88-97 .. code-block:: Python def myPythonFunction(X): x1, x2, x3, x4 = X return [np.cos(x2 * x2 + x4) / (x1 * x1 + 1.0 + x3**4)] myFunc = ot.PythonFunction(4, 1, myPythonFunction) .. GENERATED FROM PYTHON SOURCE LINES 98-99 For instance, a user-defined constant step value can be considered .. GENERATED FROM PYTHON SOURCE LINES 99-110 .. code-block:: Python gradEpsilon = [1e-8] * 4 hessianEpsilon = [1e-7] * 4 gradStep = ot.ConstantStep(gradEpsilon) # Costant gradient step hessianStep = ot.ConstantStep(hessianEpsilon) # Constant Hessian step myFunc.setGradient( ot.CenteredFiniteDifferenceGradient(gradStep, myFunc.getEvaluation()) ) myFunc.setHessian( ot.CenteredFiniteDifferenceHessian(hessianStep, myFunc.getEvaluation()) ) .. GENERATED FROM PYTHON SOURCE LINES 111-113 Alternatively, we can consider a finite difference step value which depends on the location in the input space by relying on the BlendedStep class: .. GENERATED FROM PYTHON SOURCE LINES 113-124 .. code-block:: Python gradEpsilon = [1e-8] * 4 hessianEpsilon = [1e-7] * 4 gradStep = ot.BlendedStep(gradEpsilon) # Costant gradient step hessianStep = ot.BlendedStep(hessianEpsilon) # Constant Hessian step myFunc.setGradient( ot.CenteredFiniteDifferenceGradient(gradStep, myFunc.getEvaluation()) ) myFunc.setHessian( ot.CenteredFiniteDifferenceHessian(hessianStep, myFunc.getEvaluation()) ) .. GENERATED FROM PYTHON SOURCE LINES 125-126 We can then proceed in the same way as before .. GENERATED FROM PYTHON SOURCE LINES 126-128 .. code-block:: Python Y = ot.CompositeRandomVector(myFunc, X) taylor = ot.TaylorExpansionMoments(Y) .. _sphx_glr_download_auto_reliability_sensitivity_central_dispersion_plot_estimate_moments_taylor.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_estimate_moments_taylor.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_estimate_moments_taylor.py `