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)
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)
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)
def test_gibbs_chain_get_parameter(): 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 = 5 chain.advance(steps) samples = chain.get_parameter(0) assert len(samples) == expected_len(steps) burn = 2 samples = chain.get_parameter(0, burn=burn) assert len(samples) == expected_len(steps, burn) thin = 2 samples = chain.get_parameter(1, thin=thin) assert len(samples) == expected_len(steps, step=thin) samples = chain.get_parameter(1, burn=burn, thin=thin) assert len(samples) == expected_len(steps, start=burn, step=thin)
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. # The plot_diagnostics() method can help us decide what size of burn-in to use:
# poor, to demonstrate that gibbs sampling allows each proposal width # 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. # The plot_diagnostics() method can help us decide what size of burn-in to use: chain.plot_diagnostics()
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()