-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathdist_math.py
398 lines (316 loc) · 11.4 KB
/
dist_math.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
'''
Created on Mar 7, 2011
@author: johnsalvatier
'''
from __future__ import division
import numpy as np
import scipy.linalg
import theano.tensor as tt
import theano
from .special import gammaln
from ..math import logdet as _logdet
from pymc3.theanof import floatX
c = - 0.5 * np.log(2 * np.pi)
def bound(logp, *conditions, **kwargs):
"""
Bounds a log probability density with several conditions.
Parameters
----------
logp : float
*conditions : booleans
broadcast_conditions : bool (optional, default=True)
If True, broadcasts logp to match the largest shape of the conditions.
This is used e.g. in DiscreteUniform where logp is a scalar constant and the shape
is specified via the conditions.
If False, will return the same shape as logp.
This is used e.g. in Multinomial where broadcasting can lead to differences in the logp.
Returns
-------
logp with elements set to -inf where any condition is False
"""
broadcast_conditions = kwargs.get('broadcast_conditions', True)
if broadcast_conditions:
alltrue = alltrue_elemwise
else:
alltrue = alltrue_scalar
return tt.switch(alltrue(conditions), logp, -np.inf)
def alltrue_elemwise(vals):
ret = 1
for c in vals:
ret = ret * (1 * c)
return ret
def alltrue_scalar(vals):
return tt.all([tt.all(1 * val) for val in vals])
def logpow(x, m):
"""
Calculates log(x**m) since m*log(x) will fail when m, x = 0.
"""
# return m * log(x)
return tt.switch(tt.eq(x, 0), -np.inf, m * tt.log(x))
def factln(n):
return gammaln(n + 1)
def binomln(n, k):
return factln(n) - factln(k) - factln(n - k)
def betaln(x, y):
return gammaln(x) + gammaln(y) - gammaln(x + y)
def std_cdf(x):
"""
Calculates the standard normal cumulative distribution function.
"""
return 0.5 + 0.5 * tt.erf(x / tt.sqrt(2.))
def i0(x):
"""
Calculates the 0 order modified Bessel function of the first kind""
"""
return tt.switch(tt.lt(x, 5), 1 + x**2 / 4 + x**4 / 64 + x**6 / 2304 + x**8 / 147456
+ x**10 / 14745600 + x**12 / 2123366400,
np.e**x / (2 * np.pi * x)**0.5 * (1 + 1 / (8 * x) + 9 / (128 * x**2) + 225 / (3072 * x**3)
+ 11025 / (98304 * x**4)))
def i1(x):
"""
Calculates the 1 order modified Bessel function of the first kind""
"""
return tt.switch(tt.lt(x, 5), x / 2 + x**3 / 16 + x**5 / 384 + x**7 / 18432 +
x**9 / 1474560 + x**11 / 176947200 + x**13 / 29727129600,
np.e**x / (2 * np.pi * x)**0.5 * (1 - 3 / (8 * x) + 15 / (128 * x**2) + 315 / (3072 * x**3)
+ 14175 / (98304 * x**4)))
def sd2rho(sd):
"""
`sd -> rho` theano converter
:math:`mu + sd*e = mu + log(1+exp(rho))*e`"""
return tt.log(tt.exp(sd) - 1)
def rho2sd(rho):
"""
`rho -> sd` theano converter
:math:`mu + sd*e = mu + log(1+exp(rho))*e`"""
return tt.log1p(tt.exp(rho))
def log_normal(x, mean, **kwargs):
"""
Calculate logarithm of normal distribution at point `x`
with given `mean` and `std`
Parameters
----------
x : Tensor
point of evaluation
mean : Tensor
mean of normal distribution
kwargs : one of parameters `{sd, tau, w, rho}`
Notes
-----
There are four variants for density parametrization.
They are:
1) standard deviation - `std`
2) `w`, logarithm of `std` :math:`w = log(std)`
3) `rho` that follows this equation :math:`rho = log(exp(std) - 1)`
4) `tau` that follows this equation :math:`tau = std^{-1}`
----
"""
sd = kwargs.get('sd')
w = kwargs.get('w')
rho = kwargs.get('rho')
tau = kwargs.get('tau')
eps = kwargs.get('eps', 0.0)
check = sum(map(lambda a: a is not None, [sd, w, rho, tau]))
if check > 1:
raise ValueError('more than one required kwarg is passed')
if check == 0:
raise ValueError('none of required kwarg is passed')
if sd is not None:
std = sd
elif w is not None:
std = tt.exp(w)
elif rho is not None:
std = rho2sd(rho)
else:
std = tau**(-1)
std += eps
return c - tt.log(tt.abs_(std)) - (x - mean) ** 2 / (2 * std ** 2)
def log_normal_mv(x, mean, gpu_compat=False, **kwargs):
"""
Calculate logarithm of normal distribution at point `x`
with given `mean` and `sigma` matrix
Parameters
----------
x : Tensor
point of evaluation
mean : Tensor
mean of normal distribution
kwargs : one of parameters `{cov, tau, chol}`
Flags
----------
gpu_compat : False, because LogDet is not GPU compatible yet.
If this is set as true, the GPU compatible (but numerically unstable) log(det) is used.
Notes
-----
There are three variants for density parametrization.
They are:
1) covariance matrix - `cov`
2) precision matrix - `tau`,
3) cholesky decomposition matrix - `chol`
----
"""
if gpu_compat:
def logdet(m):
return tt.log(tt.abs_(tt.nlinalg.det(m)))
else:
logdet = _logdet
T = kwargs.get('tau')
S = kwargs.get('cov')
L = kwargs.get('chol')
check = sum(map(lambda a: a is not None, [T, S, L]))
if check > 1:
raise ValueError('more than one required kwarg is passed')
if check == 0:
raise ValueError('none of required kwarg is passed')
# avoid unnecessary computations
if L is not None:
S = L.dot(L.T)
T = tt.nlinalg.matrix_inverse(S)
log_det = -logdet(S)
elif T is not None:
log_det = logdet(T)
else:
T = tt.nlinalg.matrix_inverse(S)
log_det = -logdet(S)
delta = x - mean
k = S.shape[0]
result = k * tt.log(2 * np.pi) - log_det
result += delta.dot(T).dot(delta)
return -1 / 2. * result
def MvNormalLogp():
"""Compute the log pdf of a multivariate normal distribution.
This should be used in MvNormal.logp once Theano#5908 is released.
Parameters
----------
cov : tt.matrix
The covariance matrix.
delta : tt.matrix
Array of deviations from the mean.
"""
cov = tt.matrix('cov')
cov.tag.test_value = floatX(np.eye(3))
delta = tt.matrix('delta')
delta.tag.test_value = floatX(np.zeros((2, 3)))
solve_lower = tt.slinalg.Solve(A_structure='lower_triangular')
solve_upper = tt.slinalg.Solve(A_structure='upper_triangular')
cholesky = Cholesky(nofail=True, lower=True)
n, k = delta.shape
chol_cov = cholesky(cov)
diag = tt.nlinalg.diag(chol_cov)
ok = tt.all(diag > 0)
chol_cov = tt.switch(ok, chol_cov, tt.fill(chol_cov, 1))
delta_trans = solve_lower(chol_cov, delta.T).T
result = n * k * tt.log(2 * np.pi)
result += 2.0 * n * tt.sum(tt.log(diag))
result += (delta_trans ** 2).sum()
result = -0.5 * result
logp = tt.switch(ok, result, -np.inf)
def dlogp(inputs, gradients):
g_logp, = gradients
cov, delta = inputs
g_logp.tag.test_value = floatX(np.array(1.))
n, k = delta.shape
chol_cov = cholesky(cov)
diag = tt.nlinalg.diag(chol_cov)
ok = tt.all(diag > 0)
chol_cov = tt.switch(ok, chol_cov, tt.fill(chol_cov, 1))
delta_trans = solve_lower(chol_cov, delta.T).T
inner = n * tt.eye(k) - tt.dot(delta_trans.T, delta_trans)
g_cov = solve_upper(chol_cov.T, inner)
g_cov = solve_upper(chol_cov.T, g_cov.T)
tau_delta = solve_upper(chol_cov.T, delta_trans.T)
g_delta = tau_delta.T
g_cov = tt.switch(ok, g_cov, -np.nan)
g_delta = tt.switch(ok, g_delta, -np.nan)
return [-0.5 * g_cov * g_logp, -g_delta * g_logp]
return theano.OpFromGraph(
[cov, delta], [logp], grad_overrides=dlogp, inline=True)
class Cholesky(theano.Op):
"""
Return a triangular matrix square root of positive semi-definite `x`.
This is a copy of the cholesky op in theano, that doesn't throw an
error if the matrix is not positive definite, but instead returns
nan.
This has been merged upstream and we should switch to that
version after the next theano release.
L = cholesky(X, lower=True) implies dot(L, L.T) == X.
"""
__props__ = ('lower', 'destructive', 'nofail')
def __init__(self, lower=True, nofail=False):
self.lower = lower
self.destructive = False
self.nofail = nofail
def make_node(self, x):
x = tt.as_tensor_variable(x)
if x.ndim != 2:
raise ValueError('Matrix must me two dimensional.')
return tt.Apply(self, [x], [x.type()])
def perform(self, node, inputs, outputs):
x = inputs[0]
z = outputs[0]
try:
z[0] = scipy.linalg.cholesky(x, lower=self.lower).astype(x.dtype)
except (ValueError, scipy.linalg.LinAlgError):
if self.nofail:
z[0] = np.eye(x.shape[-1])
z[0][0, 0] = np.nan
else:
raise
def grad(self, inputs, gradients):
"""
Cholesky decomposition reverse-mode gradient update.
Symbolic expression for reverse-mode Cholesky gradient taken from [0]_
References
----------
.. [0] I. Murray, "Differentiation of the Cholesky decomposition",
http://arxiv.org/abs/1602.07527
"""
x = inputs[0]
dz = gradients[0]
chol_x = self(x)
ok = tt.all(tt.nlinalg.diag(chol_x) > 0)
chol_x = tt.switch(ok, chol_x, tt.fill_diagonal(chol_x, 1))
dz = tt.switch(ok, dz, floatX(1))
# deal with upper triangular by converting to lower triangular
if not self.lower:
chol_x = chol_x.T
dz = dz.T
def tril_and_halve_diagonal(mtx):
"""Extracts lower triangle of square matrix and halves diagonal."""
return tt.tril(mtx) - tt.diag(tt.diagonal(mtx) / 2.)
def conjugate_solve_triangular(outer, inner):
"""Computes L^{-T} P L^{-1} for lower-triangular L."""
solve = tt.slinalg.Solve(A_structure="upper_triangular")
return solve(outer.T, solve(outer.T, inner.T).T)
s = conjugate_solve_triangular(
chol_x, tril_and_halve_diagonal(chol_x.T.dot(dz)))
if self.lower:
grad = tt.tril(s + s.T) - tt.diag(tt.diagonal(s))
else:
grad = tt.triu(s + s.T) - tt.diag(tt.diagonal(s))
return [tt.switch(ok, grad, floatX(np.nan))]
class SplineWrapper (theano.Op):
"""
Creates a theano operation from scipy.interpolate.UnivariateSpline
"""
__props__ = ('spline',)
itypes = [tt.dscalar]
otypes = [tt.dscalar]
def __init__(self, spline):
self.spline = spline
def perform(self, node, inputs, output_storage):
x, = inputs
output_storage[0][0] = np.asarray(self.spline(x))
class DifferentiableSplineWrapper (SplineWrapper):
"""
Creates a theano operation with defined gradient from
scipy.interpolate.UnivariateSpline
"""
def __init__(self, spline):
super(DifferentiableSplineWrapper, self).__init__(spline)
self.spline_grad = SplineWrapper(spline.derivative())
self.__props__ += ('spline_grad',)
def grad(self, inputs, grads):
x, = inputs
x_grad, = grads
return [x_grad * self.spline_grad(x)]