/
flag-manifold-distance.py
418 lines (364 loc) · 14.3 KB
/
flag-manifold-distance.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
"""
Authors' implementation of [Szwagier2023].
References
----------
.. [Szwagier2023] Szwagier, T., Pennec, X. (2023). Rethinking the Riemannian Logarithm on Flag Manifolds as an Orthogonal Alignment Problem.
In: Nielsen, F., Barbaresco, F. (eds) Geometric Science of Information. GSI 2023. Lecture Notes in Computer Science, vol 14071. Springer, Cham. https://doi.org/10.1007/978-3-031-38271-0_37
"""
from geomstats.geometry.skew_symmetric_matrices import SkewSymmetricMatrices
import itertools
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.stats import ortho_group, special_ortho_group
from scipy.linalg import expm, logm, block_diag, svd
from time import time
def projector_embedding(Q, signature_0):
r""" Embed a flag in a product of Grassmannians, by forming orthogonal projection
matrices relative to each subspace of the flag. Corresponds to the embedding map
:math:`\Pi_{\operatorname{Gr}(\I)}` in [Szwagier2023].
Parameters
----------
Q: array-like, shape=[n, n]
An orthogonal matrix representing a flag.
signature_0: tuple of ints
The signature of the flag.
Returns
-------
Q_proj: array-like, shape=[n, n * r]
The sequence of orthogonal projection matrices related to the flag.
The sequence is horizontally concatenated into one (n, n * r) array.
"""
n = signature_0[-1]
Q_proj = np.zeros((signature_0[-1], signature_0[-1] * len(signature_0[1:])))
for i in range(len(signature_0[1:])):
Q_i = Q[:, signature_0[i]: signature_0[i + 1]]
Q_proj[:, n * i:n * (i + 1)] = (Q_i @ Q_i.T)
return Q_proj
def projection_kernel(signature_0):
""" Create a binary mask with 1s on the diagonal blocks related to a given
signature.
Parameters
----------
signature_0: tuple of ints
The signature of the flag.
Returns
-------
skew_diag_kernel: array-like, shape=[n, n]
A binary mask with 1s on the diagonal blocks related to a given signature.
"""
n = signature_0[-1]
skew_diag_kernel = np.zeros((n, n))
for (d, d_) in zip(signature_0[:-1], signature_0[1:]):
skew_diag_kernel[d:d_, d:d_] = np.ones((d_ - d, d_ - d))
return skew_diag_kernel
def proj_H(X, skew_diag_kernel):
r""" Project a tangent vector of :math:`\mathcal{O}(n)` onto the horizontal space
w.r.t the flag action.
Parameters
----------
X: array-like, shape=[n, n]
A tangent vector in O(n).
skew_diag_kernel: array-like, shape=[n, n]
A binary mask with 1s on the diagonal blocks related to a given signature.
Returns
-------
H: array-like, shape=[n, n]
A horizontal vector.
"""
return X * (1 - skew_diag_kernel)
def proj_V(X, skew_diag_kernel):
r""" Project a tangent vector of :math:`\mathcal{O}(n)` onto the vertical space
w.r.t the flag action.
Parameters
----------
X: array-like, shape=[n, n]
A tangent vector in O(n).
skew_diag_kernel: array-like, shape=[n, n]
A binary mask with 1s on the diagonal blocks related to a given signature.
Returns
-------
V: array-like, shape=[n, n]
A vertical vector.
"""
return X * skew_diag_kernel
def flag_norm(H):
""" Compute the norm of a tangent vector w.r.t. the flag canonical metric.
Parameters
----------
H: array-like, shape=[n, n]
A horizontal vector.
Returns
-------
||H||_c: positive real
The norm of H w.r.t the flag canonical metric.
"""
return np.sqrt(1 / 2 * np.trace(H.T @ H))
def align_eucl(P, Q, signature_0):
""" Use orthogonal Procrustes analysis as in [Szwagier2023, Theorem 2], to find the
closest orthogonal matrix to P in the equivalence class of Q, in Frobenius distance.
Parameters
----------
P: array-like, shape=[n, n]
An orthogonal matrix representing a flag.
Q: array-like, shape=[n, n]
An orthogonal matrix representing a flag.
signature_0: tuple of ints
The signature of the flag manifold.
Returns
-------
Q_procrustes: array-like, shape=[n, n]
An orthogonal matrix representing the closest orthogonal matrix to P in the
equivalence class of Q, in Frobenius distance.
"""
R_list = []
angle_list = []
for i in range(len(signature_0) - 1):
P_i = P[:, signature_0[i]: signature_0[i + 1]]
Q_i = Q[:, signature_0[i]: signature_0[i + 1]]
U, s, Vh = svd(Q_i.T @ P_i)
angle = np.arccos(np.clip(s, 0, 1))
angle_list.append(angle)
R_list.append(U @ Vh)
R_procrustes = block_diag(*R_list)
Q_procrustes = Q @ R_procrustes
return Q_procrustes
def random_uniform_orbit(signature_0):
r""" Sample a matrix from the uniform distribution on
:math:`\mathcal{O}(\mathcal{I})`.
Parameters
----------
signature_0: tuple of ints
The signature of the flag.
Returns
-------
R: array-like, shape=[n, n]
A random uniform matrix in :math:`\mathcal{O}(\mathcal{I})`.
"""
flag_type = tuple(np.diff(signature_0))
R_list = []
for n_i in flag_type:
if n_i == 1:
R_list.append((2 * np.random.randint(2) - 1) * np.ones((1, 1)))
else:
R_list.append(ortho_group.rvs(dim=n_i))
return block_diag(*R_list)
def random_uniform_special_orbit(signature_0):
r""" Sample a matrix from the uniform distribution on
:math:`\mathcal{SO}(\mathcal{I})`.
Parameters
----------
signature_0: tuple of ints
The signature of the flag.
Returns
-------
R: array-like, shape=[n, n]
A random uniform matrix in :math:`\mathcal{SO}(\mathcal{I})`.
"""
flag_type = tuple(np.diff(signature_0))
R_list = []
for n_i in flag_type:
if n_i == 1:
R_list.append(np.ones((1, 1)))
else:
R_list.append(special_ortho_group.rvs(dim=n_i))
return block_diag(*R_list)
def switch_sign_cols(Q, signature_0):
r""" Generate a list with all the equivalents of Q in the different connected
components of :math:`\mathcal{O}(\mathcal{I})`, similarly as in [Ma2022].
Parameters
----------
Q: array-like, shape=[n, n]
A flag.
signature_0: tuple of ints
The signature of the flag.
Returns
-------
Q_list: list
The list with all the equivalents of Q in the different connected components of
:math:`\mathcal{O}(\mathcal{I})`.
References
----------
.. [Ma2022] X. Ma, M. Kirby, C.Peterson.
“Self-organizing mappings on the flag manifold with applications to
hyper-spectral image data analysis.” Neural Computing and Applications. 2022.
"""
Q_list = []
for j in range(len(signature_0[1:]) + 1):
for cols_to_switch_sign in itertools.combinations(signature_0[:-1], j):
Q_i = np.copy(Q)
Q_i[:, cols_to_switch_sign] = - Q_i[:, cols_to_switch_sign]
Q_list.append(Q_i)
return Q_list
def flag_log(P, Q, signature_0, skew_diag_kernel, itermax=50, eps=1e-5):
""" Algorithm for the approximation of the Riemannian logarithm on flag manifolds,
using [Szwagier2023, Algorithm 1].
Parameters
----------
P: array-like, shape=[n, n]
A flag.
Q: array-like, shape=[n, n]
A flag.
signature_0: tuple of ints
The signature of the flag.
skew_diag_kernel: array-like, shape=[n, n]
A binary mask with 1s on the diagonal blocks related to a given signature.
itermax: int
The maximal number of iterations.
eps: positive real
The endpoint error threshold under which we stop the algorithm.
Returns
-------
H_: array-like, shape=[n, n]
The horizontal vector approximating the Riemannian logarithm.
flag_norm(H_): positive real
Its norm, which is also the geodesic distance.
err:
The final endpoint error.
count: int
The number of iterations of the algorithm.
time: positive real
The total running time.
err_list: list of real
The history of endpoint errors along the iterations.
"""
start = time()
Q_ = np.copy(Q)
err_list = []
count = 0
while True:
X_ = logm(P.T @ Q_)
H_ = proj_H(X_, skew_diag_kernel)
M_ = P @ expm(H_)
err = np.linalg.norm(
projector_embedding(Q, signature_0) - projector_embedding(M_, signature_0),
2)
err_list.append(err)
if (count >= itermax) or (err <= eps):
break
Q_ = align_eucl(M_, Q, signature_0)
count += 1
return H_, flag_norm(H_), err, count, time() - start, err_list
def flag_log_ma(P, Q, signature_0, skew_diag_kernel, itermax=50, eps=1e-5,
try_all_orientations=True):
""" Algorithm for the approximation of the Riemannian logarithm on flag manifolds,
using [Ma2022, Algorithm 1].
Parameters
----------
P: array-like, shape=[n, n]
A flag.
Q: array-like, shape=[n, n]
A flag.
signature_0: tuple of ints
The signature of the flag.
skew_diag_kernel: array-like, shape=[n, n]
A binary mask with 1s on the diagonal blocks related to a given signature.
itermax: int
The maximal number of iterations.
eps: positive real
The endpoint error threshold under which we stop the algorithm.
try_all_orientations: bool
Whether we try all the connected components in
:math:`\mathcal{O}(\mathcal{I})`, like done in [Ma2022].
Returns
-------
H_: array-like, shape=[n, n]
The horizontal vector approximating the Riemannian logarithm.
flag_norm(H_): positive real
Its norm, which is also the geodesic distance.
err:
The final endpoint error.
count: int
The number of iterations of the algorithm.
time: positive real
The total running time.
err_list: list of real
The history of endpoint errors along the iterations.
"""
start = time()
Q_list = [Q]
if try_all_orientations:
Q_list = switch_sign_cols(Q, signature_0)
H_opt, dist_opt, err_opt, count_opt = None, None, np.inf, None
for Q in Q_list:
try:
G_ = random_uniform_special_orbit(signature_0)
err_list = []
count = 0
while True:
H_ = proj_H(logm((P.T @ Q) @ expm(-G_)), skew_diag_kernel)
err = np.linalg.norm(
projector_embedding(Q, signature_0) - projector_embedding(
P @ expm(H_), signature_0), 2)
err_list.append(err)
if (count >= itermax) or (err <= eps):
break
G_ = proj_V(logm(expm(-H_) @ (P.T @ Q)), skew_diag_kernel)
count += 1
if err < err_opt:
H_opt, dist_opt, err_opt, count_opt, err_list_opt = H_, flag_norm(
H_), err, count, err_list
except Exception:
print("Algo exploded")
continue
return H_opt, dist_opt, err_opt, count_opt, time() - start, err_list_opt
signature = (1, 3, 5, 20, 100)
k, n = signature[-2:]
signature_0 = (0,) + signature
flag_type = tuple(np.diff(signature_0))
skew_diag_kernel = projection_kernel(signature_0)
path = "C:/Users/tszwagie/repos/Obsidian/Research Diary/Writing/GSI2023/figures/"
np.random.seed(42)
n_experiments = 10
itermax = 10
for n_exp, dist_true in enumerate([.2 * np.pi, .5 * np.pi, 1 * np.pi]):
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
results_mean = np.zeros((2, 5))
results_std = np.zeros((2, 5))
results_align = np.zeros((n_experiments, 5))
results_proj = np.zeros((n_experiments, 5))
for i in range(n_experiments):
print(i)
P = ortho_group.rvs(dim=n)
H_true = proj_H(SkewSymmetricMatrices(n).random_point(), skew_diag_kernel)
H_true = H_true / flag_norm(H_true) * dist_true
Q_aligned = P @ expm(H_true)
R_true = random_uniform_orbit(signature_0)
Q = Q_aligned @ R_true
Q_procrustes = align_eucl(P, Q, signature_0)
H, dist, err, count, dt, err_list = flag_log(P, Q_procrustes, signature_0,
skew_diag_kernel, itermax=itermax,
eps=1e-5)
results_align[i] = np.array(
[err, abs(dist - dist_true), np.linalg.norm(H - H_true, 2), count, dt])
ax.plot(np.arange(0, count + 1), err_list, color='tab:red',
label='Alignment' if i == 0 else None)
H, dist, err, count, dt, err_list = flag_log_ma(P, Q, signature_0,
skew_diag_kernel,
itermax=itermax, eps=1e-5,
try_all_orientations=True)
results_proj[i] = np.array(
[err, abs(dist - dist_true), np.linalg.norm(H - H_true, 2), count, dt])
ax.plot(np.arange(0, count + 1), err_list, color='tab:blue', ls='--',
label='Alternate projections' if i == 0 else None)
ax.set_yscale('log')
ax.set_xlabel('Iteration')
ax.set_ylabel('Endpoint error')
plt.legend()
plt.savefig(path + f"Exp{n_exp+1}_(1,3,5,20,100).png", dpi='figure', format='png', transparent=True)
plt.savefig(path + f"Exp{n_exp+1}_(1,3,5,20,100).pdf", dpi='figure', format='pdf', transparent=True)
plt.savefig(path + f"Exp{n_exp+1}_(1,3,5,20,100).svg", dpi='figure', format='svg', transparent=True)
results_mean[0] = np.mean(results_align, axis=0)
results_mean[1] = np.mean(results_proj, axis=0)
results_std[0] = np.std(results_align, axis=0)
results_std[1] = np.std(results_proj, axis=0)
df_results_mean = pd.DataFrame(results_mean,
index=['Alignment', 'Alternate projections'],
columns=['endpoint error', 'dist error',
'tangent error', 'iterations', 'time'])
df_results_std = pd.DataFrame(results_std,
index=['Alignment', 'Alternate projections'],
columns=['endpoint error', 'dist error',
'tangent error', 'iterations', 'time'])
print(df_results_mean.to_latex(float_format="%.1e"))
print(df_results_std.to_latex(float_format="%.1e"))