{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Analysis of the risk of failure of the O-rings on the Challenger shuttle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On January 27, 1986, the day before the takeoff of the shuttle _Challenger_, had\n",
    "a three-hour teleconference was held between \n",
    "Morton Thiokol (the manufacturer of one of the engines) and NASA. The\n",
    "discussion focused on the consequences of the\n",
    "temperature at take-off of 31°F (just below\n",
    "0°C) for the success of the flight and in particular on the performance of the\n",
    "O-rings used in the engines. Indeed, no test\n",
    "had been performed at this temperature.\n",
    "\n",
    "The following study takes up some of the analyses carried out that\n",
    "night with the objective of assessing the potential influence of\n",
    "the temperature and pressure to which the O-rings are subjected\n",
    "on their probability of malfunction. Our starting point is \n",
    "the results of the experiments carried out by NASA engineers\n",
    "during the six years preceding the launch of the shuttle\n",
    "Challenger."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading the data\n",
    "We start by loading this data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Count</th>\n",
       "      <th>Temperature</th>\n",
       "      <th>Pressure</th>\n",
       "      <th>Malfunction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4/12/81</td>\n",
       "      <td>6</td>\n",
       "      <td>66</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11/12/81</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3/22/82</td>\n",
       "      <td>6</td>\n",
       "      <td>69</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11/11/82</td>\n",
       "      <td>6</td>\n",
       "      <td>68</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4/04/83</td>\n",
       "      <td>6</td>\n",
       "      <td>67</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6/18/82</td>\n",
       "      <td>6</td>\n",
       "      <td>72</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>8/30/83</td>\n",
       "      <td>6</td>\n",
       "      <td>73</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>11/28/83</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>100</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2/03/84</td>\n",
       "      <td>6</td>\n",
       "      <td>57</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>4/06/84</td>\n",
       "      <td>6</td>\n",
       "      <td>63</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>8/30/84</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>10/05/84</td>\n",
       "      <td>6</td>\n",
       "      <td>78</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>11/08/84</td>\n",
       "      <td>6</td>\n",
       "      <td>67</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1/24/85</td>\n",
       "      <td>6</td>\n",
       "      <td>53</td>\n",
       "      <td>200</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>4/12/85</td>\n",
       "      <td>6</td>\n",
       "      <td>67</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>4/29/85</td>\n",
       "      <td>6</td>\n",
       "      <td>75</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>6/17/85</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>7/29/85</td>\n",
       "      <td>6</td>\n",
       "      <td>81</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>8/27/85</td>\n",
       "      <td>6</td>\n",
       "      <td>76</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>10/03/85</td>\n",
       "      <td>6</td>\n",
       "      <td>79</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>10/30/85</td>\n",
       "      <td>6</td>\n",
       "      <td>75</td>\n",
       "      <td>200</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>11/26/85</td>\n",
       "      <td>6</td>\n",
       "      <td>76</td>\n",
       "      <td>200</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1/12/86</td>\n",
       "      <td>6</td>\n",
       "      <td>58</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date  Count  Temperature  Pressure  Malfunction\n",
       "0     4/12/81      6           66        50            0\n",
       "1    11/12/81      6           70        50            1\n",
       "2     3/22/82      6           69        50            0\n",
       "3    11/11/82      6           68        50            0\n",
       "4     4/04/83      6           67        50            0\n",
       "5     6/18/82      6           72        50            0\n",
       "6     8/30/83      6           73       100            0\n",
       "7    11/28/83      6           70       100            0\n",
       "8     2/03/84      6           57       200            1\n",
       "9     4/06/84      6           63       200            1\n",
       "10    8/30/84      6           70       200            1\n",
       "11   10/05/84      6           78       200            0\n",
       "12   11/08/84      6           67       200            0\n",
       "13    1/24/85      6           53       200            2\n",
       "14    4/12/85      6           67       200            0\n",
       "15    4/29/85      6           75       200            0\n",
       "16    6/17/85      6           70       200            0\n",
       "17  7/29/85      6           81       200            0\n",
       "18    8/27/85      6           76       200            0\n",
       "19   10/03/85      6           79       200            0\n",
       "20   10/30/85      6           75       200            2\n",
       "21   11/26/85      6           76       200            0\n",
       "22    1/12/86      6           58       200            1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "data = pd.read_csv(\"shuttle.csv\")\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The data set shows us the date of each test, the number of O-rings (there are 6 on the main launcher), the temperature (in Fahrenheit) and pressure (in psi), and finally the number of identified malfunctions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Graphical inspection\n",
    "Flights without incidents do not provide any information\n",
    "on the influence of temperature or pressure on malfunction.\n",
    "We thus focus on the experiments in which at least one O-ring\n",
    "was defective."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Count</th>\n",
       "      <th>Temperature</th>\n",
       "      <th>Pressure</th>\n",
       "      <th>Malfunction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11/12/81</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2/03/84</td>\n",
       "      <td>6</td>\n",
       "      <td>57</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>4/06/84</td>\n",
       "      <td>6</td>\n",
       "      <td>63</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>8/30/84</td>\n",
       "      <td>6</td>\n",
       "      <td>70</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1/24/85</td>\n",
       "      <td>6</td>\n",
       "      <td>53</td>\n",
       "      <td>200</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>10/30/85</td>\n",
       "      <td>6</td>\n",
       "      <td>75</td>\n",
       "      <td>200</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1/12/86</td>\n",
       "      <td>6</td>\n",
       "      <td>58</td>\n",
       "      <td>200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date  Count  Temperature  Pressure  Malfunction\n",
       "1   11/12/81      6           70        50            1\n",
       "8    2/03/84      6           57       200            1\n",
       "9    4/06/84      6           63       200            1\n",
       "10   8/30/84      6           70       200            1\n",
       "13   1/24/85      6           53       200            2\n",
       "20  10/30/85      6           75       200            2\n",
       "22   1/12/86      6           58       200            1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = data[data.Malfunction>0]\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have a high temperature variability but\n",
    "the pressure is almost always 200, which should\n",
    "simplify the analysis.\n",
    "\n",
    "How does the frequency of failure vary with temperature?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 7604 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "pd.set_option('mode.chained_assignment',None) # this removes a useless warning from pandas\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "data[\"Frequency\"]=data.Malfunction/data.Count\n",
    "data.plot(x=\"Temperature\",y=\"Frequency\",kind=\"scatter\",ylim=[0,1])\n",
    "plt.grid(True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At first glance, the dependence does not look very important, but let's try to\n",
    "estimate the impact of temperature $t$ on the probability of O-ring malfunction."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Estimation of the temperature influence\n",
    "\n",
    "Suppose that each of the six O-rings is damaged with the same\n",
    "probability and independently of the others and that this probability\n",
    "depends only on the temperature. If $p(t)$ is this probability, the\n",
    "number $D$ of malfunctioning O-rings during a flight at\n",
    "temperature $t$ follows a binomial law with parameters $n=6$ and\n",
    "$p=p(t)$. To link $p(t)$ to $t$, we will therefore perform a\n",
    "logistic regression."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>Generalized Linear Model Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>      <td>Frequency</td>    <th>  No. Observations:  </th>  <td>     7</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                 <td>GLM</td>       <th>  Df Residuals:      </th>  <td>     5</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model Family:</th>       <td>Binomial</td>     <th>  Df Model:          </th>  <td>     1</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Link Function:</th>        <td>logit</td>      <th>  Scale:             </th> <td>  1.0000</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>               <td>IRLS</td>       <th>  Log-Likelihood:    </th> <td> -2.5250</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>           <td>Sat, 13 Apr 2019</td> <th>  Deviance:          </th> <td> 0.22231</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>               <td>19:12:05</td>     <th>  Pearson chi2:      </th>  <td> 0.236</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Iterations:</th>         <td>4</td>        <th>  Covariance Type:   </th> <td>nonrobust</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "       <td></td>          <th>coef</th>     <th>std err</th>      <th>z</th>      <th>P>|z|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Intercept</th>   <td>   -1.3895</td> <td>    7.828</td> <td>   -0.178</td> <td> 0.859</td> <td>  -16.732</td> <td>   13.953</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Temperature</th> <td>    0.0014</td> <td>    0.122</td> <td>    0.012</td> <td> 0.991</td> <td>   -0.238</td> <td>    0.240</td>\n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                 Generalized Linear Model Regression Results                  \n",
       "==============================================================================\n",
       "Dep. Variable:              Frequency   No. Observations:                    7\n",
       "Model:                            GLM   Df Residuals:                        5\n",
       "Model Family:                Binomial   Df Model:                            1\n",
       "Link Function:                  logit   Scale:                          1.0000\n",
       "Method:                          IRLS   Log-Likelihood:                -2.5250\n",
       "Date:                Sat, 13 Apr 2019   Deviance:                      0.22231\n",
       "Time:                        19:12:05   Pearson chi2:                    0.236\n",
       "No. Iterations:                     4   Covariance Type:             nonrobust\n",
       "===============================================================================\n",
       "                  coef    std err          z      P>|z|      [0.025      0.975]\n",
       "-------------------------------------------------------------------------------\n",
       "Intercept      -1.3895      7.828     -0.178      0.859     -16.732      13.953\n",
       "Temperature     0.0014      0.122      0.012      0.991      -0.238       0.240\n",
       "===============================================================================\n",
       "\"\"\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import statsmodels.api as sm\n",
    "\n",
    "data[\"Success\"]=data.Count-data.Malfunction\n",
    "data[\"Intercept\"]=1\n",
    "\n",
    "logmodel=sm.GLM(data['Frequency'], data[['Intercept','Temperature']], family=sm.families.Binomial(sm.families.links.logit)).fit()\n",
    "\n",
    "logmodel.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The most likely estimator of the temperature parameter is 0.0014\n",
    "and the standard error of this estimator is 0.122, in other words we\n",
    "cannot distinguish any particular impact and we must take our\n",
    "estimates with caution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Estimation of the probability of O-ring malfunction\n",
    "\n",
    "The expected temperature on the take-off day is 31°F. Let's try to\n",
    "estimate the probability of O-ring malfunction at\n",
    "this temperature from the model we just built:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9480 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "data_pred = pd.DataFrame({'Temperature': np.linspace(start=30, stop=90, num=121), 'Intercept': 1})\n",
    "data_pred['Frequency'] = logmodel.predict(data_pred[['Intercept','Temperature']])\n",
    "data_pred.plot(x=\"Temperature\",y=\"Frequency\",kind=\"line\",ylim=[0,1])\n",
    "plt.scatter(x=data[\"Temperature\"],y=data[\"Frequency\"])\n",
    "plt.grid(True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hideCode": false,
    "hidePrompt": false,
    "scrolled": true
   },
   "source": [
    "As expected from the initial data, the\n",
    "temperature has no significant impact on the probability of failure of the\n",
    "O-rings. It will be about 0.2, as in the tests\n",
    "where we had a failure of at least one joint. Let's get back\n",
    "to the initial dataset to estimate the probability of failure:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.06521739130434782\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_csv(\"shuttle.csv\")\n",
    "print(np.sum(data.Malfunction)/np.sum(data.Count))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This probability is thus about $p=0.065$. Knowing that there is\n",
    "a primary and a secondary O-ring on each of the three parts of the\n",
    "launcher, the probability of failure of both joints of a launcher\n",
    "is $p^2 \\approx 0.00425$. The probability of failure of any one of the\n",
    "launchers is $1-(1-p^2)^3 \\approx 1.2%$.  That would really be\n",
    "bad luck.... Everything is under control, so the takeoff can happen\n",
    "tomorrow as planned.\n",
    "\n",
    "But the next day, the Challenger shuttle exploded and took away\n",
    "with her the seven crew members. The public was shocked and in\n",
    "the subsequent investigation, the reliability of the\n",
    "O-rings was questioned. Beyond the internal communication problems\n",
    "of NASA, which have a lot to do with this fiasco, the previous analysis\n",
    "includes (at least) a small problem.... Can you find it?\n",
    "You are free to modify this analysis and to look at this dataset\n",
    "from all angles in order to to explain what's wrong."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Hide code",
  "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
