1D Wasserstein barycenter: exact LP vs entropic regularization

This example illustrates the computation of regularized Wasserstein Barycenter as proposed in [3] and exact LP barycenters using standard LP solver.

It reproduces approximately Figure 3.1 and 3.2 from the following paper: Cuturi, M., & Peyré, G. (2016). A smoothed dual approach for variational Wasserstein problems. SIAM Journal on Imaging Sciences, 9(1), 320-343.

[3] Benamou, J. D., Carlier, G., Cuturi, M., Nenna, L., & Peyré, G. (2015). Iterative Bregman projections for regularized transportation problems SIAM Journal on Scientific Computing, 37(2), A1111-A1138.

# Author: Remi Flamary <remi.flamary@unice.fr>
#
# License: MIT License

# sphinx_gallery_thumbnail_number = 4

import numpy as np
import matplotlib.pylab as pl
import ot
# necessary for 3d plot even if not used
from mpl_toolkits.mplot3d import Axes3D  # noqa
from matplotlib.collections import PolyCollection  # noqa

#import ot.lp.cvx as cvx

Gaussian Data

problems = []

n = 100  # nb bins

# bin positions
x = np.arange(n, dtype=np.float64)

# Gaussian distributions
# Gaussian distributions
a1 = ot.datasets.make_1D_gauss(n, m=20, s=5)  # m= mean, s= std
a2 = ot.datasets.make_1D_gauss(n, m=60, s=8)

# creating matrix A containing all distributions
A = np.vstack((a1, a2)).T
n_distributions = A.shape[1]

# loss matrix + normalization
M = ot.utils.dist0(n)
M /= M.max()
pl.figure(1, figsize=(6.4, 3))
for i in range(n_distributions):
    pl.plot(x, A[:, i])
pl.title('Distributions')
pl.tight_layout()
Distributions
alpha = 0.5  # 0<=alpha<=1
weights = np.array([1 - alpha, alpha])

# l2bary
bary_l2 = A.dot(weights)

# wasserstein
reg = 1e-3
ot.tic()
bary_wass = ot.bregman.barycenter(A, M, reg, weights)
ot.toc()


ot.tic()
bary_wass2 = ot.lp.barycenter(A, M, weights, solver='interior-point', verbose=True)
ot.toc()

pl.figure(2)
pl.clf()
pl.subplot(2, 1, 1)
for i in range(n_distributions):
    pl.plot(x, A[:, i])
pl.title('Distributions')

pl.subplot(2, 1, 2)
pl.plot(x, bary_l2, 'r', label='l2')
pl.plot(x, bary_wass, 'g', label='Reg Wasserstein')
pl.plot(x, bary_wass2, 'b', label='LP Wasserstein')
pl.legend()
pl.title('Barycenters')
pl.tight_layout()

problems.append([A, [bary_l2, bary_wass, bary_wass2]])
Distributions, Barycenters

Out:

Elapsed time : 0.003314971923828125 s
Primal Feasibility  Dual Feasibility    Duality Gap         Step             Path Parameter      Objective
1.0                 1.0                 1.0                 -                1.0                 1700.336700337
0.006776453137633   0.006776453137634   0.006776453137633   0.9932238647293  0.006776453137633   125.6700527543
0.004018712867874   0.004018712867874   0.004018712867874   0.4301142633     0.004018712867874   12.26594150092
0.001172775061627   0.001172775061627   0.001172775061627   0.7599932455028  0.001172775061627   0.3378536968897
0.0004375137005385  0.0004375137005385  0.0004375137005385  0.6422331807989  0.0004375137005385  0.1468420566358
0.0002326690467341  0.0002326690467341  0.0002326690467341  0.5016999460892  0.0002326690467341  0.09381703231434
7.430121674306e-05  7.430121674303e-05  7.430121674306e-05  0.7035962305812  7.430121674306e-05  0.05777870257171
5.321227838859e-05  5.321227838854e-05  5.32122783886e-05   0.3087841864437  5.32122783886e-05   0.05266249477199
1.990900379194e-05  1.990900379192e-05  1.990900379194e-05  0.652047201324   1.990900379194e-05  0.04526054405518
6.305442046783e-06  6.305442046775e-06  6.305442046791e-06  0.7073953304071  6.305442046789e-06  0.04237597591383
2.290148391545e-06  2.290148391536e-06  2.290148391577e-06  0.6941812711489  2.290148391588e-06  0.041522849321
1.182864875233e-06  1.182864875256e-06  1.182864875283e-06  0.5084552047393  1.182864875307e-06  0.04129461872824
3.626786388206e-07  3.626786386317e-07  3.6267863863e-07    0.7101651568857  3.626786385521e-07  0.04113032448929
1.539754245663e-07  1.539754247044e-07  1.539754247171e-07  0.6279322076897  1.539754251839e-07  0.04108867636376
5.193221327742e-08  5.19322136741e-08   5.193221369586e-08  0.6843453492481  5.193221619265e-08  0.04106859618398
1.888204693305e-08  1.888204821353e-08  1.888204825218e-08  0.6673443932655  1.888205702755e-08  0.04106214175212
5.676833069444e-09  5.676836916146e-09  5.67683691281e-09   0.7281714960303  5.676872709568e-09  0.0410595864845
3.501211039724e-09  3.501200106933e-09  3.501200136609e-09  0.4140068631271  3.501217143214e-09  0.04105916266299
1.110627742105e-09  1.110588309413e-09  1.11058832049e-09   0.6999010788877  1.110632175822e-09  0.04105870073371
5.764850512272e-10  5.769261315062e-10  5.76926142792e-10   0.5002710697621  5.767305467833e-10  0.04105859764703
1.533588929132e-10  1.536273202003e-10  1.536272970712e-10  0.7516230462737  1.535573954068e-10  0.0410585167898
6.73197850596e-11   6.73459258579e-11   6.734592077332e-11  0.5945675417088  6.731164305082e-11  0.04105850032986
1.801764779443e-11  1.749150656756e-11  1.749156850764e-11  0.7552924562621  1.743070664635e-11  0.04105849090724
Optimization terminated successfully.
         Current function value: 0.041058
         Iterations: 22
