def test_plot_samples(): # create a gp model model = pygp.BasicGP(sn=1, sf=1, ell=1) # create a prior datastructure priors1 = {'sn': pygp.priors.Uniform(0.01, 1.0), 'sf': pygp.priors.Uniform(0.01, 5.0), 'ell': pygp.priors.Uniform(0.01, 1.0), 'mu': pygp.priors.Uniform(-2, 2)} # create a prior datastructure which holds fixed all but one parameter priors2 = {'sn': pygp.priors.Uniform(0.01, 1.0), 'sf': None, 'ell': None, 'mu': None} # sample and plot the models pg.plot_samples(pygp.meta.SMC(model, priors1, 200)) pg.plot_samples(pygp.meta.SMC(model, priors2, 200))
mcmc = pygp.meta.MCMC(model, priors, n=200, burn=100) smc = pygp.meta.SMC(model, priors, n=200) pl.figure(1) pl.clf() pl.subplot(131) pp.plot_posterior(model) pl.title('Type-II ML') pl.legend(loc='best') axis = pl.axis() pl.subplot(132) pp.plot_posterior(mcmc) pl.axis(axis) pl.title('MCMC') pl.subplot(133) pp.plot_posterior(mcmc) pl.axis(axis) pl.title('SMC') pl.draw() pl.figure(2) pp.plot_samples(mcmc) pl.suptitle('Samples generated by MCMC') pl.draw() pl.show()
def callback(model, bounds, info, x, index, ftrue): """ Plot the current posterior and the index. """ xx0, xx1 = np.meshgrid( # define grid np.linspace(bounds[0, 0], bounds[0, 1], 200), np.linspace(bounds[1, 0], bounds[1, 1], 200)) xx = np.c_[xx0.flat, xx1.flat] ff = ftrue(xx).reshape(xx0.shape) # compute true function acq = index(xx).reshape(xx0.shape) # compute acquisition mu, _ = model.posterior(xx) # compute the posterior mu = mu.reshape(xx0.shape) X = info['x'] # get observations and xbest = info['xbest'] # recommendations fig = pl.figure(1) pl.clf() pl.subplot(221) # top left subplot pl.contour(xx0, xx1, ff, 20) # plot true function pl.scatter(x[0], x[1], color='k', zorder=2) # plot current selection pl.scatter(X[:, 0], X[:, 1], # plot previous data facecolors='none', color='k', zorder=2) pl.scatter(xbest[-1, 0], xbest[-1, 1], marker='+', # plot recommendation s=60, lw=2, color='g', zorder=2) pl.axis(bounds.flatten()) pl.title('true function') pl.subplot(222) # top right subplot pl.contour(xx0, xx1, mu, 20) # plot posterior pl.scatter(xbest[-1, 0], xbest[-1, 1], marker='+', # plot recommendation s=60, lw=2, color='g', zorder=2) pl.axis(bounds.flatten()) pl.title('posterior mean') pl.subplot(223) # bottom left subplot pl.contour(xx0, xx1, acq, 20) # plot acquisition pl.scatter(x[0], x[1], color='k', zorder=2) # plot current selection pl.axis(bounds.flatten()) pl.title('acquisition') if not np.all(np.isnan(xbest)): pl.subplot(224) # bottom right subplot pl.plot(ftrue(xbest), 'g', lw=2, alpha=0.5) # plot value of recom. pl.axis('tight') pl.title('value of recommendation') for ax in fig.axes: # remove tick labels ax.set_xticklabels([]) ax.set_yticklabels([]) pl.draw() pl.show(block=False) pl.figure(2) # plot hyperparameter pp.plot_samples(model) # samples pl.draw() pl.show(block=False)