| 1 |
|
| 2 |
""" |
| 3 |
=========================================================== |
| 4 |
1D Wasserstein barycenter demo for Unbalanced distributions |
| 5 |
=========================================================== |
| 6 |
|
| 7 |
This example illustrates the computation of regularized Wasserstein Barycenter |
| 8 |
as proposed in [10] for Unbalanced inputs. |
| 9 |
|
| 10 |
|
| 11 |
[10] Chizat, L., Peyré, G., Schmitzer, B., & Vialard, F. X. (2016). Scaling algorithms for unbalanced transport problems. arXiv preprint arXiv:1607.05816. |
| 12 |
|
| 13 |
""" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
import numpy as np |
| 22 |
import matplotlib.pylab as pl |
| 23 |
import ot |
| 24 |
|
| 25 |
|
| 26 |
from mpl_toolkits.mplot3d import Axes3D |
| 27 |
from matplotlib.collections import PolyCollection |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
n = 100 |
| 36 |
|
| 37 |
|
| 38 |
x = np.arange(n, dtype=np.float64) |
| 39 |
|
| 40 |
|
| 41 |
a1 = ot.datasets.make_1D_gauss(n, m=20, s=5) |
| 42 |
a2 = ot.datasets.make_1D_gauss(n, m=60, s=8) |
| 43 |
|
| 44 |
|
| 45 |
a2 *= 3.0 |
| 46 |
|
| 47 |
|
| 48 |
A = np.vstack((a1, a2)).T |
| 49 |
n_distributions = A.shape[1] |
| 50 |
|
| 51 |
|
| 52 |
M = ot.utils.dist0(n) |
| 53 |
M /= M.max() |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
pl.figure(1, figsize=(6.4, 3)) |
| 62 |
for i in range(n_distributions): |
| 63 |
pl.plot(x, A[:, i]) |
| 64 |
pl.title("Distributions") |
| 65 |
pl.tight_layout() |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
weight = 0.5 |
| 74 |
weights = np.array([1 - weight, weight]) |
| 75 |
|
| 76 |
|
| 77 |
bary_l2 = A.dot(weights) |
| 78 |
|
| 79 |
|
| 80 |
reg = 1e-3 |
| 81 |
alpha = 1.0 |
| 82 |
|
| 83 |
bary_wass = ot.unbalanced.barycenter_unbalanced(A, M, reg, alpha, weights=weights) |
| 84 |
|
| 85 |
pl.figure(2) |
| 86 |
pl.clf() |
| 87 |
pl.subplot(2, 1, 1) |
| 88 |
for i in range(n_distributions): |
| 89 |
pl.plot(x, A[:, i]) |
| 90 |
pl.title("Distributions") |
| 91 |
|
| 92 |
pl.subplot(2, 1, 2) |
| 93 |
pl.plot(x, bary_l2, "r", label="l2") |
| 94 |
pl.plot(x, bary_wass, "g", label="Wasserstein") |
| 95 |
pl.legend() |
| 96 |
pl.title("Barycenters") |
| 97 |
pl.tight_layout() |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
n_weight = 11 |
| 106 |
weight_list = np.linspace(0, 1, n_weight) |
| 107 |
|
| 108 |
|
| 109 |
B_l2 = np.zeros((n, n_weight)) |
| 110 |
|
| 111 |
B_wass = np.copy(B_l2) |
| 112 |
|
| 113 |
for i in range(0, n_weight): |
| 114 |
weight = weight_list[i] |
| 115 |
weights = np.array([1 - weight, weight]) |
| 116 |
B_l2[:, i] = A.dot(weights) |
| 117 |
B_wass[:, i] = ot.unbalanced.barycenter_unbalanced( |
| 118 |
A, M, reg, alpha, weights=weights |
| 119 |
) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
pl.figure(3) |
| 125 |
|
| 126 |
cmap = pl.get_cmap("viridis") |
| 127 |
verts = [] |
| 128 |
zs = weight_list |
| 129 |
for i, z in enumerate(zs): |
| 130 |
ys = B_l2[:, i] |
| 131 |
verts.append(list(zip(x, ys))) |
| 132 |
|
| 133 |
ax = pl.gcf().add_subplot(projection="3d") |
| 134 |
|
| 135 |
poly = PolyCollection(verts, facecolors=[cmap(a) for a in weight_list]) |
| 136 |
poly.set_alpha(0.7) |
| 137 |
ax.add_collection3d(poly, zs=zs, zdir="y") |
| 138 |
ax.set_xlabel("x") |
| 139 |
ax.set_xlim3d(0, n) |
| 140 |
ax.set_ylabel(r"$\alpha$") |
| 141 |
ax.set_ylim3d(0, 1) |
| 142 |
ax.set_zlabel("") |
| 143 |
ax.set_zlim3d(0, B_l2.max() * 1.01) |
| 144 |
pl.title("Barycenter interpolation with l2") |
| 145 |
pl.tight_layout() |
| 146 |
|
| 147 |
pl.figure(4) |
| 148 |
cmap = pl.get_cmap("viridis") |
| 149 |
verts = [] |
| 150 |
zs = weight_list |
| 151 |
for i, z in enumerate(zs): |
| 152 |
ys = B_wass[:, i] |
| 153 |
verts.append(list(zip(x, ys))) |
| 154 |
|
| 155 |
ax = pl.gcf().add_subplot(projection="3d") |
| 156 |
|
| 157 |
poly = PolyCollection(verts, facecolors=[cmap(a) for a in weight_list]) |
| 158 |
poly.set_alpha(0.7) |
| 159 |
ax.add_collection3d(poly, zs=zs, zdir="y") |
| 160 |
ax.set_xlabel("x") |
| 161 |
ax.set_xlim3d(0, n) |
| 162 |
ax.set_ylabel(r"$\alpha$") |
| 163 |
ax.set_ylim3d(0, 1) |
| 164 |
ax.set_zlabel("") |
| 165 |
ax.set_zlim3d(0, B_l2.max() * 1.01) |
| 166 |
pl.title("Barycenter interpolation with Wasserstein") |
| 167 |
pl.tight_layout() |
| 168 |
|
| 169 |
pl.show() |