| 1 |
|
| 2 |
""" |
| 3 |
======================================================== |
| 4 |
2D free support Wasserstein barycenters of distributions |
| 5 |
======================================================== |
| 6 |
|
| 7 |
Illustration of 2D Wasserstein and Sinkhorn barycenters if distributions are weighted |
| 8 |
sum of Diracs. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
|
| 13 |
> |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import numpy as np |
| 21 |
import matplotlib.pylab as pl |
| 22 |
import ot |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
N = 2 |
| 30 |
d = 2 |
| 31 |
|
| 32 |
I1 = pl.imread("../../data/redcross.png").astype(np.float64)[::4, ::4, 2] |
| 33 |
I2 = pl.imread("../../data/duck.png").astype(np.float64)[::4, ::4, 2] |
| 34 |
|
| 35 |
sz = I2.shape[0] |
| 36 |
XX, YY = np.meshgrid(np.arange(sz), np.arange(sz)) |
| 37 |
|
| 38 |
x1 = np.stack((XX[I1 == 0], YY[I1 == 0]), 1) * 1.0 |
| 39 |
x2 = np.stack((XX[I2 == 0] + 80, -YY[I2 == 0] + 32), 1) * 1.0 |
| 40 |
x3 = np.stack((XX[I2 == 0], -YY[I2 == 0] + 32), 1) * 1.0 |
| 41 |
|
| 42 |
measures_locations = [x1, x2] |
| 43 |
measures_weights = [ot.unif(x1.shape[0]), ot.unif(x2.shape[0])] |
| 44 |
|
| 45 |
pl.figure(1, (12, 4)) |
| 46 |
pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5) |
| 47 |
pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5) |
| 48 |
pl.title("Distributions") |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
k = 200 |
| 56 |
X_init = np.random.normal(0.0, 1.0, (k, d)) |
| 57 |
b = ( |
| 58 |
np.ones((k,)) / k |
| 59 |
) |
| 60 |
|
| 61 |
X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b) |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
pl.figure(2, (8, 3)) |
| 68 |
pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5) |
| 69 |
pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5) |
| 70 |
pl.scatter(X[:, 0], X[:, 1], s=b * 1000, marker="s", label="2-Wasserstein barycenter") |
| 71 |
pl.title("Data measures and their barycenter") |
| 72 |
pl.legend(loc="lower right") |
| 73 |
pl.show() |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
k = 200 |
| 79 |
X_init = np.random.normal(0.0, 1.0, (k, d)) |
| 80 |
b = ( |
| 81 |
np.ones((k,)) / k |
| 82 |
) |
| 83 |
|
| 84 |
X = ot.bregman.free_support_sinkhorn_barycenter( |
| 85 |
measures_locations, measures_weights, X_init, 20, b, numItermax=15 |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pl.figure(2, (8, 3)) |
| 93 |
pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5) |
| 94 |
pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5) |
| 95 |
pl.scatter(X[:, 0], X[:, 1], s=b * 1000, marker="s", label="2-Wasserstein barycenter") |
| 96 |
pl.title("Data measures and their barycenter") |
| 97 |
pl.legend(loc="lower right") |
| 98 |
pl.show() |