.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_reliability_sensitivity/reliability/plot_create_threshold_event.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_reliability_sensitivity_reliability_plot_create_threshold_event.py: Create a threshold event ======================== .. GENERATED FROM PYTHON SOURCE LINES 6-11 Abstract -------- We present in this example the creation and the use of a :class:`~openturns.ThresholdEvent` to estimate a simple integral. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import openturns as ot import openturns.viewer as otv from matplotlib import pylab as plt .. GENERATED FROM PYTHON SOURCE LINES 17-18 We consider a standard Gaussian random vector :math:`X` and build a random vector from this distribution. .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: Python distX = ot.Normal() vecX = ot.RandomVector(distX) .. GENERATED FROM PYTHON SOURCE LINES 22-23 We consider the simple model :math:`f:x \mapsto |x|` and consider the output random variable :math:`Y = f(X)`. .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: Python f = ot.SymbolicFunction(["x1"], ["abs(x1)"]) vecY = ot.CompositeRandomVector(f, vecX) .. GENERATED FROM PYTHON SOURCE LINES 27-28 We define a very simple :class:`~openturns.ThresholdEvent` which happpens whenever :math:`|X| < 1.0` : .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: Python thresholdEvent = ot.ThresholdEvent(vecY, ot.Less(), 1.0) .. GENERATED FROM PYTHON SOURCE LINES 31-35 For the normal distribution, it is a well-known fact that the values lower than one standard deviation (here exactly 1) away from the mean (here 0) account roughly for 68.27% of the set. So the probability of the event is: .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python print("Probability of the event : %.4f" % 0.6827) .. rst-class:: sphx-glr-script-out .. code-block:: none Probability of the event : 0.6827 .. GENERATED FROM PYTHON SOURCE LINES 38-39 We can also use a basic estimator to get the probability of the event by drawing samples from the initial distribution `distX` and counting those which realize the event: .. GENERATED FROM PYTHON SOURCE LINES 39-45 .. code-block:: Python print( "Probability of the event (event sampling) : %.4f" % thresholdEvent.getSample(1000).computeMean()[0] ) .. rst-class:: sphx-glr-script-out .. code-block:: none Probability of the event (event sampling) : 0.6830 .. GENERATED FROM PYTHON SOURCE LINES 46-47 The geometric interpretation is simply the area under the PDF of the standard normal distribution for :math:`x \in [-1,1]` which we draw thereafter. .. GENERATED FROM PYTHON SOURCE LINES 50-61 .. code-block:: Python def linearSample(xmin, xmax, npoints): """ Returns a sample created from a regular grid from xmin to xmax with npoints points. """ step = (xmax - xmin) / (npoints - 1) rg = ot.RegularGrid(xmin, step, npoints) vertices = rg.getVertices() return vertices .. GENERATED FROM PYTHON SOURCE LINES 62-63 The boundary of the event are the lines :math:`x = -1.0` and :math:`x = 1.0` .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python a, b = -1, 1 .. GENERATED FROM PYTHON SOURCE LINES 66-91 .. code-block:: Python nplot = 100 # Number of points in the plot x = linearSample(a, b, nplot) y = distX.computePDF(x) def drawInTheBounds(vLow, vUp, n_test): """ Draw the area within the bounds. """ palette = ot.Drawable.BuildDefaultPalette(2) myPaletteColor = palette[0] polyData = [[vLow[i], vLow[i + 1], vUp[i + 1], vUp[i]] for i in range(n_test - 1)] polygonList = [ ot.Polygon(polyData[i], myPaletteColor, myPaletteColor) for i in range(n_test - 1) ] boundsPoly = ot.PolygonArray(polygonList) return boundsPoly vLow = [[x[i, 0], 0.0] for i in range(nplot)] vUp = [[x[i, 0], y[i, 0]] for i in range(nplot)] area = distX.computeCDF(b) - distX.computeCDF(a) boundsPoly = drawInTheBounds(vLow, vUp, nplot) .. GENERATED FROM PYTHON SOURCE LINES 92-93 We add the colored area to the PDF graph. .. GENERATED FROM PYTHON SOURCE LINES 93-99 .. code-block:: Python graph = distX.drawPDF() graph.add(boundsPoly) graph.setTitle("Probability of the event E = %.4f" % (area)) graph.setLegends([""]) view = otv.View(graph) .. image-sg:: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_threshold_event_001.png :alt: Probability of the event E = 0.6827 :srcset: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_threshold_event_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-101 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 101-102 .. code-block:: Python plt.show() .. _sphx_glr_download_auto_reliability_sensitivity_reliability_plot_create_threshold_event.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_create_threshold_event.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_create_threshold_event.py `