<jupyter_start><jupyter_text>Sample from the local scale conditionals with different $\beta / \tau$ values.<jupyter_code>n_sample = 10 ** 5
gscale = np.random.exponential()
beta_list = np.array([.01, 1., 100.]) * gscale

lscale_samples = [
    sample_local_scale(beta * np.ones(n_sample), gscale)
    for beta in beta_list
]<jupyter_output><empty_output><jupyter_text>Compare the empirical to the exact distribution.<jupyter_code>def compute_target_pdf(x, beta, gscale, normalized=True):
    log_prior = - np.log(1 + x ** 2) 
    loglik = - .5 * (beta / gscale / x) ** 2 - np.log(x)
    logp = loglik + log_prior
    logp -= np.max(logp) 
        # Avoid numerical under-flow when exponentiating.
    pdf = np.exp(logp)
    if normalized:
        pdf /= np.trapz(pdf, x)
    return pdf

def plot_hist_against_target(ax, lscale_samples, beta, gscale):
    
    # Restrict the plot range; otherwise, the empirical 
    # distribution of a heavy-tailed target is unstable.
    max_quantile = .99
    upper_lim = np.quantile(lscale_samples, max_quantile)
    bins = np.linspace(0, upper_lim, 51)
    
    x = np.linspace(0, upper_lim, 1001)[1:]
    ax.hist(lscale_samples, bins=bins, density=True,
            label='empirical dist')
    ax.plot(x, compute_target_pdf(x, beta, gscale),
            label='target pdf')
    ax.set_xlabel(
        r'$\lambda \, | \, \beta / \tau = {:1g}$'.format(beta / gscale)
    )
    ax.set_yticks([])
    
def remove_figure_box_edges(ax, sides=['left', 'right', 'top']):
    for side in sides:
        ax.spines[side].set_visible(False)
plt.rcParams['font.size'] = 22
fig, axes = plt.subplots(1, 3, figsize=(15, 4.25))

for i in range(len(beta_list)):
    plot_hist_against_target(
        axes[i], lscale_samples[i], beta_list[i], gscale
    )
    remove_figure_box_edges(axes[i])

axes[0].legend(frameon=False)
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13865546218487396, 'fraction_numerical': 0.01838235294117647, 'mean_word_length': 3.4509345794392523, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '541150', 'license': 'MIT', 'n_tokens_mistral': 735, 'n_tokens_neox': 689, 'n_words': 150, 'path': 'test_horseshoe_scale_sampler.ipynb', 'repo_name': 'aki-nishimura/horseshoe-scale-sampler', 'size': '1904'}