{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "id": "cell-0",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": "# --- house style (the Press palette: accent/navy/gold/parchment) ---\nimport matplotlib as mpl\nACCENT, NAVY, GOLD, PARCH = '#7a1f1f', '#1f3a5f', '#b8860b', '#f7f2e7'\nmpl.rcParams.update({'figure.facecolor': PARCH, 'axes.facecolor': '#fffdf6',\n                     'axes.edgecolor': '#c9bfa3', 'font.family': 'serif'})\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndef pool(a, b, c, d):          # the mediant: (a/b) + (c/d), pooled\n    return (a + c) / (b + d)\n\n# Simpson preset from Thm 3.6: team X wins BOTH seasons, loses the pool\nX1, X2 = (9, 10), (89, 100)    # (hits, at-bats)\nY1, Y2 = (10, 100), (0, 10)\nprint(f\"season 1: {X1[0]/X1[1]:.3f} > {Y1[0]/Y1[1]:.3f}   \"\n      f\"season 2: {X2[0]/X2[1]:.3f} > {Y2[0]/Y2[1]:.3f}\")\nprint(f\"pooled:   {pool(*X1, *X2):.3f} < {pool(*Y1, *Y2):.3f}   - reversal! (Thm 3.6)\")\n\n# Why: pooling is a weighted average whose weight lam = W/(W+U) differs per team\ns1x, s2x = X1[0]/X1[1], X2[0]/X2[1]\ns1y, s2y = Y1[0]/Y1[1], Y2[0]/Y2[1]\nfig, ax = plt.subplots(figsize=(7, 4))\nax.plot([s1x, s2x], [1, 2], 'o-', color=ACCENT, label='team X seasons')\nax.plot([s1y, s2y], [1, 2], 's-', color=NAVY, label='team Y seasons')\nax.plot([pool(*X1, *X2), pool(*Y1, *Y2)], [1, 2], 'd', color=GOLD, ms=12, label='pooled')\nfor s, y, t in [(s1x,1,'X1'),(s2x,2,'X2'),(s1y,1,'Y1'),(s2y,2,'Y2')]:\n    ax.annotate(t, (s, y), textcoords='offset points', xytext=(8, 0))\nax.set(yticks=[1, 2], yticklabels=['pool 1', 'pool 2'], xlabel='batting average',\n       title=\"Simpson: each pooled point is a weighted average of its two seasons\")\nax.legend(); plt.tight_layout(); plt.show()\n"
  }
 ]
}