.. 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_monte_carlo.py:
Estimate a probability with Monte Carlo
=======================================
In this example we estimate a probability by means of a simulation algorithm, the Monte-Carlo algorithm. To do this, we need the classes `MonteCarloExperiment` and `ProbabilitySimulationAlgorithm`.
We consider the :ref:`axial stressed beam ` example.
.. 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)
We load the model from the usecases module :
.. code-block:: default
from openturns.usecases import stressed_beam as stressed_beam
sm = stressed_beam.AxialStressedBeam()
We get the joint distribution of the parameters.
.. code-block:: default
distribution = sm.distribution
The model is also stored in the data class :
.. code-block:: default
model = sm.model
We create the event whose probability we want to estimate.
.. code-block:: default
vect = ot.RandomVector(distribution)
G = ot.CompositeRandomVector(model, vect)
event = ot.ThresholdEvent(G, ot.Less(), 0.0)
Create a Monte Carlo algorithm.
.. code-block:: default
experiment = ot.MonteCarloExperiment()
algo = ot.ProbabilitySimulationAlgorithm(event, experiment)
algo.setMaximumCoefficientOfVariation(0.05)
algo.setMaximumOuterSampling(int(1e5))
algo.run()
Retrieve results.
.. code-block:: default
result = algo.getResult()
probability = result.getProbabilityEstimate()
print('Pf=', probability)
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
Pf= 0.027625878843832115
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.043 seconds)
.. _sphx_glr_download_auto_reliability_sensitivity_reliability_plot_estimate_probability_monte_carlo.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_monte_carlo.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_estimate_probability_monte_carlo.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_