Debian Codegraph @

examples/barycenters/plot_barycenter_1D.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 ==============================
4 1D Wasserstein barycenter demo
5 ==============================
6
7 This example illustrates the computation of regularized Wasserstein Barycenter
8 as proposed in [3].
9
10
11 [3] Benamou, J. D., Carlier, G., Cuturi, M., Nenna, L., & Peyré, G. (2015).
12 Iterative Bregman projections for regularized transportation problems
13 SIAM Journal on Scientific Computing, 37(2), A1111-A1138.
14
15 """
16
17 # Author: Remi Flamary <[email protected]>
18 #
19 # License: MIT License
20
21 # sphinx_gallery_thumbnail_number = 1
22
23 import numpy as np
24 import matplotlib.pyplot as plt
25 import ot
26
27 # necessary for 3d plot even if not used
28 from mpl_toolkits.mplot3d import Axes3D # noqa
29 from matplotlib.collections import PolyCollection
30
31 ##############################################################################
32 # Generate data
33 # -------------
34
35 # %% parameters
36
37 n = 100 # nb bins
38
39 # bin positions
40 x = np.arange(n, dtype=np.float64)
41
42 # Gaussian distributions
43 a1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std
44 a2 = ot.datasets.make_1D_gauss(n, m=60, s=8)
45
46 # creating matrix A containing all distributions
47 A = np.vstack((a1, a2)).T
48 n_distributions = A.shape[1]
49
50 # loss matrix + normalization
51 M = ot.utils.dist0(n)
52 M /= M.max()
53
54 ##############################################################################
55 # Barycenter computation
56 # ----------------------
57
58 # %% barycenter computation
59
60 alpha = 0.2 # 0<=alpha<=1
61 weights = np.array([1 - alpha, alpha])
62
63 # l2bary
64 bary_l2 = A.dot(weights)
65
66 # wasserstein
67 reg = 1e-3
68 bary_wass = ot.bregman.barycenter(A, M, reg, weights)
69
70 f, (ax1, ax2) = plt.subplots(2, 1, tight_layout=True, num=1)
71 ax1.plot(x, A, color="black")
72 ax1.set_title("Distributions")
73
74 ax2.plot(x, bary_l2, "r", label="l2")
75 ax2.plot(x, bary_wass, "g", label="Wasserstein")
76 ax2.set_title("Barycenters")
77
78 plt.legend()
79 plt.show()
80
81 ##############################################################################
82 # Barycentric interpolation
83 # -------------------------
84
85 # %% barycenter interpolation
86
87 n_alpha = 11
88 alpha_list = np.linspace(0, 1, n_alpha)
89
90
91 B_l2 = np.zeros((n, n_alpha))
92
93 B_wass = np.copy(B_l2)
94
95 for i in range(n_alpha):
96 alpha = alpha_list[i]
97 weights = np.array([1 - alpha, alpha])
98 B_l2[:, i] = A.dot(weights)
99 B_wass[:, i] = ot.bregman.barycenter(A, M, reg, weights)
100
101 # %% plot interpolation
102 plt.figure(2)
103
104 cmap = plt.get_cmap("viridis")
105 verts = []
106 zs = alpha_list
107 for i, z in enumerate(zs):
108 ys = B_l2[:, i]
109 verts.append(list(zip(x, ys)))
110
111 ax = plt.gcf().add_subplot(projection="3d")
112
113 poly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])
114 poly.set_alpha(0.7)
115 ax.add_collection3d(poly, zs=zs, zdir="y")
116 ax.set_xlabel("x")
117 ax.set_xlim3d(0, n)
118 ax.set_ylabel("$\\alpha$")
119 ax.set_ylim3d(0, 1)
120 ax.set_zlabel("")
121 ax.set_zlim3d(0, B_l2.max() * 1.01)
122 plt.title("Barycenter interpolation with l2")
123 plt.tight_layout()
124
125 plt.figure(3)
126 cmap = plt.get_cmap("viridis")
127 verts = []
128 zs = alpha_list
129 for i, z in enumerate(zs):
130 ys = B_wass[:, i]
131 verts.append(list(zip(x, ys)))
132
133 ax = plt.gcf().add_subplot(projection="3d")
134
135 poly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_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("$\\alpha$")
141 ax.set_ylim3d(0, 1)
142 ax.set_zlabel("")
143 ax.set_zlim3d(0, B_l2.max() * 1.01)
144 plt.title("Barycenter interpolation with Wasserstein")
145 plt.tight_layout()
146
147 plt.show()