{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualization of Kernel Density Estimation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats import norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26616 characters]",
      "text/plain": [
       "<Figure size 600x240 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "samples = [-1.9, -1.6, -0.9, -0.8, -0.6, 0.1, 0.9]\n",
    "h = 0.4\n",
    "nh = len(samples) * h\n",
    "x = np.linspace(-4, 3, 200)\n",
    "y = np.array([sum([norm.pdf(xval, loc=sample, scale=h) for sample in samples]) for xval in x]) / nh\n",
    "\n",
    "plt.rcParams['text.usetex'] = True\n",
    "plt.rcParams.update({\"font.size\": 18})\n",
    "plt.figure(figsize=(10, 4), dpi=60)\n",
    "# draw samples\n",
    "plt.scatter(samples, np.zeros_like(samples) + 0.02, marker=\"x\", color=\"black\", label=\"Samples\")\n",
    "# draw density estimation\n",
    "plt.plot(x, y, color=\"red\", label=r\"$\\hat{f}(x)$\")\n",
    "# draw individual samples\n",
    "for i, sample in enumerate(samples):\n",
    "    xmin = norm.ppf(0.01, loc=sample, scale=h)\n",
    "    xmax = norm.ppf(0.99, loc=sample, scale=h)\n",
    "    x = np.linspace(xmin, xmax, 50)\n",
    "    plt.plot(x, norm.pdf(x, loc=sample, scale=h) / nh, lw=1, color=\"orange\", label=\"Kernels\" if i == 0 else None)\n",
    "    plt.plot([sample, sample], [0, norm.pdf(sample, loc=sample, scale=h) / nh], color=\"gray\", ls=\"dotted\", lw=0.8)\n",
    "plt.legend()\n",
    "plt.ylim((0.0, 1.2))\n",
    "plt.savefig(f\"kde.png\", bbox_inches=\"tight\")\n",
    "plt.savefig(f\"kde.pdf\", bbox_inches=\"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('pytorch-gpu')",
   "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.10.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "17cd5c528a3345b75540c61f907eece919c031d57a2ca1e5653325af249173c9"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
