-
Notifications
You must be signed in to change notification settings - Fork 1
/
manifold.py
298 lines (237 loc) · 9.47 KB
/
manifold.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
import warnings
import numpy as np
from numpy.testing import assert_array_less, assert_array_almost_equal
from scipy import linalg
def my_stack(arrays):
return np.concatenate([a[np.newaxis] for a in arrays])
def sqrtm(mat):
""" Matrix square-root, for symetric positive definite matrices.
"""
vals, vecs = linalg.eigh(mat)
return np.dot(vecs * np.sqrt(vals), vecs.T)
def inv_sqrtm(mat):
""" Inverse of matrix square-root, for symetric positive definite matrices.
"""
vals, vecs = linalg.eigh(mat)
return np.dot(vecs / np.sqrt(vals), vecs.T)
def inv(mat):
""" Inverse of matrix, for symmetric positive definite matrices.
"""
vals, vecs = linalg.eigh(mat)
return np.dot(vecs / vals, vecs.T)
def logm(mat):
""" Logarithm of matrix, for symetric positive definite matrices
"""
vals, vecs = linalg.eigh(mat)
return np.dot(vecs * np.log(vals), vecs.T)
def expm(mat):
""" Exponential of matrix, for real symetric matrices
"""
try:
assert_array_almost_equal(mat, mat.T)
assert(np.all(np.isreal(mat)))
except AssertionError:
raise ValueError("at least one matrix is not real symmetric")
vals, vecs = linalg.eigh(mat)
return np.dot(vecs * np.exp(vals), vecs.T)
def tangent_space_norm(v, p):
""" Norm of vector v in the tangent space at point p"""
p_inv = inv(p)
return np.sqrt(np.trace(p_inv.dot(v).dot(p_inv).dot(v)))
def log_map(x, displacement, mean=False):
""" The Riemannian log map at point 'displacement'.
See Algorithm 2 of:
P. Thomas Fletcher, Sarang Joshi. Riemannian Geometry for the
Statistical Analysis of Diffusion Tensor Data. Signal Processing, 2007.
"""
vals, vecs = linalg.eigh(displacement)
sqrt_vals = np.sqrt(vals)
whitening = (vecs / sqrt_vals).T
vals_y, vecs_y = linalg.eigh(whitening.dot(x).dot(whitening.T))
sqrt_displacement = (vecs * sqrt_vals).dot(vecs_y)
return (sqrt_displacement * np.log(vals_y)).dot(sqrt_displacement.T)
def frechet_mean(mats, max_iter=10, tol=1e-3, adaptative=False):
""" Computes Frechet mean of a list of symmetric positive definite
matrices.
Minimization of the objective function by an intrinsic gradient descent in
the manifold: moving from the current point fre to the next one is
done along a short geodesic arc in the opposite direction of the covariant
derivative of the objective function evaluated at point fre.
See Algorithm 3 of:
P. Thomas Fletcher, Sarang Joshi. Riemannian Geometry for the
Statistical Analysis of Diffusion Tensor Data. Signal Processing, 2007.
Parameters
==========
mats: list of array
list of symmetric positive definite matrices, same shape.
max_iter: int, optional
maximal number of iterations.
tol: float, optional
tolerance.
Returns
=======
fre: array
Frechet mean of the matrices.
"""
# Real, symmetry and positive definiteness check
for mat in mats:
try:
assert_array_almost_equal(mat, mat.T)
assert(np.all(np.isreal(mat)))
assert_array_less(0.0, np.linalg.eigvalsh(mat))
except AssertionError:
raise ValueError("at least one matrix is not real spd")
mats = my_stack(mats)
# Initialization
fre = np.mean(mats, axis=0)
tolerance_reached = False
norm_old = np.inf
step = 1.
for n in xrange(max_iter):
vals_fre, vecs_fre = linalg.eigh(fre)
fre_inv_sqrt = (vecs_fre / np.sqrt(vals_fre)).dot(vecs_fre.T)
eighs = [linalg.eigh(fre_inv_sqrt.dot(mat).dot(fre_inv_sqrt)) for
mat in mats]
# Log map of mats[n] at point fre is
# sqrtm(fre).dot(logms[n]).dot(sqrtm(fre))
logms = [(vecs * np.log(vals)).dot(vecs.T) for vals, vecs in eighs]
# Covariant derivative is fre.dot(logms_mean)
logms_mean = np.mean(logms, axis=0)
try:
assert np.all(np.isfinite(logms_mean))
except AssertionError:
raise FloatingPointError("Nan value after logarithm operation")
vals_log, vecs_log = linalg.eigh(logms_mean)
# Move along the geodesic with stepsize step
fre_sqrt = (vecs_fre * np.sqrt(vals_fre)).dot(vecs_fre.T)
fre = fre_sqrt.dot(
vecs_log * np.exp(vals_log * step)).dot(vecs_log.T).dot(fre_sqrt)
# Norm of the covariant derivative on the tangent space at point fre
norm = np.sqrt(np.trace(logms_mean.dot(logms_mean)))
if tol is not None and norm < tol:
tolerance_reached = True
break
if norm > norm_old:
step = step / 2.
norm = norm_old
if norm < norm_old and adaptative:
step = 2. * step
if tol is not None and not tolerance_reached:
warnings.warn("Maximum number of iterations reached without") # +\
# " getting to the requested tolerance level.")
return fre
def grad_frechet_mean(mats, max_iter=10, tol=1e-3, adaptative=True):
""" Returns at each iteration step of the frechet_mean algorithm the norm
of the covariant derivative. Norm is intrinsic norm on the tangent space at
the Frechet mean at the current step.
Parameters
==========
mats: list of array
list of symmetric positive definite matrices, same shape.
max_iter: int, optional
maximal number of iterations.
tol: float, optional
tolerance.
Returns
=======
grad_norm: list of float
Norm of the covariant derivative in the tangent space at each step.
"""
# Real, symmetry and positive definiteness check
for mat in mats:
print mat.shape
try:
# assert(is_spd(mat)) # TODO: replace by assert(is_spd(mat)) and test
assert_array_almost_equal(mat, mat.T)
assert(np.all(np.isreal(mat)))
assert_array_less(0.0, np.linalg.eigvalsh(mat))
except AssertionError:
raise ValueError("at least one matrix is not real spd")
mats = my_stack(mats)
# Initialization
fre = np.mean(mats, axis=0)
norm_old = np.inf
step = 1.
tolerance_reached = False
grad_norm = []
for n in xrange(max_iter):
vals_fre, vecs_fre = linalg.eigh(fre)
fre_inv_sqrt = (vecs_fre / np.sqrt(vals_fre)).dot(vecs_fre.T)
eighs = [linalg.eigh(fre_inv_sqrt.dot(mat).dot(fre_inv_sqrt)) for
mat in mats]
# Log map of mats[n] at point fre is
# sqrtm(fre).dot(logms[n]).dot(sqrtm(fre))
logms = [(vecs * np.log(vals)).dot(vecs.T) for vals, vecs in eighs]
# Covariant derivative is fre.dot(logms_mean)
logms_mean = np.mean(logms, axis=0)
try:
assert np.all(np.isfinite(logms_mean))
except AssertionError:
raise FloatingPointError("Nan value after logarithm operation")
vals_log, vecs_log = linalg.eigh(logms_mean)
# Move along the geodesic with stepsize step
fre_sqrt = (vecs_fre * np.sqrt(vals_fre)).dot(vecs_fre.T)
fre = fre_sqrt.dot(
vecs_log * np.exp(vals_log * step)).dot(vecs_log.T).dot(fre_sqrt)
# Norm of the covariant derivative on the tangent space at point fre
norm = np.sqrt(np.trace(logms_mean.dot(logms_mean)))
grad_norm.append(norm)
if tol is not None and norm < tol:
tolerance_reached = True
break
if norm > norm_old:
step = step / 2.
norm = norm_old
if tol is not None and not tolerance_reached:
warnings.warn("Maximum number of iterations reached without") # +\
# " getting to the requested tolerance level.")
return grad_norm
def random_diagonal(shape, d_min=0., d_max=1.):
"""Generates random diagonal matrix, with elements in the range
[d_min, d_max]
"""
d = np.random.rand(shape) * (d_max - d_min) + d_min
return np.diag(d)
def random_diagonal_spd(shape, d_min=1., d_max=2.):
"""Generates random positive definite diagonal matrix"""
assert(d_min > 0)
assert(d_max > 0)
return random_diagonal(shape, d_min, d_max)
def random_spd(shape, eig_min=1.0, eig_max=2.0):
"""Generates random symmetric positive definite matrix"""
ran = np.random.rand(shape, shape)
q, _ = linalg.qr(ran)
d = random_diagonal_spd(shape, eig_min, eig_max)
return q.dot(d).dot(q.T)
def random_non_singular(shape):
"""Generates random non singular matrix"""
d = random_diagonal_spd(shape)
ran1 = np.random.rand(shape, shape)
ran2 = np.random.rand(shape, shape)
u, _ = linalg.qr(ran1)
v, _ = linalg.qr(ran2)
return u.dot(d).dot(v.T)
def is_spd(M, decimal=15):
"""Assert that input matrix is real symmetric positive definite.
M must be symmetric down to specified decimal places and with no complex
entry.
The check is performed by checking that all eigenvalues are positive.
Parameters
==========
M: numpy.ndarray
matrix.
Returns
=======
answer: boolean
True if matrix is symmetric real positive definite, False otherwise.
"""
if not np.allclose(M, M.T, atol=0.1 ** decimal):
print("matrix not symmetric to {0} decimals".format(decimal))
return False
if np.all(np.iscomplex(M)):
print("matrix has a non real value {0}".format(M[np.iscomplex(M)][0]))
eigvalsh = np.linalg.eigvalsh(M)
ispd = eigvalsh.min() > 0
if not ispd:
print("matrix has a negative eigenvalue: %.3f" % eigvalsh.min())
return ispd