{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from random import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 44768 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 6.1.3 \n",
    "\n",
    "M = 0\n",
    "P = 0\n",
    "steps = 1000\n",
    "\n",
    "Omega = .02\n",
    "omega = .1\n",
    "gamma_m = .01\n",
    "gamma_p = .002\n",
    "\n",
    "P_history = np.zeros(steps)\n",
    "M_history = np.zeros(steps)\n",
    "time = np.zeros(steps)\n",
    "\n",
    "\n",
    "for n in range(0,steps):\n",
    "\n",
    "    t = np.zeros(4)\n",
    "    t[0] = - 1 / Omega * np.log(random())\n",
    "    \n",
    "    if M > 0:\n",
    "        t[1] = - 1 / (omega * M) * np.log(random())\n",
    "        t[2] = - 1 / (gamma_m * M) * np.log(random())\n",
    "    else:\n",
    "        t[1] = np.inf\n",
    "        t[2] = np.inf\n",
    "    \n",
    "    if P > 0:\n",
    "        t[3] = - 1 / (gamma_p * P) * np.log(random())\n",
    "    else:\n",
    "        t[3] = np.inf\n",
    "\n",
    "\n",
    "    min = np.argmin(t)\n",
    "\n",
    "    if min == 0:\n",
    "        M += 1 \n",
    "    elif min == 1:\n",
    "        P += 1\n",
    "    elif min == 2:\n",
    "        M -= 1\n",
    "    elif min == 3:\n",
    "        P -= 1\n",
    "\n",
    "    if n == 0:\n",
    "        time[n] = 0\n",
    "    else:\n",
    "        time[n] = time[n-1] + t[min]\n",
    "    P_history[n] = P\n",
    "    M_history[n] = M\n",
    "\n",
    "\n",
    "plt.title(\"Protein and mRNA production / decay\")\n",
    "plt.plot(time, M_history, label=\"mRNA\")\n",
    "plt.plot(time, P_history, label=\"proteins\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 55424 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 6.1.4\n",
    "\n",
    "u = 0\n",
    "v = 0\n",
    "steps = 10000\n",
    "\n",
    "a = 10\n",
    "b = 10\n",
    "\n",
    "u_history = np.zeros(steps)\n",
    "v_history = np.zeros(steps)\n",
    "time = np.zeros(steps)\n",
    "\n",
    "\n",
    "for n in range(0,steps):\n",
    "\n",
    "    t = np.zeros(4)\n",
    "    t[0] = - 1 / (a / (1 + v**2)) * np.log(random())\n",
    "    t[1] = - 1 / (b / (1 + u**2)) * np.log(random())\n",
    "    if u > 0:\n",
    "        t[2] = - 1 / (u) * np.log(random())\n",
    "    else:\n",
    "        t[2] = np.inf\n",
    "    if v > 0:\n",
    "        t[3] = - 1 / (v) * np.log(random())\n",
    "    else:\n",
    "        t[3] = np.inf\n",
    "\n",
    "    min = np.argmin(t)\n",
    "\n",
    "    if min == 0:\n",
    "        u += 1 \n",
    "    elif min == 1:\n",
    "        v += 1\n",
    "    elif min == 2:\n",
    "        u -= 1\n",
    "    elif min == 3:\n",
    "        v -= 1\n",
    "    \n",
    "    if n == 0:\n",
    "        time[n] = 0\n",
    "    else:\n",
    "        time[n] = time[n-1] + t[min]\n",
    "    u_history[n] = u\n",
    "    v_history[n] = v\n",
    "\n",
    "\n",
    "plt.title(\"Toggle switch\")\n",
    "plt.plot(time, u_history, label=\"u\")\n",
    "plt.plot(time, v_history, label=\"v\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 65964 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 6.2.2\n",
    "\n",
    "C1 = 0\n",
    "Cro = 0\n",
    "steps = 100000\n",
    "\n",
    "delta_prod = .1\n",
    "delta_decay = .01\n",
    "\n",
    "time = np.zeros(steps)\n",
    "C1_history = np.zeros(steps)\n",
    "Cro_history = np.zeros(steps)\n",
    "\n",
    "for n in range(0,steps):\n",
    "\n",
    "    t = np.zeros(4)\n",
    "    t[0] = - 1 / ( (.1 + (C1 / .2)**2) / (delta_prod * (1 + (C1 / .2)**2)*(1 + (Cro / .5)**2)) ) * np.log(random())\n",
    "    t[1] = - 1 / ( 1 / (delta_prod * (1 + (C1 / .2)**2))) * np.log(random())\n",
    "    if C1 > 0:\n",
    "        t[2] = - 1 / (C1 / delta_decay) * np.log(random())\n",
    "    else:\n",
    "        t[2] = np.inf\n",
    "    if Cro > 0:\n",
    "        t[3] = - 1 / (Cro / delta_decay) * np.log(random())\n",
    "    else:\n",
    "        t[3] = np.inf\n",
    "\n",
    "    min = np.argmin(t)\n",
    "\n",
    "    if min == 0:\n",
    "        C1 += delta_prod \n",
    "    elif min == 1:\n",
    "        Cro += delta_prod\n",
    "    elif min == 2:\n",
    "        C1 -= delta_decay \n",
    "    elif min == 3:\n",
    "        Cro -= delta_decay   \n",
    "    \n",
    "    if n == 0:\n",
    "        time[n] = 0\n",
    "    else:\n",
    "        time[n] = time[n-1] + t[min]\n",
    "    C1_history[n] = C1\n",
    "    Cro_history[n] = Cro\n",
    "\n",
    "\n",
    "plt.title(\"CI and Cro\")\n",
    "plt.plot(time, C1_history, label=\"CI\")\n",
    "plt.plot(time, Cro_history, label=\"Cro\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "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
}
