.. 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_threshold.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_threshold.py: Estimate threshold exceedance iteratively ========================================= .. GENERATED FROM PYTHON SOURCE LINES 7-9 In this example, we use the :class:`~openturns.IterativeThresholdExceedance` class to count the number of threshold exceedances. .. GENERATED FROM PYTHON SOURCE LINES 11-14 .. code-block:: Python import openturns as ot import openturns.viewer as otv .. GENERATED FROM PYTHON SOURCE LINES 15-16 We first create a one-dimensional Gaussian random variable to generate data. .. GENERATED FROM PYTHON SOURCE LINES 16-19 .. code-block:: Python dim = 1 distNormal = ot.Normal(dim) .. GENERATED FROM PYTHON SOURCE LINES 20-23 Let us consider a threshold value of 1.0. Each data value higher than 1.0 is counted as one exceedance. The counter used by the :class:`~openturns.IterativeThresholdExceedance` class is updated iteratively. .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python threshold = 1.0 algo = ot.IterativeThresholdExceedance(dim, ot.Greater(), threshold) .. GENERATED FROM PYTHON SOURCE LINES 29-30 A simple computation shows that the probability of the data being higher than :math:`1` is :math:`0.1587` (with 4 significant digits). .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python distribution = ot.Normal() exactProbability = distribution.computeComplementaryCDF(threshold) print("Exact probability: ", exactProbability) .. rst-class:: sphx-glr-script-out .. code-block:: none Exact probability: 0.15865525393145702 .. GENERATED FROM PYTHON SOURCE LINES 35-38 We can now perform the simulations. In our case most of the data fall below the specified threshold value so the number of exceedances should be low. .. GENERATED FROM PYTHON SOURCE LINES 40-43 We first increment the object one :class:`~openturns.Point` at a time. At any given step the current number of exceedance is obtained with the :meth:`~openturns.IterativeThresholdExceedance.getThresholdExceedance()` method. .. GENERATED FROM PYTHON SOURCE LINES 45-56 .. code-block:: Python size = 5000 exceedanceNumbers = ot.Sample() probabilityEstimateSample = ot.Sample() for i in range(size): point = distNormal.getRealization() algo.increment(point) numberOfExceedances = algo.getThresholdExceedance()[0] exceedanceNumbers.add([numberOfExceedances]) probabilityEstimate = numberOfExceedances / algo.getIterationNumber() probabilityEstimateSample.add([probabilityEstimate]) .. GENERATED FROM PYTHON SOURCE LINES 57-58 We display the evolution of the number of exceedances. .. GENERATED FROM PYTHON SOURCE LINES 60-74 .. code-block:: Python curve = ot.Curve(exceedanceNumbers) curve.setLegend("number of exceedance") # graph = ot.Graph( "Evolution of the number of exceedances", "iteration nb", "number of exceedances", True, ) graph.add(curve) graph.setLegends(["number of exceedances"]) graph.setLegendPosition("lower right") view = otv.View(graph) .. image-sg:: /auto_numerical_methods/iterative_statistics/images/sphx_glr_plot_iterative_threshold_001.png :alt: Evolution of the number of exceedances :srcset: /auto_numerical_methods/iterative_statistics/images/sphx_glr_plot_iterative_threshold_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-76 The following plot shows that the probability of exceeding the threshold converges. .. GENERATED FROM PYTHON SOURCE LINES 78-97 .. code-block:: Python iterationSample = ot.Sample.BuildFromPoint(range(1, size + 1)) curve = ot.Curve(iterationSample, probabilityEstimateSample) curve.setLegend("Prob. of exceeding the threshold") # exactCurve = ot.Curve([1, size], [exactProbability, exactProbability]) exactCurve.setLegend("Exact") # graph = ot.Graph( "Evolution of the sample probability", "iteration nb", "estimate of the probability", True, ) graph.add(curve) graph.add(exactCurve) 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_threshold_002.png :alt: Evolution of the sample probability :srcset: /auto_numerical_methods/iterative_statistics/images/sphx_glr_plot_iterative_threshold_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 98-99 We can also increment with a :class:`~openturns.Sample`. .. GENERATED FROM PYTHON SOURCE LINES 101-112 .. code-block:: Python sample = distNormal.getSample(size) algo.increment(sample) numberOfExceedances = algo.getThresholdExceedance()[0] print("Number of exceedance: ", numberOfExceedances) # The empirical probability is close to the exact value. numberOfExceedances = algo.getThresholdExceedance()[0] probabilityEstimate = numberOfExceedances / algo.getIterationNumber() print("Empirical exceedance prb: ", probabilityEstimate) otv.View.ShowAll() .. rst-class:: sphx-glr-script-out .. code-block:: none Number of exceedance: 1579.0 Empirical exceedance prb: 0.1579 .. _sphx_glr_download_auto_numerical_methods_iterative_statistics_plot_iterative_threshold.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_threshold.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_iterative_threshold.py `