{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Validate the design point with the Strong Maximum Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this basic example we are going to validate a FORM estimation using the Strong Maximum Test."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the model Y = x1 + 2*x2\n",
    "model = ot.SymbolicFunction([\"x1\", \"x2\"], [\"x1+2*x2\"])\n",
    "\n",
    "# Create the input distribution and random vector X\n",
    "inputDist = ot.Normal(2)\n",
    "inputDist.setDescription(['X1', 'X2'])\n",
    "\n",
    "inputVector = ot.RandomVector(inputDist)\n",
    "\n",
    "# Create the output random vector Y=model(X)\n",
    "output = ot.RandomVector(model, inputVector)\n",
    "output.setName(\"MyOutputY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the physical event Y > 4\n",
    "threshold = 4\n",
    "myEvent = ot.Event(output, ot.Greater(), threshold)\n",
    "\n",
    "# Create the associated standard event in the standard space\n",
    "myStandardEvent = ot.StandardEvent(myEvent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# First : FORM analyses to get the design point\n",
    "myCobyla = ot.Cobyla()\n",
    "myStartingPoint = inputDist.getMean()\n",
    "myAlgoFORM = ot.FORM(myCobyla, myEvent, myStartingPoint)\n",
    "myAlgoFORM.run()\n",
    "FORMResult = myAlgoFORM.getResult()\n",
    "standardSpaceDesignPoint = FORMResult.getStandardSpaceDesignPoint()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Confidence level =  0.99\n",
      "Points Number =  13\n"
     ]
    }
   ],
   "source": [
    "# Fix the importance level epsilon of the test\n",
    "# Care : 0<epsilon<1\n",
    "importanceLevel = 0.15\n",
    "\n",
    "# Fix the accuracy level tau of the test\n",
    "# Care : tau >0\n",
    "# It is recommended that tau <4\n",
    "accuracyLevel = 3.0\n",
    "\n",
    "# Fix the confidence level (1-q) of the test\n",
    "confidenceLevel = 0.99\n",
    "\n",
    "# Create the Strong Maximum Test\n",
    "# CARE : the event must be declared in the standard space\n",
    "# 1. From the confidenceLevel parameter\n",
    "mySMT_CL = ot.StrongMaximumTest(myStandardEvent, standardSpaceDesignPoint,  importanceLevel, accuracyLevel,  confidenceLevel)\n",
    "\n",
    "# 2. Or from the  maximum number of points sampling the sphere\n",
    "pointsNumber = 1000\n",
    "mySMT_PN = ot.StrongMaximumTest(myStandardEvent, standardSpaceDesignPoint,  importanceLevel, accuracyLevel,  pointsNumber)\n",
    "\n",
    "# Perform the test\n",
    "mySMT_CL.run()\n",
    "mySMT_PN.run()\n",
    "\n",
    "# Get (or evaluate) the confidence level\n",
    "# associated to the number of points used to sample the sphere\n",
    "print('Confidence level = ', mySMT_CL.getConfidenceLevel())\n",
    "\n",
    "# Get (or evaluate) the number of points used to sample the sphere\n",
    "# associated the confidence level used\n",
    "print('Points Number = ', mySMT_CL.getPointNumber())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Potential design points =  \n",
      "Model values =  \n",
      "Points verifying the Event in the vicinity of the design points =  0 : [  2.5556   3.5279  ]\n",
      "1 : [ -1.48846  4.0941  ]\n",
      "2 : [  3.1138   3.04654 ]\n",
      "3 : [ -1.04463  4.22918 ]\n",
      "Model values =      [ y0      ]\n",
      "0 : [ 9.6114  ]\n",
      "1 : [ 6.69974 ]\n",
      "2 : [ 9.20688 ]\n",
      "3 : [ 7.41373 ]\n",
      "Points NOT verifying the Event outside the vicinity of the design points =  0 : [ -3.8094    2.11321  ]\n",
      "1 : [ -3.80056  -2.12906  ]\n",
      "2 : [ -4.34588  -0.300764 ]\n",
      "3 : [  4.31418  -0.604166 ]\n",
      "4 : [ -3.40961   2.71141  ]\n",
      "5 : [ -4.30125   0.690212 ]\n",
      "6 : [ -3.35984  -2.77284  ]\n",
      "7 : [  4.21095  -1.11581  ]\n",
      "8 : [  1.8862   -3.92675  ]\n",
      "Model values =      [ y0        ]\n",
      "0 : [  0.417029 ]\n",
      "1 : [ -8.05868  ]\n",
      "2 : [ -4.94741  ]\n",
      "3 : [  3.10585  ]\n",
      "4 : [  2.01321  ]\n",
      "5 : [ -2.92083  ]\n",
      "6 : [ -8.90552  ]\n",
      "7 : [  1.97934  ]\n",
      "8 : [ -5.96731  ]\n",
      "Points NOT verifying the Event outside the vicinity of the design points =  \n",
      "Model values =  \n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Get all the points verifying the event  and outside the design point vicinity\n",
    "# Get also the values of limit state function at these points\n",
    "potentialDesignPoints = mySMT_CL.getFarDesignPointVerifyingEventPoints()\n",
    "values = mySMT_CL.getFarDesignPointVerifyingEventValues()\n",
    "print('Potential design points = ', potentialDesignPoints)\n",
    "print('Model values = ', values)\n",
    "\n",
    "# Get all the points verifying the event and inside the design point vicinity\n",
    "# Get also the values of limit state function at these points\n",
    "vicinityDesignPoint = mySMT_CL.getNearDesignPointVerifyingEventPoints()\n",
    "values = mySMT_CL.getNearDesignPointVerifyingEventValues()\n",
    "print('Points verifying the Event in the vicinity of the design points = ', vicinityDesignPoint)\n",
    "print('Model values = ', values)\n",
    "\n",
    "# Get all the points not verifying the event and outside the design point vicinity\n",
    "# Get also the values of limit state function at these points\n",
    "farSecurityPoints = mySMT_CL.getFarDesignPointViolatingEventPoints()\n",
    "values = mySMT_CL.getFarDesignPointViolatingEventValues()\n",
    "print('Points NOT verifying the Event outside the vicinity of the design points = ', farSecurityPoints)\n",
    "print('Model values = ', values)\n",
    "\n",
    "# Get  all the points not verifying the event and inside the design point vicinity\n",
    "# Get also the values of limit state function at these points\n",
    "vicinitySecurityPoints = mySMT_CL.getNearDesignPointViolatingEventPoints()\n",
    "values = mySMT_CL.getNearDesignPointViolatingEventValues()\n",
    "print('Points NOT verifying the Event outside the vicinity of the design points = ', vicinitySecurityPoints)\n",
    "print('Model values = ', values)"
   ]
  }
 ],
 "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": 1
}
