|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +================================================================================= |
| 4 | +1D Wasserstein barycenter comparison between exact LP and 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 | +# Author: Remi Flamary <remi.flamary@unice.fr> |
| 21 | +# |
| 22 | +# License: MIT License |
| 23 | + |
| 24 | +import numpy as np |
| 25 | +import matplotlib.pylab as pl |
| 26 | +import ot |
| 27 | +# necessary for 3d plot even if not used |
| 28 | +from mpl_toolkits.mplot3d import Axes3D # noqa |
| 29 | +from matplotlib.collections import PolyCollection # noqa |
| 30 | + |
| 31 | +#import ot.lp.cvx as cvx |
| 32 | + |
| 33 | +############################################################################## |
| 34 | +# Gaussian Data |
| 35 | +# ------------- |
| 36 | + |
| 37 | +#%% parameters |
| 38 | + |
| 39 | +problems = [] |
| 40 | + |
| 41 | +n = 100 # nb bins |
| 42 | + |
| 43 | +# bin positions |
| 44 | +x = np.arange(n, dtype=np.float64) |
| 45 | + |
| 46 | +# Gaussian distributions |
| 47 | +# Gaussian distributions |
| 48 | +a1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std |
| 49 | +a2 = ot.datasets.make_1D_gauss(n, m=60, s=8) |
| 50 | + |
| 51 | +# creating matrix A containing all distributions |
| 52 | +A = np.vstack((a1, a2)).T |
| 53 | +n_distributions = A.shape[1] |
| 54 | + |
| 55 | +# loss matrix + normalization |
| 56 | +M = ot.utils.dist0(n) |
| 57 | +M /= M.max() |
| 58 | + |
| 59 | + |
| 60 | +#%% plot the distributions |
| 61 | + |
| 62 | +pl.figure(1, figsize=(6.4, 3)) |
| 63 | +for i in range(n_distributions): |
| 64 | + pl.plot(x, A[:, i]) |
| 65 | +pl.title('Distributions') |
| 66 | +pl.tight_layout() |
| 67 | + |
| 68 | +#%% barycenter computation |
| 69 | + |
| 70 | +alpha = 0.5 # 0<=alpha<=1 |
| 71 | +weights = np.array([1 - alpha, alpha]) |
| 72 | + |
| 73 | +# l2bary |
| 74 | +bary_l2 = A.dot(weights) |
| 75 | + |
| 76 | +# wasserstein |
| 77 | +reg = 1e-3 |
| 78 | +ot.tic() |
| 79 | +bary_wass = ot.bregman.barycenter(A, M, reg, weights) |
| 80 | +ot.toc() |
| 81 | + |
| 82 | + |
| 83 | +ot.tic() |
| 84 | +bary_wass2 = ot.lp.barycenter(A, M, weights, solver='interior-point', verbose=True) |
| 85 | +ot.toc() |
| 86 | + |
| 87 | +pl.figure(2) |
| 88 | +pl.clf() |
| 89 | +pl.subplot(2, 1, 1) |
| 90 | +for i in range(n_distributions): |
| 91 | + pl.plot(x, A[:, i]) |
| 92 | +pl.title('Distributions') |
| 93 | + |
| 94 | +pl.subplot(2, 1, 2) |
| 95 | +pl.plot(x, bary_l2, 'r', label='l2') |
| 96 | +pl.plot(x, bary_wass, 'g', label='Reg Wasserstein') |
| 97 | +pl.plot(x, bary_wass2, 'b', label='LP Wasserstein') |
| 98 | +pl.legend() |
| 99 | +pl.title('Barycenters') |
| 100 | +pl.tight_layout() |
| 101 | + |
| 102 | +problems.append([A, [bary_l2, bary_wass, bary_wass2]]) |
| 103 | + |
| 104 | +############################################################################## |
| 105 | +# Dirac Data |
| 106 | +# ---------- |
| 107 | + |
| 108 | +#%% parameters |
| 109 | + |
| 110 | +a1 = 1.0 * (x > 10) * (x < 50) |
| 111 | +a2 = 1.0 * (x > 60) * (x < 80) |
| 112 | + |
| 113 | +a1 /= a1.sum() |
| 114 | +a2 /= a2.sum() |
| 115 | + |
| 116 | +# creating matrix A containing all distributions |
| 117 | +A = np.vstack((a1, a2)).T |
| 118 | +n_distributions = A.shape[1] |
| 119 | + |
| 120 | +# loss matrix + normalization |
| 121 | +M = ot.utils.dist0(n) |
| 122 | +M /= M.max() |
| 123 | + |
| 124 | + |
| 125 | +#%% plot the distributions |
| 126 | + |
| 127 | +pl.figure(1, figsize=(6.4, 3)) |
| 128 | +for i in range(n_distributions): |
| 129 | + pl.plot(x, A[:, i]) |
| 130 | +pl.title('Distributions') |
| 131 | +pl.tight_layout() |
| 132 | + |
| 133 | + |
| 134 | +#%% barycenter computation |
| 135 | + |
| 136 | +alpha = 0.5 # 0<=alpha<=1 |
| 137 | +weights = np.array([1 - alpha, alpha]) |
| 138 | + |
| 139 | +# l2bary |
| 140 | +bary_l2 = A.dot(weights) |
| 141 | + |
| 142 | +# wasserstein |
| 143 | +reg = 1e-3 |
| 144 | +ot.tic() |
| 145 | +bary_wass = ot.bregman.barycenter(A, M, reg, weights) |
| 146 | +ot.toc() |
| 147 | + |
| 148 | + |
| 149 | +ot.tic() |
| 150 | +bary_wass2 = ot.lp.barycenter(A, M, weights, solver='interior-point', verbose=True) |
| 151 | +ot.toc() |
| 152 | + |
| 153 | + |
| 154 | +problems.append([A, [bary_l2, bary_wass, bary_wass2]]) |
| 155 | + |
| 156 | +pl.figure(2) |
| 157 | +pl.clf() |
| 158 | +pl.subplot(2, 1, 1) |
| 159 | +for i in range(n_distributions): |
| 160 | + pl.plot(x, A[:, i]) |
| 161 | +pl.title('Distributions') |
| 162 | + |
| 163 | +pl.subplot(2, 1, 2) |
| 164 | +pl.plot(x, bary_l2, 'r', label='l2') |
| 165 | +pl.plot(x, bary_wass, 'g', label='Reg Wasserstein') |
| 166 | +pl.plot(x, bary_wass2, 'b', label='LP Wasserstein') |
| 167 | +pl.legend() |
| 168 | +pl.title('Barycenters') |
| 169 | +pl.tight_layout() |
| 170 | + |
| 171 | +#%% parameters |
| 172 | + |
| 173 | +a1 = np.zeros(n) |
| 174 | +a2 = np.zeros(n) |
| 175 | + |
| 176 | +a1[10] = .25 |
| 177 | +a1[20] = .5 |
| 178 | +a1[30] = .25 |
| 179 | +a2[80] = 1 |
| 180 | + |
| 181 | + |
| 182 | +a1 /= a1.sum() |
| 183 | +a2 /= a2.sum() |
| 184 | + |
| 185 | +# creating matrix A containing all distributions |
| 186 | +A = np.vstack((a1, a2)).T |
| 187 | +n_distributions = A.shape[1] |
| 188 | + |
| 189 | +# loss matrix + normalization |
| 190 | +M = ot.utils.dist0(n) |
| 191 | +M /= M.max() |
| 192 | + |
| 193 | + |
| 194 | +#%% plot the distributions |
| 195 | + |
| 196 | +pl.figure(1, figsize=(6.4, 3)) |
| 197 | +for i in range(n_distributions): |
| 198 | + pl.plot(x, A[:, i]) |
| 199 | +pl.title('Distributions') |
| 200 | +pl.tight_layout() |
| 201 | + |
| 202 | + |
| 203 | +#%% barycenter computation |
| 204 | + |
| 205 | +alpha = 0.5 # 0<=alpha<=1 |
| 206 | +weights = np.array([1 - alpha, alpha]) |
| 207 | + |
| 208 | +# l2bary |
| 209 | +bary_l2 = A.dot(weights) |
| 210 | + |
| 211 | +# wasserstein |
| 212 | +reg = 1e-3 |
| 213 | +ot.tic() |
| 214 | +bary_wass = ot.bregman.barycenter(A, M, reg, weights) |
| 215 | +ot.toc() |
| 216 | + |
| 217 | + |
| 218 | +ot.tic() |
| 219 | +bary_wass2 = ot.lp.barycenter(A, M, weights, solver='interior-point', verbose=True) |
| 220 | +ot.toc() |
| 221 | + |
| 222 | + |
| 223 | +problems.append([A, [bary_l2, bary_wass, bary_wass2]]) |
| 224 | + |
| 225 | +pl.figure(2) |
| 226 | +pl.clf() |
| 227 | +pl.subplot(2, 1, 1) |
| 228 | +for i in range(n_distributions): |
| 229 | + pl.plot(x, A[:, i]) |
| 230 | +pl.title('Distributions') |
| 231 | + |
| 232 | +pl.subplot(2, 1, 2) |
| 233 | +pl.plot(x, bary_l2, 'r', label='l2') |
| 234 | +pl.plot(x, bary_wass, 'g', label='Reg Wasserstein') |
| 235 | +pl.plot(x, bary_wass2, 'b', label='LP Wasserstein') |
| 236 | +pl.legend() |
| 237 | +pl.title('Barycenters') |
| 238 | +pl.tight_layout() |
| 239 | + |
| 240 | + |
| 241 | +############################################################################## |
| 242 | +# Final figure |
| 243 | +# ------------ |
| 244 | +# |
| 245 | + |
| 246 | +#%% plot |
| 247 | + |
| 248 | +nbm = len(problems) |
| 249 | +nbm2 = (nbm // 2) |
| 250 | + |
| 251 | + |
| 252 | +pl.figure(2, (20, 6)) |
| 253 | +pl.clf() |
| 254 | + |
| 255 | +for i in range(nbm): |
| 256 | + |
| 257 | + A = problems[i][0] |
| 258 | + bary_l2 = problems[i][1][0] |
| 259 | + bary_wass = problems[i][1][1] |
| 260 | + bary_wass2 = problems[i][1][2] |
| 261 | + |
| 262 | + pl.subplot(2, nbm, 1 + i) |
| 263 | + for j in range(n_distributions): |
| 264 | + pl.plot(x, A[:, j]) |
| 265 | + if i == nbm2: |
| 266 | + pl.title('Distributions') |
| 267 | + pl.xticks(()) |
| 268 | + pl.yticks(()) |
| 269 | + |
| 270 | + pl.subplot(2, nbm, 1 + i + nbm) |
| 271 | + |
| 272 | + pl.plot(x, bary_l2, 'r', label='L2 (Euclidean)') |
| 273 | + pl.plot(x, bary_wass, 'g', label='Reg Wasserstein') |
| 274 | + pl.plot(x, bary_wass2, 'b', label='LP Wasserstein') |
| 275 | + if i == nbm - 1: |
| 276 | + pl.legend() |
| 277 | + if i == nbm2: |
| 278 | + pl.title('Barycenters') |
| 279 | + |
| 280 | + pl.xticks(()) |
| 281 | + pl.yticks(()) |
0 commit comments