.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_reliability/reliability_analysis/plot_simulation_sensitivity_analysis.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_simulation_sensitivity_analysis.py: Estimate failure probability sensitivity with simulation ======================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-34 In this example we estimate the sensitivity of a failure probability with respect to the parameters of the input distribution using the :class:`~openturns.SimulationSensitivityAnalysis` class. For a failure event :math:`\mathcal{F} = \{g(\vect{X}) > s\}` with input density :math:`f_{\theta}(\cdot)`, the sensitivity with respect to :math:`\theta` is: .. math:: \frac{\partial P_f}{\partial \theta} = \mathbb{E}\left[\mathbf{1}_{\mathcal{F}}(\vect{X}) \frac{\partial \log f_{\theta}(\vect{X})}{\partial \theta}\right]. With Monte Carlo sampling this expectation is estimated by the empirical mean over the input sample. With importance sampling with importance density :math:`h`, each realization is weighted by the likelihood ratio :math:`w = f_{\theta}/h`: .. math:: \frac{\partial P_f}{\partial \theta} \approx \frac{1}{n}\sum_{i=1}^n \mathbf{1}_{\mathcal{F}}(\vect{X}_i) \frac{\partial \log f_{\theta}(\vect{X}_i)}{\partial \theta} w_i. We first run a Monte Carlo reference, then importance sampling in the physical space, and finally importance sampling in the standard space mapped back to the physical space. .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python import openturns as ot import openturns.viewer as otv ot.RandomGenerator.SetSeed(0) .. GENERATED FROM PYTHON SOURCE LINES 42-45 Define the model ----------------- A linear limit-state with Gaussian inputs. .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: Python distribution = ot.Normal([0.0] * 2, [1.0] * 2) model = ot.SymbolicFunction(["x0", "x1"], ["x0 + 2.0 * x1"]) threshold = 3.0 .. GENERATED FROM PYTHON SOURCE LINES 52-55 Monte Carlo reference --------------------- With uniform weights the samples stored by the memoized model are enough. .. GENERATED FROM PYTHON SOURCE LINES 57-78 .. code-block:: Python memo_mc = ot.MemoizeFunction(model) event_mc = ot.ThresholdEvent( ot.CompositeRandomVector(memo_mc, ot.RandomVector(distribution)), ot.Greater(), threshold, ) algo_mc = ot.ProbabilitySimulationAlgorithm(event_mc) algo_mc.setMaximumOuterSampling(100) algo_mc.setBlockSize(500) algo_mc.setMaximumCoefficientOfVariation(0.05) algo_mc.run() result_mc = algo_mc.getResult() print("Monte Carlo Pf =", result_mc.getProbabilityEstimate()) sensitivity_mc = ot.SimulationSensitivityAnalysis(result_mc) print("Monte Carlo sensitivity =", sensitivity_mc.computeEventProbabilitySensitivity()) print("Monte Carlo mean point =", sensitivity_mc.computeMeanPointInEventDomain()) graph = sensitivity_mc.drawImportanceFactors() view = otv.View(graph) .. image-sg:: /auto_reliability/reliability_analysis/images/sphx_glr_plot_simulation_sensitivity_analysis_001.svg :alt: Importance Factors from Simulation - v0 :srcset: /auto_reliability/reliability_analysis/images/sphx_glr_plot_simulation_sensitivity_analysis_001.svg :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Monte Carlo Pf = 0.08888888888888888 Monte Carlo sensitivity = [mu_0 : 0.0646111, sigma_0 : 0.0303473, mu_1 : 0.145922, sigma_1 : 0.181368] Monte Carlo mean point = [0.726875,1.64162] .. GENERATED FROM PYTHON SOURCE LINES 79-84 Importance sampling in the physical space ----------------------------------------- The importance distribution is centered on the failure domain. The likelihood ratio must be passed as weights, otherwise the sensitivity is biased. .. GENERATED FROM PYTHON SOURCE LINES 86-119 .. code-block:: Python memo_is = ot.MemoizeFunction(model) event_is = ot.ThresholdEvent( ot.CompositeRandomVector(memo_is, ot.RandomVector(distribution)), ot.Greater(), threshold, ) importance = ot.Normal([1.0, 2.0], [1.0] * 2) experiment = ot.ImportanceSamplingExperiment(importance) algo_is = ot.ProbabilitySimulationAlgorithm(event_is, experiment) algo_is.setMaximumOuterSampling(100) algo_is.setBlockSize(500) algo_is.setMaximumCoefficientOfVariation(0.05) algo_is.run() print("Importance sampling Pf =", algo_is.getResult().getProbabilityEstimate()) input_sample = memo_is.getInputHistory() output_sample = memo_is.getOutputHistory() weights = [ distribution.computePDF(point) / importance.computePDF(point) for point in input_sample ] sensitivity_is = ot.SimulationSensitivityAnalysis( event_is, input_sample, output_sample, weights ) print( "Importance sampling sensitivity =", sensitivity_is.computeEventProbabilitySensitivity(), ) print( "Importance sampling mean point =", sensitivity_is.computeMeanPointInEventDomain() ) .. rst-class:: sphx-glr-script-out .. code-block:: none Importance sampling Pf = 0.09553770376063518 Importance sampling sensitivity = [mu_0 : 0.0789529, sigma_0 : 0.0414191, mu_1 : 0.150642, sigma_1 : 0.170741] Importance sampling mean point = [0.826406,1.57678] .. GENERATED FROM PYTHON SOURCE LINES 120-127 Importance sampling in the standard space ----------------------------------------- In practice importance sampling is often performed in the standard space through a :class:`~openturns.StandardEvent`. The sensitivity with respect to the physical distribution is then recovered by mapping the standard input sample back to the physical space and weighting by the ratio of the standard density over the importance density. .. GENERATED FROM PYTHON SOURCE LINES 129-168 .. code-block:: Python memo_phys = ot.MemoizeFunction(model) phys_event = ot.ThresholdEvent( ot.CompositeRandomVector(memo_phys, ot.RandomVector(distribution)), ot.Greater(), threshold, ) std_event = ot.StandardEvent(phys_event) memo_std = ot.MemoizeFunction(std_event.getFunction()) std_event_is = ot.ThresholdEvent( ot.CompositeRandomVector(memo_std, std_event.getAntecedent()), std_event.getOperator(), std_event.getThreshold(), ) std_importance = ot.Normal([1.0, 2.0], ot.CovarianceMatrix(2)) std_experiment = ot.ImportanceSamplingExperiment(std_importance) std_algo = ot.ProbabilitySimulationAlgorithm(std_event_is, std_experiment) std_algo.setMaximumOuterSampling(100) std_algo.setBlockSize(500) std_algo.setMaximumCoefficientOfVariation(0.05) std_algo.run() print("Standard-space Pf =", std_algo.getResult().getProbabilityEstimate()) std_input = memo_std.getInputHistory() std_output = memo_std.getOutputHistory() std_distribution = ot.Normal(2) std_weights = [ std_distribution.computePDF(point) / std_importance.computePDF(point) for point in std_input ] phys_input = distribution.getInverseIsoProbabilisticTransformation()(std_input) sensitivity_std = ot.SimulationSensitivityAnalysis( phys_event, phys_input, std_output, std_weights ) print( "Standard-space sensitivity w.r.t. physical parameters =", sensitivity_std.computeEventProbabilitySensitivity(), ) .. rst-class:: sphx-glr-script-out .. code-block:: none Standard-space Pf = 0.0870263073049596 Standard-space sensitivity w.r.t. physical parameters = [mu_0 : 0.0646741, sigma_0 : 0.0404083, mu_1 : 0.145542, sigma_1 : 0.186382] .. GENERATED FROM PYTHON SOURCE LINES 169-170 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_reliability_reliability_analysis_plot_simulation_sensitivity_analysis.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simulation_sensitivity_analysis.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_simulation_sensitivity_analysis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_simulation_sensitivity_analysis.zip `