| 1 |
|
| 2 |
""" |
| 3 |
Optimal transport for Gaussian distributions |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import warnings |
| 12 |
|
| 13 |
from .backend import get_backend |
| 14 |
from .utils import dots, is_all_finite, list_to_array |
| 15 |
|
| 16 |
|
| 17 |
def bures_wasserstein_mapping(ms, mt, Cs, Ct, log=False): |
| 18 |
r"""Return OT linear operator between samples. |
| 19 |
|
| 20 |
The function estimates the optimal linear operator that aligns the two |
| 21 |
empirical distributions. This is equivalent to estimating the closed |
| 22 |
form mapping between two Gaussian distributions :math:`\mathcal{N}(\mu_s,\Sigma_s)` |
| 23 |
and :math:`\mathcal{N}(\mu_t,\Sigma_t)` as proposed in |
| 24 |
:ref:`[1] <references-OT-mapping-linear>` and discussed in remark 2.29 in |
| 25 |
:ref:`[2] <references-OT-mapping-linear>`. |
| 26 |
|
| 27 |
The linear operator from source to target :math:`M` |
| 28 |
|
| 29 |
.. math:: |
| 30 |
M(\mathbf{x})= \mathbf{A} \mathbf{x} + \mathbf{b} |
| 31 |
|
| 32 |
where : |
| 33 |
|
| 34 |
.. math:: |
| 35 |
\mathbf{A} &= \Sigma_s^{-1/2} \left(\Sigma_s^{1/2}\Sigma_t\Sigma_s^{1/2} \right)^{1/2} |
| 36 |
\Sigma_s^{-1/2} |
| 37 |
|
| 38 |
\mathbf{b} &= \mu_t - \mathbf{A} \mu_s |
| 39 |
|
| 40 |
Parameters |
| 41 |
---------- |
| 42 |
ms : array-like (d,) |
| 43 |
mean of the source distribution |
| 44 |
mt : array-like (d,) |
| 45 |
mean of the target distribution |
| 46 |
Cs : array-like (d,d) |
| 47 |
covariance of the source distribution |
| 48 |
Ct : array-like (d,d) |
| 49 |
covariance of the target distribution |
| 50 |
log : bool, optional |
| 51 |
record log if True |
| 52 |
|
| 53 |
|
| 54 |
Returns |
| 55 |
------- |
| 56 |
A : (d, d) array-like |
| 57 |
Linear operator |
| 58 |
b : (1, d) array-like |
| 59 |
bias |
| 60 |
log : dict |
| 61 |
log dictionary return only if log==True in parameters |
| 62 |
|
| 63 |
|
| 64 |
.. _references-OT-mapping-linear: |
| 65 |
References |
| 66 |
---------- |
| 67 |
.. [1] Knott, M. and Smith, C. S. "On the optimal mapping of |
| 68 |
distributions", Journal of Optimization Theory and Applications |
| 69 |
Vol 43, 1984 |
| 70 |
|
| 71 |
.. [2] Peyré, G., & Cuturi, M. (2017). "Computational Optimal |
| 72 |
Transport", 2018. |
| 73 |
""" |
| 74 |
ms, mt, Cs, Ct = list_to_array(ms, mt, Cs, Ct) |
| 75 |
nx = get_backend(ms, mt, Cs, Ct) |
| 76 |
|
| 77 |
Cs12 = nx.sqrtm(Cs) |
| 78 |
Cs12inv = nx.inv(Cs12) |
| 79 |
|
| 80 |
M0 = nx.sqrtm(dots(Cs12, Ct, Cs12)) |
| 81 |
|
| 82 |
A = dots(Cs12inv, M0, Cs12inv) |
| 83 |
|
| 84 |
b = mt - nx.dot(ms, A) |
| 85 |
|
| 86 |
if log: |
| 87 |
log = {} |
| 88 |
log["Cs12"] = Cs12 |
| 89 |
log["Cs12inv"] = Cs12inv |
| 90 |
return A, b, log |
| 91 |
else: |
| 92 |
return A, b |
| 93 |
|
| 94 |
|
| 95 |
def empirical_bures_wasserstein_mapping( |
| 96 |
xs, xt, reg=1e-6, ws=None, wt=None, bias=True, log=False |
| 97 |
): |
| 98 |
r"""Return OT linear operator between samples. |
| 99 |
|
| 100 |
The function estimates the optimal linear operator that aligns the two |
| 101 |
empirical distributions. This is equivalent to estimating the closed |
| 102 |
form mapping between two Gaussian distributions :math:`\mathcal{N}(\mu_s,\Sigma_s)` |
| 103 |
and :math:`\mathcal{N}(\mu_t,\Sigma_t)` as proposed in |
| 104 |
:ref:`[1] <references-OT-mapping-linear>` and discussed in remark 2.29 in |
| 105 |
:ref:`[2] <references-OT-mapping-linear>`. |
| 106 |
|
| 107 |
The linear operator from source to target :math:`M` |
| 108 |
|
| 109 |
.. math:: |
| 110 |
M(\mathbf{x})= \mathbf{A} \mathbf{x} + \mathbf{b} |
| 111 |
|
| 112 |
where : |
| 113 |
|
| 114 |
.. math:: |
| 115 |
\mathbf{A} &= \Sigma_s^{-1/2} \left(\Sigma_s^{1/2}\Sigma_t\Sigma_s^{1/2} \right)^{1/2} |
| 116 |
\Sigma_s^{-1/2} |
| 117 |
|
| 118 |
\mathbf{b} &= \mu_t - \mathbf{A} \mu_s |
| 119 |
|
| 120 |
Parameters |
| 121 |
---------- |
| 122 |
xs : array-like (ns,d) |
| 123 |
samples in the source domain |
| 124 |
xt : array-like (nt,d) |
| 125 |
samples in the target domain |
| 126 |
reg : float,optional |
| 127 |
regularization added to the diagonals of covariances (>0) |
| 128 |
ws : array-like (ns,1), optional |
| 129 |
weights for the source samples |
| 130 |
wt : array-like (ns,1), optional |
| 131 |
weights for the target samples |
| 132 |
bias: boolean, optional |
| 133 |
estimate bias :math:`\mathbf{b}` else :math:`\mathbf{b} = 0` (default:True) |
| 134 |
log : bool, optional |
| 135 |
record log if True |
| 136 |
|
| 137 |
|
| 138 |
Returns |
| 139 |
------- |
| 140 |
A : (d, d) array-like |
| 141 |
Linear operator |
| 142 |
b : (1, d) array-like |
| 143 |
bias |
| 144 |
log : dict |
| 145 |
log dictionary return only if log==True in parameters |
| 146 |
|
| 147 |
|
| 148 |
.. _references-OT-mapping-linear: |
| 149 |
References |
| 150 |
---------- |
| 151 |
.. [1] Knott, M. and Smith, C. S. "On the optimal mapping of |
| 152 |
distributions", Journal of Optimization Theory and Applications |
| 153 |
Vol 43, 1984 |
| 154 |
|
| 155 |
.. [2] Peyré, G., & Cuturi, M. (2017). "Computational Optimal |
| 156 |
Transport", 2018. |
| 157 |
""" |
| 158 |
xs, xt = list_to_array(xs, xt) |
| 159 |
nx = get_backend(xs, xt) |
| 160 |
is_input_finite = is_all_finite(xs, xt) |
| 161 |
|
| 162 |
d = xs.shape[1] |
| 163 |
|
| 164 |
if ws is None: |
| 165 |
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0] |
| 166 |
|
| 167 |
if wt is None: |
| 168 |
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0] |
| 169 |
|
| 170 |
if bias: |
| 171 |
mxs = nx.dot(ws.T, xs) / nx.sum(ws) |
| 172 |
mxt = nx.dot(wt.T, xt) / nx.sum(wt) |
| 173 |
|
| 174 |
xs = xs - mxs |
| 175 |
xt = xt - mxt |
| 176 |
else: |
| 177 |
mxs = nx.zeros((1, d), type_as=xs) |
| 178 |
mxt = nx.zeros((1, d), type_as=xs) |
| 179 |
|
| 180 |
Cs = nx.dot((xs * ws).T, xs) / nx.sum(ws) + reg * nx.eye(d, type_as=xs) |
| 181 |
Ct = nx.dot((xt * wt).T, xt) / nx.sum(wt) + reg * nx.eye(d, type_as=xt) |
| 182 |
|
| 183 |
if log: |
| 184 |
A, b, log = bures_wasserstein_mapping(mxs, mxt, Cs, Ct, log=log) |
| 185 |
else: |
| 186 |
A, b = bures_wasserstein_mapping(mxs, mxt, Cs, Ct) |
| 187 |
|
| 188 |
if is_input_finite and not is_all_finite(A, b): |
| 189 |
warnings.warn( |
| 190 |
"Numerical errors were encountered in ot.gaussian.empirical_bures_wasserstein_mapping. " |
| 191 |
"Consider increasing the regularization parameter `reg`." |
| 192 |
) |
| 193 |
|
| 194 |
if log: |
| 195 |
log["Cs"] = Cs |
| 196 |
log["Ct"] = Ct |
| 197 |
return A, b, log |
| 198 |
else: |
| 199 |
return A, b |
| 200 |
|
| 201 |
|
| 202 |
def bures_wasserstein_distance(ms, mt, Cs, Ct, log=False): |
| 203 |
r"""Return Bures Wasserstein distance between samples. |
| 204 |
|
| 205 |
The function estimates the Bures-Wasserstein distance between two |
| 206 |
empirical distributions source :math:`\mu_s` and target :math:`\mu_t`, |
| 207 |
discussed in remark 2.31 :ref:`[1] <references-bures-wasserstein-distance>`. |
| 208 |
|
| 209 |
The Bures Wasserstein distance between source and target distribution :math:`\mathcal{W}` |
| 210 |
|
| 211 |
.. math:: |
| 212 |
\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} |
| 213 |
|
| 214 |
where : |
| 215 |
|
| 216 |
.. math:: |
| 217 |
\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) |
| 218 |
|
| 219 |
Parameters |
| 220 |
---------- |
| 221 |
ms : array-like (d,) |
| 222 |
mean of the source distribution |
| 223 |
mt : array-like (d,) |
| 224 |
mean of the target distribution |
| 225 |
Cs : array-like (d,d) |
| 226 |
covariance of the source distribution |
| 227 |
Ct : array-like (d,d) |
| 228 |
covariance of the target distribution |
| 229 |
log : bool, optional |
| 230 |
record log if True |
| 231 |
|
| 232 |
|
| 233 |
Returns |
| 234 |
------- |
| 235 |
W : float |
| 236 |
Bures Wasserstein distance |
| 237 |
log : dict |
| 238 |
log dictionary return only if log==True in parameters |
| 239 |
|
| 240 |
|
| 241 |
.. _references-bures-wasserstein-distance: |
| 242 |
References |
| 243 |
---------- |
| 244 |
|
| 245 |
.. [1] Peyré, G., & Cuturi, M. (2017). "Computational Optimal |
| 246 |
Transport", 2018. |
| 247 |
""" |
| 248 |
ms, mt, Cs, Ct = list_to_array(ms, mt, Cs, Ct) |
| 249 |
nx = get_backend(ms, mt, Cs, Ct) |
| 250 |
|
| 251 |
Cs12 = nx.sqrtm(Cs) |
| 252 |
|
| 253 |
B = nx.trace(Cs + Ct - 2 * nx.sqrtm(dots(Cs12, Ct, Cs12))) |
| 254 |
W = nx.sqrt(nx.maximum(nx.norm(ms - mt) ** 2 + B, 0)) |
| 255 |
|
| 256 |
if log: |
| 257 |
log = {} |
| 258 |
log["Cs12"] = Cs12 |
| 259 |
return W, log |
| 260 |
else: |
| 261 |
return W |
| 262 |
|
| 263 |
|
| 264 |
def empirical_bures_wasserstein_distance( |
| 265 |
xs, xt, reg=1e-6, ws=None, wt=None, bias=True, log=False |
| 266 |
): |
| 267 |
r"""Return Bures Wasserstein distance from mean and covariance of distribution. |
| 268 |
|
| 269 |
The function estimates the Bures-Wasserstein distance between two |
| 270 |
empirical distributions source :math:`\mu_s` and target :math:`\mu_t`, |
| 271 |
discussed in remark 2.31 :ref:`[1] <references-bures-wasserstein-distance>`. |
| 272 |
|
| 273 |
The Bures Wasserstein distance between source and target distribution :math:`\mathcal{W}` |
| 274 |
|
| 275 |
.. math:: |
| 276 |
\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} |
| 277 |
|
| 278 |
where : |
| 279 |
|
| 280 |
.. math:: |
| 281 |
\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) |
| 282 |
|
| 283 |
Parameters |
| 284 |
---------- |
| 285 |
xs : array-like (ns,d) |
| 286 |
samples in the source domain |
| 287 |
xt : array-like (nt,d) |
| 288 |
samples in the target domain |
| 289 |
reg : float,optional |
| 290 |
regularization added to the diagonals of covariances (>0) |
| 291 |
ws : array-like (ns), optional |
| 292 |
weights for the source samples |
| 293 |
wt : array-like (ns), optional |
| 294 |
weights for the target samples |
| 295 |
bias: boolean, optional |
| 296 |
estimate bias :math:`\mathbf{b}` else :math:`\mathbf{b} = 0` (default:True) |
| 297 |
log : bool, optional |
| 298 |
record log if True |
| 299 |
|
| 300 |
|
| 301 |
Returns |
| 302 |
------- |
| 303 |
W : float |
| 304 |
Bures Wasserstein distance |
| 305 |
log : dict |
| 306 |
log dictionary return only if log==True in parameters |
| 307 |
|
| 308 |
|
| 309 |
.. _references-bures-wasserstein-distance: |
| 310 |
References |
| 311 |
---------- |
| 312 |
|
| 313 |
.. [1] Peyré, G., & Cuturi, M. (2017). "Computational Optimal |
| 314 |
Transport", 2018. |
| 315 |
""" |
| 316 |
xs, xt = list_to_array(xs, xt) |
| 317 |
nx = get_backend(xs, xt) |
| 318 |
|
| 319 |
d = xs.shape[1] |
| 320 |
|
| 321 |
if ws is None: |
| 322 |
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0] |
| 323 |
|
| 324 |
if wt is None: |
| 325 |
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0] |
| 326 |
|
| 327 |
if bias: |
| 328 |
mxs = nx.dot(ws.T, xs) / nx.sum(ws) |
| 329 |
mxt = nx.dot(wt.T, xt) / nx.sum(wt) |
| 330 |
|
| 331 |
xs = xs - mxs |
| 332 |
xt = xt - mxt |
| 333 |
else: |
| 334 |
mxs = nx.zeros((1, d), type_as=xs) |
| 335 |
mxt = nx.zeros((1, d), type_as=xs) |
| 336 |
|
| 337 |
Cs = nx.dot((xs * ws).T, xs) / nx.sum(ws) + reg * nx.eye(d, type_as=xs) |
| 338 |
Ct = nx.dot((xt * wt).T, xt) / nx.sum(wt) + reg * nx.eye(d, type_as=xt) |
| 339 |
|
| 340 |
if log: |
| 341 |
W, log = bures_wasserstein_distance(mxs, mxt, Cs, Ct, log=log) |
| 342 |
log["Cs"] = Cs |
| 343 |
log["Ct"] = Ct |
| 344 |
return W, log |
| 345 |
else: |
| 346 |
W = bures_wasserstein_distance(mxs, mxt, Cs, Ct) |
| 347 |
return W |
| 348 |
|
| 349 |
|
| 350 |
def bures_wasserstein_barycenter( |
| 351 |
m, C, weights=None, num_iter=1000, eps=1e-7, log=False |
| 352 |
): |
| 353 |
r"""Return OT linear operator between samples. |
| 354 |
|
| 355 |
The function estimates the optimal barycenter of the |
| 356 |
empirical distributions. This is equivalent to resolving the fixed point |
| 357 |
algorithm for multiple Gaussian distributions :math:`\left{\mathcal{N}(\mu,\Sigma)\right}_{i=1}^n` |
| 358 |
:ref:`[1] <references-OT-mapping-linear-barycenter>`. |
| 359 |
|
| 360 |
The barycenter still following a Gaussian distribution :math:`\mathcal{N}(\mu_b,\Sigma_b)` |
| 361 |
where : |
| 362 |
|
| 363 |
.. math:: |
| 364 |
\mu_b = \sum_{i=1}^n w_i \mu_i |
| 365 |
|
| 366 |
And the barycentric covariance is the solution of the following fixed-point algorithm: |
| 367 |
|
| 368 |
.. math:: |
| 369 |
\Sigma_b = \sum_{i=1}^n w_i \left(\Sigma_b^{1/2}\Sigma_i^{1/2}\Sigma_b^{1/2}\right)^{1/2} |
| 370 |
|
| 371 |
|
| 372 |
Parameters |
| 373 |
---------- |
| 374 |
m : array-like (k,d) |
| 375 |
mean of k distributions |
| 376 |
C : array-like (k,d,d) |
| 377 |
covariance of k distributions |
| 378 |
weights : array-like (k), optional |
| 379 |
weights for each distribution |
| 380 |
num_iter : int, optional |
| 381 |
number of iteration for the fixed point algorithm |
| 382 |
eps : float, optional |
| 383 |
tolerance for the fixed point algorithm |
| 384 |
log : bool, optional |
| 385 |
record log if True |
| 386 |
|
| 387 |
|
| 388 |
Returns |
| 389 |
------- |
| 390 |
mb : (d,) array-like |
| 391 |
mean of the barycenter |
| 392 |
Cb : (d, d) array-like |
| 393 |
covariance of the barycenter |
| 394 |
log : dict |
| 395 |
log dictionary return only if log==True in parameters |
| 396 |
|
| 397 |
|
| 398 |
.. _references-OT-mapping-linear-barycenter: |
| 399 |
References |
| 400 |
---------- |
| 401 |
.. [1] M. Agueh and G. Carlier, "Barycenters in the Wasserstein space", |
| 402 |
SIAM Journal on Mathematical Analysis, vol. 43, no. 2, pp. 904-924, |
| 403 |
2011. |
| 404 |
""" |
| 405 |
nx = get_backend( |
| 406 |
*C, |
| 407 |
*m, |
| 408 |
) |
| 409 |
|
| 410 |
if weights is None: |
| 411 |
weights = nx.ones(C.shape[0], type_as=C[0]) / C.shape[0] |
| 412 |
|
| 413 |
|
| 414 |
mb = nx.sum(m * weights[:, None], axis=0) |
| 415 |
|
| 416 |
|
| 417 |
Cb = nx.mean(C * weights[:, None, None], axis=0) |
| 418 |
|
| 419 |
for it in range(num_iter): |
| 420 |
|
| 421 |
Cb12 = nx.sqrtm(Cb) |
| 422 |
|
| 423 |
Cnew = Cb12 @ C @ Cb12 |
| 424 |
C_ = [] |
| 425 |
for i in range(len(C)): |
| 426 |
C_.append(nx.sqrtm(Cnew[i])) |
| 427 |
Cnew = nx.stack(C_, axis=0) |
| 428 |
Cnew *= weights[:, None, None] |
| 429 |
Cnew = nx.sum(Cnew, axis=0) |
| 430 |
|
| 431 |
|
| 432 |
diff = nx.norm(Cb - Cnew) |
| 433 |
if diff <= eps: |
| 434 |
break |
| 435 |
Cb = Cnew |
| 436 |
else: |
| 437 |
print("Dit not converge.") |
| 438 |
|
| 439 |
if log: |
| 440 |
log = {} |
| 441 |
log["num_iter"] = it |
| 442 |
log["final_diff"] = diff |
| 443 |
return mb, Cb, log |
| 444 |
else: |
| 445 |
return mb, Cb |
| 446 |
|
| 447 |
|
| 448 |
def empirical_bures_wasserstein_barycenter( |
| 449 |
X, reg=1e-6, weights=None, num_iter=1000, eps=1e-7, w=None, bias=True, log=False |
| 450 |
): |
| 451 |
r"""Return OT linear operator between samples. |
| 452 |
|
| 453 |
The function estimates the optimal barycenter of the |
| 454 |
empirical distributions. This is equivalent to resolving the fixed point |
| 455 |
algorithm for multiple Gaussian distributions :math:`\left{\mathcal{N}(\mu,\Sigma)\right}_{i=1}^n` |
| 456 |
:ref:`[1] <references-OT-mapping-linear-barycenter>`. |
| 457 |
|
| 458 |
The barycenter still following a Gaussian distribution :math:`\mathcal{N}(\mu_b,\Sigma_b)` |
| 459 |
where : |
| 460 |
|
| 461 |
.. math:: |
| 462 |
\mu_b = \sum_{i=1}^n w_i \mu_i |
| 463 |
|
| 464 |
And the barycentric covariance is the solution of the following fixed-point algorithm: |
| 465 |
|
| 466 |
.. math:: |
| 467 |
\Sigma_b = \sum_{i=1}^n w_i \left(\Sigma_b^{1/2}\Sigma_i^{1/2}\Sigma_b^{1/2}\right)^{1/2} |
| 468 |
|
| 469 |
|
| 470 |
Parameters |
| 471 |
---------- |
| 472 |
X : list of array-like (n,d) |
| 473 |
samples in each distribution |
| 474 |
reg : float,optional |
| 475 |
regularization added to the diagonals of covariances (>0) |
| 476 |
weights : array-like (n,), optional |
| 477 |
weights for each distribution |
| 478 |
num_iter : int, optional |
| 479 |
number of iteration for the fixed point algorithm |
| 480 |
eps : float, optional |
| 481 |
tolerance for the fixed point algorithm |
| 482 |
w : list of array-like (n,), optional |
| 483 |
weights for each sample in each distribution |
| 484 |
bias: boolean, optional |
| 485 |
estimate bias :math:`\mathbf{b}` else :math:`\mathbf{b} = 0` (default:True) |
| 486 |
log : bool, optional |
| 487 |
record log if True |
| 488 |
|
| 489 |
|
| 490 |
Returns |
| 491 |
------- |
| 492 |
mb : (d,) array-like |
| 493 |
mean of the barycenter |
| 494 |
Cb : (d, d) array-like |
| 495 |
covariance of the barycenter |
| 496 |
log : dict |
| 497 |
log dictionary return only if log==True in parameters |
| 498 |
|
| 499 |
|
| 500 |
.. _references-OT-mapping-linear-barycenter: |
| 501 |
References |
| 502 |
---------- |
| 503 |
.. [1] M. Agueh and G. Carlier, "Barycenters in the Wasserstein space", |
| 504 |
SIAM Journal on Mathematical Analysis, vol. 43, no. 2, pp. 904-924, |
| 505 |
2011. |
| 506 |
""" |
| 507 |
X = list_to_array(*X) |
| 508 |
nx = get_backend(*X) |
| 509 |
|
| 510 |
k = len(X) |
| 511 |
d = [X[i].shape[1] for i in range(k)] |
| 512 |
|
| 513 |
if w is None: |
| 514 |
w = [ |
| 515 |
nx.ones((X[i].shape[0], 1), type_as=X[i]) / X[i].shape[0] for i in range(k) |
| 516 |
] |
| 517 |
|
| 518 |
if bias: |
| 519 |
m = [nx.dot(w[i].T, X[i]) / nx.sum(w[i]) for i in range(k)] |
| 520 |
X = [X[i] - m[i] for i in range(k)] |
| 521 |
else: |
| 522 |
m = [nx.zeros((1, d[i]), type_as=X[i]) for i in range(k)] |
| 523 |
|
| 524 |
C = [ |
| 525 |
nx.dot((X[i] * w[i]).T, X[i]) / nx.sum(w[i]) + reg * nx.eye(d[i], type_as=X[i]) |
| 526 |
for i in range(k) |
| 527 |
] |
| 528 |
m = nx.stack(m, axis=0) |
| 529 |
C = nx.stack(C, axis=0) |
| 530 |
if log: |
| 531 |
mb, Cb, log = bures_wasserstein_barycenter( |
| 532 |
m, C, weights=weights, num_iter=num_iter, eps=eps, log=log |
| 533 |
) |
| 534 |
return mb, Cb, log |
| 535 |
else: |
| 536 |
mb, Cb = bures_wasserstein_barycenter( |
| 537 |
m, C, weights=weights, num_iter=num_iter, eps=eps, log=log |
| 538 |
) |
| 539 |
return mb, Cb |
| 540 |
|
| 541 |
|
| 542 |
def gaussian_gromov_wasserstein_distance(Cov_s, Cov_t, log=False): |
| 543 |
r"""Return the Gaussian Gromov-Wasserstein value from [57]. |
| 544 |
|
| 545 |
This function return the closed form value of the Gaussian Gromov-Wasserstein |
| 546 |
distance between two Gaussian distributions |
| 547 |
:math:`\mathcal{N}(\mu_s,\Sigma_s)` and :math:`\mathcal{N}(\mu_t,\Sigma_t)` |
| 548 |
when the OT plan is assumed to be also Gaussian. See [57] Theorem 4.1 for |
| 549 |
more details. |
| 550 |
|
| 551 |
Parameters |
| 552 |
---------- |
| 553 |
Cov_s : array-like (ds,ds) |
| 554 |
covariance of the source distribution |
| 555 |
Cov_t : array-like (dt,dt) |
| 556 |
covariance of the target distribution |
| 557 |
|
| 558 |
|
| 559 |
Returns |
| 560 |
------- |
| 561 |
G : float |
| 562 |
Gaussian Gromov-Wasserstein distance |
| 563 |
|
| 564 |
|
| 565 |
.. _references-gaussien_gromov_wasserstein_distance: |
| 566 |
References |
| 567 |
---------- |
| 568 |
.. [57] Delon, J., Desolneux, A., & Salmona, A. (2022). Gromov–Wasserstein |
| 569 |
distances between Gaussian distributions. Journal of Applied Probability, |
| 570 |
59(4), 1178-1198. |
| 571 |
""" |
| 572 |
|
| 573 |
nx = get_backend(Cov_s, Cov_t) |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
if Cov_s.shape[0] < Cov_t.shape[0]: |
| 578 |
Cov_s, Cov_t = Cov_t, Cov_s |
| 579 |
|
| 580 |
n = Cov_t.shape[0] |
| 581 |
|
| 582 |
|
| 583 |
d_s = nx.flip(nx.sort(nx.eigh(Cov_s)[0])) |
| 584 |
d_t = nx.flip(nx.sort(nx.eigh(Cov_t)[0])) |
| 585 |
|
| 586 |
|
| 587 |
res = ( |
| 588 |
4 * (nx.sum(d_s) - nx.sum(d_t)) ** 2 |
| 589 |
+ 8 * nx.sum((d_s[:n] - d_t) ** 2) |
| 590 |
+ 8 * nx.sum((d_s[n:]) ** 2) |
| 591 |
) |
| 592 |
if log: |
| 593 |
log = {} |
| 594 |
log["d_s"] = d_s |
| 595 |
log["d_t"] = d_t |
| 596 |
return nx.sqrt(res), log |
| 597 |
else: |
| 598 |
return nx.sqrt(res) |
| 599 |
|
| 600 |
|
| 601 |
def empirical_gaussian_gromov_wasserstein_distance(xs, xt, ws=None, wt=None, log=False): |
| 602 |
r"""Return Gaussian Gromov-Wasserstein distance between samples. |
| 603 |
|
| 604 |
The function estimates the Gaussian Gromov-Wasserstein distance between two |
| 605 |
Gaussien distributions source :math:`\mu_s` and target :math:`\mu_t`, whose |
| 606 |
parameters are estimated from the provided samples :math:`\mathcal{X}_s` and |
| 607 |
:math:`\mathcal{X}_t`. See [57] Theorem 4.1 for more details. |
| 608 |
|
| 609 |
Parameters |
| 610 |
---------- |
| 611 |
xs : array-like (ns,d) |
| 612 |
samples in the source domain |
| 613 |
xt : array-like (nt,d) |
| 614 |
samples in the target domain |
| 615 |
ws : array-like (ns,1), optional |
| 616 |
weights for the source samples |
| 617 |
wt : array-like (ns,1), optional |
| 618 |
weights for the target samples |
| 619 |
log : bool, optional |
| 620 |
record log if True |
| 621 |
|
| 622 |
|
| 623 |
Returns |
| 624 |
------- |
| 625 |
G : float |
| 626 |
Gaussian Gromov-Wasserstein distance |
| 627 |
|
| 628 |
|
| 629 |
.. _references-gaussien_gromov_wasserstein: |
| 630 |
References |
| 631 |
---------- |
| 632 |
.. [57] Delon, J., Desolneux, A., & Salmona, A. (2022). Gromov–Wasserstein |
| 633 |
distances between Gaussian distributions. Journal of Applied Probability, |
| 634 |
59(4), 1178-1198. |
| 635 |
""" |
| 636 |
xs, xt = list_to_array(xs, xt) |
| 637 |
nx = get_backend(xs, xt) |
| 638 |
|
| 639 |
if ws is None: |
| 640 |
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0] |
| 641 |
|
| 642 |
if wt is None: |
| 643 |
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0] |
| 644 |
|
| 645 |
mxs = nx.dot(ws.T, xs) / nx.sum(ws) |
| 646 |
mxt = nx.dot(wt.T, xt) / nx.sum(wt) |
| 647 |
|
| 648 |
xs = xs - mxs |
| 649 |
xt = xt - mxt |
| 650 |
|
| 651 |
Cs = nx.dot((xs * ws).T, xs) / nx.sum(ws) |
| 652 |
Ct = nx.dot((xt * wt).T, xt) / nx.sum(wt) |
| 653 |
|
| 654 |
if log: |
| 655 |
G, log = gaussian_gromov_wasserstein_distance(Cs, Ct, log=log) |
| 656 |
log["Cov_s"] = Cs |
| 657 |
log["Cov_t"] = Ct |
| 658 |
return G, log |
| 659 |
else: |
| 660 |
G = gaussian_gromov_wasserstein_distance(Cs, Ct) |
| 661 |
return G |
| 662 |
|
| 663 |
|
| 664 |
def gaussian_gromov_wasserstein_mapping( |
| 665 |
mu_s, mu_t, Cov_s, Cov_t, sign_eigs=None, log=False |
| 666 |
): |
| 667 |
r"""Return the Gaussian Gromov-Wasserstein mapping from [57]. |
| 668 |
|
| 669 |
This function return the closed form value of the Gaussian |
| 670 |
Gromov-Wasserstein mapping between two Gaussian distributions |
| 671 |
:math:`\mathcal{N}(\mu_s,\Sigma_s)` and :math:`\mathcal{N}(\mu_t,\Sigma_t)` |
| 672 |
when the OT plan is assumed to be also Gaussian. See [57] Theorem 4.1 for |
| 673 |
more details. |
| 674 |
|
| 675 |
Parameters |
| 676 |
---------- |
| 677 |
mu_s : array-like (ds,) |
| 678 |
mean of the source distribution |
| 679 |
mu_t : array-like (dt,) |
| 680 |
mean of the target distribution |
| 681 |
Cov_s : array-like (ds,ds) |
| 682 |
covariance of the source distribution |
| 683 |
Cov_t : array-like (dt,dt) |
| 684 |
covariance of the target distribution |
| 685 |
log : bool, optional |
| 686 |
record log if True |
| 687 |
|
| 688 |
|
| 689 |
Returns |
| 690 |
------- |
| 691 |
A : (dt, ds) array-like |
| 692 |
Linear operator |
| 693 |
b : (1, dt) array-like |
| 694 |
bias |
| 695 |
|
| 696 |
|
| 697 |
.. _references-gaussien_gromov_wasserstein_mapping: |
| 698 |
References |
| 699 |
---------- |
| 700 |
.. [57] Delon, J., Desolneux, A., & Salmona, A. (2022). Gromov–Wasserstein |
| 701 |
distances between Gaussian distributions. Journal of Applied Probability, |
| 702 |
59(4), 1178-1198. |
| 703 |
""" |
| 704 |
|
| 705 |
nx = get_backend(mu_s, mu_t, Cov_s, Cov_t) |
| 706 |
|
| 707 |
n = Cov_t.shape[0] |
| 708 |
m = Cov_s.shape[0] |
| 709 |
|
| 710 |
|
| 711 |
d_s, U_s = nx.eigh(Cov_s) |
| 712 |
id_s = nx.flip(nx.argsort(d_s)) |
| 713 |
d_s, U_s = d_s[id_s], U_s[:, id_s] |
| 714 |
|
| 715 |
d_t, U_t = nx.eigh(Cov_t) |
| 716 |
id_t = nx.flip(nx.argsort(d_t)) |
| 717 |
d_t, U_t = d_t[id_t], U_t[:, id_t] |
| 718 |
|
| 719 |
if sign_eigs is None: |
| 720 |
sign_eigs = nx.ones(min(m, n), type_as=mu_s) |
| 721 |
|
| 722 |
if m >= n: |
| 723 |
A = nx.concatenate( |
| 724 |
( |
| 725 |
nx.diag(sign_eigs * nx.sqrt(d_t) / nx.sqrt(d_s[:n])), |
| 726 |
nx.zeros((n, m - n), type_as=mu_s), |
| 727 |
), |
| 728 |
axis=1, |
| 729 |
).T |
| 730 |
else: |
| 731 |
A = nx.concatenate( |
| 732 |
( |
| 733 |
nx.diag(sign_eigs * nx.sqrt(d_t[:m]) / nx.sqrt(d_s)), |
| 734 |
nx.zeros((n - m, m), type_as=mu_s), |
| 735 |
), |
| 736 |
axis=0, |
| 737 |
).T |
| 738 |
|
| 739 |
A = nx.dot(nx.dot(U_s, A), U_t.T) |
| 740 |
|
| 741 |
|
| 742 |
b = mu_t - nx.dot(mu_s, A) |
| 743 |
|
| 744 |
if log: |
| 745 |
log = {} |
| 746 |
log["d_s"] = d_s |
| 747 |
log["d_t"] = d_t |
| 748 |
log["U_s"] = U_s |
| 749 |
log["U_t"] = U_t |
| 750 |
return A, b, log |
| 751 |
else: |
| 752 |
return A, b |
| 753 |
|
| 754 |
|
| 755 |
def empirical_gaussian_gromov_wasserstein_mapping( |
| 756 |
xs, xt, ws=None, wt=None, sign_eigs=None, log=False |
| 757 |
): |
| 758 |
r"""Return Gaussian Gromov-Wasserstein mapping between samples. |
| 759 |
|
| 760 |
The function estimates the Gaussian Gromov-Wasserstein mapping between two |
| 761 |
Gaussian distributions source :math:`\mu_s` and target :math:`\mu_t`, whose |
| 762 |
parameters are estimated from the provided samples :math:`\mathcal{X}_s` and |
| 763 |
:math:`\mathcal{X}_t`. See [57] Theorem 4.1 for more details. |
| 764 |
|
| 765 |
|
| 766 |
Parameters |
| 767 |
---------- |
| 768 |
xs : array-like (ns,ds) |
| 769 |
samples in the source domain |
| 770 |
xt : array-like (nt,dt) |
| 771 |
samples in the target domain |
| 772 |
ws : array-like (ns,1), optional |
| 773 |
weights for the source samples |
| 774 |
wt : array-like (ns,1), optional |
| 775 |
weights for the target samples |
| 776 |
sign_eigs : array-like (min(ds,dt),) or string, optional |
| 777 |
sign of the eigenvalues of the mapping matrix, by default all signs will |
| 778 |
be positive. If 'skewness' is provided, the sign of the eigenvalues is |
| 779 |
selected as the product of the sign of the skewness of the projected data. |
| 780 |
log : bool, optional |
| 781 |
record log if True |
| 782 |
|
| 783 |
|
| 784 |
Returns |
| 785 |
------- |
| 786 |
A : (dt, ds) array-like |
| 787 |
Linear operator |
| 788 |
b : (1, dt) array-like |
| 789 |
bias |
| 790 |
|
| 791 |
.. _references-empirical_gaussian_gromov_wasserstein_mapping: |
| 792 |
References |
| 793 |
---------- |
| 794 |
.. [57] Delon, J., Desolneux, A., & Salmona, A. (2022). Gromov–Wasserstein |
| 795 |
distances between Gaussian distributions. Journal of Applied Probability, |
| 796 |
59(4), 1178-1198. |
| 797 |
""" |
| 798 |
|
| 799 |
xs, xt = list_to_array(xs, xt) |
| 800 |
|
| 801 |
nx = get_backend(xs, xt) |
| 802 |
|
| 803 |
m = xs.shape[1] |
| 804 |
n = xt.shape[1] |
| 805 |
|
| 806 |
if ws is None: |
| 807 |
ws = nx.ones((xs.shape[0], 1), type_as=xs) / xs.shape[0] |
| 808 |
|
| 809 |
if wt is None: |
| 810 |
wt = nx.ones((xt.shape[0], 1), type_as=xt) / xt.shape[0] |
| 811 |
|
| 812 |
|
| 813 |
mu_s = nx.dot(ws.T, xs) / nx.sum(ws) |
| 814 |
mu_t = nx.dot(wt.T, xt) / nx.sum(wt) |
| 815 |
|
| 816 |
xs = xs - mu_s |
| 817 |
xt = xt - mu_t |
| 818 |
|
| 819 |
Cov_s = nx.dot((xs * ws).T, xs) / nx.sum(ws) |
| 820 |
Cov_t = nx.dot((xt * wt).T, xt) / nx.sum(wt) |
| 821 |
|
| 822 |
|
| 823 |
d_s, U_s = nx.eigh(Cov_s) |
| 824 |
id_s = nx.flip(nx.argsort(d_s)) |
| 825 |
d_s, U_s = d_s[id_s], U_s[:, id_s] |
| 826 |
|
| 827 |
d_t, U_t = nx.eigh(Cov_t) |
| 828 |
id_t = nx.flip(nx.argsort(d_t)) |
| 829 |
d_t, U_t = d_t[id_t], U_t[:, id_t] |
| 830 |
|
| 831 |
|
| 832 |
if sign_eigs is None: |
| 833 |
sign_eigs = nx.ones(min(m, n), type_as=mu_s) |
| 834 |
elif sign_eigs == "skewness": |
| 835 |
size = min(m, n) |
| 836 |
skew_s = nx.sum((nx.dot(xs, U_s[:, :size])) ** 3 * ws, axis=0) |
| 837 |
skew_t = nx.sum((nx.dot(xt, U_t[:, :size])) ** 3 * wt, axis=0) |
| 838 |
sign_eigs = nx.sign(skew_t * skew_s) |
| 839 |
|
| 840 |
if m >= n: |
| 841 |
A = nx.concatenate( |
| 842 |
( |
| 843 |
nx.diag(sign_eigs * nx.sqrt(d_t) / nx.sqrt(d_s[:n])), |
| 844 |
nx.zeros((n, m - n), type_as=mu_s), |
| 845 |
), |
| 846 |
axis=1, |
| 847 |
).T |
| 848 |
else: |
| 849 |
A = nx.concatenate( |
| 850 |
( |
| 851 |
nx.diag(sign_eigs * nx.sqrt(d_t[:m]) / nx.sqrt(d_s)), |
| 852 |
nx.zeros((n - m, m), type_as=mu_s), |
| 853 |
), |
| 854 |
axis=0, |
| 855 |
).T |
| 856 |
|
| 857 |
A = nx.dot(nx.dot(U_s, A), U_t.T) |
| 858 |
|
| 859 |
|
| 860 |
b = mu_t - nx.dot(mu_s, A) |
| 861 |
|
| 862 |
if log: |
| 863 |
log = {} |
| 864 |
log["d_s"] = d_s |
| 865 |
log["d_t"] = d_t |
| 866 |
log["U_s"] = U_s |
| 867 |
log["U_t"] = U_t |
| 868 |
log["Cov_s"] = Cov_s |
| 869 |
log["Cov_t"] = Cov_t |
| 870 |
return A, b, log |
| 871 |
else: |
| 872 |
return A, b |