{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Estimate a probability with LHS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this basic example we are going to estimate a probability by means of a simulation algorithm.\n",
    "\n",
    "This model is a simple beam, restrained at one side and stressed by a traction load F at the other side.\n",
    "\n",
    "The geometry is supposed to be deterministic: the diameter D is fixed to D=20 mm.\n",
    "\n",
    "It is considered that failure occurs when the beam plastifies, i.e. when the axial stress gets bigger than the yield stress:\n",
    "$$\\sigma_e = \\frac{F}{\\pi-D^2/4} \\leq 0$$\n",
    "\n",
    "Therefore, the state limit G used here is:\n",
    "$$G = \\sigma_e - \\frac{F}{\\pi-D^2/4}$$\n",
    "\n",
    "Two independent random variables R and S are considered:\n",
    "\n",
    " - R (the strength): $$R = \\sigma_e$$\n",
    " - S (the load): $$S = \\frac{F}{\\pi-D^2/4}$$\n",
    "\n",
    "Stochastic model:\n",
    "\n",
    " - F ~ Normal(75e3, 5e3) [N]\n",
    " - R ~ LogNormal(300, 30) [N]\n",
    "\n",
    "Theoretical results:\n",
    "\n",
    "This two dimensional stochastic problem can be solved by calculating directly the failure probability: \n",
    "\n",
    "$$P_f = \\text{Prob}(R-S \\leq 0) = \\int_{r-s \\leq 0} f_{R, S}(r, s)drds$$\n",
    "\n",
    "If R and S are independant, then: \n",
    "\n",
    "$$\\int_{-\\infty}^{+\\infty}f_S(x)F_R(x)dx$$\n",
    "\n",
    "The numerical application gives: $$P_f = 0.0292$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# create the joint distribution of the parameters\n",
    "distribution_R = ot.LogNormalMuSigma(300.0, 30.0, 0.0).getDistribution()\n",
    "distribution_F = ot.Normal(75e3, 5e3)\n",
    "marginals = [distribution_R, distribution_F]\n",
    "distribution = ot.ComposedDistribution(marginals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# create the model\n",
    "model = ot.SymbolicFunction(['R', 'F'], ['R-F/(_pi*100.0)'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# create the event we want to estimate the probability\n",
    "vect = ot.RandomVector(distribution)\n",
    "G = ot.RandomVector(model, vect)\n",
    "event = ot.Event(G, ot.Less(), 0.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# create a Monte Carlo algorithm\n",
    "algo = ot.LHS(event)\n",
    "algo.setMaximumCoefficientOfVariation(0.05)\n",
    "algo.setMaximumOuterSampling(int(1e5))\n",
    "algo.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pf= 0.029342988609791055\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
}
