codex/overlap-feasible-ellipses.py

overlap-feasible-ellipses.py
"""Illustrative joint overlap bounds; Python 3.14, NumPy 2.3.5, Matplotlib 3.10.7.

Writes overlap-feasible-ellipses.png to cwd. Ellipses are stated deterministic
sets, not assigned probability coverage regions.
"""
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np


def main():
    h = np.linspace(0.62, 0.78, 400)
    theta = np.linspace(0, 2 * np.pi, 700)
    circle = np.vstack((np.cos(theta), np.sin(theta)))
    center = np.array([0.7, 2.5])
    fig, ax = plt.subplots(figsize=(8, 4), dpi=120, facecolor='white')
    ax.fill_between(h, 4 * h, 2.88, where=4 * h <= 2.88,
                    color='#f6e8df', label='No finite positive inverse')
    for corr, color, label in [(0, '#bd562b', 'Uncorrelated joint set'),
                                (0.8, '#225ea8', 'Correlation +0.8')]:
        cov = np.array([[0.03**2, corr * 0.03 * 0.1],
                        [corr * 0.03 * 0.1, 0.1**2]])
        xy = center[:, None] + 2 * np.linalg.cholesky(cov) @ circle
        ax.plot(xy[0], xy[1], lw=2, color=color, label=label)
    ax.plot(h, 4 * h, '--', color='0.2', label=r'Boundary $D=4h$')
    ax.scatter([center[0]], [center[1]], color='black', s=24, zorder=5)
    ax.text(0.704, 2.48, 'Same center and marginal scales', fontsize=9)
    ax.set(xlim=(0.62, 0.78), ylim=(2.22, 2.88),
           xlabel=r'Radius-setting parameter $h$', ylabel=r'Overlap log-ratio $D$',
           title='Joint calibration geometry controls boundary compatibility')
    ax.grid(alpha=0.2)
    ax.legend(loc='upper right', fontsize=8)
    fig.tight_layout()
    fig.savefig(Path('overlap-feasible-ellipses.png'), dpi=120, facecolor='white')
    plt.close(fig)


if __name__ == '__main__':
    main()