Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multivariate Gaussian sensitivities #978

Merged
merged 4 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pints/_log_priors.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,17 @@ def __init__(self, mean, cov):
self._mean = mean
self._cov = cov
self._n_parameters = mean.shape[0]
self._cov_inverse = np.linalg.inv(self._cov)

def __call__(self, x):
return np.log(
scipy.stats.multivariate_normal.pdf(
x, mean=self._mean, cov=self._cov))

def evaluateS1(self, x):
""" See :meth:`LogPDF.evaluateS1()`. """
return self(x), -np.matmul(self._cov_inverse, x - self._mean)

def mean(self):
""" See :meth:`LogPrior.mean()`. """
return self._mean
Expand Down
30 changes: 29 additions & 1 deletion pints/tests/test_log_priors.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,13 @@ def test_multivariate_normal_prior(self):
# Basic test
covariance = [[1]]
p = pints.MultivariateGaussianLogPrior(mean, covariance)
self.assertEqual(p([0]), -0.5 * np.log(2 * np.pi))
x = [0]
y = p(x)
self.assertEqual(y, -0.5 * np.log(2 * np.pi))
y1, dy = p.evaluateS1(x)
MichaelClerx marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(y, y1)
self.assertTrue(len(dy), 1)
self.assertEqual(dy[0], 0)

# 5d tests
mean = [1, 2, 3, 4, 5]
Expand All @@ -623,6 +629,28 @@ def test_multivariate_normal_prior(self):
ValueError, pints.MultivariateGaussianLogPrior, [1, 2],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]])

# Test sensitivities
mean = [1, 3]
covariance = [[2, 0.5], [0.5, 2]]
p = pints.MultivariateGaussianLogPrior(mean, covariance)
y, dy = p.evaluateS1([4, 5])
self.assertEqual(len(dy), 2)
self.assertAlmostEqual(y, -5.165421653067172, places=6)
dy_test = [-float(4 / 3), -float(2 / 3)]
self.assertAlmostEqual(dy[0], dy_test[0], places=6)
self.assertAlmostEqual(dy[1], dy_test[1], places=6)

mean = [-5.5, 6.7, 3.2]
covariance = [[3.4, -0.5, -0.7], [-0.5, 2.7, 1.4], [-0.7, 1.4, 5]]
p = pints.MultivariateGaussianLogPrior(mean, covariance)
y, dy = p.evaluateS1([4.4, 3.5, -3])
self.assertEqual(len(dy), 3)
self.assertAlmostEqual(y, -20.855279298674258, places=6)
dy_test = [-2.709773397444412, 0.27739553170576203, 0.7829609754801692]
self.assertAlmostEqual(dy[0], dy_test[0], places=6)
self.assertAlmostEqual(dy[1], dy_test[1], places=6)
self.assertAlmostEqual(dy[2], dy_test[2], places=6)

def test_multivariate_normal_sampling(self):
d = 1
mean = 2
Expand Down