예제 #1
0
def test_gibbs_chain_set_boundary(line_posterior):
    chain = GibbsChain(posterior=line_posterior, start=[0.5, 0.1])

    left, right = (0.45, 0.55)
    chain.set_boundaries(0, [left, right])
    chain.advance(100)

    gradient = array(chain.get_parameter(0))

    assert all(gradient >= left)
    assert all(gradient <= right)
예제 #2
0
def test_gibbs_chain_remove_boundary(line_posterior):
    chain = GibbsChain(posterior=line_posterior, start=[0.5, 0.1])

    left, right = (0.45, 0.4500000000001)
    chain.set_boundaries(0, [left, right])
    chain.set_boundaries(0, None, remove=True)
    chain.advance(100)

    gradient = array(chain.get_parameter(0))

    # Some values should be outside the original boundary
    assert not all(gradient >= left) or not all(gradient <= right)
# plot the synthetic data and underlying line
plt.plot(x, m*x + c)
plt.plot(x, y, '.')
plt.grid()
plt.show()

# create an instance of the posterior class
posterior = LinePosterior(x = x, y = y, err = ones(N)*sigma)

# pass the posterior to the MCMC sampler
chain = GibbsChain(posterior = posterior, start = [0.5, 0.1])

# Now suppose we know the offset parameter must be non-negative.
# This constraint can be imposed by passing the index of the
# parameter to the set_non_negative method as follows:
chain.set_non_negative(1)

# For the purposes of this demo, let's assume we also know that
# the gradient must exist in the range [0.45, 0.55].
# The gradient can be constrained to values between chosen boundaries
# by passing the parameter index and the boundary values to the
# set_boundaries method as follows:
chain.set_boundaries(0, [0.45, 0.55])

# Advance the chain
chain.advance(50000)
chain.burn = 5000

# Use the matrix plot functionality to check the constraints are working
chain.matrix_plot()