-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathgaussian.py
695 lines (534 loc) · 20.4 KB
/
gaussian.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# -*- coding: utf-8 -*-
"""
Optimal transport for Gaussian distributions
"""
# Author: Theo Gnassounou <[email protected]>
# Remi Flamary <[email protected]>
#
# License: MIT License
import warnings
from .backend import get_backend
from .utils import dots, is_all_finite, list_to_array
def bures_wasserstein_mapping(ms, mt, Cs, Ct, log=False):
r"""Return OT linear operator between samples.
The function estimates the optimal linear operator that aligns the two
empirical distributions. This is equivalent to estimating the closed
form mapping between two Gaussian distributions :math:`\mathcal{N}(\mu_s,\Sigma_s)`
and :math:`\mathcal{N}(\mu_t,\Sigma_t)` as proposed in
:ref:`[1] <references-OT-mapping-linear>` and discussed in remark 2.29 in
:ref:`[2] <references-OT-mapping-linear>`.
The linear operator from source to target :math:`M`
.. math::
M(\mathbf{x})= \mathbf{A} \mathbf{x} + \mathbf{b}
where :
.. math::
\mathbf{A} &= \Sigma_s^{-1/2} \left(\Sigma_s^{1/2}\Sigma_t\Sigma_s^{1/2} \right)^{1/2}
\Sigma_s^{-1/2}
\mathbf{b} &= \mu_t - \mathbf{A} \mu_s
Parameters
----------
ms : array-like (d,)
mean of the source distribution
mt : array-like (d,)
mean of the target distribution
Cs : array-like (d,d)
covariance of the source distribution
Ct : array-like (d,d)
covariance of the target distribution
log : bool, optional
record log if True
Returns
-------
A : (d, d) array-like
Linear operator
b : (1, d) array-like
bias
log : dict
log dictionary return only if log==True in parameters
.. _references-OT-mapping-linear:
References
----------
.. [1] Knott, M. and Smith, C. S. "On the optimal mapping of
distributions", Journal of Optimization Theory and Applications
Vol 43, 1984
.. [2] Peyré, G., & Cuturi, M. (2017). "Computational Optimal
Transport", 2018.
"""
ms, mt, Cs, Ct = list_to_array(ms, mt, Cs, Ct)
nx = get_backend(ms, mt, Cs, Ct)
Cs12 = nx.sqrtm(Cs)
Cs12inv = nx.inv(Cs12)
M0 = nx.sqrtm(dots(Cs12, Ct, Cs12))
A = dots(Cs12inv, M0, Cs12inv)
b = mt - nx.dot(ms, A)
if log:
log = {}
log['Cs12'] = Cs12
log['Cs12inv'] = Cs12inv
return A, b, log
else:
return A, b
def empirical_bures_wasserstein_mapping(xs, xt, reg=1e-6, ws=None,
wt=None, bias=True, log=False):
r"""Return OT linear operator between samples.
The function estimates the optimal linear operator that aligns the two
empirical distributions. This is equivalent to estimating the closed
form mapping between two Gaussian distributions :math:`\mathcal{N}(\mu_s,\Sigma_s)`
and :math:`\mathcal{N}(\mu_t,\Sigma_t)` as proposed in
:ref:`[1] <references-OT-mapping-linear>` and discussed in remark 2.29 in
:ref:`[2] <references-OT-mapping-linear>`.
The linear operator from source to target :math:`M`
.. math::
M(\mathbf{x})= \mathbf{A} \mathbf{x} + \mathbf{b}
where :
.. math::
\mathbf{A} &= \Sigma_s^{-1/2} \left(\Sigma_s^{1/2}\Sigma_t\Sigma_s^{1/2} \right)^{1/2}
\Sigma_s^{-1/2}
\mathbf{b} &= \mu_t - \mathbf{A} \mu_s
Parameters
----------
xs : array-like (ns,d)
samples in the source domain
xt : array-like (nt,d)
samples in the target domain
reg : float,optional
regularization added to the diagonals of covariances (>0)
ws : array-like (ns,1), optional
weights for the source samples
wt : array-like (ns,1), optional
weights for the target samples
bias: boolean, optional
estimate bias :math:`\mathbf{b}` else :math:`\mathbf{b} = 0` (default:True)
log : bool, optional
record log if True
Returns
-------
A : (d, d) array-like
Linear operator
b : (1, d) array-like
bias
log : dict
log dictionary return only if log==True in parameters
.. _references-OT-mapping-linear:
References
----------
.. [1] Knott, M. and Smith, C. S. "On the optimal mapping of
distributions", Journal of Optimization Theory and Applications
Vol 43, 1984
.. [2] Peyré, G., & Cuturi, M. (2017). "Computational Optimal
Transport", 2018.
"""
xs, xt = list_to_array(xs, xt)
nx = get_backend(xs, xt)
is_input_finite = is_all_finite(xs, xt)
d = xs.shape[1]
if bias:
mxs = nx.mean(xs, axis=0)[None, :]
mxt = nx.mean(xt, axis=0)[None, :]
xs = xs - mxs
xt = xt - mxt
else:
mxs = nx.zeros((1, d), type_as=xs)
mxt = nx.zeros((1, d), type_as=xs)
if ws is None:
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0]
if wt is None:
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0]
Cs = nx.dot((xs * ws).T, xs) / nx.sum(ws) + reg * nx.eye(d, type_as=xs)
Ct = nx.dot((xt * wt).T, xt) / nx.sum(wt) + reg * nx.eye(d, type_as=xt)
if log:
A, b, log = bures_wasserstein_mapping(mxs, mxt, Cs, Ct, log=log)
else:
A, b = bures_wasserstein_mapping(mxs, mxt, Cs, Ct)
if is_input_finite and not is_all_finite(A, b):
warnings.warn(
"Numerical errors were encountered in ot.gaussian.empirical_bures_wasserstein_mapping. "
"Consider increasing the regularization parameter `reg`.")
if log:
log['Cs'] = Cs
log['Ct'] = Ct
return A, b, log
else:
return A, b
def bures_wasserstein_distance(ms, mt, Cs, Ct, log=False):
r"""Return Bures Wasserstein distance between samples.
The function estimates the Bures-Wasserstein distance between two
empirical distributions source :math:`\mu_s` and target :math:`\mu_t`,
discussed in remark 2.31 :ref:`[1] <references-bures-wasserstein-distance>`.
The Bures Wasserstein distance between source and target distribution :math:`\mathcal{W}`
.. math::
\mathcal{W}(\mu_s, \mu_t)_2^2= \left\lVert \mathbf{m}_s - \mathbf{m}_t \right\rVert^2 + \mathcal{B}(\Sigma_s, \Sigma_t)^{2}
where :
.. math::
\mathbf{B}(\Sigma_s, \Sigma_t)^{2} = \text{Tr}\left(\Sigma_s + \Sigma_t - 2 \sqrt{\Sigma_s^{1/2}\Sigma_t\Sigma_s^{1/2}} \right)
Parameters
----------
ms : array-like (d,)
mean of the source distribution
mt : array-like (d,)
mean of the target distribution
Cs : array-like (d,d)
covariance of the source distribution
Ct : array-like (d,d)
covariance of the target distribution
log : bool, optional
record log if True
Returns
-------
W : float
Bures Wasserstein distance
log : dict
log dictionary return only if log==True in parameters
.. _references-bures-wasserstein-distance:
References
----------
.. [1] Peyré, G., & Cuturi, M. (2017). "Computational Optimal
Transport", 2018.
"""
ms, mt, Cs, Ct = list_to_array(ms, mt, Cs, Ct)
nx = get_backend(ms, mt, Cs, Ct)
Cs12 = nx.sqrtm(Cs)
B = nx.trace(Cs + Ct - 2 * nx.sqrtm(dots(Cs12, Ct, Cs12)))
W = nx.sqrt(nx.norm(ms - mt)**2 + B)
if log:
log = {}
log['Cs12'] = Cs12
return W, log
else:
return W
def empirical_bures_wasserstein_distance(xs, xt, reg=1e-6, ws=None,
wt=None, bias=True, log=False):
r"""Return Bures Wasserstein distance from mean and covariance of distribution.
The function estimates the Bures-Wasserstein distance between two
empirical distributions source :math:`\mu_s` and target :math:`\mu_t`,
discussed in remark 2.31 :ref:`[1] <references-bures-wasserstein-distance>`.
The Bures Wasserstein distance between source and target distribution :math:`\mathcal{W}`
.. math::
\mathcal{W}(\mu_s, \mu_t)_2^2= \left\lVert \mathbf{m}_s - \mathbf{m}_t \right\rVert^2 + \mathcal{B}(\Sigma_s, \Sigma_t)^{2}
where :
.. math::
\mathbf{B}(\Sigma_s, \Sigma_t)^{2} = \text{Tr}\left(\Sigma_s + \Sigma_t - 2 \sqrt{\Sigma_s^{1/2}\Sigma_t\Sigma_s^{1/2}} \right)
Parameters
----------
xs : array-like (ns,d)
samples in the source domain
xt : array-like (nt,d)
samples in the target domain
reg : float,optional
regularization added to the diagonals of covariances (>0)
ws : array-like (ns), optional
weights for the source samples
wt : array-like (ns), optional
weights for the target samples
bias: boolean, optional
estimate bias :math:`\mathbf{b}` else :math:`\mathbf{b} = 0` (default:True)
log : bool, optional
record log if True
Returns
-------
W : float
Bures Wasserstein distance
log : dict
log dictionary return only if log==True in parameters
.. _references-bures-wasserstein-distance:
References
----------
.. [1] Peyré, G., & Cuturi, M. (2017). "Computational Optimal
Transport", 2018.
"""
xs, xt = list_to_array(xs, xt)
nx = get_backend(xs, xt)
d = xs.shape[1]
if bias:
mxs = nx.mean(xs, axis=0)[None, :]
mxt = nx.mean(xt, axis=0)[None, :]
xs = xs - mxs
xt = xt - mxt
else:
mxs = nx.zeros((1, d), type_as=xs)
mxt = nx.zeros((1, d), type_as=xs)
if ws is None:
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0]
if wt is None:
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0]
Cs = nx.dot((xs * ws).T, xs) / nx.sum(ws) + reg * nx.eye(d, type_as=xs)
Ct = nx.dot((xt * wt).T, xt) / nx.sum(wt) + reg * nx.eye(d, type_as=xt)
if log:
W, log = bures_wasserstein_distance(mxs, mxt, Cs, Ct, log=log)
log['Cs'] = Cs
log['Ct'] = Ct
return W, log
else:
W = bures_wasserstein_distance(mxs, mxt, Cs, Ct)
return W
def gaussian_gromov_wasserstein_distance(Cov_s, Cov_t, log=False):
r""" Return the Gaussian Gromov-Wasserstein value from [57].
This function return the closed form value of the Gaussian Gromov-Wasserstein
distance between two Gaussian distributions
:math:`\mathcal{N}(\mu_s,\Sigma_s)` and :math:`\mathcal{N}(\mu_t,\Sigma_t)`
when the OT plan is assumed to be also Gaussian. See [57] Theorem 4.1 for
more details.
Parameters
----------
Cov_s : array-like (ds,ds)
covariance of the source distribution
Cov_t : array-like (dt,dt)
covariance of the target distribution
Returns
-------
G : float
Gaussian Gromov-Wasserstein distance
.. _references-gaussien_gromov_wasserstein_distance:
References
----------
.. [57] Delon, J., Desolneux, A., & Salmona, A. (2022). Gromov–Wasserstein
distances between Gaussian distributions. Journal of Applied Probability,
59(4), 1178-1198.
"""
nx = get_backend(Cov_s, Cov_t)
# ensure that Cov_s is the largest covariance matrix
# that is m >= n
if Cov_s.shape[0] < Cov_t.shape[0]:
Cov_s, Cov_t = Cov_t, Cov_s
n = Cov_t.shape[0]
# compte and sort eigenvalues decerasingly
d_s = nx.flip(nx.sort(nx.eigh(Cov_s)[0]))
d_t = nx.flip(nx.sort(nx.eigh(Cov_t)[0]))
# compute the gaussien Gromov-Wasserstein distance
res = 4 * (nx.sum(d_s) - nx.sum(d_t))**2 + 8 * nx.sum((d_s[:n] - d_t)**2) + 8 * nx.sum((d_s[n:])**2)
if log:
log = {}
log['d_s'] = d_s
log['d_t'] = d_t
return nx.sqrt(res), log
else:
return nx.sqrt(res)
def empirical_gaussian_gromov_wasserstein_distance(xs, xt, ws=None,
wt=None, log=False):
r"""Return Gaussian Gromov-Wasserstein distance between samples.
The function estimates the Gaussian Gromov-Wasserstein distance between two
Gaussien distributions source :math:`\mu_s` and target :math:`\mu_t`, whose
parameters are estimated from the provided samples :math:`\mathcal{X}_s` and
:math:`\mathcal{X}_t`. See [57] Theorem 4.1 for more details.
Parameters
----------
xs : array-like (ns,d)
samples in the source domain
xt : array-like (nt,d)
samples in the target domain
ws : array-like (ns,1), optional
weights for the source samples
wt : array-like (ns,1), optional
weights for the target samples
log : bool, optional
record log if True
Returns
-------
G : float
Gaussian Gromov-Wasserstein distance
.. _references-gaussien_gromov_wasserstein:
References
----------
.. [57] Delon, J., Desolneux, A., & Salmona, A. (2022). Gromov–Wasserstein
distances between Gaussian distributions. Journal of Applied Probability,
59(4), 1178-1198.
"""
xs, xt = list_to_array(xs, xt)
nx = get_backend(xs, xt)
if ws is None:
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0]
if wt is None:
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0]
mxs = nx.dot(ws.T, xs) / nx.sum(ws)
mxt = nx.dot(wt.T, xt) / nx.sum(wt)
xs = xs - mxs
xt = xt - mxt
Cs = nx.dot((xs * ws).T, xs) / nx.sum(ws)
Ct = nx.dot((xt * wt).T, xt) / nx.sum(wt)
if log:
G, log = gaussian_gromov_wasserstein_distance(Cs, Ct, log=log)
log['Cov_s'] = Cs
log['Cov_t'] = Ct
return G, log
else:
G = gaussian_gromov_wasserstein_distance(Cs, Ct)
return G
def gaussian_gromov_wasserstein_mapping(mu_s, mu_t, Cov_s, Cov_t, sign_eigs=None, log=False):
r""" Return the Gaussian Gromov-Wasserstein mapping from [57].
This function return the closed form value of the Gaussian
Gromov-Wasserstein mapping between two Gaussian distributions
:math:`\mathcal{N}(\mu_s,\Sigma_s)` and :math:`\mathcal{N}(\mu_t,\Sigma_t)`
when the OT plan is assumed to be also Gaussian. See [57] Theorem 4.1 for
more details.
Parameters
----------
mu_s : array-like (ds,)
mean of the source distribution
mu_t : array-like (dt,)
mean of the target distribution
Cov_s : array-like (ds,ds)
covariance of the source distribution
Cov_t : array-like (dt,dt)
covariance of the target distribution
log : bool, optional
record log if True
Returns
-------
A : (dt, ds) array-like
Linear operator
b : (1, dt) array-like
bias
.. _references-gaussien_gromov_wasserstein_mapping:
References
----------
.. [57] Delon, J., Desolneux, A., & Salmona, A. (2022). Gromov–Wasserstein
distances between Gaussian distributions. Journal of Applied Probability,
59(4), 1178-1198.
"""
nx = get_backend(mu_s, mu_t, Cov_s, Cov_t)
n = Cov_t.shape[0]
m = Cov_s.shape[0]
# compte and sort eigenvalues/eigenvectors decreasingly
d_s, U_s = nx.eigh(Cov_s)
id_s = nx.flip(nx.argsort(d_s))
d_s, U_s = d_s[id_s], U_s[:, id_s]
d_t, U_t = nx.eigh(Cov_t)
id_t = nx.flip(nx.argsort(d_t))
d_t, U_t = d_t[id_t], U_t[:, id_t]
if sign_eigs is None:
sign_eigs = nx.ones(min(m, n), type_as=mu_s)
if m >= n:
A = nx.concatenate((nx.diag(sign_eigs * nx.sqrt(d_t) / nx.sqrt(d_s[:n])), nx.zeros((n, m - n), type_as=mu_s)), axis=1).T
else:
A = nx.concatenate((nx.diag(sign_eigs * nx.sqrt(d_t[:m]) / nx.sqrt(d_s)), nx.zeros((n - m, m), type_as=mu_s)), axis=0).T
A = nx.dot(nx.dot(U_s, A), U_t.T)
# compute the gaussien Gromov-Wasserstein dis
b = mu_t - nx.dot(mu_s, A)
if log:
log = {}
log['d_s'] = d_s
log['d_t'] = d_t
log['U_s'] = U_s
log['U_t'] = U_t
return A, b, log
else:
return A, b
def empirical_gaussian_gromov_wasserstein_mapping(xs, xt, ws=None,
wt=None, sign_eigs=None, log=False):
r"""Return Gaussian Gromov-Wasserstein mapping between samples.
The function estimates the Gaussian Gromov-Wasserstein mapping between two
Gaussien distributions source :math:`\mu_s` and target :math:`\mu_t`, whose
parameters are estimated from the provided samples :math:`\mathcal{X}_s` and
:math:`\mathcal{X}_t`. See [57] Theorem 4.1 for more details.
Parameters
----------
xs : array-like (ns,ds)
samples in the source domain
xt : array-like (nt,dt)
samples in the target domain
ws : array-like (ns,1), optional
weights for the source samples
wt : array-like (ns,1), optional
weights for the target samples
sign_eigs : array-like (min(ds,dt),) or string, optional
sign of the eigenvalues of the mapping matrix, by default all signs will
be positive. If 'skewness' is provided, the sign of the eigenvalues is
selected as the product of the sign of the skewness of the projected data.
log : bool, optional
record log if True
Returns
-------
A : (dt, ds) array-like
Linear operator
b : (1, dt) array-like
bias
.. _references-empirical_gaussian_gromov_wasserstein_mapping:
References
----------
.. [57] Delon, J., Desolneux, A., & Salmona, A. (2022). Gromov–Wasserstein
distances between Gaussian distributions. Journal of Applied Probability,
59(4), 1178-1198.
"""
xs, xt = list_to_array(xs, xt)
nx = get_backend(xs, xt)
m = xs.shape[1]
n = xt.shape[1]
if ws is None:
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0]
if wt is None:
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0]
# estimate mean and covariance
mu_s = nx.dot(ws.T, xs) / nx.sum(ws)
mu_t = nx.dot(wt.T, xt) / nx.sum(wt)
xs = xs - mu_s
xt = xt - mu_t
Cov_s = nx.dot((xs * ws).T, xs) / nx.sum(ws)
Cov_t = nx.dot((xt * wt).T, xt) / nx.sum(wt)
# compte and sort eigenvalues/eigenvectors decreasingly
d_s, U_s = nx.eigh(Cov_s)
id_s = nx.flip(nx.argsort(d_s))
d_s, U_s = d_s[id_s], U_s[:, id_s]
d_t, U_t = nx.eigh(Cov_t)
id_t = nx.flip(nx.argsort(d_t))
d_t, U_t = d_t[id_t], U_t[:, id_t]
# select the sign of the eigenvalues
if sign_eigs is None:
sign_eigs = nx.ones(min(m, n), type_as=mu_s)
elif sign_eigs == 'skewness':
size = min(m, n)
skew_s = nx.sum((nx.dot(xs, U_s[:, :size]))**3 * ws, axis=0)
skew_t = nx.sum((nx.dot(xt, U_t[:, :size]))**3 * wt, axis=0)
sign_eigs = nx.sign(skew_t * skew_s)
if m >= n:
A = nx.concatenate((nx.diag(sign_eigs * nx.sqrt(d_t) / nx.sqrt(d_s[:n])), nx.zeros((n, m - n), type_as=mu_s)), axis=1).T
else:
A = nx.concatenate((nx.diag(sign_eigs * nx.sqrt(d_t[:m]) / nx.sqrt(d_s)), nx.zeros((n - m, m), type_as=mu_s)), axis=0).T
A = nx.dot(nx.dot(U_s, A), U_t.T)
# compute the gaussien Gromov-Wasserstein dis
b = mu_t - nx.dot(mu_s, A)
if log:
log = {}
log['d_s'] = d_s
log['d_t'] = d_t
log['U_s'] = U_s
log['U_t'] = U_t
log['Cov_s'] = Cov_s
log['Cov_t'] = Cov_t
return A, b, log
else:
return A, b
def dual_gaussian_init(xs, xt, ws=None, wt=None, reg=1e-6):
r""" Return the source dual potential gaussian initialization.
This function return the dual potential gaussian initialization that can be
used to initialize the Sinkhorn algorithm. This initialization is based on
the Monge mapping between the source and target distributions seen as two
Gaussian distributions [60].
Parameters
----------
xs : array-like (ns,ds)
samples in the source domain
xt : array-like (nt,dt)
samples in the target domain
ws : array-like (ns,1), optional
weights for the source samples
wt : array-like (ns,1), optional
weights for the target samples
reg : float,optional
regularization added to the diagonals of covariances (>0)
.. [60] Thornton, James, and Marco Cuturi. "Rethinking initialization of the
sinkhorn algorithm." International Conference on Artificial Intelligence
and Statistics. PMLR, 2023.
"""
nx = get_backend(xs, xt)
if ws is None:
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0]
if wt is None:
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0]
# estimate mean and covariance
mu_s = nx.dot(ws.T, xs) / nx.sum(ws)
mu_t = nx.dot(wt.T, xt) / nx.sum(wt)
A, b = empirical_bures_wasserstein_mapping(xs, xt, ws=ws, wt=wt, reg=reg)
xsc = xs - mu_s
# compute the dual potential (see appendix D in [60])
f = nx.sum(xs**2 - nx.dot(xsc, A) * xsc - mu_t * xs, 1)
return f