{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5490b8ad",
   "metadata": {},
   "source": [
    "# DCT Laboratory — Volume II, Chapter 16\n",
    "## Enterprise Applications and Integrated Case Studies\n",
    "**Seed `26216`** · Companion to the chapter and AXIOM Module **AXIOM-16 (Vol. II)**\n",
    "\n",
    "The capstone. **Seven sector cases Pareto-sorted** on (value uplift, risk\n",
    "reduction): four efficient, three dominated — Chapter 12's machinery grading\n",
    "the book's own case studies. The **integrated-methodology scorecard**: four\n",
    "stages, one weighting, PE/VC crowned at 8.3. And the **final exam battery**:\n",
    "five of the volume's anchor numbers recomputed from scratch in one place —\n",
    "duality gap 0, switch optimum 39.6863, $\\lambda_0 = 1.0629$, HJB\n",
    "$V(10) = -5$, and the DRO flip at 0.125. The whole volume, alive in one sheet.\n",
    "Mirrored in `DCT_V2_Ch16_Lab.xlsx`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47d25d2f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-14T01:05:24.943022Z",
     "iopub.status.busy": "2026-07-14T01:05:24.942790Z",
     "iopub.status.idle": "2026-07-14T01:05:25.513729Z",
     "shell.execute_reply": "2026-07-14T01:05:25.512381Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['figure.dpi']=110\n",
    "\n",
    "import numpy as np\n",
    "SEED = 26216\n",
    "CASES = {\"MFG\":(7.2,5.1),\"FIN\":(6.5,7.8),\"HLT\":(4.8,8.5),\"ENE\":(5.9,6.2),\n",
    "         \"TEC\":(9.1,3.4),\"GOV\":(3.6,7.1),\"PEV\":(8.3,5.8)}\n",
    "def dominated(name):\n",
    "    u,r = CASES[name]\n",
    "    return any((u2>=u and r2>=r and (u2>u or r2>r)) for n2,(u2,r2) in CASES.items() if n2!=name)\n",
    "def pareto(): return [n for n in CASES if not dominated(n)]\n",
    "STAGES = {\"MFG\":(8,7,9,6),\"FIN\":(7,8,7,8),\"HLT\":(6,7,6,9),\"ENE\":(7,6,7,7),\n",
    "          \"TEC\":(9,9,8,6),\"GOV\":(5,6,5,8),\"PEV\":(8,9,8,8)}\n",
    "W = (0.2,0.3,0.3,0.2)\n",
    "def composite(name): return round(sum(w*s for w,s in zip(W,STAGES[name])),4)\n",
    "# --- the final exam battery: five anchors, from scratch ---\n",
    "def anchor_duality():\n",
    "    primal = min((u-4)**2 for u in np.linspace(0,2,20001))\n",
    "    lam = 4.0; dual = 2*lam - lam*lam/4\n",
    "    return round(primal - dual, 4)\n",
    "def anchor_switch():\n",
    "    return round(max(3*(6-m)*np.sqrt(4+3*m) for m in range(7)), 4)\n",
    "def anchor_lambda0():\n",
    "    return round(2*0.9**6, 4)\n",
    "def anchor_hjb():\n",
    "    rho, r = 0.10, 0.05\n",
    "    return round((np.log(rho)+r/rho-1)/rho + np.log(10)/rho, 4)\n",
    "def anchor_flip():\n",
    "    return round((6.8-4.8)/(18-2), 4)\n",
    "\n",
    "def reference_values():\n",
    "    comps = {n: composite(n) for n in CASES}\n",
    "    order = sorted(comps, key=comps.get, reverse=True)\n",
    "    vals = np.array(list(comps.values()))\n",
    "    anchors = {\"anchor_duality_gap\": anchor_duality(), \"anchor_switch_J\": anchor_switch(),\n",
    "               \"anchor_lambda0\": anchor_lambda0(), \"anchor_V10_hjb\": anchor_hjb(),\n",
    "               \"anchor_flip_delta\": anchor_flip()}\n",
    "    expect = {\"anchor_duality_gap\":0.0,\"anchor_switch_J\":39.6863,\"anchor_lambda0\":1.0629,\n",
    "              \"anchor_V10_hjb\":-5.0,\"anchor_flip_delta\":0.125}\n",
    "    all_ok = int(all(abs(anchors[k]-expect[k])<5e-4 for k in anchors))\n",
    "    return {\n",
    "        \"n_cases\": len(CASES), \"n_pareto\": len(pareto()), \"n_dominated\": len(CASES)-len(pareto()),\n",
    "        \"top_composite\": comps[order[0]], \"top_is_PEV\": int(order[0]==\"PEV\"),\n",
    "        \"second_composite\": comps[order[1]],\n",
    "        \"mean_composite\": round(float(vals.mean()),4),\n",
    "        \"std_composite\": round(float(vals.std(ddof=0)),4),\n",
    "        **anchors, \"all_anchors_pass\": all_ok,\n",
    "    }\n",
    "if __name__ == \"__main__\":\n",
    "    print(\"pareto:\", pareto(), \" composites:\", {n:composite(n) for n in CASES})\n",
    "    [print(f\"{k:20s} {v}\") for k,v in reference_values().items()]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57feb3f6",
   "metadata": {},
   "source": [
    "## Panel 1 — Cross-case comparative analysis, Pareto-graded\n",
    "Seven transformations scored on what boards actually trade: value uplift\n",
    "against risk reduction. Chapter 12's dominance test does the first cut:\n",
    "Manufacturing falls to PE/VC, Energy to Financial Services, Government to\n",
    "Healthcare — **three dominated, four efficient** {FIN, HLT, TEC, PEV}. The\n",
    "capstone's quiet point: even the book's own case studies obey the book — no\n",
    "sector wins both axes, and the surviving four ARE the strategy conversation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b7c6f71",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-14T01:05:25.515934Z",
     "iopub.status.busy": "2026-07-14T01:05:25.515589Z",
     "iopub.status.idle": "2026-07-14T01:05:25.787418Z",
     "shell.execute_reply": "2026-07-14T01:05:25.786085Z"
    }
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(7.6,4.6))\n",
    "for n,(u,r) in CASES.items():\n",
    "    on = n in pareto()\n",
    "    ax.scatter([u],[r], s=130, c=\"#C8A24B\" if on else \"#8A8F8B\", zorder=5,\n",
    "               edgecolors=\"#0B3D2E\", linewidths=1.2 if on else 0.5)\n",
    "    ax.annotate(n, (u,r), textcoords=\"offset points\", xytext=(7,5), fontsize=11,\n",
    "                color=\"#0B3D2E\" if on else \"#8A8F8B\", fontweight=\"bold\" if on else \"normal\")\n",
    "ax.set(xlabel=\"value uplift\", ylabel=\"risk reduction\", title=\"Seven sectors, one frontier — seed 26216\")\n",
    "ax.grid(alpha=.25); plt.tight_layout(); plt.show()\n",
    "print(f\"efficient: {pareto()}   dominated: {[n for n in CASES if dominated(n)]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b29bb936",
   "metadata": {},
   "source": [
    "## Panel 2 — The integrated methodology, scored\n",
    "Each case graded 1–10 on the methodology's four stages — Diagnose, Design,\n",
    "Execute, Sustain — composited with weights $(0.2, 0.3, 0.3, 0.2)$ (design and\n",
    "execution carry the middle). The table crowns **PE/VC at 8.3** with Technology\n",
    "at 8.1; Government's 5.9 marks where the methodology met the hardest\n",
    "institutional friction. Cross-case mean 7.29, spread 0.78: the methodology\n",
    "transfers, and the residual variation is the Lessons Learned section's\n",
    "subject."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b202e195",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-14T01:05:25.789645Z",
     "iopub.status.busy": "2026-07-14T01:05:25.789383Z",
     "iopub.status.idle": "2026-07-14T01:05:25.985317Z",
     "shell.execute_reply": "2026-07-14T01:05:25.983910Z"
    }
   },
   "outputs": [],
   "source": [
    "comps = {n: composite(n) for n in CASES}\n",
    "order = sorted(comps, key=comps.get, reverse=True)\n",
    "fig, ax = plt.subplots(figsize=(7.8,4.0))\n",
    "ax.bar(order, [comps[n] for n in order],\n",
    "       color=[\"#0B3D2E\" if n==order[0] else \"#C8A24B\" for n in order], width=.6)\n",
    "ax.axhline(np.mean(list(comps.values())), c=\"#8A8F8B\", ls=\"--\", lw=1.4, label=f\"mean {np.mean(list(comps.values())):.2f}\")\n",
    "ax.set(ylabel=\"methodology composite\", title=\"Diagnose · Design · Execute · Sustain, weighted (seed 26216)\")\n",
    "ax.legend(frameon=False, fontsize=9); ax.grid(alpha=.25, axis=\"y\"); plt.tight_layout(); plt.show()\n",
    "for n in order: print(f\"  {n}: {comps[n]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b76a5cdf",
   "metadata": {},
   "source": [
    "## Panel 3 — The final exam battery\n",
    "Five anchors from five arcs, recomputed from scratch — no imports from earlier\n",
    "labs, just the mathematics: the **duality gap** (Ch. 3) at 0; the **switch\n",
    "optimum** (Chs. 5 and 7, thrice derived) at 39.6863; **Pontryagin's**\n",
    "$\\lambda_0 = 2 \\cdot 0.9^6 = 1.0629$ (Ch. 6); the **HJB value**\n",
    "$V(10) = -5$ exactly (Ch. 8); the **DRO flip** at $\\delta^* = 0.125$\n",
    "(Ch. 11). Five arcs, five decimals, one verdict: `all_anchors_pass = 1`. The\n",
    "course's oldest habit — cross-checking everything against exact small cases —\n",
    "applied, at the very end, to itself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4ee4e5f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-14T01:05:25.987487Z",
     "iopub.status.busy": "2026-07-14T01:05:25.987188Z",
     "iopub.status.idle": "2026-07-14T01:05:26.001400Z",
     "shell.execute_reply": "2026-07-14T01:05:25.999971Z"
    }
   },
   "outputs": [],
   "source": [
    "battery = [(\"duality gap (Ch.3)\", anchor_duality(), 0.0),\n",
    "           (\"switch optimum (Chs.5/7)\", anchor_switch(), 39.6863),\n",
    "           (\"Pontryagin lambda_0 (Ch.6)\", anchor_lambda0(), 1.0629),\n",
    "           (\"HJB V(10) (Ch.8)\", anchor_hjb(), -5.0),\n",
    "           (\"DRO flip delta* (Ch.11)\", anchor_flip(), 0.125)]\n",
    "print(\"anchor                          recomputed   expected\")\n",
    "ok = True\n",
    "for name, got, exp in battery:\n",
    "    ok &= abs(got-exp) < 5e-4\n",
    "    print(f\"{name:30s} {got:10.4f} {exp:10.4f}   {'OK' if abs(got-exp)<5e-4 else 'FAIL'}\")\n",
    "print(f\"\\nall_anchors_pass = {int(ok)} — the volume's spine, verified end to end.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e513f8d7",
   "metadata": {},
   "source": [
    "## Validation — agrees with `DCT_V2_Ch16_Lab.xlsx`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e90182f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-14T01:05:26.004078Z",
     "iopub.status.busy": "2026-07-14T01:05:26.003284Z",
     "iopub.status.idle": "2026-07-14T01:05:26.017328Z",
     "shell.execute_reply": "2026-07-14T01:05:26.015939Z"
    }
   },
   "outputs": [],
   "source": [
    "ref = reference_values()\n",
    "expected = {\"n_cases\":7,\"n_pareto\":4,\"n_dominated\":3,\"top_composite\":8.3,\"top_is_PEV\":1,\n",
    " \"second_composite\":8.1,\"mean_composite\":7.2857,\"std_composite\":0.7791,\n",
    " \"anchor_duality_gap\":0.0,\"anchor_switch_J\":39.6863,\"anchor_lambda0\":1.0629,\n",
    " \"anchor_V10_hjb\":-5.0,\"anchor_flip_delta\":0.125,\"all_anchors_pass\":1}\n",
    "for k,v in expected.items():\n",
    "    assert abs(ref[k]-v)<5e-4, f\"MISMATCH {k}\"\n",
    "    print(f\"PASS  {k:20s} {ref[k]}\")\n",
    "print(\"\\nAll checkpoints agree — seed 26216. Volume II complete.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b269a70",
   "metadata": {},
   "source": [
    "**The course closes here.** Exercises 16.1–16.13 send each sector case back through the full methodology; AXIOM-16's capstone board lets you score your own transformation on the four stages and see where it lands on the frontier. Solutions: IM Vol. II, Ch. 16 — and thank you for taking the course."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
