Debian Codegraph @

examples/barycenters/plot_free_support_sinkhorn_barycenter.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 ========================================================
4 2D free support Sinkhorn barycenters of distributions
5 ========================================================
6
7 Illustration of Sinkhorn barycenter calculation between empirical distributions understood as point clouds
8
9 """
10
11 # Authors: Eduardo Fernandes Montesuma <[email protected]>
12 #
13 # License: MIT License
14
15 import numpy as np
16 import matplotlib.pyplot as plt
17 import ot
18
19 # %%
20 # General Parameters
21 # ------------------
22 reg = 1e-2 # Entropic Regularization
23 numItermax = 20 # Maximum number of iterations for the Barycenter algorithm
24 numInnerItermax = 50 # Maximum number of sinkhorn iterations
25 n_samples = 200
26
27 # %%
28 # Generate Data
29 # -------------
30
31 X1 = np.random.randn(200, 2)
32 X2 = 2 * np.concatenate(
33 [
34 np.concatenate([-np.ones([50, 1]), np.linspace(-1, 1, 50)[:, None]], axis=1),
35 np.concatenate([np.linspace(-1, 1, 50)[:, None], np.ones([50, 1])], axis=1),
36 np.concatenate([np.ones([50, 1]), np.linspace(1, -1, 50)[:, None]], axis=1),
37 np.concatenate([np.linspace(1, -1, 50)[:, None], -np.ones([50, 1])], axis=1),
38 ],
39 axis=0,
40 )
41 X3 = np.random.randn(200, 2)
42 X3 = 2 * (X3 / np.linalg.norm(X3, axis=1)[:, None])
43 X4 = np.random.multivariate_normal(
44 np.array([0, 0]), np.array([[1.0, 0.5], [0.5, 1.0]]), size=200
45 )
46
47 a1, a2, a3, a4 = ot.unif(len(X1)), ot.unif(len(X1)), ot.unif(len(X1)), ot.unif(len(X1))
48
49 # %%
50 # Inspect generated distributions
51 # -------------------------------
52
53 fig, axes = plt.subplots(1, 4, figsize=(16, 4))
54
55 axes[0].scatter(x=X1[:, 0], y=X1[:, 1], c="steelblue", edgecolor="k")
56 axes[1].scatter(x=X2[:, 0], y=X2[:, 1], c="steelblue", edgecolor="k")
57 axes[2].scatter(x=X3[:, 0], y=X3[:, 1], c="steelblue", edgecolor="k")
58 axes[3].scatter(x=X4[:, 0], y=X4[:, 1], c="steelblue", edgecolor="k")
59
60 axes[0].set_xlim([-3, 3])
61 axes[0].set_ylim([-3, 3])
62 axes[0].set_title("Distribution 1")
63
64 axes[1].set_xlim([-3, 3])
65 axes[1].set_ylim([-3, 3])
66 axes[1].set_title("Distribution 2")
67
68 axes[2].set_xlim([-3, 3])
69 axes[2].set_ylim([-3, 3])
70 axes[2].set_title("Distribution 3")
71
72 axes[3].set_xlim([-3, 3])
73 axes[3].set_ylim([-3, 3])
74 axes[3].set_title("Distribution 4")
75
76 plt.tight_layout()
77 plt.show()
78
79 # %%
80 # Interpolating Empirical Distributions
81 # -------------------------------------
82
83 fig = plt.figure(figsize=(10, 10))
84
85 weights = np.array(
86 [
87 [3 / 3, 0 / 3],
88 [2 / 3, 1 / 3],
89 [1 / 3, 2 / 3],
90 [0 / 3, 3 / 3],
91 ]
92 ).astype(np.float32)
93
94 for k in range(4):
95 XB_init = np.random.randn(n_samples, 2)
96 XB = ot.bregman.free_support_sinkhorn_barycenter(
97 measures_locations=[X1, X2],
98 measures_weights=[a1, a2],
99 weights=weights[k],
100 X_init=XB_init,
101 reg=reg,
102 numItermax=numItermax,
103 numInnerItermax=numInnerItermax,
104 )
105 ax = plt.subplot2grid((4, 4), (0, k))
106 ax.scatter(XB[:, 0], XB[:, 1], color="steelblue", edgecolor="k")
107 ax.set_xlim([-3, 3])
108 ax.set_ylim([-3, 3])
109
110 for k in range(1, 4, 1):
111 XB_init = np.random.randn(n_samples, 2)
112 XB = ot.bregman.free_support_sinkhorn_barycenter(
113 measures_locations=[X1, X3],
114 measures_weights=[a1, a2],
115 weights=weights[k],
116 X_init=XB_init,
117 reg=reg,
118 numItermax=numItermax,
119 numInnerItermax=numInnerItermax,
120 )
121 ax = plt.subplot2grid((4, 4), (k, 0))
122 ax.scatter(XB[:, 0], XB[:, 1], color="steelblue", edgecolor="k")
123 ax.set_xlim([-3, 3])
124 ax.set_ylim([-3, 3])
125
126 for k in range(1, 4, 1):
127 XB_init = np.random.randn(n_samples, 2)
128 XB = ot.bregman.free_support_sinkhorn_barycenter(
129 measures_locations=[X3, X4],
130 measures_weights=[a1, a2],
131 weights=weights[k],
132 X_init=XB_init,
133 reg=reg,
134 numItermax=numItermax,
135 numInnerItermax=numInnerItermax,
136 )
137 ax = plt.subplot2grid((4, 4), (3, k))
138 ax.scatter(XB[:, 0], XB[:, 1], color="steelblue", edgecolor="k")
139 ax.set_xlim([-3, 3])
140 ax.set_ylim([-3, 3])
141
142 for k in range(1, 3, 1):
143 XB_init = np.random.randn(n_samples, 2)
144 XB = ot.bregman.free_support_sinkhorn_barycenter(
145 measures_locations=[X2, X4],
146 measures_weights=[a1, a2],
147 weights=weights[k],
148 X_init=XB_init,
149 reg=reg,
150 numItermax=numItermax,
151 numInnerItermax=numInnerItermax,
152 )
153 ax = plt.subplot2grid((4, 4), (k, 3))
154 ax.scatter(XB[:, 0], XB[:, 1], color="steelblue", edgecolor="k")
155 ax.set_xlim([-3, 3])
156 ax.set_ylim([-3, 3])
157
158 plt.show()