Elapsed time : 2.9790444374084473 s

Stair Data

a1 = 1.0 * (x > 10) * (x < 50)
a2 = 1.0 * (x > 60) * (x < 80)

a1 /= a1.sum()
a2 /= a2.sum()

# creating matrix A containing all distributions
A = np.vstack((a1, a2)).T
n_distributions = A.shape[1]

# loss matrix + normalization
M = ot.utils.dist0(n)
M /= M.max()
pl.figure(1, figsize=(6.4, 3))
for i in range(n_distributions):
    pl.plot(x, A[:, i])
pl.title('Distributions')
pl.tight_layout()
Distributions
alpha = 0.5  # 0<=alpha<=1
weights = np.array([1 - alpha, alpha])

# l2bary
bary_l2 = A.dot(weights)

# wasserstein
reg = 1e-3
ot.tic()
bary_wass = ot.bregman.barycenter(A, M, reg, weights)
ot.toc()


ot.tic()
bary_wass2 = ot.lp.barycenter(A, M, weights, solver='interior-point', verbose=True)
ot.toc()


problems.append([A, [bary_l2, bary_wass, bary_wass2]])

pl.figure(2)
pl.clf()
pl.subplot(2, 1, 1)
for i in range(n_distributions):
    pl.plot(x, A[:, i])
pl.title('Distributions')

pl.subplot(2, 1, 2)
pl.plot(x, bary_l2, 'r', label='l2')
pl.plot(x, bary_wass, 'g', label='Reg Wasserstein')
pl.plot(x, bary_wass2, 'b', label='LP Wasserstein')
pl.legend()
pl.title('Barycenters')
pl.tight_layout()
Distributions, Barycenters

Out:

Elapsed time : 0.0067598819732666016 s
Primal Feasibility  Dual Feasibility    Duality Gap         Step             Path Parameter      Objective
1.0                 1.0                 1.0                 -                1.0                 1700.336700337
0.006776466288967   0.006776466288967   0.006776466288967   0.9932238515788  0.006776466288968   125.6649255808
0.004036918865497   0.004036918865497   0.004036918865497   0.4272973099314  0.004036918865497   12.34716170111
0.001219232687069   0.001219232687069   0.001219232687069   0.7496986855994  0.001219232687069   0.3243835647407
0.0003837422984427  0.0003837422984427  0.0003837422984427  0.6926882608286  0.0003837422984427  0.1361719397492
0.0001070128410182  0.0001070128410182  0.0001070128410182  0.7643889137854  0.0001070128410182  0.07581952832515
0.0001001275033711  0.000100127503371   0.0001001275033711  0.07058704837782 0.0001001275033711  0.07347394936348
4.550897507833e-05  4.550897507832e-05  4.550897507833e-05  0.5761172484836  4.550897507833e-05  0.05555077655044
8.557124125502e-06  8.557124125495e-06  8.557124125502e-06  0.8535925441154  8.557124125503e-06  0.0443981466022
3.611995628362e-06  3.611995628359e-06  3.611995628375e-06  0.6002277331592  3.611995628377e-06  0.04283007762151
7.590393750322e-07  7.59039375014e-07   7.590393750355e-07  0.822148653339   7.590393750361e-07  0.04192322976247
8.299929287269e-08  8.299929283142e-08  8.299929287457e-08  0.9017467938803  8.299929287509e-08  0.04170825633295
3.117560215317e-10  3.117560474123e-10  3.117560204434e-10  0.9970399692198  3.117560202277e-10  0.04168179329766
1.559775055372e-14  1.561428379782e-14  1.559760203488e-14  0.9999499685222  1.559752674386e-14  0.04168169240444
Optimization terminated successfully.
         Current function value: 0.041682
         Iterations: 13
