.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_numerical_methods/iterative_statistics/plot_iterative_moments.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_numerical_methods_iterative_statistics_plot_iterative_moments.py: Estimate moments iteratively ============================ .. GENERATED FROM PYTHON SOURCE LINES 7-12 In this example, we use the :class:`~openturns.IterativeMoments` class to compute iterative statistics. This class stores centered moments up to a prescribed order iteratively. Then several statistics based on the moments are available depending on the chosen order. .. GENERATED FROM PYTHON SOURCE LINES 14-17 .. code-block:: default import openturns as ot import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 18-19 We first create a one-dimensional Gaussian random variable to generate data. .. GENERATED FROM PYTHON SOURCE LINES 19-22 .. code-block:: default dim = 1 distNormal = ot.Normal(dim) .. GENERATED FROM PYTHON SOURCE LINES 23-26 Then we use the centered moments up to order 4 with the :class:`~openturns.IterativeMoments` class by giving the order (here 4) and the dimension (here 1): .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: default order = 4 iterMoments = ot.IterativeMoments(order, dim) .. GENERATED FROM PYTHON SOURCE LINES 33-41 We can now perform the simulations. The :class:`~openturns.IterativeMoments` object stores the centered moments iteratively. We first increment the object with one :class:`~openturns.Point` at a time. At any given step the current mean is obtained thanks to the :meth:`~openturns.IterativeMoments.getMean` method and the current number of iterations is given by the :meth:`~openturns.IterativeMoments.getIterationNumber` method. .. GENERATED FROM PYTHON SOURCE LINES 41-48 .. code-block:: default size = 2000 meanEvolution = ot.Sample() for i in range(size): point = distNormal.getRealization() iterMoments.increment(point) meanEvolution.add(iterMoments.getMean()) .. GENERATED FROM PYTHON SOURCE LINES 49-50 We display the evolution of the mean. .. GENERATED FROM PYTHON SOURCE LINES 50-57 .. code-block:: default iterationSample = ot.Sample.BuildFromPoint(range(1, size + 1)) curve = ot.Curve(iterationSample, meanEvolution) graph = ot.Graph("Evolution of the mean", "iteration nb", "mean", True) graph.add(curve) graph.setLogScale(ot.GraphImplementation.LOGX) view = otv.View(graph) .. image-sg:: /auto_numerical_methods/iterative_statistics/images/sphx_glr_plot_iterative_moments_001.png :alt: Evolution of the mean :srcset: /auto_numerical_methods/iterative_statistics/images/sphx_glr_plot_iterative_moments_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 58-59 We can also increment with a :class:`~openturns.Sample`. .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: default sample = distNormal.getSample(size) iterMoments.increment(sample) .. GENERATED FROM PYTHON SOURCE LINES 65-66 We print the total number of iterations and the mean. .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: default print("Total number of iteration: ", iterMoments.getIterationNumber()) print("Mean: ", iterMoments.getMean()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Total number of iteration: 4000 Mean: [-0.0137755] .. GENERATED FROM PYTHON SOURCE LINES 70-74 For the order of the `iterMoments` object is 4, we also have access to other statistics such as the variance (order 2), the skewness (order 3) or the kurtosis (order 4). For instance, a specified order of 3 would leave only the variance and the skewness available. .. GENERATED FROM PYTHON SOURCE LINES 74-79 .. code-block:: default print("Variance: ", iterMoments.getVariance()) print("Skewness: ", iterMoments.getSkewness()) print("Kurtosis: ", iterMoments.getKurtosis()) otv.View.ShowAll() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Variance: [1.01255] Skewness: [0.00849145] Kurtosis: [3.02643] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.165 seconds) .. _sphx_glr_download_auto_numerical_methods_iterative_statistics_plot_iterative_moments.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_iterative_moments.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_iterative_moments.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_