{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "68135be6",
   "metadata": {},
   "source": [
    "# 信息价值分析 — VoI\n",
    "\n",
    "对应案例: [VoI](/docs/case-studies/risk/voi-case/)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d81157a",
   "metadata": {},
   "source": [
    "## 1. 决策树框架"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4cfe5e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np, matplotlib.pyplot as plt\n",
    "np.random.seed(42)\n",
    "# Baseline: P(success)=60%, profit on success=$1M, loss on failure=-$300K\n",
    "p_success=0.6; profit=1000000; loss=-300000\n",
    "EV_baseline=p_success*profit+(1-p_success)*loss\n",
    "print(f'Baseline EV: ${EV_baseline:,.0f}')\n",
    "# Perfect information value\n",
    "EV_perfect_info=p_success*max(profit,0)+(1-p_success)*max(0,loss)-max(EV_baseline,0)\n",
    "print(f'EVPI (perfect info): ${EV_perfect_info:,.0f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3241b3ba",
   "metadata": {},
   "source": [
    "## 2. 不完全信息 (Research)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2da10a86",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Research costs $50K, improves P(detect success|success)=80%, P(detect failure|failure)=70%\n",
    "research_cost=50000\n",
    "p_pos=p_success*0.8+(1-p_success)*0.3  # P(test positive)\n",
    "p_success_given_pos=p_success*0.8/p_pos\n",
    "p_success_given_neg=p_success*0.2/(1-p_pos)\n",
    "EV_research_pos=p_success_given_pos*profit+(1-p_success_given_pos)*loss\n",
    "EV_research_neg=p_success_given_neg*profit+(1-p_success_given_neg)*loss\n",
    "EV_research=p_pos*max(EV_research_pos,0)+(1-p_pos)*max(EV_research_neg,0)-research_cost\n",
    "print(f'P(test positive): {p_pos:.2f}')\n",
    "print(f'P(success|positive): {p_success_given_pos:.2f}')\n",
    "print(f'P(success|negative): {p_success_given_neg:.2f}')\n",
    "print(f'EV with research: ${EV_research:,.0f}')\n",
    "print(f'VoI (imperfect): ${EV_research-max(EV_baseline,0):,.0f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da873a90",
   "metadata": {},
   "source": [
    "## 3. VoI 敏感性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccf5cabb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Vary research cost\n",
    "costs=np.linspace(0,150000,50)\n",
    "vois=[]\n",
    "for c in costs:\n",
    "    ev=p_pos*max(EV_research_pos,0)+(1-p_pos)*max(EV_research_neg,0)-c\n",
    "    vois.append(ev-max(EV_baseline,0))\n",
    "plt.figure(figsize=(10,5))\n",
    "plt.plot(costs,vois,color='#2196F3',lw=2)\n",
    "plt.axhline(0,color='gray',ls='--')\n",
    "plt.axvline(costs[np.argmin(np.abs(np.array(vois)))],color='red',ls='--',label='Break-even cost')\n",
    "plt.xlabel('Research Cost ($)'); plt.ylabel('VoI ($)')\n",
    "plt.title('Value of Information vs Research Cost')\n",
    "plt.legend(); plt.grid(alpha=0.3); plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fefa208e",
   "metadata": {},
   "source": [
    "---\n",
    "*更多分析见 [案例文档](/docs/case-studies/risk/voi-case/)*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
