| 1 |
|
| 2 |
""" |
| 3 |
======================================================== |
| 4 |
Gaussian Bures-Wasserstein barycenters |
| 5 |
======================================================== |
| 6 |
|
| 7 |
Illustration of Gaussian Bures-Wasserstein barycenters. |
| 8 |
|
| 9 |
""" |
| 10 |
|
| 11 |
> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
from matplotlib import colors |
| 18 |
from matplotlib.patches import Ellipse |
| 19 |
import numpy as np |
| 20 |
import matplotlib.pylab as pl |
| 21 |
import ot |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
C1 = np.array([[0.5, -0.4], [-0.4, 0.5]]) |
| 29 |
C2 = np.array([[1, 0.3], [0.3, 1]]) |
| 30 |
C3 = np.array([[1.5, 0], [0, 0.5]]) |
| 31 |
C4 = np.array([[0.5, 0], [0, 1.5]]) |
| 32 |
|
| 33 |
C = np.stack((C1, C2, C3, C4)) |
| 34 |
|
| 35 |
m1 = np.array([0, 0]) |
| 36 |
m2 = np.array([0, 4]) |
| 37 |
m3 = np.array([4, 0]) |
| 38 |
m4 = np.array([4, 4]) |
| 39 |
|
| 40 |
m = np.stack((m1, m2, m3, m4)) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
def draw_cov(mu, C, color=None, label=None, nstd=1): |
| 48 |
def eigsorted(cov): |
| 49 |
vals, vecs = np.linalg.eigh(cov) |
| 50 |
order = vals.argsort()[::-1] |
| 51 |
return vals[order], vecs[:, order] |
| 52 |
|
| 53 |
vals, vecs = eigsorted(C) |
| 54 |
theta = np.degrees(np.arctan2(*vecs[:, 0][::-1])) |
| 55 |
w, h = 2 * nstd * np.sqrt(vals) |
| 56 |
ell = Ellipse( |
| 57 |
xy=(mu[0], mu[1]), |
| 58 |
width=w, |
| 59 |
height=h, |
| 60 |
alpha=0.5, |
| 61 |
angle=theta, |
| 62 |
facecolor=color, |
| 63 |
edgecolor=color, |
| 64 |
label=label, |
| 65 |
fill=True, |
| 66 |
) |
| 67 |
pl.gca().add_artist(ell) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
axis = [-1.5, 5.5, -1.5, 5.5] |
| 72 |
|
| 73 |
pl.figure(1, (8, 2)) |
| 74 |
pl.clf() |
| 75 |
|
| 76 |
pl.subplot(1, 4, 1) |
| 77 |
draw_cov(m1, C1, color="C0") |
| 78 |
pl.axis(axis) |
| 79 |
pl.title("$\mathcal{N}(m_1,\Sigma_1)$") |
| 80 |
|
| 81 |
pl.subplot(1, 4, 2) |
| 82 |
draw_cov(m2, C2, color="C1") |
| 83 |
pl.axis(axis) |
| 84 |
pl.title("$\mathcal{N}(m_2,\Sigma_2)$") |
| 85 |
|
| 86 |
pl.subplot(1, 4, 3) |
| 87 |
draw_cov(m3, C3, color="C2") |
| 88 |
pl.axis(axis) |
| 89 |
pl.title("$\mathcal{N}(m_3,\Sigma_3)$") |
| 90 |
|
| 91 |
pl.subplot(1, 4, 4) |
| 92 |
draw_cov(m4, C4, color="C3") |
| 93 |
pl.axis(axis) |
| 94 |
pl.title("$\mathcal{N}(m_4,\Sigma_4)$") |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
v1 = np.array((1, 0, 0, 0)) |
| 102 |
v2 = np.array((0, 1, 0, 0)) |
| 103 |
v3 = np.array((0, 0, 1, 0)) |
| 104 |
v4 = np.array((0, 0, 0, 1)) |
| 105 |
|
| 106 |
|
| 107 |
colors = np.stack( |
| 108 |
(colors.to_rgb("C0"), colors.to_rgb("C1"), colors.to_rgb("C2"), colors.to_rgb("C3")) |
| 109 |
) |
| 110 |
|
| 111 |
pl.figure(2, (8, 8)) |
| 112 |
|
| 113 |
nb_interp = 6 |
| 114 |
|
| 115 |
for i in range(nb_interp): |
| 116 |
for j in range(nb_interp): |
| 117 |
tx = float(i) / (nb_interp - 1) |
| 118 |
ty = float(j) / (nb_interp - 1) |
| 119 |
|
| 120 |
|
| 121 |
tmp1 = (1 - tx) * v1 + tx * v2 |
| 122 |
tmp2 = (1 - tx) * v3 + tx * v4 |
| 123 |
weights = (1 - ty) * tmp1 + ty * tmp2 |
| 124 |
|
| 125 |
color = np.dot(colors.T, weights) |
| 126 |
|
| 127 |
mb, Cb = ot.gaussian.bures_wasserstein_barycenter(m, C, weights) |
| 128 |
|
| 129 |
draw_cov(mb, Cb, color=color, label=None, nstd=0.3) |
| 130 |
|
| 131 |
pl.axis(axis) |
| 132 |
pl.axis("off") |
| 133 |
pl.tight_layout() |