.. 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_extrema.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_numerical_methods_iterative_statistics_plot_iterative_extrema.py: Estimate extrema iteratively ============================ .. GENERATED FROM PYTHON SOURCE LINES 7-8 In this example, we compute extrema iteratively. .. GENERATED FROM PYTHON SOURCE LINES 10-13 .. code-block:: Python import openturns as ot import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 14-15 We first create a one-dimensional Uniform random variable to generate data. .. GENERATED FROM PYTHON SOURCE LINES 15-18 .. code-block:: Python dim = 1 distNormal = ot.Uniform() .. GENERATED FROM PYTHON SOURCE LINES 19-21 The :class:`~openturns.IterativeExtrema` class needs the dimension of the sample (here 1): .. GENERATED FROM PYTHON SOURCE LINES 23-25 .. code-block:: Python iterExtrema = ot.IterativeExtrema(dim) .. GENERATED FROM PYTHON SOURCE LINES 26-36 We can now perform the simulations. In our case most of the data should be in the [-3,3] interval. Consequently with few samples the expected minimum should be around -3 and the expected maximum should be around 3. We first increment the object with one :class:`~openturns.Point` at a time. At any given step the current minimum is obtained thanks to the :meth:`~openturns.IterativeExtrema.getMin` method, the current maximum with the :meth:`~openturns.IterativeExtrema.getMax` method and the current number of iterations is given by the :meth:`~openturns.IterativeMoments.getIterationNumber` method. .. GENERATED FROM PYTHON SOURCE LINES 38-47 .. code-block:: Python size = 2000 minEvolution = ot.Sample() maxEvolution = ot.Sample() for i in range(size): point = distNormal.getRealization() iterExtrema.increment(point) minEvolution.add(iterExtrema.getMin()) maxEvolution.add(iterExtrema.getMax()) .. GENERATED FROM PYTHON SOURCE LINES 48-49 We display the evolution of the minimum (in blue) and the maximum (orange). .. GENERATED FROM PYTHON SOURCE LINES 49-64 .. code-block:: Python iterationSample = ot.Sample.BuildFromPoint(range(1, size + 1)) # curveMin = ot.Curve(iterationSample, minEvolution) curveMin.setLegend("min.") # curveMax = ot.Curve(iterationSample, maxEvolution) curveMax.setLegend("max.") # graph = ot.Graph("Evolution of the min/max", "iteration nb", "min/max", True) graph.add(curveMin) graph.add(curveMax) graph.setLegendPosition("upper left") graph.setLogScale(ot.GraphImplementation.LOGX) view = otv.View(graph) .. image-sg:: /auto_numerical_methods/iterative_statistics/images/sphx_glr_plot_iterative_extrema_001.png :alt: Evolution of the min/max :srcset: /auto_numerical_methods/iterative_statistics/images/sphx_glr_plot_iterative_extrema_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-66 We can also increment with a :class:`~openturns.Sample`. .. GENERATED FROM PYTHON SOURCE LINES 68-71 .. code-block:: Python sample = distNormal.getSample(size) iterExtrema.increment(sample) .. GENERATED FROM PYTHON SOURCE LINES 72-73 We print the total number of iterations and the extrema. .. GENERATED FROM PYTHON SOURCE LINES 73-78 .. code-block:: Python print("Total number of iterations: " + str(iterExtrema.getIterationNumber())) print("Minimum: ", iterExtrema.getMin()) print("Maximum: ", iterExtrema.getMax()) otv.View.ShowAll() .. rst-class:: sphx-glr-script-out .. code-block:: none Total number of iterations: 4000 Minimum: [-0.999998] Maximum: [0.999377] .. _sphx_glr_download_auto_numerical_methods_iterative_statistics_plot_iterative_extrema.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_iterative_extrema.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_iterative_extrema.py `