{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7479751d-b04c-4e6a-8985-8406bf4462f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==================================================\n",
      "a. B国的产出可能性边界图\n",
      "==================================================\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 62264 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B国最大橙子产量: 150\n",
      "B国最大葡萄产量: 450\n",
      "B国PPF斜率: -3.00\n",
      "斜率的绝对值表示B国的机会成本: 生产1单位橙子需放弃3.00单位葡萄\n",
      "\n",
      "==================================================\n",
      "b. 全球橙子与葡萄的相对供给曲线\n",
      "==================================================\n",
      "各国机会成本分析:\n",
      "A国生产橙子的机会成本: 1.333 (葡萄/橙子)\n",
      "B国生产橙子的机会成本: 3.000 (葡萄/橙子)\n",
      "A国生产葡萄的机会成本: 0.750 (橙子/葡萄)\n",
      "B国生产葡萄的机会成本: 0.333 (橙子/葡萄)\n",
      "\n",
      "比较优势分析:\n",
      "A国在生产橙子上具有比较优势 (机会成本更低)\n",
      "B国在生产葡萄上具有比较优势 (机会成本更低)\n",
      "\n",
      "各国最大产量:\n",
      "A国最大橙子产量: 375\n",
      "A国最大葡萄产量: 500\n",
      "B国最大橙子产量: 150\n",
      "B国最大葡萄产量: 450\n",
      "全球最大橙子产量: 525\n",
      "全球最大葡萄产量: 950\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 93696 characters]",
      "text/plain": [
       "<Figure size 1200x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全球相对供给曲线关键点分析:\n",
      "当相对价格 P_O/P_G < 0.33 时，两国都只生产葡萄，橙子相对供给为0\n",
      "当相对价格 P_O/P_G = 0.33 时，B国开始有生产橙子的动力\n",
      "当相对价格 0.33 < P_O/P_G < 1.33 时，B国生产葡萄，A国生产橙子，相对供给约为 375 单位橙子\n",
      "当相对价格 P_O/P_G = 1.33 时，A国也开始只生产橙子，相对供给跃升至 525 单位橙子\n",
      "当相对价格 P_O/P_G > 1.33 时，A国只生产橙子，B国也开始生产橙子，相对供给量达到全球最大值 525 并保持不变\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 设置中文字体\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "\n",
    "def plot_ppf():\n",
    "    \"\"\"绘制B国的产出可能性边界图\"\"\"\n",
    "    # B国参数\n",
    "    L_B = 900  # B国劳动单位总数\n",
    "    a_L_O_B = 6  # B国生产橙子的单位劳动量\n",
    "    a_L_G_B = 2  # B国生产葡萄的单位劳动量\n",
    "    \n",
    "    # 计算B国最大产量\n",
    "    max_oranges_B = L_B / a_L_O_B  # 只生产橙子时的最大产量\n",
    "    max_grapes_B = L_B / a_L_G_B   # 只生产葡萄时的最大产量\n",
    "    \n",
    "    # 创建图形\n",
    "    fig, ax = plt.subplots(1, 1, figsize=(10, 6))\n",
    "    \n",
    "    # 绘制B国PPF线\n",
    "    x_oranges = [0, max_oranges_B]  # 橙子产量从0到最大值\n",
    "    y_grapes = [max_grapes_B, 0]     # 葡萄产量对应从最大值到0\n",
    "    ax.plot(x_oranges, y_grapes, 'b-', linewidth=2, label='B国PPF')\n",
    "    \n",
    "    # 标记轴点\n",
    "    ax.plot(0, max_grapes_B, 'bo', markersize=8)\n",
    "    ax.text(0, max_grapes_B, f'({0}, {int(max_grapes_B)})', fontsize=10, ha='left', va='bottom')\n",
    "    ax.plot(max_oranges_B, 0, 'bo', markersize=8)\n",
    "    ax.text(max_oranges_B, 0, f'({int(max_oranges_B)}, {0})', fontsize=10, ha='right', va='top')\n",
    "    \n",
    "    # 添加斜率标注\n",
    "    slope = -max_grapes_B / max_oranges_B\n",
    "    ax.text(max_oranges_B/2, max_grapes_B/2, f'斜率 = {slope:.2f}', \n",
    "            fontsize=10, ha='center', va='center', bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.8))\n",
    "    \n",
    "    # 设置图形属性\n",
    "    ax.set_xlabel('橙子产量', fontsize=12)\n",
    "    ax.set_ylabel('葡萄产量', fontsize=12)\n",
    "    ax.set_title('B国的产出可能性边界图 (PPF)', fontsize=14, fontweight='bold')\n",
    "    ax.grid(True, linestyle='--', alpha=0.6)\n",
    "    ax.legend()\n",
    "    \n",
    "    # 设置坐标轴范围\n",
    "    ax.set_xlim(-5, max_oranges_B * 1.1)\n",
    "    ax.set_ylim(-5, max_grapes_B * 1.1)\n",
    "    \n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "    \n",
    "    print(f\"B国最大橙子产量: {int(max_oranges_B)}\")\n",
    "    print(f\"B国最大葡萄产量: {int(max_grapes_B)}\")\n",
    "    print(f\"B国PPF斜率: {slope:.2f}\")\n",
    "    print(f\"斜率的绝对值表示B国的机会成本: 生产1单位橙子需放弃{-slope:.2f}单位葡萄\")\n",
    "    print()\n",
    "\n",
    "def plot_world_rs():\n",
    "    \"\"\"构建全球橙子与葡萄的相对供给曲线\"\"\"\n",
    "    # A国参数\n",
    "    L_A = 1500\n",
    "    a_L_O_A = 4\n",
    "    a_L_G_A = 3\n",
    "    \n",
    "    # B国参数\n",
    "    L_B = 900\n",
    "    a_L_O_B = 6\n",
    "    a_L_G_B = 2\n",
    "    \n",
    "    # 计算各国的单位劳动生产率\n",
    "    P_O_A = 1 / a_L_O_A  # A国生产橙子的单位劳动生产率\n",
    "    P_G_A = 1 / a_L_G_A  # A国生产葡萄的单位劳动生产率\n",
    "    P_O_B = 1 / a_L_O_B  # B国生产橙子的单位劳动生产率\n",
    "    P_G_B = 1 / a_L_G_B  # B国生产葡萄的单位劳动生产率\n",
    "    \n",
    "    # 计算各国的机会成本\n",
    "    OC_O_A = P_G_A / P_O_A  # A国生产橙子的机会成本 (葡萄/橙子)\n",
    "    OC_O_B = P_G_B / P_O_B  # B国生产橙子的机会成本 (葡萄/橙子)\n",
    "    OC_G_A = P_O_A / P_G_A  # A国生产葡萄的机会成本 (橙子/葡萄)\n",
    "    OC_G_B = P_O_B / P_G_B  # B国生产葡萄的机会成本 (橙子/葡萄)\n",
    "    \n",
    "    print(\"各国机会成本分析:\")\n",
    "    print(f\"A国生产橙子的机会成本: {OC_O_A:.3f} (葡萄/橙子)\")\n",
    "    print(f\"B国生产橙子的机会成本: {OC_O_B:.3f} (葡萄/橙子)\")\n",
    "    print(f\"A国生产葡萄的机会成本: {OC_G_A:.3f} (橙子/葡萄)\")\n",
    "    print(f\"B国生产葡萄的机会成本: {OC_G_B:.3f} (橙子/葡萄)\")\n",
    "    print()\n",
    "    \n",
    "    # 比较优势分析\n",
    "    if OC_O_A < OC_O_B:\n",
    "        print(\"比较优势分析:\")\n",
    "        print(\"A国在生产橙子上具有比较优势 (机会成本更低)\")\n",
    "        print(\"B国在生产葡萄上具有比较优势 (机会成本更低)\")\n",
    "    elif OC_O_B < OC_O_A:\n",
    "        print(\"比较优势分析:\")\n",
    "        print(\"B国在生产橙子上具有比较优势 (机会成本更低)\")\n",
    "        print(\"A国在生产葡萄上具有比较优势 (机会成本更低)\")\n",
    "    else:\n",
    "        print(\"两国在生产橙子上的机会成本相同，没有比较优势。\")\n",
    "    print()\n",
    "    \n",
    "    # 计算各国最大产量\n",
    "    max_oranges_A = L_A / a_L_O_A\n",
    "    max_grapes_A = L_A / a_L_G_A\n",
    "    max_oranges_B = L_B / a_L_O_B\n",
    "    max_grapes_B = L_B / a_L_G_B\n",
    "    \n",
    "    # 全球最大产量\n",
    "    world_max_oranges = max_oranges_A + max_oranges_B\n",
    "    world_max_grapes = max_grapes_A + max_grapes_B\n",
    "    \n",
    "    print(\"各国最大产量:\")\n",
    "    print(f\"A国最大橙子产量: {int(max_oranges_A)}\")\n",
    "    print(f\"A国最大葡萄产量: {int(max_grapes_A)}\")\n",
    "    print(f\"B国最大橙子产量: {int(max_oranges_B)}\")\n",
    "    print(f\"B国最大葡萄产量: {int(max_grapes_B)}\")\n",
    "    print(f\"全球最大橙子产量: {int(world_max_oranges)}\")\n",
    "    print(f\"全球最大葡萄产量: {int(world_max_grapes)}\")\n",
    "    print()\n",
    "    \n",
    "    # 构建相对价格轴 (P_Orange / P_Grape)\n",
    "    # 相对价格范围从B国葡萄的机会成本到A国葡萄的机会成本 (因为A国在橙子上具有比较优势，B国在葡萄上具有比较优势)\n",
    "    p_o_per_p_g_min = OC_G_B  # B国生产葡萄的机会成本 (最低相对价格)\n",
    "    p_o_per_p_g_max = OC_G_A  # A国生产葡萄的机会成本 (最高相对价格)\n",
    "    \n",
    "    # 定义相对价格点\n",
    "    p1 = p_o_per_p_g_min  # B国葡萄机会成本\n",
    "    p2 = OC_O_A  # A国橙子机会成本 (也是B国葡萄机会成本时的临界点)\n",
    "    p3 = p_o_per_p_g_max  # A国葡萄机会成本\n",
    "    \n",
    "    # 相对供给量计算\n",
    "    # 在 p < p1 时，两国都只生产葡萄，Q_orange = 0\n",
    "    # 在 p1 <= p <= p2 时，B国只生产葡萄，A国生产橙子和葡萄，Q_orange = max_oranges_A * (p - p1) / (p2 - p1) (简化线性，实际为阶梯)\n",
    "    # 在 p2 < p <= p3 时，A国只生产橙子，B国只生产葡萄，Q_orange = max_oranges_A\n",
    "    # 在 p > p3 时，A国只生产橙子，B国生产橙子和葡萄，Q_orange = max_oranges_A + max_oranges_B * (p - p3) / (p3 * 1.5 - p3) (简化，实际为阶梯到最大)\n",
    "    \n",
    "    # 为了绘制阶梯状RS曲线，我们定义关键点\n",
    "    # p < p1: RS = 0 (橙子/葡萄)\n",
    "    # p = p1: RS从0跳跃到A国单独生产时的量 (A国生产部分橙子)\n",
    "    # p1 < p < p2: B国只生产葡萄，A国生产橙子，RS = max_oranges_A / max_grapes_B (A国生产所有橙子时)\n",
    "    # p = p2: RS从A国单独生产跳跃到A+B两国生产所有橙子\n",
    "    # p > p2: RS = (max_oranges_A + max_oranges_B) / 0 = 无穷大 (实际为B国也开始生产橙子后，量迅速增加)\n",
    "    \n",
    "    # 简化模型：绘制一个代表性的阶梯状RS曲线\n",
    "    # 1. p < p1: RS = 0\n",
    "    # 2. p = p1: B国开始生产橙子，RS开始增加\n",
    "    # 3. p1 < p < p2: A国生产橙子，B国生产葡萄，RS = max_oranges_A / max_grapes_B\n",
    "    # 4. p = p2: A国也只生产橙子，RS = max_oranges_A / (A国生产葡萄的量) -> 无穷 (在图中表现为垂直线)\n",
    "    # 5. p > p2: A国只生产橙子，B国生产橙子，RS非常大 (水平线在最高点)\n",
    "    \n",
    "    rel_price = [0, p1, p1, p2, p2, p2*1.5]  # 相对价格 (P_Orange/P_Grape)\n",
    "    rel_supply = [0, 0, max_oranges_A, max_oranges_A, world_max_oranges, world_max_oranges]  # 相对供给量 (橙子/葡萄)\n",
    "    \n",
    "    # 创建图形\n",
    "    fig, ax = plt.subplots(1, 1, figsize=(12, 8))\n",
    "    \n",
    "    # 绘制相对供给曲线 (阶梯状)\n",
    "    ax.plot(rel_price, rel_supply, 'r-', linewidth=2, label='世界相对供给曲线 (RS)', drawstyle='steps-post')\n",
    "    \n",
    "    # 标记关键点\n",
    "    ax.plot([p1, p1], [0, max_oranges_A], 'r--', linewidth=1)\n",
    "    ax.plot([p2, p2], [0, world_max_oranges], 'r--', linewidth=1)\n",
    "    ax.plot([0, p1], [max_oranges_A, max_oranges_A], 'r--', linewidth=1)\n",
    "    ax.plot([p1, p2], [max_oranges_A, max_oranges_A], 'r--', linewidth=1)\n",
    "    \n",
    "    ax.plot(p1, max_oranges_A, 'ro', markersize=8, label=f'关键点1: (p={p1:.2f}, Q={int(max_oranges_A)})')\n",
    "    ax.plot(p2, world_max_oranges, 'ro', markersize=8, label=f'关键点2: (p={p2:.2f}, Q={int(world_max_oranges)})')\n",
    "    \n",
    "    # 添加文字标注\n",
    "    ax.annotate(f'B国开始生产\\n橙子 (p={p1:.2f})', xy=(p1, 0), xytext=(p1*0.7, max_oranges_A*0.3),\n",
    "                arrowprops=dict(arrowstyle='->', color='blue', lw=1),\n",
    "                fontsize=10, ha='center', bbox=dict(boxstyle='round,pad=0.3', facecolor='lightblue', alpha=0.8))\n",
    "                \n",
    "    ax.annotate(f'A国开始只生产\\n橙子 (p={p2:.2f})', xy=(p2, max_oranges_A), xytext=(p2*1.2, world_max_oranges*0.7),\n",
    "                arrowprops=dict(arrowstyle='->', color='red', lw=1),\n",
    "                fontsize=10, ha='center', bbox=dict(boxstyle='round,pad=0.3', facecolor='mistyrose', alpha=0.8))\n",
    "    \n",
    "    # 设置图形属性\n",
    "    ax.set_xlabel('相对价格 (P_橙子 / P_葡萄)', fontsize=12)\n",
    "    ax.set_ylabel('相对供给量 (橙子 / 葡萄)', fontsize=12)\n",
    "    ax.set_title('全球橙子与葡萄的相对供给曲线 (RS)', fontsize=14, fontweight='bold')\n",
    "    ax.grid(True, linestyle='--', alpha=0.6)\n",
    "    ax.legend(loc='upper left')\n",
    "    \n",
    "    # 设置坐标轴比例，使图形更清晰\n",
    "    ax.set_xlim(0, max(rel_price)*1.1)\n",
    "    ax.set_ylim(0, max(rel_supply)*1.1)\n",
    "    \n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "    \n",
    "    print(\"全球相对供给曲线关键点分析:\")\n",
    "    print(f\"当相对价格 P_O/P_G < {p1:.2f} 时，两国都只生产葡萄，橙子相对供给为0\")\n",
    "    print(f\"当相对价格 P_O/P_G = {p1:.2f} 时，B国开始有生产橙子的动力\")\n",
    "    print(f\"当相对价格 {p1:.2f} < P_O/P_G < {p2:.2f} 时，B国生产葡萄，A国生产橙子，相对供给约为 {int(max_oranges_A)} 单位橙子\")\n",
    "    print(f\"当相对价格 P_O/P_G = {p2:.2f} 时，A国也开始只生产橙子，相对供给跃升至 {int(world_max_oranges)} 单位橙子\")\n",
    "    print(f\"当相对价格 P_O/P_G > {p2:.2f} 时，A国只生产橙子，B国也开始生产橙子，相对供给量达到全球最大值 {int(world_max_oranges)} 并保持不变\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    print(\"=\"*50)\n",
    "    print(\"a. B国的产出可能性边界图\")\n",
    "    print(\"=\"*50)\n",
    "    plot_ppf()\n",
    "    \n",
    "    print(\"=\"*50)\n",
    "    print(\"b. 全球橙子与葡萄的相对供给曲线\")\n",
    "    print(\"=\"*50)\n",
    "    plot_world_rs()\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c77dc5b9-5d23-4252-94e3-b2ac5fa421b1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
