Пример #1
0
# pass each through the forward model
curves = array([PeakModel.forward_model(x_fits, theta) for theta in sample])

# We could plot the predictions for each sample all on a single graph, but this is
# often cluttered and difficult to interpret.

# A better option is to use the hdi_plot function from the plotting module to plot
# highest-density intervals for each point where the model is evaluated:
from inference.plotting import hdi_plot
fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot(111)
hdi_plot(x_fits, curves, intervals=[0.68, 0.95], axis=ax)

# plot the MAP estimate (the sample with the single highest posterior probability)
ax.plot(x_fits,
        PeakModel.forward_model(x_fits, chain.mode()),
        ls='dashed',
        lw=3,
        c='C0',
        label='MAP estimate')
# build the rest of the plot
ax.errorbar(x_data,
            y_data,
            yerr=y_error,
            linestyle='none',
            c='red',
            label='data',
            marker='D',
            markerfacecolor='none',
            markeredgewidth=1.5,
            markersize=6)
Пример #2
0
sample = chain.get_sample()
# pass each through the forward model
curves = array([PeakModel.forward_model(x_fits, theta) for theta in sample])

# We could plot the predictions for each sample all on a single graph, but this is
# often cluttered and difficult to interpret.

# A better option is to use the hdi_plot function from the plotting module to plot
# highest-density intervals for each point where the model is evaluated:
from inference.plotting import hdi_plot
fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot(111)
hdi_plot(x_fits, curves, intervals=[0.68, 0.95], axis=ax)

# plot the MAP estimate (the sample with the single highest posterior probability)
MAP_prediction = PeakModel.forward_model(x_fits, chain.mode())
ax.plot(x_fits,
        MAP_prediction,
        ls='dashed',
        lw=3,
        c='C0',
        label='MAP estimate')
# build the rest of the plot
ax.errorbar(x_data,
            y_data,
            yerr=y_error,
            linestyle='none',
            c='red',
            label='data',
            marker='D',
            markerfacecolor='none',
M = 500
x_fits = linspace(400, 450, M)
# get the sample
sample = chain.get_sample()
# pass each through the forward model
curves = array([ posterior.forward_model(x_fits, theta) for theta in sample])

# we can use the sample_hdi function from the pdf_tools module to produce highest-density
# intervals for each point where the model is evaluated:
from inference.pdf_tools import sample_hdi
hdi_1sigma = array([sample_hdi(c, 0.68, force_single = True) for c in curves.T])
hdi_2sigma = array([sample_hdi(c, 0.95, force_single = True) for c in curves.T])

# construct the plot
plt.figure(figsize = (8,5))
# plot the 1 and 2-sigma highest-density intervals
plt.fill_between(x_fits, hdi_2sigma[:,0], hdi_2sigma[:,1], color = 'red', alpha = 0.10, label = '2-sigma HDI')
plt.fill_between(x_fits, hdi_1sigma[:,0], hdi_1sigma[:,1], color = 'red', alpha = 0.20, label = '1-sigma HDI')
# plot the MAP estimate
MAP = posterior.forward_model(x_fits, chain.mode())
plt.plot(x_fits, MAP, c = 'red', lw = 2, ls = 'dashed', label = 'MAP estimate')
# plot the data
plt.plot( x_data, y_data, 'D', c = 'blue', markeredgecolor = 'black', markersize = 5, label = 'data')
# configure the plot
plt.xlabel('wavelength (nm)')
plt.ylabel('intensity')
plt.xlim([410, 440])
plt.legend()
plt.grid()
plt.tight_layout()
plt.show()