Elapsed time : 2.3045260906219482 s

Dirac Data

a1 = np.zeros(n)
a2 = np.zeros(n)

a1[10] = .25
a1[20] = .5
a1[30] = .25
a2[80] = 1


a1 /= a1.sum()
a2 /= a2.sum()

# creating matrix A containing all distributions
A = np.vstack((a1, a2)).T
n_distributions = A.shape[1]

# loss matrix + normalization
M = ot.utils.dist0(n)
M /= M.max()
pl.figure(1, figsize=(6.4, 3))
for i in range(n_distributions):
    pl.plot(x, A[:, i])
pl.title('Distributions')
pl.tight_layout()
Distributions
alpha = 0.5  # 0<=alpha<=1
weights = np.array([1 - alpha, alpha])

# l2bary
bary_l2 = A.dot(weights)

# wasserstein
reg = 1e-3
ot.tic()
bary_wass = ot.bregman.barycenter(A, M, reg, weights)
ot.toc()


ot.tic()
bary_wass2 = ot.lp.barycenter(A, M, weights, solver='interior-point', verbose=True)
ot.toc()


problems.append([A, [bary_l2, bary_wass, bary_wass2]])

pl.figure(2)
pl.clf()
pl.subplot(2, 1, 1)
for i in range(n_distributions):
    pl.plot(x, A[:, i])
pl.title('Distributions')

pl.subplot(2, 1, 2)
pl.plot(x, bary_l2, 'r', label='l2')
pl.plot(x, bary_wass, 'g', label='Reg Wasserstein')
pl.plot(x, bary_wass2, 'b', label='LP Wasserstein')
pl.legend()
pl.title('Barycenters')
pl.tight_layout()
Distributions, Barycenters

Out:

Elapsed time : 0.0009958744049072266 s
Primal Feasibility  Dual Feasibility    Duality Gap         Step             Path Parameter      Objective
1.0                 1.0                 1.0                 -                1.0                 1700.336700337
0.006774675520687   0.006774675520687   0.006774675520687   0.9932256422636  0.006774675520687   125.6956034733
0.002048208707529   0.002048208707529   0.002048208707529   0.7343095368176  0.002048208707529   5.213991622021
0.0002697365474824  0.0002697365474824  0.0002697365474824  0.8839403501148  0.0002697365474824  0.5059383903968
6.832109993819e-05  6.832109993818e-05  6.832109993819e-05  0.7601171076044  6.832109993818e-05  0.2339657807265
2.437682932226e-05  2.437682932226e-05  2.437682932226e-05  0.6663448297416  2.437682932226e-05  0.1471256246326
1.134983216297e-05  1.134983216299e-05  1.134983216297e-05  0.5553643816468  1.134983216297e-05  0.1181584941169
3.342312725774e-06  3.342312725781e-06  3.342312725774e-06  0.723813357168   3.342312725774e-06  0.1006387519744
7.078561231268e-07  7.078561231235e-07  7.078561231266e-07  0.8033142552548  7.078561231266e-07  0.09474734646262
1.966870958108e-07  1.966870953918e-07  1.966870953989e-07  0.7525479178458  1.966870954248e-07  0.09354342735761
4.199895240123e-10  4.199895384126e-10  4.199895235332e-10  0.9984019849396  4.199895236104e-10  0.0931036778586
2.1017793609e-14    2.101768735229e-14  2.101780822184e-14  0.9999499563876  2.101780420221e-14  0.0931027446646
Optimization terminated successfully.
         Current function value: 0.093103
         Iterations: 11
Elapsed time : 2.1947948932647705 s

Final figure

nbm = len(problems)
nbm2 = (nbm // 2)


pl.figure(2, (20, 6))
pl.clf()

for i in range(nbm):

    A = problems[i][0]
    bary_l2 = problems[i][1][0]
    bary_wass = problems[i][1][1]
    bary_wass2 = problems[i][1][2]

    pl.subplot(2, nbm, 1 + i)
    for j in range(n_distributions):
        pl.plot(x, A[:, j])
    if i == nbm2:
        pl.title('Distributions')
    pl.xticks(())
    pl.yticks(())

    pl.subplot(2, nbm, 1 + i + nbm)

    pl.plot(x, bary_l2, 'r', label='L2 (Euclidean)')
    pl.plot(x, bary_wass, 'g', label='Reg Wasserstein')
    pl.plot(x, bary_wass2, 'b', label='LP Wasserstein')
    if i == nbm - 1:
        pl.legend()
    if i == nbm2:
        pl.title('Barycenters')

    pl.xticks(())
    pl.yticks(())
Distributions, Barycenters

Total running time of the script: ( 0 minutes 8.497 seconds)

Gallery generated by Sphinx-Gallery