.. 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 Click :ref:`here ` 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:: default 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-20 .. code-block:: default from openturns.usecases import ishigami_function as ishigami_function im = ishigami_function.IshigamiModel() .. GENERATED FROM PYTHON SOURCE LINES 21-23 The Ishigami `model` and the distribution of the input variables are stored in the `im` object : .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: default model = im.model distribution = im.distributionX .. GENERATED FROM PYTHON SOURCE LINES 27-28 We create a random vector that follows the distribution of the input variables. .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: default inputVector = ot.RandomVector(distribution) .. GENERATED FROM PYTHON SOURCE LINES 31-32 The output vector is a :class:`~openturns.CompositeRandomVector`. .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: default outputVector = ot.CompositeRandomVector(model, inputVector) .. GENERATED FROM PYTHON SOURCE LINES 35-36 The mean of the output vector is .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: default print("Mean of the output random vector : %.5f" % im.expectation) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Mean of the output random vector : 3.50000 .. GENERATED FROM PYTHON SOURCE LINES 40-41 We define the algorithm simply by calling it with the output vector : .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: default algo = ot.ExpectationSimulationAlgorithm(outputVector) .. GENERATED FROM PYTHON SOURCE LINES 44-45 We can also set the algorithm parameters : .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: default algo.setMaximumOuterSampling(80000) algo.setBlockSize(1) algo.setCoefficientOfVariationCriterionType('NONE') .. GENERATED FROM PYTHON SOURCE LINES 51-52 We are then ready to launch the algorithm and store the result. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: default algo.run() result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 57-58 As usual for Monte Carlo estimation we can draw the convergence history. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: default 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 63-65 The result obtained with the previous algorithm is an instance of the :class:~openturns.ExpectationSimulationResult` class. .. GENERATED FROM PYTHON SOURCE LINES 67-68 The expected value of the mean is given by the `getExpectationEstimate` method : .. GENERATED FROM PYTHON SOURCE LINES 68-71 .. code-block:: default expectation = result.getExpectationEstimate() print("Estimated mean of the output random vector : %.5f" % expectation[0]) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Estimated mean of the output random vector : 3.49442 .. GENERATED FROM PYTHON SOURCE LINES 72-73 The variance and standard deviation of the estimated mean are respectively given by `getVarianceEstimate` and `getStandardDeviation`: .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: default 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 Out: .. code-block:: none Variance of the estimated mean of the output random vector : 0.00017 Standard deviation : 0.01311 .. GENERATED FROM PYTHON SOURCE LINES 80-81 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 81-85 .. code-block:: default print("Ishigami variance : %.5f" % im.variance) print("Ishigami standard deviation : %.5f" % im.variance**(1/2)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Ishigami variance : 13.84459 Ishigami standard deviation : 3.72083 .. GENERATED FROM PYTHON SOURCE LINES 86-87 The asymptotic confidence distribution of the output random vector mean estimate is .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default expectationDistribution = result.getExpectationDistribution() print(expectationDistribution) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Normal(mu = 3.49442, sigma = 0.0131128) .. GENERATED FROM PYTHON SOURCE LINES 91-92 Let us draw it: .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: default 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 98-99 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 99-100 .. code-block:: default plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.648 seconds) .. _sphx_glr_download_auto_reliability_sensitivity_central_dispersion_plot_expectation_simulation_algorithm.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_expectation_simulation_algorithm.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_expectation_simulation_algorithm.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_