def log_ml_estimate(self, proposal, n=1): """ Computes an estimate for the marginal likelihood p(y) using importance sampling the provided proposal distribution """ prior = self.get_gp_prior() # sample from proposal samples = proposal.sample(n).samples # compute log likelihoods of samples log_likelihood = self.log_likelihood_multiple(samples) log_prior = prior.log_pdf(samples) log_proposal = proposal.log_pdf(samples) # compute estimate of marginal likelihood, log sum exp trick X = log_likelihood + log_prior - log_proposal return GPTools.log_mean_exp(X)
def log_ml_estimate(self, proposal, n=1): """ Computes an estimate for the marginal likelihood p(y) using importance sampling the provided proposal distribution """ prior = self.get_gp_prior() # sample from proposal samples = proposal.sample(n).samples # compute log likelihoods of samples log_likelihood=self.log_likelihood_multiple(samples) log_prior = prior.log_pdf(samples) log_proposal = proposal.log_pdf(samples) # compute estimate of marginal likelihood, log sum exp trick X=log_likelihood+log_prior-log_proposal return GPTools.log_mean_exp(X)
def test_log_mean_exp(self): X = asarray([-1, 1]) X = reshape(X, (len(X), 1)) y = asarray([+1. if x >= 0 else -1. for x in X]) covariance = SquaredExponentialCovariance(sigma=1, scale=1) likelihood = LogitLikelihood() gp = GaussianProcess(y, X, covariance, likelihood) laplace = LaplaceApproximation(gp, newton_start=asarray([3, 3])) proposal = laplace.get_gaussian() n = 200 prior = gp.get_gp_prior() samples = proposal.sample(n).samples log_likelihood = asarray([gp.log_likelihood(f) for f in samples]) log_prior = prior.log_pdf(samples) log_proposal = proposal.log_pdf(samples) X = log_likelihood + log_prior - log_proposal a = log(mean(exp(X))) b = GPTools.log_mean_exp(X) self.assertLessEqual(a - b, 1e-5)
def test_log_mean_exp(self): X = asarray([-1, 1]) X = reshape(X, (len(X), 1)) y = asarray([+1. if x >= 0 else -1. for x in X]) covariance = SquaredExponentialCovariance(sigma=1, scale=1) likelihood = LogitLikelihood() gp = GaussianProcess(y, X, covariance, likelihood) laplace = LaplaceApproximation(gp, newton_start=asarray([3, 3])) proposal=laplace.get_gaussian() n=200 prior = gp.get_gp_prior() samples = proposal.sample(n).samples log_likelihood=asarray([gp.log_likelihood(f) for f in samples]) log_prior = prior.log_pdf(samples) log_proposal = proposal.log_pdf(samples) X=log_likelihood+log_prior-log_proposal a=log(mean(exp(X))) b=GPTools.log_mean_exp(X) self.assertLessEqual(a-b, 1e-5)
def test_log_sum_exp(self): X = asarray([0.1, 0.2, 0.3, 0.4]) direct = log(sum(exp(X))) indirect = GPTools.log_sum_exp(X) self.assertLessEqual(norm(direct - indirect), 1e-10)
def test_log_sum_exp(self): X=asarray([0.1,0.2,0.3,0.4]) direct=log(sum(exp(X))) indirect=GPTools.log_sum_exp(X) self.assertLessEqual(norm(direct-indirect), 1e-10)