Example #1
0
def test_gibbs_chain_get_probabilities():
    start_location = array([2.0, -4.0])
    width_guesses = array([5.0, 0.05])

    chain = GibbsChain(posterior=rosenbrock,
                       start=start_location,
                       widths=width_guesses)
    steps = 10
    chain.advance(steps)

    probabilities = chain.get_probabilities()
    assert len(probabilities) == steps
    assert probabilities[-1] > probabilities[0]

    burn = 2
    probabilities = chain.get_probabilities(burn=burn)
    assert len(probabilities) == expected_len(steps, start=burn)
    assert probabilities[-1] > probabilities[0]

    thin = 2
    probabilities = chain.get_probabilities(thin=thin)
    assert len(probabilities) == expected_len(steps, step=thin)
    assert probabilities[-1] > probabilities[0]

    probabilities = chain.get_probabilities(burn=burn, thin=thin)
    assert len(probabilities) == expected_len(steps, start=burn, step=thin)
    assert probabilities[-1] > probabilities[0]
Example #2
0
# to be adjusted individually toward an optimal value.
width_guesses = array([5., 0.05])

# create the chain object
chain = GibbsChain(posterior=rosenbrock,
                   start=start_location,
                   widths=width_guesses)

# advance the chain 150k steps
chain.advance(150000)

# the samples for the n'th parameter can be accessed through the
# get_parameter(n) method. We could use this to plot the path of
# the chain through the 2D parameter space:

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()
plt.show()

# We can see from this plot that in order to take a representative sample,
# 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.
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')
plt.show()