codex/single-draw-conservative-variance.py

single-draw-conservative-variance.py
"""Exact one-draw example: conservativeness in mean is not a samplewise bound."""
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np


def main():
    values = np.array([3.0, -1.0])
    total_estimates = 2 * values
    true_total = values.sum()
    true_variance = np.mean((total_estimates - true_total) ** 2)
    diagonal = 2 * values**2
    conservative = 4 * values**2
    assert true_variance == 16
    assert diagonal.mean() == 10
    assert conservative.mean() == 20
    assert conservative.mean() - true_variance == values.sum() ** 2

    fig, ax = plt.subplots(figsize=(7.6, 4.0), dpi=100, layout="constrained")
    fig.set_facecolor("white")
    x = np.arange(3)
    for offset, heights, color, label in [
        (-0.18, [*diagonal, diagonal.mean()], "#b16b38", "Diagonal only: D"),
        (0.18, [*conservative, conservative.mean()], "#2379a8", "Conservative: C"),
    ]:
        bars = ax.bar(x + offset, heights, width=0.36, color=color, label=label)
        ax.bar_label(bars, labels=[f"{h:g}" for h in heights], padding=3)
    ax.axhline(true_variance, color="#333333", linestyle="--", linewidth=1.3,
               label="True variance = 16")
    ax.axvline(1.5, color="#aaaaaa", linewidth=1)
    ax.set(xticks=x, xticklabels=["Observe 3\nProbability 1/2",
                                "Observe −1\nProbability 1/2",
                                "Design expectation"],
           ylabel="Variance estimate", ylim=(0, 45),
           title="Conservative in expectation; sometimes below the true variance")
    ax.grid(axis="y", alpha=0.15)
    ax.set_axisbelow(True)
    ax.legend(loc="upper right", fontsize=9)
    fig.savefig(Path(__file__).with_suffix(".png").name, facecolor="white")
    plt.close(fig)


if __name__ == "__main__":
    main()