"""Exact two-point thickness example: rare values dominate an inverse moment. Python 3.14; NumPy and Matplotlib from root pyproject.toml. Run in the output directory to write a white PNG to cwd. """ from pathlib import Path import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt p=np.array([.9999,.0001]);delta=np.array([10.001,.001]);contribution=p/delta fig,axes=plt.subplots(1,2,figsize=(8,3.5),dpi=120) fig.patch.set_facecolor('white') labels=['Common thickness\n10.001 µm','Rare thickness\n0.001 µm'] colors=['#3c72a6','#bd6130'] axes[0].bar(range(2),p,color=colors,width=.62) axes[0].set_yscale('log');axes[0].set_ylim(4e-5,3) axes[0].set_ylabel('Probability (log scale)') axes[0].set_title('Only 1 in 10,000 observations',fontsize=11) for j,v in enumerate(p):axes[0].text(j,v*1.2,f'{100*v:g}%',ha='center',fontsize=9) axes[1].bar(range(2),contribution,color=colors,width=.62) axes[1].set_ylim(0,.135);axes[1].set_ylabel('Contribution to E[1/thickness] (µm⁻¹)') axes[1].set_title('About half the inverse moment',fontsize=11) for j,v in enumerate(contribution):axes[1].text(j,v+.004,f'{v:.5f}',ha='center',fontsize=9) for ax in axes: ax.set_facecolor('white');ax.set_xticks(range(2),labels,fontsize=9);ax.grid(axis='y',alpha=.15);ax.set_axisbelow(True) fig.suptitle('A rare thin layer can control mean inferred density',fontsize=12) fig.tight_layout(rect=(0,0,1,.93));fig.savefig(Path.cwd()/'rare-thickness-density-leverage.png',facecolor='white');plt.close(fig)