.. 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_expectation_simulation_algorithm.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_expectation_simulation_algorithm.py: Evaluate the mean of a random vector by simulations =================================================== .. GENERATED FROM PYTHON SOURCE LINES 6-10 Abstract -------- We introduce the :class:`~openturns.ExpectationSimulationAlgorithm` class which implements an incremental Monte Carlo sampling algorithm to estimate the mean of a random vector. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: Python import openturns as ot import openturns.viewer as otv from matplotlib import pylab as plt .. GENERATED FROM PYTHON SOURCE LINES 15-17 We shall use this algorithm for the :ref:`Ishigami function ` that we load from the `usecases` module : .. GENERATED FROM PYTHON SOURCE LINES 17-22 .. code-block:: Python from openturns.usecases import ishigami_function im = ishigami_function.IshigamiModel() .. GENERATED FROM PYTHON SOURCE LINES 23-25 The Ishigami `model` and the distribution of the input variables are stored in the `im` object : .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python model = im.model distribution = im.distributionX .. GENERATED FROM PYTHON SOURCE LINES 29-30 We create a random vector that follows the distribution of the input variables. .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python inputVector = ot.RandomVector(distribution) .. GENERATED FROM PYTHON SOURCE LINES 33-34 The output vector is a :class:`~openturns.CompositeRandomVector`. .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python outputVector = ot.CompositeRandomVector(model, inputVector) .. GENERATED FROM PYTHON SOURCE LINES 37-38 The mean of the output vector is .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python print("Mean of the output random vector : %.5f" % im.expectation) .. rst-class:: sphx-glr-script-out .. code-block:: none Mean of the output random vector : 3.50000 .. GENERATED FROM PYTHON SOURCE LINES 42-43 We define the algorithm simply by calling it with the output vector : .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: Python algo = ot.ExpectationSimulationAlgorithm(outputVector) .. GENERATED FROM PYTHON SOURCE LINES 46-47 We can also set the algorithm parameters : .. GENERATED FROM PYTHON SOURCE LINES 47-52 .. code-block:: Python algo.setMaximumOuterSampling(80000) algo.setBlockSize(1) algo.setCoefficientOfVariationCriterionType("NONE") .. GENERATED FROM PYTHON SOURCE LINES 53-54 We are then ready to launch the algorithm and store the result. .. GENERATED FROM PYTHON SOURCE LINES 54-58 .. code-block:: Python algo.run() result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 59-60 As usual for Monte Carlo estimation we can draw the convergence history. .. GENERATED FROM PYTHON SOURCE LINES 60-64 .. code-block:: Python graphConvergence = algo.drawExpectationConvergence() view = otv.View(graphConvergence) .. image-sg:: /auto_reliability_sensitivity/central_dispersion/images/sphx_glr_plot_expectation_simulation_algorithm_001.png :alt: Expectation convergence graph at level 0.95 :srcset: /auto_reliability_sensitivity/central_dispersion/images/sphx_glr_plot_expectation_simulation_algorithm_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-67 The result obtained with the previous algorithm is an instance of the :class:`~openturns.ExpectationSimulationResult` class. .. GENERATED FROM PYTHON SOURCE LINES 69-70 The expected value of the mean is given by the `getExpectationEstimate` method : .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python expectation = result.getExpectationEstimate() print("Estimated mean of the output random vector : %.5f" % expectation[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none Estimated mean of the output random vector : 3.50261 .. GENERATED FROM PYTHON SOURCE LINES 74-75 The variance and standard deviation of the estimated mean are respectively given by `getVarianceEstimate` and `getStandardDeviation`: .. GENERATED FROM PYTHON SOURCE LINES 75-83 .. code-block:: Python expectationVariance = result.getVarianceEstimate() print( "Variance of the estimated mean of the output random vector : %.5f" % expectationVariance[0] ) standardDeviation = result.getStandardDeviation() print("Standard deviation : %.5f" % standardDeviation[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none Variance of the estimated mean of the output random vector : 0.00017 Standard deviation : 0.01316 .. GENERATED FROM PYTHON SOURCE LINES 84-85 This variance and this standard deviation must not to be confused with the variance and the standard deviation of the Ishigami model! .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. code-block:: Python print("Ishigami variance : %.5f" % im.variance) print("Ishigami standard deviation : %.5f" % im.variance ** (1 / 2)) .. rst-class:: sphx-glr-script-out .. code-block:: none Ishigami variance : 13.84459 Ishigami standard deviation : 3.72083 .. GENERATED FROM PYTHON SOURCE LINES 90-91 The asymptotic confidence distribution of the output random vector mean estimate is .. GENERATED FROM PYTHON SOURCE LINES 91-94 .. code-block:: Python expectationDistribution = result.getExpectationDistribution() print(expectationDistribution) .. rst-class:: sphx-glr-script-out .. code-block:: none Normal(mu = 3.50261, sigma = 0.0131575) .. GENERATED FROM PYTHON SOURCE LINES 95-96 Let us draw it: .. GENERATED FROM PYTHON SOURCE LINES 96-102 .. code-block:: Python graphExpectationDistribution = expectationDistribution.drawPDF() graphExpectationDistribution.setTitle( "Normal asymptotic distribution of the mean estimate" ) view = otv.View(graphExpectationDistribution) .. image-sg:: /auto_reliability_sensitivity/central_dispersion/images/sphx_glr_plot_expectation_simulation_algorithm_002.png :alt: Normal asymptotic distribution of the mean estimate :srcset: /auto_reliability_sensitivity/central_dispersion/images/sphx_glr_plot_expectation_simulation_algorithm_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 103-104 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 104-105 .. code-block:: Python plt.show() .. _sphx_glr_download_auto_reliability_sensitivity_central_dispersion_plot_expectation_simulation_algorithm.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_expectation_simulation_algorithm.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_expectation_simulation_algorithm.py `