--- title: Blackbox MCMC Testing format: html: copy-code: true embed-resources: true fig-responsive: true fig-width: 8 --- ```{python} from blackbox import mystery import numpy as np import matplotlib.pyplot as plt plt.style.use(['seaborn-v0_8-darkgrid', 'seaborn-v0_8-poster']) ``` Making sure initially that we can plot the mystery function: ```{python} xs = np.linspace(-30,30, 1000) plt.plot(xs, mystery(xs), '.') plt.show() ``` Setting up our sampler: ```{python} def mcmc_sampler(func, start, step_size, num_steps): samples = [start] accepted = 1 while len(samples) < num_steps: current = samples[-1] proposed = np.random.normal(current, step_size) ratio = func(proposed) / func(current) if ratio > np.random.uniform(0,1): samples.append(proposed) accepted += 1 else: samples.append(current) return samples, accepted / len(samples) ``` Running the sampler ```{python} samples, accept_rate = mcmc_sampler(mystery, -20, .5, 50000) accept_rate ``` Looking at the trace plot: ```{python} plt.figure(figsize=(16,9)) plt.plot(samples[:2000]) plt.xlabel('Iterations') plt.ylabel('x') plt.savefig('mcmc_trace.png', facecolor='#ffffff00') plt.show() ``` Looking at the lag plot: ```{python} burned = samples[1000:] plt.figure(figsize=(16,9)) plt.plot(burned[:-1], burned[1:], '.', alpha=0.1) plt.xlabel(r"$x_{j}$") plt.ylabel(r"$x_{j+1}$") plt.savefig('mcmc_lag.png', facecolor='#ffffff00') plt.show() ``` And looking at the overall histogram: ```{python} plt.hist(burned, bins=200) plt.xlabel('x') plt.ylabel('Counts') plt.show() ```