{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ROC curves\n",
    "The purpose of this notebook to generate and plot different ROC curves for illustration purposes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import roc_curve, auc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Random guesser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12156 characters]",
      "text/plain": [
       "<Figure size 288x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels = np.random.randint(2, size=500)\n",
    "df = np.random.standard_normal(size=500)\n",
    "fpr, tpr, thresholds = roc_curve(labels, df)\n",
    "auc_score = auc(fpr, tpr)\n",
    "\n",
    "plt.figure(figsize=(4, 4))\n",
    "plt.plot(fpr, tpr, lw=1)\n",
    "plt.fill_between(fpr, tpr, label=f\"AUC = {auc_score:.3f}\", alpha=0.5)\n",
    "plt.plot([0, 1], [0, 1], color=\"gray\", linestyle=\"dotted\")\n",
    "plt.xlim([0.0, 1.0])\n",
    "plt.ylim([0.0, 1.0])\n",
    "plt.xlabel(\"FPR\")\n",
    "plt.ylabel(\"TPR\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "plt.savefig(f\"roc_random.png\", bbox_inches=\"tight\")\n",
    "plt.savefig(f\"roc_random.pdf\", bbox_inches=\"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Good classifier\n",
    "Simulated by two normal distributions, one with $\\mu=1$ for positively labeled samples and one with $\\mu=-1$ for negatively labeled samples. The overlap of the two distributions ensures that the \"classifier\" still makes mistakes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 11572 characters]",
      "text/plain": [
       "<Figure size 288x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels = np.random.randint(2, size=500)\n",
    "df = np.where(labels == 1, np.random.normal(loc=1.0, size=500), np.random.normal(loc=-1.0, size=500))\n",
    "fpr, tpr, thresholds = roc_curve(labels, df)\n",
    "auc_score = auc(fpr, tpr)\n",
    "\n",
    "plt.figure(figsize=(4, 4))\n",
    "plt.plot(fpr, tpr, lw=1)\n",
    "plt.fill_between(fpr, tpr, label=f\"AUC = {auc_score:.3f}\", alpha=0.5)\n",
    "plt.plot([0, 1], [0, 1], color=\"gray\", linestyle=\"dotted\")\n",
    "plt.xlim([0.0, 1.0])\n",
    "plt.ylim([0.0, 1.0])\n",
    "plt.xlabel(\"FPR\")\n",
    "plt.ylabel(\"TPR\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "plt.savefig(f\"roc_goodclf.png\", bbox_inches=\"tight\")\n",
    "plt.savefig(f\"roc_goodclf.pdf\", bbox_inches=\"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Inverse\n",
    "If we inverse the decision function, the $AUC$ changes to $1-AUC$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12192 characters]",
      "text/plain": [
       "<Figure size 288x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fpr, tpr, thresholds = roc_curve(labels, -df)\n",
    "auc_score = auc(fpr, tpr)\n",
    "\n",
    "plt.figure(figsize=(4, 4))\n",
    "plt.plot(fpr, tpr, lw=1)\n",
    "plt.fill_between(fpr, tpr, label=f\"AUC = {auc_score:.3f}\", alpha=0.5)\n",
    "plt.plot([0, 1], [0, 1], color=\"gray\", linestyle=\"dotted\")\n",
    "plt.xlim([0.0, 1.0])\n",
    "plt.ylim([0.0, 1.0])\n",
    "plt.xlabel(\"FPR\")\n",
    "plt.ylabel(\"TPR\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "plt.savefig(f\"roc_goodclf_inv.png\", bbox_inches=\"tight\")\n",
    "plt.savefig(f\"roc_goodclf_inv.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
}
