def log_likelihood_and_gradient(self, evidence): """ Run inference on the model to find the log-likelihood of the model given evidence and its gradient with respect to the model parameters. :param evidence: A dictionary where the key is a variable name and the value its observed value. :returns: The log-likelihood and a vector of derivatives. """ self._update_order.reset() inference = LoopyBeliefUpdateInference(self._model, update_order=self._update_order) inference.calibrate(parameters=self.parameters) log_z_total = inference.partition_approximation() model_expected_counts = self._accumulate_expected_counts(inference) self._update_order.reset() inference = LoopyBeliefUpdateInference(self._model, update_order=self._update_order) inference.calibrate(evidence, parameters=self.parameters) log_z_observed = inference.partition_approximation() empirical_expected_counts = self._accumulate_expected_counts(inference) log_likelihood = log_z_observed - log_z_total derivative = empirical_expected_counts - model_expected_counts if self._dimension > 0: derivative += -numpy.dot(self._prior_precision, (self._parameters - self._prior_location)) log_likelihood += -0.5 * numpy.dot(numpy.dot((self._parameters - self._prior_location).T, self._prior_precision), (self._parameters - self._prior_location)) log_likelihood += self._prior_normaliser return log_likelihood, derivative
def test_belief_update_larger_tree(self): a = DiscreteFactor([0, 1], data=np.array([[1, 2], [2, 2]], dtype=np.float64)) b = DiscreteFactor([1, 2], data=np.array([[3, 2], [1, 2]], dtype=np.float64)) c = DiscreteFactor([2, 3], data=np.array([[1, 2], [3, 4]], dtype=np.float64)) d = DiscreteFactor([3], data=np.array([2, 1], dtype=np.float64)) e = DiscreteFactor([0], data=np.array([4, 1], dtype=np.float64)) f = DiscreteFactor([2], data=np.array([1, 2], dtype=np.float64)) # # a{0 1} - b{1 2} - c{2 3} - d{3} # | | # e{0} f{2} # model = Model([a, b, c, d, e, f]) print 'edges', model.edges update_order = DistributeCollectProtocol(model) inference = LoopyBeliefUpdateInference(model, update_order=update_order) exact_inference = ExhaustiveEnumeration(model) exhaustive_answer = exact_inference.calibrate().belief print 'bp' change = inference.calibrate() print change for factor in model.factors: print factor for variable in model.variables: marginal_beliefs = inference.get_marginals(variable) true_marginal = exhaustive_answer.marginalize([variable]) for marginal in marginal_beliefs: assert_array_almost_equal(true_marginal.normalized_data, marginal.normalized_data) expected_ln_Z = np.log(exhaustive_answer.data.sum()) self.assertAlmostEqual(expected_ln_Z, inference.partition_approximation())
def test_update_beliefs_disconnected(self): a = DiscreteFactor([(1, 2), (2, 2)], data=np.array([[1, 2], [3, 4]], dtype=np.float64)) b = DiscreteFactor([(2, 2), (3, 2)], data=np.array([[1, 2], [3, 4]], dtype=np.float64)) c = DiscreteFactor([(4, 2), (5, 2)], data=np.array([[5, 6], [8, 9]], dtype=np.float64)) d = DiscreteFactor([(5, 2), (6, 2)], data=np.array([[1, 6], [2, 3]], dtype=np.float64)) e = DiscreteFactor([(7, 2), (8, 2)], data=np.array([[2, 1], [2, 3]], dtype=np.float64)) model = Model([a, b, c, d, e]) for factor in model.factors: print 'before', factor, np.sum(factor.data) update_order = DistributeCollectProtocol(model) inference = LoopyBeliefUpdateInference(model, update_order=update_order) exact_inference = ExhaustiveEnumeration(model) exhaustive_answer = exact_inference.calibrate().belief print 'Exhaust', np.sum(exhaustive_answer.data) change = inference.calibrate() print change for factor in model.factors: print factor, np.sum(factor.data) for variable in model.variables: marginal_beliefs = inference.get_marginals(variable) true_marginal = exhaustive_answer.marginalize([variable]) for marginal in marginal_beliefs: assert_array_almost_equal(true_marginal.normalized_data, marginal.normalized_data) expected_ln_Z = np.log(exhaustive_answer.data.sum()) self.assertAlmostEqual(expected_ln_Z, inference.partition_approximation())
def test_loopy_distribute_collect(self): a = DiscreteFactor([0, 1], data=np.array([[1, 2], [2, 2]], dtype=np.float64)) b = DiscreteFactor([1, 2], data=np.array([[3, 2], [1, 2]], dtype=np.float64)) c = DiscreteFactor([2, 0], data=np.array([[1, 2], [3, 4]], dtype=np.float64)) # # a{0 1} - b{1 2} # \ / # c{2 0} # # a{0 1} - {0} - c{2 0} # # # # model = Model([a, b, c]) update_order = LoopyDistributeCollectProtocol(model, max_iterations=40) inference = LoopyBeliefUpdateInference(model, update_order=update_order) inference.calibrate() exact_inference = ExhaustiveEnumeration(model) exhaustive_answer = exact_inference.calibrate().belief for factor in model.factors: print factor, np.sum(factor.data) for var in model.variables_to_factors.keys(): print var, exhaustive_answer.marginalize([var]).data print for var in model.variables_to_factors.keys(): print var, inference.get_marginals(var)[0].data for variable in model.variables: for factor in inference.get_marginals(variable): expected_table = exhaustive_answer.marginalize([variable]) actual_table = factor.marginalize([variable]) assert_array_almost_equal(expected_table.normalized_data, actual_table.normalized_data, decimal=2) expected_ln_Z = np.log(exhaustive_answer.data.sum()) self.assertAlmostEqual(expected_ln_Z, inference.partition_approximation(), places=1)
def test_belief_update_long_tree(self): label_template = np.array([['same', 'different'], ['different', 'same']]) observation_template = np.array([['obs_low'] * 32, ['obs_high'] * 32]) observation_template[0, 13:17] = 'obs_high' observation_template[1, 13:17] = 'obs_low' N = 2 pairs = [DiscreteFactor([(i, 2), (i + 1, 2)], parameters=label_template) for i in xrange(N - 1)] obs = [DiscreteFactor([(i, 2), (i + N, 32)], parameters=observation_template) for i in xrange(N)] repe = [16., 16., 14., 13., 15., 16., 14., 13., 15., 16., 15., 13., 14., 16., 16., 15., 13., 13., 14., 14., 13., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 14., 9., 4., 4., 4., 4., 5., 3., 2., 3., 2., 3., 3., 3., 3., 3., 3., 3., 3., 4., 4., 5., 5., 5.] evidence = dict((i + N, 0 if repe[i % len(repe)] >= 13 and repe[i % len(repe)] < 17 else 1) for i in xrange(N)) model = Model(pairs + obs) parameters = {'same': 2.0, 'different': -1.0, 'obs_high': 0.0, 'obs_low': -0.0} update_order = FloodingProtocol(model, max_iterations=4) inference = LoopyBeliefUpdateInference(model, update_order=update_order) inference.calibrate(evidence, parameters) exact_inference = ExhaustiveEnumeration(model) exhaustive_answer = exact_inference.calibrate(evidence, parameters).belief for i in xrange(N): expected_marginal = exhaustive_answer.marginalize([i]) for actual_marginal in inference.get_marginals(i): print i print expected_marginal.normalized_data print actual_marginal.normalized_data assert_array_almost_equal(expected_marginal.normalized_data, actual_marginal.normalized_data) expected_ln_Z = np.log(exhaustive_answer.data.sum()) self.assertAlmostEqual(expected_ln_Z, inference.partition_approximation())