<jupyter_start><jupyter_text>Generate data without background and compare MFM and DPMM to NS<jupyter_code>npr.seed(0)

true_model = NeymanScottModel(mu, alpha, beta, 1e-8, 
                              bkgd_class(**bkgd_hypers), 
                              DefaultClusterFactory(cluster_class, cluster_hypers))

# Sample from the model
sample_kwargs = dict(time_kwargs=dict(), mark_kwargs=dict())
data, parents, clusters, weights = true_model.generate(**sample_kwargs)
nodes, times, marks = data[:, 0], data[:, 1], data[:, 2:]

# Only keep the valid times
# valid = times < T
valid = np.ones_like(times, dtype=bool)
nodes = nodes[valid]
times = times[valid]
marks = marks[valid]
parents = parents[valid]

# Sort the data by time
perm = np.argsort(times)
nodes = nodes[perm]
times = times[perm]
marks = marks[perm]
parents = parents[perm]
data = np.column_stack((nodes, times, marks))
plt.figure(figsize=(12, 15))
ylim = 1.1 * abs(marks[:, 0]).max()
for m in range(M):
    plt.subplot(M, 1, m+1)
    for z in np.unique(parents):
        inds = np.where((parents == z) & (nodes == m))[0]
        plt.plot(times[inds], marks[inds, 0], 'o' if z >= 0 else 'ko')
    
    plt.ylabel("Mark Dim 1")
    plt.ylim(-ylim, ylim)
    if m == M-1:
        plt.xlabel("time")
    else:
        plt.xticks([])
        
    plt.title("Node {}".format(m+1))

plt.tight_layout()
# Fit the correct model
num_samples = 100
nsm = NeymanScottModel(mu, alpha, beta, 1e-8, 
                       bkgd_class(**bkgd_hypers), 
                       DefaultClusterFactory(cluster_class, cluster_hypers))
nsm_samples = nsm.gibbs_sample_posterior(data, num_samples=num_samples)

# Permute the sampled parents to match the true ones
nsm_parents = [permute_to_match(parents, s["parents"][perm]) for s in nsm_samples]
# Fit the MFM with Gibbs
from models import MixtureOfFiniteMixtureModel
num_samples = 100
mfm = MixtureOfFiniteMixtureModel(mu, alpha, DefaultClusterFactory(cluster_class, cluster_hypers))
mfm_samples = mfm.gibbs_sample_posterior(data, num_samples=num_samples)

# Permute the sampled parents to match the true ones
mfm_parents = [permute_to_match(parents, s["parents"][perm]) for s in mfm_samples]
# Fit the DPMM with Gibbs
from models import DirichletProcessMixtureModel
num_samples = 100
dpmm = DirichletProcessMixtureModel(alpha, DefaultClusterFactory(cluster_class, cluster_hypers))
dpmm_samples = dpmm.gibbs_sample_posterior(data, num_samples=num_samples)

# Permute the sampled parents to match the true ones
dpmm_parents = [permute_to_match(parents, s["parents"][perm]) for s in dpmm_samples]
# Plot the number of clusters over samples
plt.figure()
plt.plot([s["num_clusters"] for s in nsm_samples], label="nsm")
plt.plot([s["num_clusters"] for s in mfm_samples], label="mfm")
plt.plot([s["num_clusters"] for s in dpmm_samples], label="dpmm")
plt.plot([0, len(mfm_samples)], [len(clusters), len(clusters)], '--k', label="True")
plt.xlabel("Iteration")
plt.ylabel("Num clusters")
plt.legend(loc="lower right")
plt.show()
# Plot the number of clusters over samples
kwargs = dict(density=True, alpha=0.65, linewidth=1, edgecolor='gray')

plt.figure(figsize=(3.36, 2.25))
plt.hist([s["num_clusters"] for s in dpmm_samples[10:]], np.arange(10, 40),label="DPMM", **kwargs)
# plt.hist([s["num_clusters"] for s in dpmm_samples[10:]], np.arange(20, 40),label="dpmm", **kwargs)
plt.hist([s["num_clusters"] for s in mfm_samples[10:]], np.arange(10, 40), label="MFM", **kwargs)
plt.hist([s["num_clusters"] for s in nsm_samples[10:]], np.arange(10, 40), label="NSM", **kwargs)
plt.plot([len(clusters), len(clusters)], [0, 0.4], '--k', label="True")
plt.ylim([0, 0.4])
plt.xlabel("number of clusters")
plt.ylabel("probability")
plt.legend(loc="upper right", fontsize=6)
plt.tight_layout(pad=0.05)
# plt.savefig("synth2.pdf")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15063851967683087, 'fraction_numerical': 0.020328381548084442, 'mean_word_length': 5.044094488188977, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '111102', 'license': 'MIT', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1343, 'n_words': 328, 'path': 'analyses/synthetic/2019_05_14_synthetic_comparison_2.ipynb', 'repo_name': 'slinderman/neymanscott', 'size': '3837'}