.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_reliability_sensitivity/reliability/plot_probability_simulation_parametrization.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_probability_simulation_parametrization.py: Specify a simulation algorithm ============================== .. GENERATED FROM PYTHON SOURCE LINES 6-13 In this example we are going to parameterize a simulation algorithm: - parameters linked to the number of points generated - the precision of the probability estimator - the sample storage strategy - using callbacks to monitor progress and stopping criteria. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: default from __future__ import print_function 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 22-23 Create the joint distribution of the parameters. .. GENERATED FROM PYTHON SOURCE LINES 25-30 .. code-block:: default distribution_R = ot.LogNormalMuSigma(300.0, 30.0, 0.0).getDistribution() distribution_F = ot.Normal(75e3, 5e3) marginals = [distribution_R, distribution_F] distribution = ot.ComposedDistribution(marginals) .. GENERATED FROM PYTHON SOURCE LINES 31-32 Create the model. .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: default model = ot.SymbolicFunction(['R', 'F'], ['R-F/(pi_*100.0)']) .. GENERATED FROM PYTHON SOURCE LINES 37-38 Create the event whose probability we want to estimate. .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: default vect = ot.RandomVector(distribution) G = ot.CompositeRandomVector(model, vect) event = ot.ThresholdEvent(G, ot.Less(), 0.0) .. GENERATED FROM PYTHON SOURCE LINES 45-46 Create a Monte Carlo algorithm. .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: default experiment = ot.MonteCarloExperiment() algo = ot.ProbabilitySimulationAlgorithm(event, experiment) .. GENERATED FROM PYTHON SOURCE LINES 52-53 Criteria 1: Define the Maximum Coefficient of variation of the probability estimator. .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: default algo.setMaximumCoefficientOfVariation(0.05) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Criteria 2: Define the number of iterations of the simulation. .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: default algo.setMaximumOuterSampling(int(1e4)) .. GENERATED FROM PYTHON SOURCE LINES 64-65 The block size parameter represents the number of samples evaluated per iteration, useful for parallelization. .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: default algo.setBlockSize(2) .. GENERATED FROM PYTHON SOURCE LINES 70-71 HistoryStrategy to store the values of the probability used to draw the convergence graph. .. GENERATED FROM PYTHON SOURCE LINES 73-74 Null strategy .. GENERATED FROM PYTHON SOURCE LINES 74-84 .. code-block:: default algo.setConvergenceStrategy(ot.Null()) # Full strategy algo.setConvergenceStrategy(ot.Full()) # Compact strategy: N points N = 1000 algo.setConvergenceStrategy(ot.Compact(N)) .. GENERATED FROM PYTHON SOURCE LINES 85-86 Use a callback to display the progress every 10%. .. GENERATED FROM PYTHON SOURCE LINES 88-99 .. code-block:: default def progress(p): if p >= progress.t: progress.t += 10.0 print('progress=', p, '%') return False progress.t = 10.0 algo.setProgressCallback(progress) .. GENERATED FROM PYTHON SOURCE LINES 100-101 Use a callback to stop the simulation. .. GENERATED FROM PYTHON SOURCE LINES 103-110 .. code-block:: default def stop(): # here we never stop, but we could return False algo.setStopCallback(stop) .. GENERATED FROM PYTHON SOURCE LINES 111-113 .. code-block:: default algo.run() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none progress= 10.0 % progress= 20.0 % progress= 30.0 % progress= 40.0 % progress= 50.0 % progress= 60.0 % progress= 70.0 % progress= 80.0 % progress= 90.0 % progress= 100.0 % .. GENERATED FROM PYTHON SOURCE LINES 114-115 Retrieve results. .. GENERATED FROM PYTHON SOURCE LINES 117-120 .. code-block:: default result = algo.getResult() probability = result.getProbabilityEstimate() print('Pf=', probability) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Pf= 0.027899999999999977 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.068 seconds) .. _sphx_glr_download_auto_reliability_sensitivity_reliability_plot_probability_simulation_parametrization.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_probability_simulation_parametrization.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_probability_simulation_parametrization.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_