<jupyter_start><jupyter_text>Import some cool visualization stuff.<jupyter_code>from matplotlib import pyplot as plt
import corner
import prism

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

prism.inline_ipynb()<jupyter_output><empty_output><jupyter_text>2-D Gaussian Target Distribution<jupyter_code>ndim = 2<jupyter_output><empty_output><jupyter_text>Construct a pickleable, callable object to hold the target distribution.<jupyter_code>class Target(object):
    def __init__(self, cov):
        self.cov = cov
        self.ndim = self.cov.shape[0]
        
    def logpdf(self, x):
        return mvn.logpdf(x, mean=np.zeros(self.ndim), cov=self.cov)
        
    def __call__(self, x):
        return self.logpdf(x)<jupyter_output><empty_output><jupyter_text>Generate a random covariance matrix and construct the target.<jupyter_code>A = np.random.rand(ndim, ndim)
cov = A*A.T + ndim*np.eye(ndim);

lnpdf = Target(cov)<jupyter_output><empty_output><jupyter_text>Create a uniformly distributed ensemble and burn it in.<jupyter_code>nwalkers = 500                    
sampler = kombine.Sampler(nwalkers, ndim, lnpdf)

p0 = np.random.uniform(-10, 10, size=(nwalkers, ndim))
p, post, q = sampler.burnin(p0)<jupyter_output><empty_output><jupyter_text>See what burnin did.<jupyter_code>prism.corner(sampler.chain)<jupyter_output><empty_output><jupyter_text>Generate some more samples.<jupyter_code>p, post, q = sampler.run_mcmc(100)
fig, [ax1, ax2, ax3] = plt.subplots(1, 3, figsize=(15, 5))

ax1.plot(sampler.acceptance_fraction, 'k', alpha=.5, label="Mean Acceptance Rate");

for p, ax in zip(range(sampler.dim), [ax2, ax3]):
    ax.plot(sampler.chain[..., p], alpha=0.1)

ax1.legend(loc='lower right');<jupyter_output><empty_output><jupyter_text>Plot independent samples.<jupyter_code>acls = np.ceil(2/np.mean(sampler.acceptance[-100:], axis=0) - 1).astype(int)

ind_samps = np.concatenate([sampler.chain[-100::acl, c].reshape(-1, 2) for c, acl in enumerate(acls)])
print("{} independent samples collected with a mean ACL of {}.".format(len(ind_samps), np.mean(acls)))

corner.corner(ind_samps);<jupyter_output>25000 independent samples collected with a mean ACL of 2.0.
{'fraction_non_alphanumeric': 0.1364051094890511, 'fraction_numerical': 0.021897810218978103, 'mean_word_length': 5.585585585585585, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '131134', 'license': 'MIT', 'n_tokens_mistral': 855, 'n_tokens_neox': 809, 'n_words': 160, 'path': 'examples/2D_gaussian.ipynb', 'repo_name': 'paulthebaker/kombine', 'size': '2192'}