def test_gibbs_chain_non_negative(line_posterior): chain = GibbsChain(posterior=line_posterior, start=[0.5, 0.1]) chain.set_non_negative(1) chain.advance(100) offset = array(chain.get_parameter(1)) assert all(offset >= 0)
# 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()
def test_gibbs_chain_remove_non_negative(line_posterior): chain = GibbsChain(posterior=line_posterior, start=[0.5, 0.1]) chain.set_non_negative(1, True) chain.set_non_negative(1, False) assert chain.params[1].non_negative is False