.. 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 Click :ref:`here ` 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:: default 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:: default 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:: default thresholdValue = 1.0 iterThreshold = ot.IterativeThresholdExceedance(dim, thresholdValue) .. 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:: default distribution = ot.Normal() exactProbability = distribution.computeComplementaryCDF(thresholdValue) print("Exact probability: ", exactProbability) .. rst-class:: sphx-glr-script-out 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:: default size = 5000 exceedanceNumbers = ot.Sample() probabilityEstimateSample = ot.Sample() for i in range(size): point = distNormal.getRealization() iterThreshold.increment(point) numberOfExceedances = iterThreshold.getThresholdExceedance()[0] exceedanceNumbers.add([numberOfExceedances]) probabilityEstimate = numberOfExceedances / iterThreshold.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:: default 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("bottomright") 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-100 .. code-block:: default palette = ot.Drawable().BuildDefaultPalette(2) iterationSample = ot.Sample.BuildFromPoint(range(1, size + 1)) curve = ot.Curve(iterationSample, probabilityEstimateSample) curve.setLegend("Prob. of exceeding the threshold") curve.setColor(palette[0]) # exactCurve = ot.Curve([1, size], [exactProbability, exactProbability]) exactCurve.setLegend("Exact") exactCurve.setColor(palette[1]) # graph = ot.Graph( "Evolution of the sample probability", "iteration nb", "estimate of the probability", True, ) graph.add(curve) graph.add(exactCurve) graph.setLegendPosition("topleft") 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 101-102 We can also increment with a :class:`~openturns.Sample`. .. GENERATED FROM PYTHON SOURCE LINES 104-115 .. code-block:: default sample = distNormal.getSample(size) iterThreshold.increment(sample) numberOfExceedances = iterThreshold.getThresholdExceedance()[0] print("Number of exceedance: ", numberOfExceedances) # The empirical probability is close to the exact value. numberOfExceedances = iterThreshold.getThresholdExceedance()[0] probabilityEstimate = numberOfExceedances / iterThreshold.getIterationNumber() print("Empirical exceedance prb: ", probabilityEstimate) otv.View.ShowAll() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Number of exceedance: 1556.0 Empirical exceedance prb: 0.1556 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.289 seconds) .. _sphx_glr_download_auto_numerical_methods_iterative_statistics_plot_iterative_threshold.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_threshold.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_iterative_threshold.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_