| 1 |
|
| 2 |
""" |
| 3 |
================================================================================= |
| 4 |
1D Wasserstein barycenter: exact LP vs entropic regularization |
| 5 |
================================================================================= |
| 6 |
|
| 7 |
This example illustrates the computation of regularized Wasserstein Barycenter |
| 8 |
as proposed in [3] and exact LP barycenters using standard LP solver. |
| 9 |
|
| 10 |
It reproduces approximately Figure 3.1 and 3.2 from the following paper: |
| 11 |
Cuturi, M., & Peyré, G. (2016). A smoothed dual approach for variational |
| 12 |
Wasserstein problems. SIAM Journal on Imaging Sciences, 9(1), 320-343. |
| 13 |
|
| 14 |
[3] Benamou, J. D., Carlier, G., Cuturi, M., Nenna, L., & Peyré, G. (2015). |
| 15 |
Iterative Bregman projections for regularized transportation problems |
| 16 |
SIAM Journal on Scientific Computing, 37(2), A1111-A1138. |
| 17 |
|
| 18 |
""" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
import numpy as np |
| 27 |
import matplotlib.pylab as pl |
| 28 |
import ot |
| 29 |
|
| 30 |
|
| 31 |
from mpl_toolkits.mplot3d import Axes3D |
| 32 |
from matplotlib.collections import PolyCollection |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
problems = [] |
| 43 |
|
| 44 |
n = 100 |
| 45 |
|
| 46 |
|
| 47 |
x = np.arange(n, dtype=np.float64) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
a1 = ot.datasets.make_1D_gauss(n, m=20, s=5) |
| 52 |
a2 = ot.datasets.make_1D_gauss(n, m=60, s=8) |
| 53 |
|
| 54 |
|
| 55 |
A = np.vstack((a1, a2)).T |
| 56 |
n_distributions = A.shape[1] |
| 57 |
|
| 58 |
|
| 59 |
M = ot.utils.dist0(n) |
| 60 |
M /= M.max() |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
pl.figure(1, figsize=(6.4, 3)) |
| 66 |
for i in range(n_distributions): |
| 67 |
pl.plot(x, A[:, i]) |
| 68 |
pl.title("Distributions") |
| 69 |
pl.tight_layout() |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
alpha = 0.5 |
| 74 |
weights = np.array([1 - alpha, alpha]) |
| 75 |
|
| 76 |
|
| 77 |
bary_l2 = A.dot(weights) |
| 78 |
|
| 79 |
|
| 80 |
reg = 1e-3 |
| 81 |
ot.tic() |
| 82 |
bary_wass = ot.bregman.barycenter(A, M, reg, weights) |
| 83 |
ot.toc() |
| 84 |
|
| 85 |
|
| 86 |
ot.tic() |
| 87 |
bary_wass2 = ot.lp.barycenter(A, M, weights) |
| 88 |
ot.toc() |
| 89 |
|
| 90 |
pl.figure(2) |
| 91 |
pl.clf() |
| 92 |
pl.subplot(2, 1, 1) |
| 93 |
for i in range(n_distributions): |
| 94 |
pl.plot(x, A[:, i]) |
| 95 |
pl.title("Distributions") |
| 96 |
|
| 97 |
pl.subplot(2, 1, 2) |
| 98 |
pl.plot(x, bary_l2, "r", label="l2") |
| 99 |
pl.plot(x, bary_wass, "g", label="Reg Wasserstein") |
| 100 |
pl.plot(x, bary_wass2, "b", label="LP Wasserstein") |
| 101 |
pl.legend() |
| 102 |
pl.title("Barycenters") |
| 103 |
pl.tight_layout() |
| 104 |
|
| 105 |
problems.append([A, [bary_l2, bary_wass, bary_wass2]]) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
a1 = 1.0 * (x > 10) * (x < 50) |
| 114 |
a2 = 1.0 * (x > 60) * (x < 80) |
| 115 |
|
| 116 |
a1 /= a1.sum() |
| 117 |
a2 /= a2.sum() |
| 118 |
|
| 119 |
|
| 120 |
A = np.vstack((a1, a2)).T |
| 121 |
n_distributions = A.shape[1] |
| 122 |
|
| 123 |
|
| 124 |
M = ot.utils.dist0(n) |
| 125 |
M /= M.max() |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
pl.figure(1, figsize=(6.4, 3)) |
| 131 |
for i in range(n_distributions): |
| 132 |
pl.plot(x, A[:, i]) |
| 133 |
pl.title("Distributions") |
| 134 |
pl.tight_layout() |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
alpha = 0.5 |
| 140 |
weights = np.array([1 - alpha, alpha]) |
| 141 |
|
| 142 |
|
| 143 |
bary_l2 = A.dot(weights) |
| 144 |
|
| 145 |
|
| 146 |
reg = 1e-3 |
| 147 |
ot.tic() |
| 148 |
bary_wass = ot.bregman.barycenter(A, M, reg, weights) |
| 149 |
ot.toc() |
| 150 |
|
| 151 |
|
| 152 |
ot.tic() |
| 153 |
bary_wass2 = ot.lp.barycenter(A, M, weights) |
| 154 |
ot.toc() |
| 155 |
|
| 156 |
|
| 157 |
problems.append([A, [bary_l2, bary_wass, bary_wass2]]) |
| 158 |
|
| 159 |
pl.figure(2) |
| 160 |
pl.clf() |
| 161 |
pl.subplot(2, 1, 1) |
| 162 |
for i in range(n_distributions): |
| 163 |
pl.plot(x, A[:, i]) |
| 164 |
pl.title("Distributions") |
| 165 |
|
| 166 |
pl.subplot(2, 1, 2) |
| 167 |
pl.plot(x, bary_l2, "r", label="l2") |
| 168 |
pl.plot(x, bary_wass, "g", label="Reg Wasserstein") |
| 169 |
pl.plot(x, bary_wass2, "b", label="LP Wasserstein") |
| 170 |
pl.legend() |
| 171 |
pl.title("Barycenters") |
| 172 |
pl.tight_layout() |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
a1 = np.zeros(n) |
| 182 |
a2 = np.zeros(n) |
| 183 |
|
| 184 |
a1[10] = 0.25 |
| 185 |
a1[20] = 0.5 |
| 186 |
a1[30] = 0.25 |
| 187 |
a2[80] = 1 |
| 188 |
|
| 189 |
|
| 190 |
a1 /= a1.sum() |
| 191 |
a2 /= a2.sum() |
| 192 |
|
| 193 |
|
| 194 |
A = np.vstack((a1, a2)).T |
| 195 |
n_distributions = A.shape[1] |
| 196 |
|
| 197 |
|
| 198 |
M = ot.utils.dist0(n) |
| 199 |
M /= M.max() |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
pl.figure(1, figsize=(6.4, 3)) |
| 205 |
for i in range(n_distributions): |
| 206 |
pl.plot(x, A[:, i]) |
| 207 |
pl.title("Distributions") |
| 208 |
pl.tight_layout() |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
alpha = 0.5 |
| 214 |
weights = np.array([1 - alpha, alpha]) |
| 215 |
|
| 216 |
|
| 217 |
bary_l2 = A.dot(weights) |
| 218 |
|
| 219 |
|
| 220 |
reg = 1e-3 |
| 221 |
ot.tic() |
| 222 |
bary_wass = ot.bregman.barycenter(A, M, reg, weights) |
| 223 |
ot.toc() |
| 224 |
|
| 225 |
|
| 226 |
ot.tic() |
| 227 |
bary_wass2 = ot.lp.barycenter(A, M, weights) |
| 228 |
ot.toc() |
| 229 |
|
| 230 |
|
| 231 |
problems.append([A, [bary_l2, bary_wass, bary_wass2]]) |
| 232 |
|
| 233 |
pl.figure(2) |
| 234 |
pl.clf() |
| 235 |
pl.subplot(2, 1, 1) |
| 236 |
for i in range(n_distributions): |
| 237 |
pl.plot(x, A[:, i]) |
| 238 |
pl.title("Distributions") |
| 239 |
|
| 240 |
pl.subplot(2, 1, 2) |
| 241 |
pl.plot(x, bary_l2, "r", label="l2") |
| 242 |
pl.plot(x, bary_wass, "g", label="Reg Wasserstein") |
| 243 |
pl.plot(x, bary_wass2, "b", label="LP Wasserstein") |
| 244 |
pl.legend() |
| 245 |
pl.title("Barycenters") |
| 246 |
pl.tight_layout() |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
nbm = len(problems) |
| 257 |
nbm2 = nbm // 2 |
| 258 |
|
| 259 |
|
| 260 |
pl.figure(2, (20, 6)) |
| 261 |
pl.clf() |
| 262 |
|
| 263 |
for i in range(nbm): |
| 264 |
A = problems[i][0] |
| 265 |
bary_l2 = problems[i][1][0] |
| 266 |
bary_wass = problems[i][1][1] |
| 267 |
bary_wass2 = problems[i][1][2] |
| 268 |
|
| 269 |
pl.subplot(2, nbm, 1 + i) |
| 270 |
for j in range(n_distributions): |
| 271 |
pl.plot(x, A[:, j]) |
| 272 |
if i == nbm2: |
| 273 |
pl.title("Distributions") |
| 274 |
pl.xticks(()) |
| 275 |
pl.yticks(()) |
| 276 |
|
| 277 |
pl.subplot(2, nbm, 1 + i + nbm) |
| 278 |
|
| 279 |
pl.plot(x, bary_l2, "r", label="L2 (Euclidean)") |
| 280 |
pl.plot(x, bary_wass, "g", label="Reg Wasserstein") |
| 281 |
pl.plot(x, bary_wass2, "b", label="LP Wasserstein") |
| 282 |
if i == nbm - 1: |
| 283 |
pl.legend() |
| 284 |
if i == nbm2: |
| 285 |
pl.title("Barycenters") |
| 286 |
|
| 287 |
pl.xticks(()) |
| 288 |
pl.yticks(()) |