Debian Codegraph @

examples/barycenters/plot_barycenter_lp_vs_entropic.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
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 # Author: Remi Flamary <[email protected]>
21 #
22 # License: MIT License
23
24 # sphinx_gallery_thumbnail_number = 4
25
26 import numpy as np
27 import matplotlib.pylab as pl
28 import ot
29
30 # necessary for 3d plot even if not used
31 from mpl_toolkits.mplot3d import Axes3D # noqa
32 from matplotlib.collections import PolyCollection # noqa
33
34 # import ot.lp.cvx as cvx
35
36 ##############################################################################
37 # Gaussian Data
38 # -------------
39
40 # %% parameters
41
42 problems = []
43
44 n = 100 # nb bins
45
46 # bin positions
47 x = np.arange(n, dtype=np.float64)
48
49 # Gaussian distributions
50 # Gaussian distributions
51 a1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std
52 a2 = ot.datasets.make_1D_gauss(n, m=60, s=8)
53
54 # creating matrix A containing all distributions
55 A = np.vstack((a1, a2)).T
56 n_distributions = A.shape[1]
57
58 # loss matrix + normalization
59 M = ot.utils.dist0(n)
60 M /= M.max()
61
62
63 # %% plot the distributions
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 # %% barycenter computation
72
73 alpha = 0.5 # 0<=alpha<=1
74 weights = np.array([1 - alpha, alpha])
75
76 # l2bary
77 bary_l2 = A.dot(weights)
78
79 # wasserstein
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 # Stair Data
109 # ----------
110
111 # %% parameters
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 # creating matrix A containing all distributions
120 A = np.vstack((a1, a2)).T
121 n_distributions = A.shape[1]
122
123 # loss matrix + normalization
124 M = ot.utils.dist0(n)
125 M /= M.max()
126
127
128 # %% plot the distributions
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 # %% barycenter computation
138
139 alpha = 0.5 # 0<=alpha<=1
140 weights = np.array([1 - alpha, alpha])
141
142 # l2bary
143 bary_l2 = A.dot(weights)
144
145 # wasserstein
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 # Dirac Data
177 # ----------
178
179 # %% parameters
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 # creating matrix A containing all distributions
194 A = np.vstack((a1, a2)).T
195 n_distributions = A.shape[1]
196
197 # loss matrix + normalization
198 M = ot.utils.dist0(n)
199 M /= M.max()
200
201
202 # %% plot the distributions
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 # %% barycenter computation
212
213 alpha = 0.5 # 0<=alpha<=1
214 weights = np.array([1 - alpha, alpha])
215
216 # l2bary
217 bary_l2 = A.dot(weights)
218
219 # wasserstein
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 # Final figure
251 # ------------
252 #
253
254 # %% plot
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(())