def testMetropolisHastingsStep(self): if not tf.executing_eagerly(): return accepted, is_accepted, _ = fun_mcmc.metropolis_hastings_step( current_state=0., proposed_state=1., energy_change=-np.inf) self.assertAllEqual(1., accepted) self.assertAllEqual(True, is_accepted) accepted, is_accepted, _ = fun_mcmc.metropolis_hastings_step( current_state=0., proposed_state=1., energy_change=np.inf) self.assertAllEqual(0., accepted) self.assertAllEqual(False, is_accepted) accepted, is_accepted, _ = fun_mcmc.metropolis_hastings_step( current_state=0., proposed_state=1., energy_change=np.nan) self.assertAllEqual(0., accepted) self.assertAllEqual(False, is_accepted) accepted, is_accepted, _ = fun_mcmc.metropolis_hastings_step( current_state=None, proposed_state=1., energy_change=np.nan) self.assertAllEqual(1., accepted) self.assertAllEqual(False, is_accepted) accepted, _, _ = fun_mcmc.metropolis_hastings_step( current_state=tf.zeros(1000), proposed_state=tf.ones(1000), energy_change=-tf.math.log(0.5 * tf.ones(1000)), seed=tfp_test_util.test_seed()) self.assertAllClose(0.5, tf.reduce_mean(input_tensor=accepted), rtol=0.1)
def testMetropolisHastingsStepStructure(self): struct_type = collections.namedtuple('Struct', 'a, b') current = struct_type([1, 2], (3, [4, [0, 0]])) proposed = struct_type([5, 6], (7, [8, [0, 0]])) accepted, is_accepted, _ = fun_mcmc.metropolis_hastings_step( current_state=current, proposed_state=proposed, energy_change=-np.inf) self.assertAllEqual(True, is_accepted) self.assertAllEqual(tf.nest.flatten(proposed), tf.nest.flatten(accepted))