{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33656 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "SENSITIVITY = 0.95\n",
    "SPECIFICITY = 0.95\n",
    "\n",
    "def posterior(prior, sensitivity, specificity):\n",
    "    return (sensitivity * prior) / (sensitivity * prior + (1 - specificity) * (1 - prior))\n",
    "\n",
    "priors = np.linspace(0, 1, 100)\n",
    "posteriors = posterior(priors, SENSITIVITY, SPECIFICITY)\n",
    "\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.plot(priors, posteriors, color = 'purple')\n",
    "plt.xlim(0, 1) \n",
    "plt.ylim(0, 1)\n",
    "plt.xlabel('Prior')\n",
    "plt.ylabel('Posterior')\n",
    "plt.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37612 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "prior = 0.01\n",
    "specificity = 0.90\n",
    "\n",
    "def posterior_prob(prior, sensitivity, specificity):\n",
    "    return (sensitivity * prior) / ((sensitivity * prior) + ((1 - specificity) * (1 - prior)))\n",
    "\n",
    "sensitivities = np.linspace(0, 1, 100)\n",
    "posteriors = [posterior_prob(prior, sens, specificity) for sens in sensitivities]\n",
    "\n",
    "# Plot\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.plot(sensitivities, posteriors, color = 'red')\n",
    "plt.xlabel('Sensitivity')\n",
    "plt.ylabel('Posterior')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25080 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "prior = 0.01\n",
    "sensitivity = 0.95\n",
    "\n",
    "def posterior_prob(prior, sensitivity, specificity):\n",
    "    return (sensitivity * prior) / ((sensitivity * prior) + ((1 - specificity) * (1 - prior)))\n",
    "\n",
    "# Vary specificity from 0 to 1\n",
    "specificities = np.linspace(0, 1, 100)\n",
    "posteriors = [posterior_prob(prior, sensitivity, sp) for sp in specificities]\n",
    "\n",
    "# Plot\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.plot(specificities, posteriors, color = 'blue') \n",
    "plt.xlabel('Specificity')\n",
    "plt.ylabel('Posterior')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "As the disease becomes more common (higher prior), the posterior increases because a positive test is more believable.\n",
    "\n",
    "Increasing sensitivity (correctly detecting disease) lowers false negatives, raising the posterior.\n",
    "\n",
    "Increasing specificity (correctly identifying healthy individuals) lowers false positives, also raising the posterior.\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 698044 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 466360 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 557248 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "\n",
    "def posterior_prob(prior, sensitivity, specificity, eps=1e-10):\n",
    "    denom = (sensitivity * prior) + ((1 - specificity) * (1 - prior))\n",
    "    return (sensitivity * prior) / (denom + eps)\n",
    "\n",
    "points = 120\n",
    "prior_values = np.linspace(0, 1, points)\n",
    "sensitivity_values = np.linspace(0, 1, points)\n",
    "specificity_values = np.linspace(0, 1, points)\n",
    "\n",
    "# First graph\n",
    "fixed_specificity = 0.90\n",
    "prior_grid, sensitivity_grid = np.meshgrid(prior_values, sensitivity_values)\n",
    "posterior_grid1 = posterior_prob(prior_grid, sensitivity_grid, fixed_specificity)\n",
    "fig1 = plt.figure(figsize=(10, 10))\n",
    "graph1 = fig1.add_subplot(111, projection='3d')\n",
    "graph1.scatter(prior_grid, sensitivity_grid, posterior_grid1, c='purple', marker='x', s=10)\n",
    "graph1.set_xlabel('Prior')\n",
    "graph1.set_ylabel('Sensitivity')\n",
    "graph1.set_zlabel('Posterior')\n",
    "plt.tight_layout()\n",
    "plt.show()\n",
    "\n",
    "# Second graph\n",
    "fixed_sensitivity = 0.95\n",
    "prior_grid2, specificity_grid = np.meshgrid(prior_values, specificity_values)\n",
    "posterior_grid2 = posterior_prob(prior_grid2, fixed_sensitivity, specificity_grid)\n",
    "fig2 = plt.figure(figsize=(10, 10))\n",
    "graph2 = fig2.add_subplot(111, projection='3d')\n",
    "graph2.scatter(prior_grid2, specificity_grid, posterior_grid2, c='red', marker='o', s=10)\n",
    "graph2.set_xlabel('Prior')\n",
    "graph2.set_ylabel('Specificity')\n",
    "graph2.set_zlabel('Posterior')\n",
    "plt.tight_layout()\n",
    "plt.show()\n",
    "\n",
    "# Third graph\n",
    "fixed_prior = 0.01\n",
    "sensitivity_grid3, specificity_grid3 = np.meshgrid(sensitivity_values, specificity_values)\n",
    "posterior_grid3 = posterior_prob(fixed_prior, sensitivity_grid3, specificity_grid3)\n",
    "fig3 = plt.figure(figsize=(10, 10))\n",
    "graph3 = fig3.add_subplot(111, projection='3d')\n",
    "graph3.scatter(sensitivity_grid3, specificity_grid3, posterior_grid3, c='blue', marker='.', s=10)\n",
    "graph3.set_xlabel('Sensitivity')\n",
    "graph3.set_ylabel('Specificity')\n",
    "graph3.set_zlabel('Posterior')\n",
    "graph3.view_init(elev=30, azim=45)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pystan_env",
   "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.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
