.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_reliability_sensitivity/reliability/plot_estimate_probability_importance_sampling.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_reliability_sensitivity_reliability_plot_estimate_probability_importance_sampling.py: Use the Importance Sampling algorithm ===================================== .. GENERATED FROM PYTHON SOURCE LINES 6-16 In this example we estimate a failure probability with the importance sampling simulation algorithm provided by the `ImportanceSamplingExperiment` class. The main steps of this method are: * run a FORM analysis, * create an importance distribution based on the results of the FORM results, * run a sampling-based probability estimate algorithm. We shall consider the analytical example of a :ref:`cantilever beam `. .. GENERATED FROM PYTHON SOURCE LINES 18-20 Define the cantilever beam model -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-29 .. code-block:: default from __future__ import print_function from openturns.usecases import cantilever_beam as cantilever_beam import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 30-31 The cantilever beam example can be accessed in the usecases module : .. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: default cb = cantilever_beam.CantileverBeam() .. GENERATED FROM PYTHON SOURCE LINES 34-35 The joint probability distribution of the input parameters is stored in the object `cb` : .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: default distribution = cb.distribution .. GENERATED FROM PYTHON SOURCE LINES 38-39 We create the model. .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: default model = cb.model .. GENERATED FROM PYTHON SOURCE LINES 42-44 Define the event ---------------- .. GENERATED FROM PYTHON SOURCE LINES 46-47 We create the event whose probability we want to estimate. .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. code-block:: default vect = ot.RandomVector(distribution) G = ot.CompositeRandomVector(model, vect) event = ot.ThresholdEvent(G, ot.Greater(), 0.30) .. GENERATED FROM PYTHON SOURCE LINES 54-56 Run a FORM analysis ------------------- .. GENERATED FROM PYTHON SOURCE LINES 58-59 Define a solver .. GENERATED FROM PYTHON SOURCE LINES 59-66 .. code-block:: default optimAlgo = ot.Cobyla() optimAlgo.setMaximumEvaluationNumber(1000) optimAlgo.setMaximumAbsoluteError(1.0e-10) optimAlgo.setMaximumRelativeError(1.0e-10) optimAlgo.setMaximumResidualError(1.0e-10) optimAlgo.setMaximumConstraintError(1.0e-10) .. GENERATED FROM PYTHON SOURCE LINES 67-68 Run FORM .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: default algo = ot.FORM(optimAlgo, event, distribution.getMean()) algo.run() result = algo.getResult() .. GENERATED FROM PYTHON SOURCE LINES 73-75 Configure an importance sampling algorithm ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 77-78 The `getStandardSpaceDesignPoint` method returns the design point in the U-space. .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default standardSpaceDesignPoint = result.getStandardSpaceDesignPoint() standardSpaceDesignPoint .. raw:: html

[-0.665643,4.31264,1.23029,-1.3689]



.. GENERATED FROM PYTHON SOURCE LINES 84-85 The key point is to define the importance distribution in the U-space. To define it, we use a multivariate standard Gaussian centered around the design point in the U-space. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default dimension = distribution.getDimension() dimension .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 91-95 .. code-block:: default myImportance = ot.Normal(dimension) myImportance.setMean(standardSpaceDesignPoint) myImportance .. raw:: html

Normal(mu = [-0.665643,4.31264,1.23029,-1.3689], sigma = [1,1,1,1], R = [[ 1 0 0 0 ]
[ 0 1 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]])



.. GENERATED FROM PYTHON SOURCE LINES 96-97 Create the design of experiment corresponding to importance sampling. This generates a `WeightedExperiment` with weights fitting to the importance distribution. .. GENERATED FROM PYTHON SOURCE LINES 99-102 .. code-block:: default experiment = ot.ImportanceSamplingExperiment(myImportance) type(experiment) .. GENERATED FROM PYTHON SOURCE LINES 103-104 Create the standard event corresponding to the event. This pushes the original problem to the U-space, with Gaussian independent marginals. .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: default standardEvent = ot.StandardEvent(event) .. GENERATED FROM PYTHON SOURCE LINES 109-111 Run the importance sampling simulation -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 113-114 We then create the simulation algorithm. .. GENERATED FROM PYTHON SOURCE LINES 116-121 .. code-block:: default algo = ot.ProbabilitySimulationAlgorithm(standardEvent, experiment) algo.setMaximumCoefficientOfVariation(0.1) algo.setMaximumOuterSampling(40000) algo.run() .. GENERATED FROM PYTHON SOURCE LINES 122-123 We can retrieve results of this estimate : .. GENERATED FROM PYTHON SOURCE LINES 123-127 .. code-block:: default result = algo.getResult() probability = result.getProbabilityEstimate() print("Probability = ", probability) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Probability = 4.897240963541003e-07 .. GENERATED FROM PYTHON SOURCE LINES 128-129 In order to compute the confidence interval, we use the `getConfidenceLength` method, which returns the length of the interval. In order to compute the bounds of the interval, we divide this length by 2. .. GENERATED FROM PYTHON SOURCE LINES 131-133 .. code-block:: default alpha = 0.05 .. GENERATED FROM PYTHON SOURCE LINES 134-138 .. code-block:: default pflen = result.getConfidenceLength(1-alpha) print("%.2f%% confidence interval = [%.10f,%.10f]" % ( (1-alpha)*100, probability-pflen/2, probability+pflen/2)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 95.00% confidence interval = [0.0000003939,0.0000005856] .. GENERATED FROM PYTHON SOURCE LINES 139-141 We can observe the convergence history of the estimate with the `drawProbabilityConvergence` method which displays the estimate and confidence interval evolution. .. GENERATED FROM PYTHON SOURCE LINES 141-144 .. code-block:: default graph = algo.drawProbabilityConvergence() graph.setLogScale(ot.GraphImplementation.LOGX) view = viewer.View(graph) .. image-sg:: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_estimate_probability_importance_sampling_001.png :alt: ProbabilitySimulationAlgorithm convergence graph at level 0.95 :srcset: /auto_reliability_sensitivity/reliability/images/sphx_glr_plot_estimate_probability_importance_sampling_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.244 seconds) .. _sphx_glr_download_auto_reliability_sensitivity_reliability_plot_estimate_probability_importance_sampling.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_estimate_probability_importance_sampling.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_estimate_probability_importance_sampling.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_