.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_reliability/reliability_analysis/plot_nais.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_reliability_analysis_plot_nais.py: Non parametric Adaptive Importance Sampling (NAIS) ================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-34 The objective is to evaluate a probability from the Non parametric Adaptive Importance Sampling (NAIS) technique. We consider the four-branch function :math:`g : \mathbb{R}^2 \rightarrow \mathbb{R}` defined by: .. math:: \begin{align*} g(\vect{X}) = \min \begin{pmatrix}5+0.1(x_1-x_2)^2-\frac{(x_1+x_2)}{\sqrt{2}}\\ 5+0.1(x_1-x_2)^2+\frac{(x_1+x_2)}{\sqrt{2}}\\ (x_1-x_2)+ \frac{9}{\sqrt{2}}\\ (x_2-x_1)+ \frac{9}{\sqrt{2}} \end{pmatrix} \end{align*} and the input random vector :math:`\vect{X} = (X_1, X_2)` which follows the standard 2-dimensional Normal distribution: .. math:: \begin{align*} \vect{X} \sim \mathcal{N}(\mu = [0, 0], \sigma = [1,1], corr = \mat{I}_2) \end{align*} We want to evaluate the probability: .. math:: \begin{align*} p = \mathbb{P} ( g(\vect{X}) \leq 0 ) \end{align*} .. GENERATED FROM PYTHON SOURCE LINES 37-38 First, import the python modules: .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python import openturns as ot import openturns.viewer as otv import math .. GENERATED FROM PYTHON SOURCE LINES 45-47 Create the probabilistic model :math:`Y = g(\vect{X})` ------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 49-50 Create the input random vector :math:`\vect{X}`: .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python X = ot.RandomVector(ot.Normal(2)) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Create the function :math:`g` from a :class:`~openturns.PythonFunction`: .. GENERATED FROM PYTHON SOURCE LINES 58-74 .. code-block:: Python def fourBranch(x): x1 = x[0] x2 = x[1] g1 = 5 + 0.1 * (x1 - x2) ** 2 - (x1 + x2) / math.sqrt(2) g2 = 5 + 0.1 * (x1 - x2) ** 2 + (x1 + x2) / math.sqrt(2) g3 = (x1 - x2) + 9 / math.sqrt(2) g4 = (x2 - x1) + 9 / math.sqrt(2) return [min((g1, g2, g3, g4))] g = ot.PythonFunction(2, 1, fourBranch) .. GENERATED FROM PYTHON SOURCE LINES 75-76 Draw the function :math:`g` to help to understand the shape of the limit state function: .. GENERATED FROM PYTHON SOURCE LINES 78-85 .. code-block:: Python graph = ot.Graph("Four Branch function", "x1", "x2") graph.setLegendPosition("upper right") drawfunction = g.draw([-8] * 2, [8] * 2, [100] * 2) graph.add(drawfunction) view = otv.View(graph) .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_nais_001.svg :alt: Four Branch function :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_nais_001.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-87 In order to be able to get the NAIS samples used in the algorithm, it is necessary to transform the :class:`~openturns.PythonFunction` into a :class:`~openturns.MemoizeFunction`: .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python g = ot.MemoizeFunction(g) .. GENERATED FROM PYTHON SOURCE LINES 92-93 Create the output random vector :math:`Y = g(\vect{X})`: .. GENERATED FROM PYTHON SOURCE LINES 95-97 .. code-block:: Python Y = ot.CompositeRandomVector(g, X) .. GENERATED FROM PYTHON SOURCE LINES 98-100 Create the event :math:`\{ Y = g(\vect{X}) \leq 0 \}` ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: Python threshold = 0.0 myEvent = ot.ThresholdEvent(Y, ot.Less(), threshold) .. GENERATED FROM PYTHON SOURCE LINES 106-108 Evaluate the probability with the NAIS technique ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: Python quantileLevel = 0.1 algo = ot.NAIS(myEvent, quantileLevel) .. GENERATED FROM PYTHON SOURCE LINES 114-115 In order to get all the inputs and outputs that realize the event, you have to mention it now: .. GENERATED FROM PYTHON SOURCE LINES 117-119 .. code-block:: Python algo.setKeepSample(True) .. GENERATED FROM PYTHON SOURCE LINES 120-121 Now you can run the algorithm. .. GENERATED FROM PYTHON SOURCE LINES 123-129 .. code-block:: Python algo.run() result = algo.getResult() proba = result.getProbabilityEstimate() print("Proba NAIS = ", proba) print("Current coefficient of variation = ", result.getCoefficientOfVariation()) .. rst-class:: sphx-glr-script-out .. code-block:: none Proba NAIS = 7.053555398577317e-06 Current coefficient of variation = 0.09506021147598274 .. GENERATED FROM PYTHON SOURCE LINES 130-131 The confidence interval of level :math:`95\%` is: .. GENERATED FROM PYTHON SOURCE LINES 131-134 .. code-block:: Python ci95 = result.getProbabilityDistribution().computeBilateralConfidenceInterval(0.95) print("Confidence interval (0.95) = ", ci95) .. rst-class:: sphx-glr-script-out .. code-block:: none Confidence interval (0.95) = [5.73938e-06, 8.36774e-06] .. GENERATED FROM PYTHON SOURCE LINES 135-136 You can also get the successive thresholds used by the algorithm: .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: Python levels = algo.getThresholdPerStep() print("Levels of g = ", levels) .. rst-class:: sphx-glr-script-out .. code-block:: none Levels of g = [3.38131,2.0896,0.492353,0] .. GENERATED FROM PYTHON SOURCE LINES 142-144 Draw the NAIS samples used by the algorithm --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 146-147 You can get the number :math:`N_s` of steps with: .. GENERATED FROM PYTHON SOURCE LINES 147-150 .. code-block:: Python Ns = algo.getStepsNumber() print("Number of steps= ", Ns) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of steps= 4 .. GENERATED FROM PYTHON SOURCE LINES 151-152 Get all the inputs where :math:`g` was evaluated at each step .. GENERATED FROM PYTHON SOURCE LINES 152-156 .. code-block:: Python list_subSamples = list() for step in range(Ns): list_subSamples.append(algo.getInputSample(step)) .. GENERATED FROM PYTHON SOURCE LINES 157-158 The following graph draws each NAIS sample and the frontier :math:`g(x_1, x_2) = l_i` where :math:`l_i` is the threshold at the step :math:`i`: .. GENERATED FROM PYTHON SOURCE LINES 160-168 .. code-block:: Python graph = ot.Graph() graph.setAxes(True) graph.setGrid(True) graph.setTitle("NAIS sampling: samples") graph.setXTitle(r"$x_1$") graph.setYTitle(r"$x_2$") graph.setLegendPosition("lower left") .. GENERATED FROM PYTHON SOURCE LINES 169-170 Add all the NAIS samples: .. GENERATED FROM PYTHON SOURCE LINES 172-178 .. code-block:: Python for i in range(Ns): cloud = ot.Cloud(list_subSamples[i]) # cloud.setPointStyle("dot") graph.add(cloud) col = graph.getColors() .. GENERATED FROM PYTHON SOURCE LINES 179-180 Add the frontiers :math:`g(x_1, x_2) = l_i` where :math:`l_i` is the threshold at the step :math:`i`: .. GENERATED FROM PYTHON SOURCE LINES 182-192 .. code-block:: Python gIsoLines = g.draw([-5] * 2, [5] * 2, [128] * 2) dr = gIsoLines.getDrawable(0) for i, lv in enumerate(levels): dr.setLevels([lv]) dr.setLineStyle("solid") dr.setLegend(r"$g(X) = $" + str(round(lv, 2))) dr.setLineWidth(3) dr.setColor(col[i]) graph.add(dr) .. GENERATED FROM PYTHON SOURCE LINES 193-195 .. code-block:: Python _ = otv.View(graph) .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_nais_002.svg :alt: NAIS sampling: samples :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_nais_002.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 196-200 Draw the frontiers only ----------------------- The following graph enables to understand the progression of the algorithm: .. GENERATED FROM PYTHON SOURCE LINES 202-221 .. code-block:: Python graph = ot.Graph() graph.setAxes(True) graph.setGrid(True) dr = gIsoLines.getDrawable(0) for i, lv in enumerate(levels): dr.setLevels([lv]) dr.setLineStyle("solid") dr.setLegend(r"$g(X) = $" + str(round(lv, 2))) dr.setLineWidth(3) graph.add(dr) graph.setColors(col) graph.setLegendPosition("lower left") graph.setTitle("NAIS sampling: thresholds") graph.setXTitle(r"$x_1$") graph.setYTitle(r"$x_2$") _ = otv.View(graph) .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_nais_003.svg :alt: NAIS sampling: thresholds :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_nais_003.svg :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 222-225 Get all the input and output points that realized the event ----------------------------------------------------------- The following lines are possible only if you have mentioned that you wanted to keep samples with the method *algo.setKeepSample(True)* .. GENERATED FROM PYTHON SOURCE LINES 227-233 .. code-block:: Python select = ot.NAIS.EVENT1 # points that realize the event step = Ns - 1 # get the working sample from last iteration inputEventSample = algo.getInputSample(step, select) outputEventSample = algo.getOutputSample(step, select) print("Number of event realizations = ", inputEventSample.getSize()) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of event realizations = 443 .. GENERATED FROM PYTHON SOURCE LINES 234-235 Draw them! They are all in the event space. .. GENERATED FROM PYTHON SOURCE LINES 237-251 .. code-block:: Python graph = ot.Graph() graph.setAxes(True) graph.setGrid(True) cloud = ot.Cloud(inputEventSample) cloud.setPointStyle("bullet") graph.add(cloud) gIsoLines = g.draw([-5] * 2, [5] * 2, [1000] * 2) dr = gIsoLines.getDrawable(0) dr.setLevels([0.0]) dr.setColor("red") graph.add(dr) _ = otv.View(graph) otv.View.ShowAll() .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_nais_004.svg :alt: plot nais :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_nais_004.svg :class: sphx-glr-single-img .. _sphx_glr_download_auto_reliability_reliability_analysis_plot_nais.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_nais.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_nais.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_nais.zip `