def animate(i, Z, max_lags): line1.set_data(Z[:i, 0][::-1], range(len(Z[:i, 0]))) line2.set_data(range(len(Z[:i, 1])), Z[:i, 1][::-1]) line3.set_data(Z[:i, 0], Z[:i, 1]) line4.set_data(Z[:i, 0], Z[:i, 1]) line5.set_data([Z[i - 1, 0], Z[i - 1, 0]], [Z[i - 1, 1], s_width[1]]) line6.set_data([Z[i - 1, 0], i_width[1]], [Z[i - 1, 1], Z[i - 1, 1]]) # Calculate the ACF and plot on graph after some samples if i >= samples * 0.10: lags = np.arange(1, max_lags) line7.set_data(lags, [autocorr(Z[:i, 0], l) for l in lags]) line8.set_data(lags, [autocorr(Z[:i, 1], l) for l in lags]) return lines
# ax.plot(posterior_small); # _ = ax.set(xlabel='sample', ylabel='mu') # plt.show() # large width posterior_large = sampler(data, samples=5000, mu_init=1., proposal_width=3.) # fig, ax = plt.subplots() # ax.plot(posterior_large); plt.xlabel('sample'); plt.ylabel('mu'); # _ = ax.set(xlabel='sample', ylabel='mu') # plt.show() from pymc3.stats import autocorr lags = np.arange(1, 100) fig, ax = plt.subplots() ax.plot(lags, [autocorr(posterior_large, l) for l in lags], label='large step size') ax.plot(lags, [autocorr(posterior_small, l) for l in lags], label='small step size') ax.plot(lags, [autocorr(posterior, l) for l in lags], label='medium step size') ax.legend(loc=0) _ = ax.set(xlabel='lag', ylabel='autocorrelation', ylim=(-.1, 1)) import pymc3 as pm with pm.Model(): mu = pm.Normal('mu', 0, 1) sigma = 1. returns = pm.Normal('returns', mu=mu, sd=sigma, observed=data) step = pm.Metropolis() trace = pm.sample(15000, step)
def autocorr(self, posterior_chain): """Produce autocorrelation plots""" lags = np.arange(1, 250) plt.plot(lags, [autocorr(posterior_chain[self.burn_in:], l) for l in lags]) plt.show()
# plt.show() # Plot too narow proposal width posterior_small = sampler(data, samples=5000, mu_init=1., proposal_width=.01) fig, ax = plt.subplots() ax.plot(posterior_small) _ = ax.set(xlabel='sample', ylabel='$T_{\mu}$') # plt.show() # Plot too wide proposal width proposal width posterior_large = sampler(data, samples=5000, mu_init=1., proposal_width=3.) fig, ax = plt.subplots() ax.plot(posterior_large) plt.xlabel('sample') plt.ylabel('mu') _ = ax.set(xlabel='sample', ylabel='$T_{\mu}$') # plt.show() from pymc3.stats import autocorr lags = np.arange(1, 100) fig, ax = plt.subplots() ax.plot(lags, [autocorr(posterior_large, l) for l in lags], label='Wide proposal width') ax.plot(lags, [autocorr(posterior_small, l) for l in lags], label='Narrow proposal width') ax.plot(lags, [autocorr(posterior, l) for l in lags], label='Medium proposal width') ax.legend(loc=0) _ = ax.set(xlabel='lag', ylabel='autocorrelation', ylim=(-.1, 1)) plt.show()