1 import numpy as np, matplotlib.pyplot as plt
   2 from scipy.stats import gaussian_kde
   3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   4 setup_style()
   5 
   6 np.random.seed(42)
   7 fake = np.random.normal(0, 0.08, 500); real = -0.291
   8 
   9 fig, ax = plt.subplots(figsize=(8, 5))
  10 kde = gaussian_kde(fake)
  11 xk = np.linspace(fake.min() - 0.08, fake.max() + 0.08, 300)
  12 yk = kde(xk)
  13 
  14 n_layers = 20
  15 for k in range(n_layers):
  16     ax.fill_between(xk, 0, yk * (1 - k*0.02), alpha=0.03, color=PALETTE[0], linewidth=0)
  17 ax.fill_between(xk, 0, yk, alpha=0.15, color=PALETTE[0], linewidth=0)
  18 ax.plot(xk, yk, color=PALETTE[0], linewidth=2.5, zorder=4)
  19 ax.hist(fake, bins=45, density=True, color=_lighten(PALETTE[0], 0.4), alpha=0.5, edgecolor=PALETTE[0], linewidth=0.8, zorder=2)
  20 
  21 for pval_pct, plabel in [(np.percentile(fake, 5), '5th'), (np.percentile(fake, 95), '95th')]:
  22     ax.axvline(x=pval_pct, color=COLORS['ref_line'], linestyle=':', linewidth=1.0, alpha=0.7)
  23     ax.text(pval_pct, max(yk)*0.92, plabel, ha='center', va='bottom', fontsize=8, color=COLORS['text'],
  24             bbox=dict(boxstyle='round,pad=0.2', facecolor='white', edgecolor=COLORS['grid'], alpha=0.8))
  25 
  26 ax.axvline(x=real, color=PALETTE[1], linewidth=2.5, zorder=5)
  27 ax.annotate(f'Real = {real:.3f}', xy=(real, max(yk)*0.15),
  28             xytext=(real - 0.12, max(yk)*0.65), fontsize=10, fontweight='bold', color=PALETTE[1],
  29             arrowprops=dict(arrowstyle='->', color=PALETTE[1], lw=1.5, connectionstyle='arc3,rad=0.2'),
  30             bbox=dict(boxstyle='round,pad=0.4', facecolor='white', edgecolor=PALETTE[1], alpha=0.95))
  31 
  32 p_value = np.mean(np.abs(fake) >= np.abs(real))
  33 ax.text(0.97, 0.95, f'Permutations: 500\np-value = {p_value:.3f}', transform=ax.transAxes,
  34         fontsize=8.5, va='top', ha='right',
  35         bbox=dict(boxstyle='round,pad=0.5', facecolor=COLORS['bg_box'], edgecolor=COLORS['grid'], alpha=0.95), color=COLORS['text'])
  36 
  37 ax.set_xlabel('Placebo Coefficients', fontsize=11); ax.set_ylabel('Density', fontsize=11)
  38 ax.spines['top'].set_visible(False)
  39 ax.spines['right'].set_visible(False)
  40 ax.grid(alpha=0.15, linestyle='--')
  41 fig.tight_layout()
  42 save_fig(fig, 'figures/fig_placebo.pdf')