Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
### Deprecated
### Removed
### Fixed
- Fixed censored Gaussian sensitivities for models with different numbers of parameters and outputs, and for parameters shared by multiple outputs.
- [#1746](https://github.com/pints-team/pints/pull/1746) Corrected the function signature in the docstring for `OptimisationController.set_callback`.


Expand Down
35 changes: 10 additions & 25 deletions pints/_log_likelihoods.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,29 +439,12 @@ def evaluateS1(self, x):
# the most expensive step in inference, especially for ODE models
output = self._problem.evaluate(theta)

# Make conditions for where data isn't censored and is lower/upper
# censored into 3D arrays

if self._values.ndim == 1:
where_condition = np.reshape(
self._not_censored_condition,
(np.shape(self._not_censored_condition)[0], 1, 1))
lower_where_condition = np.reshape(
self._lower_condition,
(np.shape(self._lower_condition)[0], 1, 1))
upper_where_condition = np.reshape(
self._upper_condition,
(np.shape(self._upper_condition)[0], 1, 1))
else:
where_condition = np.repeat(
self._not_censored_condition[:, :, np.newaxis],
np.shape(self._not_censored_condition)[-1], axis=2)
lower_where_condition = np.repeat(
self._lower_condition[:, :, np.newaxis],
np.shape(self._lower_condition)[-1], axis=2)
upper_where_condition = np.repeat(
self._upper_condition[:, :, np.newaxis],
np.shape(self._upper_condition)[-1], axis=2)
# Keep a singleton parameter axis so each output's mask applies
# to all model parameters, independently of the number of outputs.
condition_shape = (self._nt, self._no, 1)
where_condition = self._not_censored_condition.reshape(condition_shape)
lower_where_condition = self._lower_condition.reshape(condition_shape)
upper_where_condition = self._upper_condition.reshape(condition_shape)

# 1. Parts of the derivative corresponding to the data that
# isn't censored
Expand Down Expand Up @@ -497,14 +480,16 @@ def evaluateS1(self, x):
lower_inner_val = (lower_numerator / lower_denominator).T
lower_inner_sum = np.sum(lower_inner_val, where=lower_where_condition,
axis=0)
lower_censored_dL = -np.sum((sigma**(-1) * lower_inner_sum).T, axis=0)
lower_censored_dL = -np.sum(
(sigma**(-1))[:, np.newaxis] * lower_inner_sum, axis=0)

upper_numerator = upper_pdf.T * dy.T
upper_denominator = 1 - upper_cdf.T
upper_inner_val = (upper_numerator / upper_denominator).T
upper_inner_sum = np.sum(upper_inner_val, where=upper_where_condition,
axis=0)
upper_censored_dL = np.sum((sigma**(-1) * upper_inner_sum).T, axis=0)
upper_censored_dL = np.sum(
(sigma**(-1))[:, np.newaxis] * upper_inner_sum, axis=0)

# Calculate derivative wrt sigma
lower_dsigma_inner_val = (lower_pdf.T *
Expand Down
82 changes: 82 additions & 0 deletions pints/tests/test_log_likelihoods.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,88 @@ def setUpClass(cls):
[1.1, 0.0, -5.5]])
cls.sigma = 0.1

def test_evaluateS1_parameter_count(self):
# Model parameter and output axes need not have the same length.
class LinearModel(pints.ForwardModelS1):
def __init__(self, n_parameters, n_outputs):
self.n = n_parameters
self.outputs = n_outputs

def n_parameters(self):
return self.n

def n_outputs(self):
return self.outputs

def simulate(self, parameters, times):
values = np.column_stack((parameters[0] * times,
2 * parameters[0] * times))
if self.n > 1:
values[:, 0] += parameters[1]
if self.n > 2:
if self.outputs == 1:
values[:, 0] += parameters[2] * times**2
else:
values[:, 1] += parameters[2]
return values[:, 0] if self.outputs == 1 else values

def simulateS1(self, parameters, times):
derivatives = np.zeros((len(times), 2, self.n))
derivatives[:, 0, 0] = times
derivatives[:, 1, 0] = 2 * times
if self.n > 1:
derivatives[:, 0, 1] = 1
if self.n > 2:
if self.outputs == 1:
derivatives[:, 0, 2] = times**2
else:
derivatives[:, 1, 2] = 1
if self.outputs == 1:
derivatives = derivatives[:, 0, :]
return self.simulate(parameters, times), derivatives

times = np.array([0., 0.5, 1., 1.5])
values = np.array([[-0.4, 0.4], [0.6, 1.2],
[1.4, 1.8], [2.2, 3.2]])
for n_outputs in (1, 2):
for n_parameters in (1, 2, 3):
for lower, upper in ((None, None), ([0., 0.5], None),
(None, [1., 2.]),
([0., 0.5], [1., 2.])):
with self.subTest(n_outputs=n_outputs,
n_parameters=n_parameters,
lower=lower, upper=upper):
model = LinearModel(n_parameters, n_outputs)
data = np.clip(values,
-np.inf if lower is None else lower,
np.inf if upper is None else upper)
if n_outputs == 1:
data = data[:, 0]
lower = None if lower is None else lower[0]
upper = None if upper is None else upper[0]
problem = pints.SingleOutputProblem(
model, times, data)
else:
problem = pints.MultiOutputProblem(
model, times, data)
likelihood = pints.CensoredGaussianLogLikelihood(
problem, lower=lower, upper=upper)
parameters = np.r_[np.array([0.4, 0.1, 0.3])[
:n_parameters], [0.9, 1.1][:n_outputs]]
with np.errstate(divide='ignore', invalid='ignore'):
score, gradient = likelihood.evaluateS1(parameters)
self.assertEqual(gradient.shape, parameters.shape)
self.assertEqual(score, likelihood(parameters))
expected = np.empty_like(parameters)
for j in range(len(parameters)):
step = np.zeros_like(parameters)
step[j] = 1e-5
expected[j] = (likelihood(parameters + step)
- likelihood(parameters - step)
) / (2 * step[j])
np.testing.assert_allclose(gradient, expected,
rtol=1e-5, atol=1e-5)

def test_call_list_none_censored(self):
values = self.data_single.tolist()
problem = pints.SingleOutputProblem(
Expand Down
Loading