{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "path_data = '../../data/'\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('fivethirtyeight')\n",
    "\n",
    "import functools\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "def standard_units(any_numbers):\n",
    "    \"Convert any array of numbers to standard units.\"\n",
    "    return (any_numbers - np.mean(any_numbers))/np.std(any_numbers)  \n",
    "\n",
    "def correlation(t, x, y):\n",
    "    return np.mean(standard_units(t[x])*standard_units(t[y]))\n",
    "\n",
    "def slope(table, x, y):\n",
    "    r = correlation(table, x, y)\n",
    "    return r * np.std(table[y])/np.std(table[x])\n",
    "\n",
    "def intercept(table, x, y):\n",
    "    a = slope(table, x, y)\n",
    "    return np.mean(table[y]) - a * np.mean(table[x])\n",
    "\n",
    "def fit(table, x, y):\n",
    "    \"\"\"Return the height of the regression line at each x value.\"\"\"\n",
    "    a = slope(table, x, y)\n",
    "    b = intercept(table, x, y)\n",
    "    return a * table[x] + b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Method of Least Squares\n",
    "We have retraced the steps that Galton and Pearson took to develop the equation of the regression line that runs through a football shaped scatter plot. But not all scatter plots are football shaped, not even linear ones. Does every scatter plot have a \"best\" line that goes through it? If so, can we still use the formulas for the slope and intercept developed in the previous section, or do we need new ones?\n",
    "\n",
    "To address these questions, we need a reasonable definition of \"best\". Recall that the purpose of the line is to *predict* or *estimate* values of $y$, given values of $x$. Estimates typically aren't perfect. Each one is off the true value by an *error*. A reasonable criterion for a line to be the \"best\" is for it to have the smallest possible overall error among all straight lines.\n",
    "\n",
    "In this section we will make this criterion precise and see if we can identify the best straight line under the criterion."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our first example is a dataset that has one row for every chapter of the novel \"Little Women.\" The goal is to estimate the number of characters (that is, letters, spaces punctuation marks, and so on) based on the number of periods. Recall that we attempted to do this in the very first lecture of this course."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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>Periods</th>\n",
       "      <th>Characters</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>189</td>\n",
       "      <td>21759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>188</td>\n",
       "      <td>22148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>231</td>\n",
       "      <td>20558</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Periods  Characters\n",
       "0      189       21759\n",
       "1      188       22148\n",
       "2      231       20558"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "little_women = pd.read_csv(path_data + 'little_women.csv')\n",
    "\n",
    "periods = little_women['Periods']\n",
    "\n",
    "little_women.pop('Periods')\n",
    "\n",
    "little_women.insert(0, 'Periods', periods)\n",
    "\n",
    "little_women.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23124 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(figsize=(6,6))\n",
    "\n",
    "ax.scatter(little_women['Periods'], \n",
    "           little_women['Characters'],  \n",
    "           color='darkblue')\n",
    "\n",
    "x_label = 'Periods'\n",
    "\n",
    "y_label = 'Characters'\n",
    "\n",
    "y_vals = ax.get_yticks()\n",
    "\n",
    "plt.ylabel(y_label)\n",
    "\n",
    "plt.xlabel(x_label)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To explore the data, we will need to use the functions `correlation`, `slope`, `intercept`, and `fit` defined in the previous section."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9229576895854817"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "correlation(little_women, 'Periods', 'Characters')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The scatter plot is remarkably close to linear, and the correlation is more than 0.92."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Error in Estimation\n",
    "The graph below shows the scatter plot and line that we developed in the previous section. We don't yet know if that's the best among all lines. We first have to say precisely what \"best\" means."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31148 characters]\n",
      "text/plain": [
       "<Figure size 576x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lw_with_predictions = little_women.copy()\n",
    "\n",
    "lw_with_predictions['Linear Prediction'] = fit(little_women, 'Periods', 'Characters')\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(8,5))\n",
    "\n",
    "ax.scatter(lw_with_predictions['Periods'], \n",
    "           lw_with_predictions['Characters'], \n",
    "           label='Characters', \n",
    "           color='darkblue')\n",
    "\n",
    "ax.scatter(lw_with_predictions['Periods'], \n",
    "           lw_with_predictions['Linear Prediction'], \n",
    "           label='Linear Prediction', \n",
    "           color='gold')\n",
    "\n",
    "x_label = 'Periods'\n",
    "\n",
    "y_label = ''\n",
    "\n",
    "y_vals = ax.get_yticks()\n",
    "\n",
    "plt.ylabel(y_label)\n",
    "\n",
    "plt.xlabel(x_label)\n",
    "\n",
    "ax.legend(bbox_to_anchor=(1.04,1), loc=\"upper left\", frameon=False)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Corresponding to each point on the scatter plot, there is an error of prediction calculated as the actual value minus the predicted value. It is the vertical distance between the point and the line, with a negative sign if the point is below the line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "actual = lw_with_predictions['Characters']\n",
    "predicted = lw_with_predictions['Linear Prediction']\n",
    "errors = actual - predicted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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>Periods</th>\n",
       "      <th>Characters</th>\n",
       "      <th>Linear Prediction</th>\n",
       "      <th>Error</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>189</td>\n",
       "      <td>21759</td>\n",
       "      <td>21183.596794</td>\n",
       "      <td>575.403206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>188</td>\n",
       "      <td>22148</td>\n",
       "      <td>21096.618953</td>\n",
       "      <td>1051.381047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>231</td>\n",
       "      <td>20558</td>\n",
       "      <td>24836.666127</td>\n",
       "      <td>-4278.666127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>195</td>\n",
       "      <td>25526</td>\n",
       "      <td>21705.463842</td>\n",
       "      <td>3820.536158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>255</td>\n",
       "      <td>23395</td>\n",
       "      <td>26924.134317</td>\n",
       "      <td>-3529.134317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>140</td>\n",
       "      <td>14622</td>\n",
       "      <td>16921.682573</td>\n",
       "      <td>-2299.682573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>131</td>\n",
       "      <td>14431</td>\n",
       "      <td>16138.882001</td>\n",
       "      <td>-1707.882001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>214</td>\n",
       "      <td>22476</td>\n",
       "      <td>23358.042826</td>\n",
       "      <td>-882.042826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>337</td>\n",
       "      <td>33767</td>\n",
       "      <td>34056.317301</td>\n",
       "      <td>-289.317301</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>185</td>\n",
       "      <td>18508</td>\n",
       "      <td>20835.685429</td>\n",
       "      <td>-2327.685429</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Periods  Characters  Linear Prediction        Error\n",
       "0      189       21759       21183.596794   575.403206\n",
       "1      188       22148       21096.618953  1051.381047\n",
       "2      231       20558       24836.666127 -4278.666127\n",
       "3      195       25526       21705.463842  3820.536158\n",
       "4      255       23395       26924.134317 -3529.134317\n",
       "5      140       14622       16921.682573 -2299.682573\n",
       "6      131       14431       16138.882001 -1707.882001\n",
       "7      214       22476       23358.042826  -882.042826\n",
       "8      337       33767       34056.317301  -289.317301\n",
       "9      185       18508       20835.685429 -2327.685429"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lw_with_predictions['Error'] = errors\n",
    "lw_with_predictions.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use `slope` and `intercept` to calculate the slope and intercept of the fitted line. The graph below shows the line (in light blue). The errors corresponding to four of the points are shown in red. There is nothing special about those four points. They were just chosen for clarity of the display. The function `lw_errors` takes a slope and an intercept (in that order) as its arguments and draws the figure. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lw_reg_slope = 86.97784125829823\n",
      "lw_reg_intercept = 4744.784796574924\n"
     ]
    }
   ],
   "source": [
    "lw_reg_slope = slope(little_women, 'Periods', 'Characters')\n",
    "\n",
    "lw_reg_intercept = intercept(little_women, 'Periods', 'Characters')\n",
    "\n",
    "print('lw_reg_slope =', lw_reg_slope )\n",
    "print('lw_reg_intercept =', lw_reg_intercept)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "sample = [[131, 14431], [231, 20558], [392, 40935], [157, 23524]]\n",
    "\n",
    "def lw_errors(slope, intercept):\n",
    "    fig, ax = plt.subplots(figsize=(6,6))\n",
    "\n",
    "    ax.scatter(little_women['Periods'], \n",
    "               little_women['Characters'],  \n",
    "               color='darkblue')\n",
    "\n",
    "    x_label = 'Periods'\n",
    "\n",
    "    y_label = 'Characters'\n",
    "\n",
    "    y_vals = ax.get_yticks()\n",
    "\n",
    "    plt.ylabel(y_label)\n",
    "\n",
    "    plt.xlabel(x_label)\n",
    "    \n",
    "    xlims = np.array([50, 450])\n",
    "    \n",
    "    plt.plot(xlims, (slope * xlims) + intercept, lw=2)\n",
    "    \n",
    "    for x, y in sample:\n",
    "        plt.plot([x, x], [y, slope * x + intercept], color='r', lw=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Slope of Regression Line:     87.0 characters per period\n",
      "Intercept of Regression Line: 4745.0 characters\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 33792 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print('Slope of Regression Line:    ', np.round(lw_reg_slope), 'characters per period')\n",
    "print('Intercept of Regression Line:', np.round(lw_reg_intercept), 'characters')\n",
    "lw_errors(lw_reg_slope, lw_reg_intercept)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Had we used a different line to create our estimates, the errors would have been different. The graph below shows how big the errors would be if we were to use another line for estimation. The second graph shows large errors obtained by using a line that is downright silly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30532 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lw_errors(50, 10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35836 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lw_errors(-100, 50000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Root Mean Squared Error\n",
    "What we need now is one overall measure of the rough size of the errors. You will recognize the approach to creating this – it's exactly the way we developed the SD.\n",
    "\n",
    "If you use any arbitrary line to calculate your estimates, then some of your errors are likely to be positive and others negative. To avoid cancellation when measuring the rough size of the errors, we will take the mean of the squared errors rather than the mean of the errors themselves. \n",
    "\n",
    "The mean squared error of estimation is a measure of roughly how big the squared errors are, but as we have noted earlier, its units are hard to interpret. Taking the square root yields the root mean square error (rmse), which is in the same units as the variable being predicted and therefore much easier to understand. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Minimizing the Root Mean Squared Error\n",
    "Our observations so far can be summarized as follows.\n",
    "\n",
    "- To get estimates of $y$ based on $x$, you can use any line you want.\n",
    "- Every line has a root mean squared error of estimation.\n",
    "- \"Better\" lines have smaller errors.\n",
    "\n",
    "Is there a \"best\" line? That is, is there a line that minimizes the root mean squared error among all lines? \n",
    "\n",
    "To answer this question, we will start by defining a function `lw_rmse` to compute the root mean squared error of any line through the Little Women scatter diagram. The function takes the slope and the intercept (in that order) as its arguments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lw_rmse(slope, intercept):\n",
    "    lw_errors(slope, intercept)\n",
    "    x = little_women['Periods']\n",
    "    y = little_women['Characters']\n",
    "    fitted = slope * x + intercept\n",
    "    mse = np.mean((y - fitted) ** 2)\n",
    "    print(\"Root mean squared error:\", mse ** 0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Root mean squared error: 4322.167831766537\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 30532 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lw_rmse(50, 10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Root mean squared error: 16710.11983735375\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 35836 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lw_rmse(-100, 50000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bad lines have big values of rmse, as expected. But the rmse is much smaller if we choose a slope and intercept close to those of the regression line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Root mean squared error: 2715.5391063834586\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 33916 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lw_rmse(90, 4000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is the root mean squared error corresponding to the regression line. By a remarkable fact of mathematics, no other line can beat this one. \n",
    "\n",
    "- **The regression line is the unique straight line that minimizes the mean squared error of estimation among all straight lines.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Root mean squared error: 2701.6907853118555\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 33792 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lw_rmse(lw_reg_slope, lw_reg_intercept)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The proof of this statement requires abstract mathematics that is beyond the scope of this course. On the other hand, we do have a powerful tool – Python – that performs large numerical computations with ease. So we can use Python to confirm that the regression line minimizes the mean squared error."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Numerical Optimization\n",
    "First note that a line that minimizes the root mean squared error is also a line that minimizes the squared error. The square root makes no difference to the minimization. So we will save ourselves a step of computation and just minimize the mean squared error (mse).\n",
    "\n",
    "We are trying to predict the number of characters ($y$) based on the number of periods ($x$) in chapters of Little Women. If we use the line \n",
    "$$\n",
    "\\mbox{prediction} ~=~ ax + b\n",
    "$$\n",
    "it will have an mse that depends on the slope $a$ and the intercept $b$. The function `lw_mse` takes the slope and intercept as its arguments and returns the corresponding mse."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lw_mse(any_slope, any_intercept):\n",
    "    x = little_women['Periods']\n",
    "    y = little_women['Characters']\n",
    "    fitted = any_slope*x + any_intercept\n",
    "    return np.mean((y - fitted) ** 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's check that `lw_mse` gets the right answer for the root mean squared error of the regression line. Remember that `lw_mse` returns the mean squared error, so we have to take the square root to get the rmse."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2701.6907853118555"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lw_mse(lw_reg_slope, lw_reg_intercept)**0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's the same as the value we got by using `lw_rmse` earlier:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Root mean squared error: 2701.6907853118555\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 33792 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lw_rmse(lw_reg_slope, lw_reg_intercept)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can confirm that `lw_mse` returns the correct value for other slopes and intercepts too. For example, here is the rmse of the extremely bad line that we tried earlier."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16710.11983735375"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lw_mse(-100, 50000)**0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here is the rmse for a line that is close to the regression line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2715.5391063834586"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lw_mse(90, 4000)**0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we experiment with different values, we can find a low-error slope and intercept through trial and error, but that would take a while. Fortunately, there is a Python function that does all the trial and error for us.\n",
    "\n",
    "The `minimize` function (defined below) can be used to find the arguments of a function for which the function returns its minimum value. Python uses a similar trial-and-error approach, following the changes that lead to incrementally lower output values. \n",
    "\n",
    "The argument of `minimize` is a function that itself takes numerical arguments and returns a numerical value. For example, the function `lw_mse` takes a numerical slope and intercept as its arguments and returns the corresponding mse. \n",
    "\n",
    "The call `minimize(lw_mse)` returns an array consisting of the slope and the intercept that minimize the mse. These minimizing values are excellent approximations arrived at by intelligent trial-and-error, not exact values based on formulas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import optimize\n",
    "\n",
    "def minimize(f, start=None, smooth=False, log=None, array=False, **vargs):\n",
    "    \"\"\"Minimize a function f of one or more arguments.\n",
    "    Args:\n",
    "        f: A function that takes numbers and returns a number\n",
    "        start: A starting value or list of starting values\n",
    "        smooth: Whether to assume that f is smooth and use first-order info\n",
    "        log: Logging function called on the result of optimization (e.g. print)\n",
    "        vargs: Other named arguments passed to scipy.optimize.minimize\n",
    "    Returns either:\n",
    "        (a) the minimizing argument of a one-argument function\n",
    "        (b) an array of minimizing arguments of a multi-argument function\n",
    "    \"\"\"\n",
    "    if start is None:\n",
    "        assert not array, \"Please pass starting values explicitly when array=True\"\n",
    "        arg_count = f.__code__.co_argcount\n",
    "        assert arg_count > 0, \"Please pass starting values explicitly for variadic functions\"\n",
    "        start = [0] * arg_count\n",
    "    if not hasattr(start, '__len__'):\n",
    "        start = [start]\n",
    "\n",
    "    if array:\n",
    "        objective = f\n",
    "    else:\n",
    "        @functools.wraps(f)\n",
    "        def objective(args):\n",
    "            return f(*args)\n",
    "\n",
    "    if not smooth and 'method' not in vargs:\n",
    "        vargs['method'] = 'Powell'\n",
    "    result = optimize.minimize(objective, start, **vargs)\n",
    "    if log is not None:\n",
    "        log(result)\n",
    "    if len(start) == 1:\n",
    "        return result.x.item(0)\n",
    "    else:\n",
    "        return result.x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  86.97784101, 4744.78484127])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best = minimize(lw_mse)\n",
    "best"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These values are the same as the values we calculated earlier by using the `slope` and `intercept` functions. We see small deviations due to the inexact nature of `minimize`, but the values are essentially the same."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "slope from formula:         86.97784125829823\n",
      "slope from minimize:        86.97784101391096\n",
      "intercept from formula:     4744.784796574924\n",
      "intercept from minimize:    4744.784841267639\n"
     ]
    }
   ],
   "source": [
    "print(\"slope from formula:        \", lw_reg_slope)\n",
    "print(\"slope from minimize:       \", best.item(0))\n",
    "print(\"intercept from formula:    \", lw_reg_intercept)\n",
    "print(\"intercept from minimize:   \", best.item(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Least Squares Line\n",
    "Therefore, we have found not only that the regression line minimizes mean squared error, but also that minimizing mean squared error gives us the regression line. The regression line is the only line that minimizes mean squared error.\n",
    "\n",
    "That is why the regression line is sometimes called the \"least squares line.\""
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
