def test_hamiltonian_chain_burn_in(): posterior = ToroidalGaussian() chain = HamiltonianChain(posterior=posterior, start=[1, 0.1, 0.1]) steps = 10 chain.advance(steps) burn = chain.estimate_burn_in() assert 0 < burn <= steps chain.autoselect_burn() assert chain.burn == burn
def test_hamiltonian_chain_advance_no_gradient(): posterior = ToroidalGaussian() chain = HamiltonianChain(posterior=posterior, start=[1, 0.1, 0.1]) first_n = chain.n steps = 10 chain.advance(steps) assert chain.n == first_n + steps chain.burn = 0 assert len(chain.get_parameter(0)) == chain.n assert len(chain.get_parameter(1)) == chain.n assert len(chain.get_parameter(2)) == chain.n assert len(chain.probs) == chain.n
def test_hamiltonian_chain_advance_bounds(line_posterior): chain = HamiltonianChain( posterior=line_posterior, start=[0.5, 0.1], bounds=(array([0.45, 0.0]), array([0.55, 10.0])), ) chain.advance(10) gradient = array(chain.get_parameter(0)) assert all(gradient >= 0.45) assert all(gradient <= 0.55) offset = array(chain.get_parameter(1)) assert all(offset >= 0)
def test_hamiltonian_chain_restore(tmp_path): posterior = ToroidalGaussian() chain = HamiltonianChain(posterior=posterior, grad=posterior.gradient, start=[1, 0.1, 0.1]) steps = 10 chain.advance(steps) filename = tmp_path / "restore_file.npz" chain.save(filename) new_chain = HamiltonianChain.load(filename) assert new_chain.n == chain.n assert new_chain.probs == chain.probs assert all(new_chain.get_last() == chain.get_last())
R = sqrt(x**2 + y**2) K = 1 - self.R0 / R g = array([K * x, K * y, z]) return -g / self.w2 # create an instance of our posterior class posterior = ToroidalGaussian() # create the chain object chain = HamiltonianChain(posterior=posterior, grad=posterior.gradient, start=[1, 0.1, 0.1]) # advance the chain to generate the sample chain.advance(6000) # choose how many samples will be thrown away from the start # of the chain as 'burn-in' chain.burn = 2000 chain.matrix_plot(filename='hmc_matrix_plot.png') # extract sample and probability data from the chain probs = chain.get_probabilities() colors = exp(probs - max(probs)) xs, ys, zs = [chain.get_parameter(i) for i in [0, 1, 2]] import plotly.graph_objects as go from plotly import offline
x, y, z = theta R = sqrt(x**2 + y**2) K = 1 - self.R0 / R g = array([K * x, K * y, z]) return -g / self.w2 posterior = ToroidalGaussian() from inference.mcmc import HamiltonianChain hmc = HamiltonianChain(posterior=posterior, grad=posterior.gradient, start=[1, 0.1, 0.1]) hmc.advance(6000) hmc.burn = 1000 from mpl_toolkits.mplot3d import Axes3D fig = plt.figure(figsize=(5, 4)) ax = fig.add_subplot(111, projection='3d') ax.set_xticks([-1, -0.5, 0., 0.5, 1.]) ax.set_yticks([-1, -0.5, 0., 0.5, 1.]) ax.set_zticks([-1, -0.5, 0., 0.5, 1.]) # ax.set_title('Hamiltonian Monte-Carlo') L = 1.1 ax.set_xlim([-L, L]) ax.set_ylim([-L, L]) ax.set_zlim([-L, L]) probs = array(hmc.get_probabilities()) inds = argsort(probs)