.. 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_domain_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_domain_event.py: Create a domain event ===================== .. GENERATED FROM PYTHON SOURCE LINES 6-12 Abstract -------- We present in this example the creation and the use of a :class:`~openturns.DomainEvent` through a simple Monte-Carlo estimator. .. GENERATED FROM PYTHON SOURCE LINES 12-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-19 We consider a standard unit Gaussian bivariate random vector :math:`\vect{X} = (X_1,X_2)` with independent marginals. .. GENERATED FROM PYTHON SOURCE LINES 19-22 .. code-block:: Python dim = 2 distX = ot.Normal(dim) .. GENERATED FROM PYTHON SOURCE LINES 23-30 We define a model :math:`f` which maps a vector of :math:`\mathbb{R}^2` to another vector of :math:`\mathbb{R}^2` .. math:: f : (x_1, x_2) \mapsto (x_1 + x_2, 2x_1) .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python f = ot.SymbolicFunction(["x1", "x2"], ["x1+x2", "2*x1"]) .. GENERATED FROM PYTHON SOURCE LINES 34-36 We build a :class:`~openturns.RandomVector` out of the input distribution and a :class:`~openturns.CompositeRandomVector` by using the model. .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: Python vecX = ot.RandomVector(distX) vecY = ot.CompositeRandomVector(f, vecX) .. GENERATED FROM PYTHON SOURCE LINES 41-44 Definition and vizualisation of a domain event ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-47 We define for each marginals of the output random vector `vecY` a domain of interest, say :math:`[0,1] \times [0,1]` .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python domain = ot.Interval([0.0, 0.0], [1.0, 1.0]) .. GENERATED FROM PYTHON SOURCE LINES 50-51 The :class:`~openturns.DomainEvent` is then built from the output random vector `vecY` and the `domain` : .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python event = ot.DomainEvent(vecY, domain) .. GENERATED FROM PYTHON SOURCE LINES 54-61 This domain is .. math:: \mathcal{D} = \{ \vect{x}=(x_1, x_2) \in \mathbb{R}^2 \; | \; x_1+x_2 \in [0,1] \; \mathrm{and} \; 2x_1 \in [0,1] \}. .. GENERATED FROM PYTHON SOURCE LINES 63-65 We plot both marginals of the model and the domain of interest for each marginal using contour curves. .. GENERATED FROM PYTHON SOURCE LINES 67-68 We represent the first marginal of `vecY`. .. GENERATED FROM PYTHON SOURCE LINES 68-74 .. code-block:: Python ot.ResourceMap.SetAsUnsignedInteger("Contour-DefaultLevelsNumber", 7) graphModel0 = f.draw(0, 1, 0, [0.0, 0.0], [-5.0, -5.0], [5.0, 5.0]) graphModel0.setXTitle(r"$x_1$") graphModel0.setYTitle(r"$x_2$") graphModel0.setTitle(r"Isolines of the model : $Y = f(X)$, first marginal") .. GENERATED FROM PYTHON SOURCE LINES 75-76 We represent the second marginal of `vecY`. .. GENERATED FROM PYTHON SOURCE LINES 76-81 .. code-block:: Python graphModel1 = f.draw(0, 1, 1, [0.0, 0.0], [-5.0, -5.0], [5.0, 5.0]) graphModel1.setXTitle(r"$x_1$") graphModel1.setYTitle(r"$x_2$") graphModel1.setTitle(r"Isolines of the model : $Y = f(X)$, second marginal") .. GENERATED FROM PYTHON SOURCE LINES 82-84 We shall now represent the curves delimiting the domain of interest : .. GENERATED FROM PYTHON SOURCE LINES 84-90 .. code-block:: Python nx, ny = 15, 15 xx = ot.Box([nx], ot.Interval([-5.0], [5.0])).generate() yy = ot.Box([ny], ot.Interval([-5.0], [5.0])).generate() inputData = ot.Box([nx, ny], ot.Interval([-5.0, -5.0], [5.0, 5.0])).generate() outputData = f(inputData) .. GENERATED FROM PYTHON SOURCE LINES 91-92 The contour line associated with the 0.0 value for the first marginal. .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: Python mycontour0 = ot.Contour(xx, yy, outputData.getMarginal(0), [0.0], ["0.0"]) mycontour0.setColor("black") mycontour0.setLineStyle("dashed") graphModel0.add(mycontour0) .. GENERATED FROM PYTHON SOURCE LINES 98-99 The contour line associated with the 1.0 value for the first marginal. .. GENERATED FROM PYTHON SOURCE LINES 99-105 .. code-block:: Python mycontour1 = ot.Contour(xx, yy, outputData.getMarginal(0), [1.0], ["1.0"]) mycontour1.setColor("black") mycontour1.setLineStyle("dashed") graphModel0.add(mycontour1) view = otv.View(graphModel0) .. image-sg:: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_001.png :alt: Isolines of the model : $Y = f(X)$, first marginal :srcset: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 106-107 The contour line associated with the 0.0 value for the second marginal. .. GENERATED FROM PYTHON SOURCE LINES 107-112 .. code-block:: Python mycontour2 = ot.Contour(xx, yy, outputData.getMarginal(1), [0.0], ["0.0"]) mycontour2.setColor("black") mycontour2.setLineStyle("dashed") graphModel1.add(mycontour2) .. GENERATED FROM PYTHON SOURCE LINES 113-114 The contour line associated with the 1.0 value for the second marginal. .. GENERATED FROM PYTHON SOURCE LINES 114-121 .. code-block:: Python mycontour3 = ot.Contour(xx, yy, outputData.getMarginal(1), [1.0], ["1.0"]) mycontour3.setColor("black") mycontour3.setLineStyle("dashed") graphModel1.add(mycontour3) view = otv.View(graphModel1) .. image-sg:: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_002.png :alt: Isolines of the model : $Y = f(X)$, second marginal :srcset: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 122-126 For each marginal the domain of interest is the area between the two black dashed curves. The domain event :math:`\mathcal{D}` is the intersection of these two areas. Here the intersection of both events is a parallelogram with the following vertices : .. GENERATED FROM PYTHON SOURCE LINES 126-128 .. code-block:: Python data = [[0.0, 0.0], [0.5, -0.5], [0.5, 0.5], [0.0, 1.0], [0.0, 0.0]] .. GENERATED FROM PYTHON SOURCE LINES 129-131 We create a polygon from these vertices with the :class:`~openturns.Polygon` class : that is our domain event. .. GENERATED FROM PYTHON SOURCE LINES 131-148 .. code-block:: Python myGraph = ot.Graph("Domain event", r"$x_1$", r"$x_2$", True, "", 1.0) myPolygon = ot.Polygon(data) myPolygon.setColor("darkgray") myPolygon.setEdgeColor("darkgray") myGraph.add(myPolygon) # Some annotation texts = [ r"$\mathcal{D} = \{ \mathbf{x}=(x_1, x_2) \in \mathbb{R}^2 \; | \; x_1+x_2 \in [0,1] \; \mathrm{and} \; 2x_1 \in [0,1] \}$" ] myText = ot.Text([0.25], [0.0], texts) myText.setTextSize(1) myGraph.add(myText) view = otv.View(myGraph) .. image-sg:: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_003.png :alt: Domain event :srcset: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 149-160 An example ---------- Consider the integral .. math:: P_f = \int_{\mathcal{D}} \mathbf{1}_{\mathcal{D}}(\vect{x}) f_{X_1,X_2}(\vect{x}) d \vect{x} where :math:`{\mathcal{D}}` is the previous domain event, :math:`\mathbf{1}_{\mathcal{D}}` is the indicator function on the domain and :math:`f_{X_1,X_2}` is the probability density function of the input variable. .. GENERATED FROM PYTHON SOURCE LINES 162-163 We observe the integration domain :math:`{\mathcal{D}}` superimposed on the 2D-PDF. .. GENERATED FROM PYTHON SOURCE LINES 163-170 .. code-block:: Python graphPDF = distX.drawPDF([-5.0, -5.0], [5.0, 5.0]) graphPDF.setXTitle(r"$x_1$") graphPDF.setYTitle(r"$x_2$") graphPDF.setTitle(r"Isolines of the 2D-PDF") graphPDF.add(myPolygon) view = otv.View(graphPDF) .. image-sg:: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_004.png :alt: Isolines of the 2D-PDF :srcset: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 171-173 We shall use a basic Monte Carlo algorithm using the domain event to estimate the probability. .. GENERATED FROM PYTHON SOURCE LINES 173-181 .. code-block:: Python algoMC = ot.ProbabilitySimulationAlgorithm(event) algoMC.setMaximumOuterSampling(1000) algoMC.setBlockSize(100) algoMC.setMaximumCoefficientOfVariation(0.02) algoMC.run() print("Pf = %.4f" % algoMC.getResult().getProbabilityEstimate()) .. rst-class:: sphx-glr-script-out .. code-block:: none Pf = 0.0680 .. GENERATED FROM PYTHON SOURCE LINES 182-183 We draw the convergence history : .. GENERATED FROM PYTHON SOURCE LINES 183-186 .. code-block:: Python graphConvergence = algoMC.drawProbabilityConvergence() view = otv.View(graphConvergence) .. image-sg:: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_005.png :alt: ProbabilitySimulationAlgorithm convergence graph at level 0.95 :srcset: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_create_domain_event_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 187-191 We can use the :meth:`~openturns.DomainEvent.getSample` method of the event to estimate the probability :math:`P_f`. This method draws realizations of the underlying random input vector `vecX` and returns `True` if the corresponding output random vector is in the domain event. Then the ratio between the number of realizations in the domain and the total of realizations is a rough estimate of the probability :math:`P_f` which we compare with the previous Monte-Carlo estimator. .. GENERATED FROM PYTHON SOURCE LINES 191-196 .. code-block:: Python N = 30000 samples = event.getSample(N) print("Basic estimator : %.4f" % (sum(samples)[0] / N)) .. rst-class:: sphx-glr-script-out .. code-block:: none Basic estimator : 0.0680 .. GENERATED FROM PYTHON SOURCE LINES 197-198 Display all figures .. GENERATED FROM PYTHON SOURCE LINES 198-200 .. code-block:: Python plt.show() .. GENERATED FROM PYTHON SOURCE LINES 201-202 Reset default settings .. GENERATED FROM PYTHON SOURCE LINES 202-203 .. code-block:: Python ot.ResourceMap.Reload() .. _sphx_glr_download_auto_reliability_sensitivity_reliability_plot_create_domain_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_domain_event.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_create_domain_event.py `