.. 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-9 Abstract -------- We introduce the :class:`ExpectationSimulationAlgorithm` class which implements an incremental Monte Carlo sampling algorithm to estimate the mean of a random vector. .. GENERATED FROM PYTHON SOURCE LINES 9-13 .. code-block:: Python import openturns as ot import openturns.viewer as otv from matplotlib import pylab as plt .. GENERATED FROM PYTHON SOURCE LINES 14-16 We shall use this algorithm for the :ref:`Ishigami function ` that we load from the `usecases` module : .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python from openturns.usecases import ishigami_function im = ishigami_function.IshigamiModel() .. GENERATED FROM PYTHON SOURCE LINES 22-24 The Ishigami `model` and the distribution of the input variables are stored in the `im` object : .. GENERATED FROM PYTHON SOURCE LINES 24-27 .. code-block:: Python model = im.model distribution = im.distributionX .. GENERATED FROM PYTHON SOURCE LINES 28-29 We create a random vector that follows the distribution of the input variables. .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python inputVector = ot.RandomVector(distribution) .. GENERATED FROM PYTHON SOURCE LINES 32-33 The output vector is a :class:`~openturns.CompositeRandomVector`. .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: Python outputVector = ot.CompositeRandomVector(model, inputVector) .. GENERATED FROM PYTHON SOURCE LINES 36-37 The mean of the output vector is .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. 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 41-42 We define the algorithm simply by calling it with the output vector : .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: Python algo = ot.ExpectationSimulationAlgorithm(outputVector) .. GENERATED FROM PYTHON SOURCE LINES 45-46 We can also set the algorithm parameters : .. GENERATED FROM PYTHON SOURCE LINES 46-51 .. code-block:: Python algo.setMaximumOuterSampling(80000) algo.setBlockSize(1) algo.setCoefficientOfVariationCriterionType("NONE") .. GENERATED FROM PYTHON SOURCE LINES 52-53 We are then ready to launch the algorithm and store the result. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python algo.run() result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 58-59 As usual for Monte Carlo estimation we can draw the convergence history. .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. 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 64-66 The result obtained with the previous algorithm is an instance of the :class:~openturns.ExpectationSimulationResult` class. .. GENERATED FROM PYTHON SOURCE LINES 68-69 The expected value of the mean is given by the `getExpectationEstimate` method : .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. 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 73-74 The variance and standard deviation of the estimated mean are respectively given by `getVarianceEstimate` and `getStandardDeviation`: .. GENERATED FROM PYTHON SOURCE LINES 74-82 .. 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 83-84 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 84-88 .. 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 89-90 The asymptotic confidence distribution of the output random vector mean estimate is .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. 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 94-95 Let us draw it: .. GENERATED FROM PYTHON SOURCE LINES 95-101 .. 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 102-103 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 103-104 .. 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 `