{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "import GPy\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "from scipy import stats\n",
    "from scipy.stats import multivariate_normal\n",
    "\n",
    "# See kernels: https://www.cs.toronto.edu/~duvenaud/cookbook/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f9ad4bde5f8>]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18876 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "np.random.seed(5)\n",
    "x = np.linspace(0, 10)[:, None]\n",
    "y = np.sin(x)\n",
    "\n",
    "X = np.random.choice(x.flatten(), 7, replace=False)[:, None]\n",
    "Y = np.sin(X)\n",
    "\n",
    "plt.plot(x, y)\n",
    "plt.plot(X, Y, 'o')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       ".tg  {font-family:\"Courier New\", Courier, monospace !important;padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;}\n",
       ".tg td{font-family:\"Courier New\", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}\n",
       ".tg th{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}\n",
       ".tg .tg-left{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;text-align:left;}\n",
       ".tg .tg-center{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;text-align:center;}\n",
       ".tg .tg-right{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;text-align:right;}\n",
       "</style>\n",
       "<table class=\"tg\"><tr><th><b>  rbf.       </b></th><th><b>value</b></th><th><b>constraints</b></th><th><b>priors</b></th></tr>\n",
       "<tr><td class=tg-left>  variance   </td><td class=tg-right>  1.0</td><td class=tg-center>    +ve    </td><td class=tg-center>      </td></tr>\n",
       "<tr><td class=tg-left>  lengthscale</td><td class=tg-right>  1.0</td><td class=tg-center>    +ve    </td><td class=tg-center>      </td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<GPy.kern.src.rbf.RBF at 0x7f9ad4b41eb8>"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kernel = GPy.kern.RBF(input_dim=1)\n",
    "kernel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type=\"text/css\">\n",
       ".pd{\n",
       "    font-family: \"Courier New\", Courier, monospace !important;\n",
       "    width: 100%;\n",
       "    padding: 3px;\n",
       "}\n",
       "</style>\n",
       "\n",
       "<p class=pd>\n",
       "<b>Model</b>: GP regression<br>\n",
       "<b>Objective</b>: 9.071735102889447<br>\n",
       "<b>Number of Parameters</b>: 3<br>\n",
       "<b>Number of Optimization Parameters</b>: 3<br>\n",
       "<b>Updates</b>: True<br>\n",
       "</p>\n",
       "<style type=\"text/css\">\n",
       ".tg  {font-family:\"Courier New\", Courier, monospace !important;padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;}\n",
       ".tg td{font-family:\"Courier New\", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}\n",
       ".tg th{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}\n",
       ".tg .tg-left{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;text-align:left;}\n",
       ".tg .tg-center{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;text-align:center;}\n",
       ".tg .tg-right{font-family:\"Courier New\", Courier, monospace !important;font-weight:normal;text-align:right;}\n",
       "</style>\n",
       "<table class=\"tg\"><tr><th><b>  GP_regression.         </b></th><th><b>value</b></th><th><b>constraints</b></th><th><b>priors</b></th></tr>\n",
       "<tr><td class=tg-left>  rbf.variance           </td><td class=tg-right>  1.0</td><td class=tg-center>    +ve    </td><td class=tg-center>      </td></tr>\n",
       "<tr><td class=tg-left>  rbf.lengthscale        </td><td class=tg-right>  1.0</td><td class=tg-center>    +ve    </td><td class=tg-center>      </td></tr>\n",
       "<tr><td class=tg-left>  Gaussian_noise.variance</td><td class=tg-right>  1.0</td><td class=tg-center>    +ve    </td><td class=tg-center>      </td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<GPy.models.gp_regression.GPRegression at 0x7f9acf6c27b8>"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = GPy.models.GPRegression(X, Y, kernel)\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'dataplot': [<matplotlib.collections.PathCollection at 0x7f9acd282278>],\n",
       " 'gpmean': [[<matplotlib.lines.Line2D at 0x7f9acd1d9470>]],\n",
       " 'gpconfidence': [<matplotlib.collections.PolyCollection at 0x7f9acd1d98d0>]}"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 28708 characters]\n",
      "text/plain": [
       "<Figure size 1152x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "kernel = GPy.kern.RBF(input_dim=1)\n",
    "m = GPy.models.GPRegression(X, Y, kernel)\n",
    "m.plot(plot_density=False, figsize=(16, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<paramz.optimization.optimization.opt_lbfgsb at 0x7f9ace7c5160>"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.optimize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'dataplot': [<matplotlib.collections.PathCollection at 0x7f9acd268978>],\n",
       " 'gpmean': [[<matplotlib.lines.Line2D at 0x7f9acd268940>]],\n",
       " 'gpconfidence': [<matplotlib.collections.PolyCollection at 0x7f9acd2689e8>]}"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " /opt/miniconda3/lib/python3.7/site-packages/matplotlib/figure.py:2369: UserWarning:This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 42764 characters]\n",
      "text/plain": [
       "<Figure size 1152x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "m.plot(plot_density=0, figsize=(16, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 90100 characters]\n",
      "text/plain": [
       "<Figure size 1152x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# make prediction by parameterizing Gaussian\n",
    "mu, cov = m.predict(x, full_cov=True)\n",
    "a = multivariate_normal(mu.flatten(), cov).rvs(75)\n",
    "\n",
    "plt.figure(figsize=(16, 4))\n",
    "for i in range(a.shape[0]):\n",
    "    plt.plot(a[i, :], alpha=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 84868 characters]\n",
      "text/plain": [
       "<Figure size 1152x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# or sample directly from posterior\n",
    "a = m.posterior_samples(x, size=75)\n",
    "a = np.squeeze(a)\n",
    "\n",
    "plt.figure(figsize=(16, 4))\n",
    "for i in range(a.shape[0]):\n",
    "    plt.plot(a[:, i], alpha=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'gpmean': [[<matplotlib.lines.Line2D at 0x7f9ace055ef0>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace31e0f0>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace04b9b0>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace04bb70>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace0564e0>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace056828>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace056b70>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace056eb8>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace056f98>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accff1588>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accff18d0>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9ace060160>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accff1b70>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accfd3fd0>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accfd7e80>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accfc1c18>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accfd8d30>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accfebb00>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accfebbe0>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accffdf98>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accfe87b8>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accfe8898>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accedb400>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accedb5c0>],\n",
       "  [<matplotlib.lines.Line2D at 0x7f9accedbd68>]]}"
      ]
     },
     "execution_count": 269,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " /opt/miniconda3/lib/python3.7/site-packages/matplotlib/figure.py:2369: UserWarning:This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 159700 characters]\n",
      "text/plain": [
       "<Figure size 1152x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot prior\n",
    "kernel = GPy.kern.RBF(input_dim=1, lengthscale=1, variance=0.001)\n",
    "m = GPy.models.GPRegression(X, Y, kernel)\n",
    "m.plot_samples(samples=25, figsize=(16, 4))"
   ]
  },
  {
   "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": 4
}
