{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from random import random\n",
    "from math import log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 42448 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eta = 1\n",
    "beta = 10 #Try beta=50 also.\n",
    "delta = 1 \n",
    "dt = .005\n",
    "\n",
    "t = np.linspace(0,100,int(100 / dt))\n",
    "B = np.zeros_like(t)\n",
    "P = np.zeros_like(t)\n",
    "\n",
    "B[0] = .01\n",
    "P[0] = .000001\n",
    "for i in range (0, int(100 / dt) - 1):\n",
    "    B[i+1] = B[i] + dt * (B[i] * (1 - B[i]) - eta * B[i] * P[i])\n",
    "    P[i+1] = P[i] + dt * (beta * eta * B[i] * P[i] - delta * P[i])\n",
    "\n",
    "plt.title(\"Without resistant\")\n",
    "plt.plot(t, B, label=\"Bacteria\")\n",
    "plt.plot(t, P, label=\"Phage\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 49040 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eta = 1\n",
    "beta = 10 #Try beta=50 also.\n",
    "delta = 1 \n",
    "dt = .005\n",
    "g = 0.8\n",
    "alpha = 0.2\n",
    "\n",
    "t = np.linspace(0,1000,int(1000 / dt))\n",
    "B = np.zeros_like(t)\n",
    "P = np.zeros_like(t)\n",
    "R = np.zeros_like(t)\n",
    "\n",
    "B[0] = .01\n",
    "P[0] = .000001\n",
    "R[0] = .01\n",
    "\n",
    "for i in range (0, int(1000 / dt) - 1):\n",
    "    B[i+1] = B[i] + dt * (B[i] * (1 - (B[i] + R[i])) - alpha * B[i] - B[i] * eta * P[i])\n",
    "    R[i+1] = R[i] + dt * (g * R[i] * (1 - (B[i] + R[i])) - alpha * R[i])\n",
    "    P[i+1] = P[i] + dt * (beta * B[i] * eta * P[i] - delta * P[i])\n",
    "\n",
    "\n",
    "plt.title(\"With resistant\")\n",
    "plt.plot(t, B, label=\"Bacteria\")\n",
    "plt.plot(t, R, label=\"Resistant Bacteria\")\n",
    "plt.plot(t, P, label=\"Phage\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 55712 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 12.3.3\n",
    "\n",
    "Omega = 2\n",
    "omega = 1e-3 \n",
    "p = .1\n",
    "x1 = .01\n",
    "x2 = .9\n",
    "xstar = p * Omega / (Omega - 1) - (1-p) * omega / (omega - 1)\n",
    "if xstar > 1:\n",
    "    xstar = 1\n",
    "elif xstar < 0:\n",
    "    xstar = 0\n",
    "\n",
    "steps = 500\n",
    "\n",
    "Lambda1 = np.zeros(steps)\n",
    "Lambda2 = np.zeros(steps)\n",
    "Lambdastar = np.zeros(steps)\n",
    "\n",
    "\n",
    "for i in range (0, steps - 1):\n",
    "    if random() < p:\n",
    "        Lambda1[i+1] = Lambda1[i] + log(omega * (1-x1) + x1)\n",
    "        Lambda2[i+1] = Lambda2[i] + log(omega * (1-x2) + x2)\n",
    "        Lambdastar[i+1] = Lambdastar[i] + log(omega * (1-xstar) + xstar)\n",
    "\n",
    "    else:\n",
    "        Lambda1[i+1] = Lambda1[i] + log(Omega * (1-x1) + x1)\n",
    "        Lambda2[i+1] = Lambda2[i] + log(Omega * (1-x2) + x2)        \n",
    "        Lambdastar[i+1] = Lambdastar[i] + log(Omega * (1-xstar) + xstar)\n",
    "\n",
    "\n",
    "plt.title(\"Bet Hedging\")\n",
    "plt.plot(Lambda1, label=\"x = 0.01\")\n",
    "plt.plot(Lambda2, label=\"x = 0.9\")\n",
    "plt.plot(Lambdastar, label=f\"x* = {round(xstar,2)}\")\n",
    "plt.ylabel(\"log(Population)\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
