{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "499cfd44",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/MeiraDavidson/biology_aging/blob/main/notebooks/build-cure-vs-slow-aging.ipynb)\n\n",
    "# Curing One Disease, or Slowing Aging? 🎯\n",
    "### A hands-on companion to Chapter 8\n",
    "\n",
    "Chapter 8 asks the question the whole book has been building toward: should we fight the diseases of aging **one at a time**, or go after **aging itself**? Here you'll build a population that's dying of several age-driven diseases at once, then try both strategies and watch which one actually moves the needle. The result is the book's central argument — and it surprises almost everyone.\n",
    "\n",
    "No setup. Runs in Colab. Press ▶ top to bottom."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83bc2671",
   "metadata": {},
   "source": [
    "## Step 0 — Tools, and how we'll model death\n",
    "\n",
    "Each disease gets an **age-rising risk** that roughly *doubles every 8 years* — the real pattern of aging mortality, named after Benjamin Gompertz: `hazard(age) = A · exp(B · age)`. `A` sets how common it is; `B` how fast it accelerates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0294c838",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "age = np.linspace(0, 121, 12101); da = age[1]-age[0]\n",
    "\n",
    "# each cause: (A, B) for hazard A*exp(B*age). Numbers are illustrative but realistic.\n",
    "causes = {\n",
    "    \"heart disease\":   (7e-6, 0.093),\n",
    "    \"cancer\":          (7e-6, 0.091),\n",
    "    \"dementia\":        (5e-8, 0.150),   # rare until very old, then steep\n",
    "    \"everything else\": (1.1e-5, 0.092), # stroke, diabetes, kidney, infection...\n",
    "}\n",
    "print(\"Four competing causes loaded.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9dcd82cf",
   "metadata": {},
   "source": [
    "## Part 1 — A population that ages\n",
    "\n",
    "Let's look at the risk of each disease as people get older. (Note the log scale — these risks rise *enormously* with age.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a2f4c91",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(7,4.3))\n",
    "for name,(A,B) in causes.items():\n",
    "    plt.semilogy(age, A*np.exp(B*age), label=name)\n",
    "plt.xlabel(\"age (years)\"); plt.ylabel(\"yearly risk of dying from cause (log scale)\")\n",
    "plt.title(\"Every disease of aging rises steeply with age\"); plt.legend(); plt.xlim(0,100)\n",
    "plt.tight_layout(); plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0093298",
   "metadata": {},
   "source": [
    "## Part 2 — Who survives, and for how long\n",
    "\n",
    "At every age you face **all** these risks at once, and whichever strikes first ends the story. Add the hazards, turn them into a survival curve, and read off **life expectancy** (the average age at death = the area under the survival curve)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5e72582",
   "metadata": {},
   "outputs": [],
   "source": [
    "def survival_and_LE(causes, slow=0.0, cure=None):\n",
    "    H = np.zeros_like(age)\n",
    "    for name,(A,B) in causes.items():\n",
    "        if name == cure: continue\n",
    "        H += A*np.exp(B*(1-slow)*age)          # 'slow' stretches the aging clock\n",
    "    S = np.exp(-np.cumsum(H)*da)               # fraction still alive at each age\n",
    "    LE = np.sum((S[:-1]+S[1:])/2)*da           # area under survival curve\n",
    "    return S, LE\n",
    "\n",
    "S0, LE0 = survival_and_LE(causes)\n",
    "print(f\"Baseline life expectancy: {LE0:.1f} years\")\n",
    "\n",
    "plt.figure(figsize=(7,4.3))\n",
    "plt.plot(age, 100*S0, color=\"#264653\", lw=2)\n",
    "plt.xlabel(\"age (years)\"); plt.ylabel(\"% of people still alive\")\n",
    "plt.title(f\"Survival curve - life expectancy {LE0:.0f} years\"); plt.xlim(0,110)\n",
    "plt.tight_layout(); plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "940fc324",
   "metadata": {},
   "source": [
    "## Part 3 — Strategy A: cure a disease outright\n",
    "\n",
    "Now the best case for the one-disease-at-a-time approach: imagine we **completely cure cancer** — not treat it, *eliminate* it. How many years does that add?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4607a73a",
   "metadata": {},
   "outputs": [],
   "source": [
    "S_nocancer, LE_nocancer = survival_and_LE(causes, cure=\"cancer\")\n",
    "S_noheart,  LE_noheart  = survival_and_LE(causes, cure=\"heart disease\")\n",
    "S_nodem,    LE_nodem    = survival_and_LE(causes, cure=\"dementia\")\n",
    "\n",
    "print(f\"Cure CANCER entirely     -> life expectancy {LE_nocancer:.1f}  (gain {LE_nocancer-LE0:+.1f} years)\")\n",
    "print(f\"Cure HEART DISEASE       -> life expectancy {LE_noheart:.1f}  (gain {LE_noheart-LE0:+.1f} years)\")\n",
    "print(f\"Cure DEMENTIA            -> life expectancy {LE_nodem:.1f}  (gain {LE_nodem-LE0:+.1f} years)\")\n",
    "\n",
    "plt.figure(figsize=(7,4.3))\n",
    "plt.plot(age, 100*S0,        color=\"#264653\", lw=2, label=\"today\")\n",
    "plt.plot(age, 100*S_nocancer,color=\"#e76f51\", lw=2, ls=\"--\", label=\"cancer cured\")\n",
    "plt.xlabel(\"age (years)\"); plt.ylabel(\"% still alive\")\n",
    "plt.title(\"Curing cancer outright barely moves the curve\"); plt.xlim(0,110); plt.legend()\n",
    "plt.tight_layout(); plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1996317",
   "metadata": {},
   "source": [
    "Only a couple of years — for *eliminating* the disease. Why so little? Because the people you saved from cancer are now exposed to **heart disease and dementia, waiting right behind it.** That's **competing risks**: when several causes all rise with age, removing one just hands you to the others. It's why, in the real world, curing cancer is estimated to add only a few years to life expectancy."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd77f2e2",
   "metadata": {},
   "source": [
    "## Part 4 — Strategy B: slow aging itself\n",
    "\n",
    "Instead of removing one cause, what if we **slow the rate at which *all* the risks rise** — turn down everyone's biological clock a little? Let's compare."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a710ef1",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"Cure cancer outright   : {LE_nocancer-LE0:+.1f} years\")\n",
    "for s in (0.10, 0.20, 0.30):\n",
    "    _, LEs = survival_and_LE(causes, slow=s)\n",
    "    print(f\"Slow aging by {int(s*100)}%      : {LEs-LE0:+.1f} years\")\n",
    "\n",
    "S_slow, LE_slow = survival_and_LE(causes, slow=0.20)\n",
    "\n",
    "plt.figure(figsize=(7,4.3))\n",
    "plt.plot(age, 100*S0,         color=\"#264653\", lw=2, label=\"today\")\n",
    "plt.plot(age, 100*S_nocancer, color=\"#e76f51\", lw=2, ls=\"--\", label=\"cancer cured\")\n",
    "plt.plot(age, 100*S_slow,     color=\"#2a9d8f\", lw=2, label=\"aging slowed 20%\")\n",
    "plt.xlabel(\"age (years)\"); plt.ylabel(\"% still alive\")\n",
    "plt.title(\"Slowing aging shifts the WHOLE curve\"); plt.xlim(0,115); plt.legend()\n",
    "plt.tight_layout(); plt.show()\n",
    "\n",
    "plt.figure(figsize=(6.5,3.8))\n",
    "plt.bar([\"cure\\ncancer\",\"cure\\nheart\",\"slow aging\\n20%\"],\n",
    "        [LE_nocancer-LE0, LE_noheart-LE0, LE_slow-LE0],\n",
    "        color=[\"#e76f51\",\"#e76f51\",\"#2a9d8f\"])\n",
    "plt.ylabel(\"years of life expectancy gained\"); plt.title(\"One disease vs the root\")\n",
    "plt.tight_layout(); plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7052e037",
   "metadata": {},
   "source": [
    "There's the book's whole argument in one bar chart. Curing any *single* disease buys a few years; **slowing aging itself buys far more**, because the diseases of aging share a common root — pull the root and you push them all back at once.\n",
    "\n",
    "And the deeper prize (Chapter 8's real point) isn't just *more* years — slowing aging pushes back the diseases *together*, so it compresses the sick years at the end rather than just extending them: more **healthy** life, not just more life.\n",
    "\n",
    "> The honesty still holds. This is the *case for* targeting aging — it is not proof we can do it in humans. The model shows why the strategy is worth pursuing; whether any intervention truly slows human aging is exactly what remains unproven.\n",
    "\n",
    "### Go further\n",
    "- Add more causes (stroke, diabetes as their own curves) and watch curing one matter even less.\n",
    "- Change the Gompertz doubling time `B`. What if aging accelerated faster, or slower?\n",
    "- Model a *partial* cure (cut a hazard in half instead of to zero) and compare to a *partial* slowing of aging."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}