<jupyter_start><jupyter_text>Geometric distribution example<jupyter_code># Load generated data from file:
import pickle

from evaluation import *

runs = []
num_chains = 10
for i in range(num_chains):
    with open(f"samples_produced/geometric{i}__count1000_eps0.1_leapfrogsteps5.pickle", "rb") as f:
        runs.append(pickle.load(f))
# Read Anglican files:
for i in range(num_chains):
    for method in anglican_methods:
        runs[i][method] = {}
        with open(f"anglican_samples/geo/{method}{i}.txt") as f:
            contents = f.read()
            # Skip parentheses:
            contents = contents[1:-1]
            # Extract numbers:
            numbers = [int(x) for x in contents.split()]
            runs[i][method]["samples"] = numbers

# Read timings:
if anglican_methods:
    timings = parse_anglican_timings("anglican_samples/geo/timing.txt")
    for method in anglican_methods:
        for i in range(len(runs)):
            runs[i][method]["time"] = timings[method][i]
thinned_runs = thin_runs(all_methods, runs)
values = collect_values(all_methods, thinned_runs)
print_running_time(all_methods, runs, thinned_runs)
def total_variational_distance(samples, p = 0.2):
    samples = sorted(samples)
    N = len(samples)
    mx = int(samples[-1])
    freq = [0 for _ in range(mx + 1)]
    for sample in samples:
        freq[int(sample)] += 1
    dist = 0
    for x in range(1,mx + 1):
        dist += abs(freq[x] / N - (1 - p)**(x-1)*p)
    dist += (1 - p)**mx # unsampled tail
    return dist / 2

print("Total variation distance from the ground truth:")
for method in all_methods:
    print(f"{method}:  {total_variational_distance(values[method]):.4f}")<jupyter_output>Total variation distance from the ground truth:
hmc:  0.0136
is:  0.0165
lmh:  0.0224
pgibbs:  0.0158
rmh:  0.0196
ipmcmc:  0.0164
{'fraction_non_alphanumeric': 0.12657534246575342, 'fraction_numerical': 0.030136986301369864, 'mean_word_length': 3.3476190476190477, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '111827', 'license': 'MIT', 'n_tokens_mistral': 672, 'n_tokens_neox': 639, 'n_words': 156, 'path': 'evaluation_geometric.ipynb', 'repo_name': 'fzaiser/nonparametric-hmc', 'size': '1825'}