{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from utils import *\n",
    "from ipywidgets import interact, widgets\n",
    "from scipy import stats\n",
    "np.set_printoptions(precision=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualizing multivariate gaussian distributions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linspace(-3, 3, 101)\n",
    "y = np.linspace(-3, 3, 101)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "XY = np.stack([X, Y], axis=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_mvg(mean, cov):\n",
    "    plt.figure(figsize=(7.5, 6))\n",
    "    mvg = stats.multivariate_normal(mean, cov)\n",
    "    Z = -mvg.logpdf(XY)\n",
    "    plt.title(\"Negative log probability\")\n",
    "    plt.contourf(X, Y, Z, cmap=plt.cm.Blues, vmin=0.0, vmax=10.0)\n",
    "    plt.colorbar()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Covariance matrices lambda * id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "66ac859e5adb44dfac28272d1b88a4ce",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=50.5, description='lam', min=1.0), Output()), _dom_classes=('widget-in…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>(lam)>"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interact(lambda lam: plot_mvg(np.zeros(2), 0.1 * lam * np.eye(2)), lam=(1.0, 100.0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Diagonal covariance matrices\n",
    "\n",
    "covariance matrix will look like \n",
    "\n",
    "    [lam1 0]\n",
    "    [0 lam2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "df7b7ba203e948e69a2fff48b9275438",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=50.5, description='lam1', min=1.0), FloatSlider(value=50.5, descriptio…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>(lam1, lam2)>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interact(lambda lam1, lam2: plot_mvg(np.zeros(2), 0.1 * np.array([[lam1, 0], [0, lam2]])), \n",
    "         lam1=(1.0, 100.0), lam2=(1.0, 100.0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Full covariance matrices\n",
    "covariance matrix will look like\n",
    "\n",
    "    [sig_11 sig_21\n",
    "     sig_12 sig_22]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fc5f2daecac04d2495ddd72fac0f9059",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=5.5, description='sig11', max=10.0, min=1.0), FloatSlider(value=3.1415…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>(sig11, sig21, sig22)>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eps = 1e-3\n",
    "interact(lambda sig11, sig21, sig22: plot_mvg(np.zeros(2), \n",
    "        \n",
    "         0.1 * np.array([[sig11, np.sqrt(sig11 * sig22) * np.cos(sig21) + eps], \n",
    "                         [np.sqrt(sig11 * sig22) * np.cos(sig21) + eps, sig22]])), \n",
    "         \n",
    "         sig11=(1.0, 10.0), sig21=(0, 2 * np.pi), sig22=(1.0, 10.0))"
   ]
  },
  {
   "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
