-
Notifications
You must be signed in to change notification settings - Fork 45
/
priors.py
557 lines (448 loc) · 16.7 KB
/
priors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import logging
import math
from copy import deepcopy
from typing import Callable, Dict, List, Sequence, Tuple, Union
import numpy as np
from .. import C
from .aggregated import AggregatedObjective
from .base import ResultDict
from .function import ObjectiveBase
logger = logging.getLogger(__name__)
class NegLogPriors(AggregatedObjective):
"""
Aggregates different forms of negative log-prior distributions.
Allows to distinguish priors from the likelihood by testing the type of
an objective.
Consists basically of a list of individual negative log-priors,
given in self.objectives.
"""
class NegLogParameterPriors(ObjectiveBase):
"""
Implements Negative Log Priors on Parameters.
Contains a list of prior dictionaries for the individual parameters
of the format
{'index': [int],
'density_fun': [Callable],
'density_dx': [Callable],
'density_ddx': [Callable]}
A prior instance can be added to e.g. an objective, that gives the
likelihood, by an AggregatedObjective.
Notes
-----
All callables should correspond to log-densities. That is, they return
log-densities and their corresponding derivatives.
Internally, values are multiplied by -1, since pyPESTO expects the
Objective function to be of a negative log-density type.
"""
def __init__(
self,
prior_list: List[Dict],
x_names: Sequence[str] = None,
):
"""
Initialize.
Parameters
----------
prior_list:
List of dicts containing the individual parameter priors.
Format see above.
x_names:
Sequence of parameter names (optional).
"""
self.prior_list = prior_list
super().__init__(x_names)
def __deepcopy__(self, memodict=None):
"""Create deepcopy of object."""
other = NegLogParameterPriors(deepcopy(self.prior_list))
return other
def call_unprocessed(
self,
x: np.ndarray,
sensi_orders: Tuple[int, ...],
mode: C.ModeType,
**kwargs,
) -> ResultDict:
"""
Call objective function without pre- or post-processing and formatting.
Returns
-------
result:
A dict containing the results.
"""
res = {}
res[C.FVAL] = self.neg_log_density(x)
if mode == C.MODE_FUN:
for order in sensi_orders:
if order == 0:
continue
elif order == 1:
res[C.GRAD] = self.gradient_neg_log_density(x)
elif order == 2:
res[C.HESS] = self.hessian_neg_log_density(x)
else:
raise ValueError(f'Invalid sensi order {order}.')
if mode == C.MODE_RES:
for order in sensi_orders:
if order == 0:
res[C.RES] = self.residual(x)
elif order == 1:
res[C.SRES] = self.residual_jacobian(x)
else:
raise ValueError(f'Invalid sensi order {order}.')
return res
def check_sensi_orders(
self,
sensi_orders: Tuple[int, ...],
mode: C.ModeType,
) -> bool:
"""See `ObjectiveBase` documentation."""
if mode == C.MODE_FUN:
for order in sensi_orders:
if not (0 <= order <= 2):
return False
elif mode == C.MODE_RES:
for order in sensi_orders:
if order == 0:
return all(
prior.get('residual', None) is not None
for prior in self.prior_list
)
elif order == 1:
return all(
prior.get('residual_dx', None) is not None
for prior in self.prior_list
)
else:
return False
else:
raise ValueError(
f'Invalid input: Expected mode {C.MODE_FUN} or '
f'{C.MODE_RES}, received {mode} instead.'
)
return True
def check_mode(self, mode: C.ModeType) -> bool:
"""See `ObjectiveBase` documentation."""
if mode == C.MODE_FUN:
return True
elif mode == C.MODE_RES:
return all(
prior.get('residual', None) is not None
for prior in self.prior_list
)
else:
raise ValueError(
f'Invalid input: Expected mode {C.MODE_FUN} or '
f'{C.MODE_RES}, received {mode} instead.'
)
def neg_log_density(self, x):
"""Evaluate the negative log-density at x."""
density_val = 0
for prior in self.prior_list:
density_val -= prior['density_fun'](x[prior['index']])
return density_val
def gradient_neg_log_density(self, x):
"""Evaluate the gradient of the negative log-density at x."""
grad = np.zeros_like(x)
for prior in self.prior_list:
grad[prior['index']] -= prior['density_dx'](x[prior['index']])
return grad
def hessian_neg_log_density(self, x):
"""Evaluate the hessian of the negative log-density at x."""
hessian = np.zeros((len(x), len(x)))
for prior in self.prior_list:
hessian[prior['index'], prior['index']] -= prior['density_ddx'](
x[prior['index']]
)
return hessian
def hessian_vp_neg_log_density(self, x, p):
"""Compute vector product of the hessian at x with a vector p."""
h_dot_p = np.zeros_like(p)
for prior in self.prior_list:
h_dot_p[prior['index']] -= (
prior['density_ddx'](x[prior['index']]) * p[prior['index']]
)
return h_dot_p
def residual(self, x):
"""Evaluate the residual representation of the prior at x."""
return np.asarray(
[prior['residual'](x[prior['index']]) for prior in self.prior_list]
)
def residual_jacobian(self, x):
"""
Evaluate residual Jacobian.
Evaluate the Jacobian of the residual representation of the prior
for a parameter vector x w.r.t. x, if available.
"""
sres = np.zeros((len(self.prior_list), len(x)))
for iprior, prior in enumerate(self.prior_list):
sres[iprior, prior['index']] = prior['residual_dx'](
x[prior['index']]
)
return sres
def get_parameter_prior_dict(
index: int,
prior_type: str,
prior_parameters: list,
parameter_scale: str = C.LIN,
):
"""
Return the prior dict used to define priors for some default priors.
index:
index of the parameter in x_full
prior_type:
Prior is defined in LINEAR=untransformed parameter space,
unless it starts with "parameterScale". prior_type
can be any of {"uniform", "normal", "laplace", "logNormal",
"parameterScaleUniform", "parameterScaleNormal",
"parameterScaleLaplace"}
prior_parameters:
Parameters of the priors. Parameters are defined in linear scale.
parameter_scale:
scale in which the parameter is defined (since a parameter can be
log-transformed, while the prior is always defined in the linear
space, unless it starts with "parameterScale")
"""
log_f, d_log_f_dx, dd_log_f_ddx, res, d_res_dx = _prior_densities(
prior_type, prior_parameters
)
if parameter_scale == C.LIN or prior_type.startswith('parameterScale'):
return {
'index': index,
'density_fun': log_f,
'density_dx': d_log_f_dx,
'density_ddx': dd_log_f_ddx,
'residual': res,
'residual_dx': d_res_dx,
}
elif parameter_scale == C.LOG:
def log_f_log(x_log):
"""Log-prior for log-parameters."""
return log_f(np.exp(x_log))
def d_log_f_log(x_log):
"""First derivative of log-prior w.r.t. log-parameters."""
return d_log_f_dx(np.exp(x_log)) * np.exp(x_log)
def dd_log_f_log(x_log):
"""Second derivative of log-prior w.r.t. log-parameters."""
return np.exp(x_log) * (
d_log_f_dx(np.exp(x_log))
+ np.exp(x_log) * dd_log_f_ddx(np.exp(x_log))
)
if res is not None:
def res_log(x_log):
"""Residual-prior for log-parameters."""
return res(np.exp(x_log))
else:
res_log = None
if d_res_dx is not None:
def d_res_log(x_log):
"""Residual-prior for log-parameters."""
return d_res_dx(np.exp(x_log)) * np.exp(x_log)
else:
d_res_log = None
return {
'index': index,
'density_fun': log_f_log,
'density_dx': d_log_f_log,
'density_ddx': dd_log_f_log,
'residual': res_log,
'residual_dx': d_res_log,
}
elif parameter_scale == C.LOG10:
log10 = np.log(10)
def log_f_log10(x_log10):
"""Log-prior for log10-parameters."""
return log_f(10**x_log10)
def d_log_f_log10(x_log10):
"""Rerivative of log-prior w.r.t. log10-parameters."""
return d_log_f_dx(10**x_log10) * log10 * 10**x_log10
def dd_log_f_log10(x_log10):
"""Second derivative of log-prior w.r.t. log10-parameters."""
return (
log10**2
* 10**x_log10
* (
dd_log_f_ddx(10**x_log10) * 10**x_log10
+ d_log_f_dx(10**x_log10)
)
)
res_log = None
if res is not None:
def res_log(x_log10):
"""Residual-prior for log10-parameters."""
return res(10**x_log10)
d_res_log = None
if d_res_dx is not None:
def d_res_log(x_log10):
"""Residual-prior for log10-parameters."""
return d_res_dx(10**x_log10) * log10 * 10**x_log10
return {
'index': index,
'density_fun': log_f_log10,
'density_dx': d_log_f_log10,
'density_ddx': dd_log_f_log10,
'residual': res_log,
'residual_dx': d_res_log,
}
else:
raise ValueError(
"NegLogPriors in parameters in scale "
f"{parameter_scale} are currently not supported."
)
def _prior_densities(
prior_type: str,
prior_parameters: np.array,
) -> [
Callable,
Callable,
Callable,
Union[Callable, None],
Union[Callable, None],
]:
"""
Create prior density functions.
Return a tuple of Callables of the (log-)density (in untransformed =
linear scale), unless prior_types starts with "parameterScale",
together with their first + second derivative (= sensis) w.r.t.
the parameters. If possible, a residual representation and its first
derivative w.r.t. the parameters is included as 4th and 5th element of
the vector. If a reformulation as residual is not possible, the respective
entries will be `None`.
Currently the following distributions are supported:
* uniform:
Uniform distribution on transformed parameter scale.
* parameterScaleUniform:
Uniform distribution on original parameter scale.
* normal:
Normal distribution on transformed parameter scale.
* parameterScaleNormal:
Normal distribution on original parameter scale.
* laplace:
Laplace distribution on transformed parameter scale
* parameterScaleLaplace:
Laplace distribution on original parameter scale.
* logNormal:
LogNormal distribution on transformed parameter scale
Currently not supported, but eventually planned are the
following distributions:
* logUniform
* logLaplace
Parameters
----------
prior_type:
string identifier indicating the distribution to be used. Here
"transformed" parameter scale refers to the scale in which
optimization is performed. For example, for parameters with scale
"log", "parameterScaleNormal" will apply a normally distributed prior
to logarithmic parameters, while "normal" will apply a normally
distributed prior to linear parameters. For parameters with scale
"lin", "parameterScaleNormal" and "normal" are equivalent.
prior_parameters:
parameters for the distribution
* uniform/parameterScaleUniform:
- prior_parameters[0]: lower bound
- prior_parameters[1]: upper bound
* laplace/parameterScaleLaplace:
- prior_parameters[0]: location parameter
- prior_parameters[1]: scale parameter
* normal/parameterScaleNormal:
- prior_parameters[0]: mean
- prior_parameters[1]: standard deviation
* logNormal:
- prior_parameters[0]: mean of log-parameters
- prior_parameters[1]: standard deviation of log-parameters
Returns
-------
log_f, d_log_f_dx, dd_log_f_ddx, res, d_res_dx:
Log density, first and second derivative, and if possible a residual
representation and its first derivative.
"""
if prior_type in [C.UNIFORM, C.PARAMETER_SCALE_UNIFORM]:
def log_f(x):
if prior_parameters[0] <= x <= prior_parameters[1]:
return -np.log(prior_parameters[1] - prior_parameters[0])
else:
return -np.inf
d_log_f_dx = _get_constant_function(0)
dd_log_f_ddx = _get_constant_function(0)
def res(x):
if prior_parameters[0] <= x <= prior_parameters[1]:
return 0
else:
return np.inf
d_res_dx = _get_constant_function(0)
return log_f, d_log_f_dx, dd_log_f_ddx, res, d_res_dx
elif prior_type in [C.NORMAL, C.PARAMETER_SCALE_NORMAL]:
mean = prior_parameters[0]
sigma = prior_parameters[1]
sigma2 = sigma**2
def log_f(x):
return -np.log(2 * np.pi * sigma2) / 2 - (x - mean) ** 2 / (
2 * sigma2
)
d_log_f_dx = _get_linear_function(-1 / sigma2, mean / sigma2)
dd_log_f_ddx = _get_constant_function(-1 / sigma2)
def res(x):
return (x - mean) / (np.sqrt(2) * sigma)
d_res_dx = _get_constant_function(1 / (np.sqrt(2) * sigma))
return log_f, d_log_f_dx, dd_log_f_ddx, res, d_res_dx
elif prior_type in [C.LAPLACE, C.PARAMETER_SCALE_LAPLACE]:
mean = prior_parameters[0]
scale = prior_parameters[1]
log_2_sigma = np.log(2 * prior_parameters[1])
def log_f(x):
return -log_2_sigma - abs(x - mean) / scale
def d_log_f_dx(x):
if x > mean:
return -1 / scale
else:
return 1 / scale
dd_log_f_ddx = _get_constant_function(0)
def res(x):
return np.sqrt(abs(x - mean) / scale)
def d_res_dx(x):
if x == mean:
logger.warning(
"x == mean in d_res_dx of Laplace prior. Returning NaN."
)
return math.nan
return 1 / 2 * (x - mean) / np.sqrt(scale * abs(x - mean) ** 3)
return log_f, d_log_f_dx, dd_log_f_ddx, res, d_res_dx
elif prior_type == C.LOG_UNIFORM:
# when implementing: add to tests
raise NotImplementedError
elif prior_type == C.LOG_NORMAL:
# TODO check again :)
mean = prior_parameters[0]
sigma = prior_parameters[1]
sqrt2_pi = np.sqrt(2 * np.pi)
def log_f(x):
return -np.log(sqrt2_pi * sigma * x) - (np.log(x) - mean) ** 2 / (
2 * sigma**2
)
def d_log_f_dx(x):
return -1 / x - (np.log(x) - mean) / (sigma**2 * x)
def dd_log_f_ddx(x):
return 1 / (x**2) - (1 - np.log(x) + mean) / (
sigma**2 * x**2
)
return log_f, d_log_f_dx, dd_log_f_ddx, None, None
elif prior_type == C.LOG_LAPLACE:
# when implementing: add to tests
raise NotImplementedError
else:
raise ValueError(
f'NegLogPriors of type {prior_type} are currently ' 'not supported'
)
def _get_linear_function(
slope: float,
intercept: float = 0.0,
):
"""Return a linear function."""
def function(x):
return slope * x + intercept
return function
def _get_constant_function(constant: float):
"""Define a callable returning the constant, regardless of the input."""
def function(x):
return constant
return function