{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 1: Learning math with code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**NOTE:** Most of this code is not explained in the chapter. It's just here in case you are curious!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 Solving lucrative problems with math and software"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.1 Predicting financial market movements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from scipy import stats\n",
    "from math import sqrt, log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26812 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ys = [30]\n",
    "xs = range(0,501)\n",
    "np.random.seed(seed=42)\n",
    "for delta in np.random.normal(0,0.5,500):\n",
    "    ys.append(ys[-1] + delta)\n",
    "    \n",
    "plt.plot(ys)\n",
    "plt.ylabel('Stock Price ($)')\n",
    "plt.xlabel('Elapsed Time (min)')\n",
    "plt.savefig('figures/1.01.svg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = stats.linregress(xs,ys)\n",
    "line =  [r.slope * x + r.intercept for x in xs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "std = np.std([(y-y0) for y,y0 in zip(ys,line)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "top = [y + std for y in line]\n",
    "bottom = [y - std for y in line]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39056 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(xs,ys)\n",
    "plt.plot(xs,line)\n",
    "plt.plot(xs,top)\n",
    "plt.plot(xs,bottom)\n",
    "plt.ylabel('Stock Price ($)')\n",
    "plt.xlabel('Elapsed Time (min)')\n",
    "plt.savefig('figures/1.02.svg')\n",
    "plt.savefig('figures/1.03.svg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.2 Finding a good deal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "mileages = [4.1429, 8.9173, 6.5, 6.0601, 12.3, 6.2, 2.5782, 0.9, 1.7, 13.1045, 24.7, 9.2699, 17.2, 10.0, 10.0, 2.8, 12.3773, 19.6, 7.3397, 2.1178, 12.9886, 10.9884, 16.9, 6.0, 12.9, 8.1936, 10.5, 8.0713, 1.7, 10.0, 15.6097, 17.0, 16.7, 5.6, 11.3, 19.9, 9.6, 21.6, 20.3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "prices = [16980.0, 15973.0, 9900.0, 15998.0, 3900.0, 12540.0, 21688.0, 17086.0, 23000.0, 8900.0, 3875.0, 10500.0, 3500.0, 26992.0, 17249.0, 19627.0, 9450.0, 3000.0, 14999.0, 24990.0, 7967.0, 7257.0, 4799.0, 13982.0, 5299.0, 14310.0, 7800.0, 12250.0, 23000.0, 14686.0, 7495.0, 4950.0, 3500.0, 11999.0, 9600.0, 1999.0, 4300.0, 3500.0, 4200.0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def price(mileage): \n",
    "    return 26500 * (0.905 ** mileage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16328 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "xs = np.linspace(0,25,100)\n",
    "ys = [price(x) for x in xs]\n",
    "\n",
    "plt.scatter(mileages,prices) \n",
    "plt.plot(xs,ys, color='C1')\n",
    "plt.xlabel('Mileage (10,000s of miles)')\n",
    "plt.ylabel('Price ($)')\n",
    "plt.savefig('fig1.5.svg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17460 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "xs = np.linspace(0,25,100)\n",
    "ys = [price(mileage) for mileage in xs]\n",
    "\n",
    "\n",
    "target_mileage = log(10/26.5)/log(0.905)\n",
    "\n",
    "plt.scatter(mileages,prices) \n",
    "plt.plot(xs,ys, color='C1')\n",
    "\n",
    "xlim,ylim = plt.xlim(), plt.ylim()\n",
    "\n",
    "plt.plot([-5,30],[10000,10000],color=\"gray\",linestyle=\"dashed\")\n",
    "plt.plot([target_mileage,target_mileage],[-5000,10000],color=\"gray\",linestyle=\"dashed\")\n",
    "plt.xlim(*xlim)\n",
    "plt.ylim(*ylim)\n",
    "plt.xlabel('Mileage (10,000s of miles)')\n",
    "plt.ylabel('Price ($)')\n",
    "plt.savefig('fig1.6.svg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6070757653156251"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.905 ** 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.3 Building 3D graphics and animations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 46244 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 432x288 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append('../Chapter 03')\n",
    "from draw3d import *\n",
    "\n",
    "triangle = [(2.3,1.1,0.9), (4.5,3.3,2.0), (1.0,3.5,3.9)]\n",
    "\n",
    "draw3d(\n",
    "    Polygon3D(*triangle),\n",
    "    Points3D(*triangle),\n",
    "    axes=False,\n",
    "    origin=False\n",
    ")\n",
    "plt.savefig('figures/1.08.svg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
