codexpast-exam-of-the-msc-in-advanced-computer-science-course-of-the-university-of-oxford2021computational-game-theory-column-security.py
past-exam-of-the-msc-in-advanced-computer-science-course-of-the-university-of-oxford/2021/computational-game-theory-column-security.py
"""Original solution diagram. Python 3.14; NumPy 2.3.5; Matplotlib 3.10.7.
Run from the directory where the PNG should be written.
"""
from pathlib import Path
import io
import os
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, FancyArrowPatch, Ellipse
plt.rcParams.update({"font.family": "DejaVu Sans", "font.size": 11, "figure.facecolor": "white", "axes.facecolor": "white", "savefig.facecolor": "white", "savefig.transparent": False})
x = np.linspace(0, 1, 701)
fig, ax = plt.subplots(figsize=(7.2, 4.8), layout="constrained")
f, g = 5*x-3, 1-2*x
ax.plot(x, f, color="#2678b2", lw=1.8, label=r"Pure row $T$: $5q-3$")
ax.plot(x, g, color="#d0781c", lw=1.8, label=r"Pure row $B$: $1-2q$")
ax.plot(x, np.maximum(f, g), color="#101010", lw=4.2, label="Row's best payoff: upper envelope")
star = 4/7
ax.set(xlabel=r"Column player's probability $q=P(L)$", ylabel="Row player's expected payoff", title="Column player minimizes the upper envelope")
annotation=r"$q^*=4/7,\quad v=-1/7$"
ax.scatter([star], [-1/7], s=65, color="#a92a3b", zorder=5)
ax.axvline(star, ls=":", color="#a92a3b", lw=1)
ax.annotate(annotation, (star, -1/7), xytext=(star+0.06, 0.45), arrowprops={"arrowstyle": "->", "color": "#a92a3b"}, color="#a92a3b")
ax.set_xlim(0, 1)
ax.set_ylim(-3.25, 2.45)
ax.axhline(0, color="0.75", lw=0.8)
ax.grid(alpha=0.18)
ax.legend(loc="lower left", fontsize=9)
buf = io.BytesIO()
fig.savefig(buf, format="png", dpi=160, facecolor="white", transparent=False, metadata={"Software": "Matplotlib"})
output = Path(Path(__file__).with_suffix(".png").name)
temporary = output.with_suffix(".png.tmp")
temporary.write_bytes(buf.getvalue())
os.replace(temporary, output)
plt.close(fig)