{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Estimate a probability using randomized quasi Monte Carlo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this basic example we are going to estimate a failure probability.\n",
    "\n",
    "Let's consider the following analytical example of a cantilever beam, of Young's modulus E, length L, section modulus I.\n",
    "\n",
    "One end is built in a wall and we apply a concentrated bending load F at the other end of the beam, resulting in a deviation:\n",
    "$$d = \\frac{F*L^3}{3*E*I}$$\n",
    "\n",
    "It is considered that failure occurs when the beam deviation is too important:\n",
    "\n",
    "$$d \\ge 30 (cm) $$\n",
    "\n",
    "Four independent random variables are considered:\n",
    "\n",
    " - E (the Young's modulus) [Pa]\n",
    " - F (the load) [N]\n",
    " - L (the length) # [m]\n",
    " - I (the section) # [m^4]\n",
    "\n",
    "Stochastic model (simplified model, no units):\n",
    "\n",
    " - E ~ Beta(0.93, 3.2, 2.8e7, 4.8e7)\n",
    " - F ~ LogNormal(30000, 9000, 15000)\n",
    " - L ~ Uniform(250, 260)\n",
    " - I ~ Beta(2.5, 4.0, 3.1e2, 4.5e2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create the marginal distributions of the parameters\n",
    "dist_E = ot.Beta(0.93, 3.2, 2.8e7, 4.8e7)\n",
    "dist_F = ot.LogNormalMuSigma(30000, 9000, 15000).getDistribution()\n",
    "dist_L = ot.Uniform(250, 260)\n",
    "dist_I = ot.Beta(2.5, 4.0, 3.1e2, 4.5e2)\n",
    "marginals = [dist_E, dist_F, dist_L, dist_I]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the Copula\n",
    "RS = ot.CorrelationMatrix(4)\n",
    "#RS[2, 3] = -0.2\n",
    "# Evaluate the correlation matrix of the Normal copula from RS\n",
    "R = ot.NormalCopula.GetCorrelationFromSpearmanCorrelation(RS)\n",
    "# Create the Normal copula parametrized by R\n",
    "copula = ot.NormalCopula(R) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the joint probability distribution\n",
    "distribution = ot.ComposedDistribution(marginals, copula)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# create the model\n",
    "model = ot.SymbolicFunction(['E', 'F', 'L', 'I'], ['F*L^3/(3*E*I)'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# create the event we want to estimate the probability\n",
    "vect = ot.RandomVector(distribution)\n",
    "G = ot.CompositeRandomVector(model, vect)\n",
    "event = ot.Event(G, ot.Greater(), 30.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# define the low discrepancy sequence\n",
    "sequence = ot.SobolSequence()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create a simulation algorithm\n",
    "experiment = ot.LowDiscrepancyExperiment(sequence, 1)\n",
    "experiment.setRandomize(True)\n",
    "algo = ot.ProbabilitySimulationAlgorithm(event, experiment)\n",
    "algo.setMaximumCoefficientOfVariation(0.05)\n",
    "algo.setMaximumOuterSampling(int(1e5))\n",
    "algo.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pf= 0.005471091194017522\n"
     ]
    }
   ],
   "source": [
    "# retrieve results\n",
    "result = algo.getResult()\n",
    "probability = result.getProbabilityEstimate()\n",
    "print('Pf=', probability)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
