コード例 #1
0
# some early portion of the chain must be removed. This is referred to as
# the 'burn-in' period. This period allows the chain to both find the high
# density areas, and adjust the proposal widths to their optimal values.

# The plot_diagnostics() method can help us decide what size of burn-in to use:
chain.plot_diagnostics(filename='gibbs_diagnostics.png')

# Occasionally samples are also 'thinned' by a factor of n (where only every
# n'th sample is used) in order to reduce the size of the data set for
# storage, or to produce uncorrelated samples.

# based on the diagnostics we can choose to manually set a global burn and
# thin value, which is used (unless otherwise specified) by all methods which
# access the samples
chain.burn = 10000
chain.thin = 10

# the burn-in and thinning can also be set automatically as follows:
# chain.autoselect_burn_and_thin()

# After discarding burn-in, what we have left should be a representative
# sample drawn from the posterior. Repeating the previous plot as a
# scatter-plot shows the sample:
p = chain.get_probabilities()  # color the points by their probability value
plt.scatter(chain.get_parameter(0),
            chain.get_parameter(1),
            c=exp(p - max(p)),
            marker='.')
plt.xlabel('parameter 1')
plt.ylabel('parameter 2')
plt.grid()
コード例 #2
0
# some early portion of the chain must be removed. This is referred to as
# the 'burn-in' period. This period allows the chain to both find the high
# density areas, and adjust the proposal widths to their optimal values.

# The plot_diagnostics() method can help us decide what size of burn-in to use:
chain.plot_diagnostics()

# Occasionally samples are also 'thinned' by a factor of n (where only every
# n'th sample is used) in order to reduce the size of the data set for
# storage, or to produce uncorrelated samples.

# based on the diagnostics we can choose to manually set a global burn and
# thin value, which is used (unless otherwise specified) by all methods which
# access the samples
chain.burn = 2000
chain.thin = 5

# the burn-in and thinning can also be set automatically as follows:
chain.autoselect_burn_and_thin()

# After discarding burn-in, what we have left should be a representative
# sample drawn from the posterior. Repeating the previous plot as a
# scatter-plot shows the sample:
p = chain.get_probabilities()  # color the points by their probability value
plt.scatter(chain.get_parameter(0),
            chain.get_parameter(1),
            c=exp(p - max(p)),
            marker='.')
plt.xlabel('parameter 1')
plt.ylabel('parameter 2')
plt.grid()
コード例 #3
0

def rosenbrock(t):
    x, y = t
    x2 = x**2
    b = 15.  # correlation strength parameter
    v = 3.  # variance of the gaussian term
    return -x2 - b * (y - x2)**2 - 0.5 * (x2 + y**2) / v


# create the chain object
from inference.mcmc import GibbsChain
gibbs = GibbsChain(posterior=rosenbrock, start=array([2., -4.]))
gibbs.advance(150000)
gibbs.burn = 10000
gibbs.thin = 70

p = gibbs.get_probabilities()  # color the points by their probability value
fig = plt.figure(figsize=(5, 4))
ax1 = fig.add_subplot(111)
ax1.scatter(gibbs.get_parameter(0),
            gibbs.get_parameter(1),
            c=exp(p - max(p)),
            marker='.')
ax1.set_ylim([None, 2.8])
ax1.set_xlim([-1.8, 1.8])
ax1.set_xticks([])
ax1.set_yticks([])
# ax1.set_title('Gibbs sampling')
plt.tight_layout()
plt.savefig('gallery_gibbs_sampling.png')