1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
"""Tests for module gaussian"""
# Author: Theo Gnassounou <theo.gnassounou@inria.fr>
# Remi Flamary <remi.flamary@polytehnique.edu>
#
# License: MIT License
import numpy as np
import pytest
import ot
from ot.datasets import make_data_classif
from ot.utils import is_all_finite
def test_bures_wasserstein_mapping(nx):
ns = 50
nt = 50
Xs, ys = make_data_classif("3gauss", ns)
Xt, yt = make_data_classif("3gauss2", nt)
ms = np.mean(Xs, axis=0)[None, :]
mt = np.mean(Xt, axis=0)[None, :]
Cs = np.cov(Xs.T)
Ct = np.cov(Xt.T)
Xsb, msb, mtb, Csb, Ctb = nx.from_numpy(Xs, ms, mt, Cs, Ct)
A_log, b_log, log = ot.gaussian.bures_wasserstein_mapping(
msb, mtb, Csb, Ctb, log=True
)
A, b = ot.gaussian.bures_wasserstein_mapping(msb, mtb, Csb, Ctb, log=False)
Xst = nx.to_numpy(nx.dot(Xsb, A) + b)
Xst_log = nx.to_numpy(nx.dot(Xsb, A_log) + b_log)
Cst = np.cov(Xst.T)
Cst_log = np.cov(Xst_log.T)
np.testing.assert_allclose(Cst_log, Cst, rtol=1e-2, atol=1e-2)
np.testing.assert_allclose(Ct, Cst, rtol=1e-2, atol=1e-2)
@pytest.mark.parametrize("bias", [True, False])
def test_empirical_bures_wasserstein_mapping(nx, bias):
ns = 50
nt = 50
Xs, ys = make_data_classif("3gauss", ns)
Xt, yt = make_data_classif("3gauss2", nt)
if not bias:
ms = np.mean(Xs, axis=0)[None, :]
mt = np.mean(Xt, axis=0)[None, :]
Xs = Xs - ms
Xt = Xt - mt
Xsb, Xtb = nx.from_numpy(Xs, Xt)
A, b, log = ot.gaussian.empirical_bures_wasserstein_mapping(
Xsb, Xtb, log=True, bias=bias
)
A_log, b_log = ot.gaussian.empirical_bures_wasserstein_mapping(
Xsb, Xtb, log=False, bias=bias
)
Xst = nx.to_numpy(nx.dot(Xsb, A) + b)
Xst_log = nx.to_numpy(nx.dot(Xsb, A_log) + b_log)
Ct = np.cov(Xt.T)
Cst = np.cov(Xst.T)
Cst_log = np.cov(Xst_log.T)
np.testing.assert_allclose(Cst_log, Cst, rtol=1e-2, atol=1e-2)
np.testing.assert_allclose(Ct, Cst, rtol=1e-2, atol=1e-2)
def test_empirical_bures_wasserstein_mapping_numerical_error_warning():
rng = np.random.RandomState(42)
Xs = rng.rand(766, 800) * 5
Xt = rng.rand(295, 800) * 2
with pytest.warns():
A, b = ot.gaussian.empirical_bures_wasserstein_mapping(Xs, Xt, reg=1e-8)
assert not is_all_finite(A, b)
def test_bures_wasserstein_distance(nx):
ms, mt = np.array([0]).astype(np.float32), np.array([10]).astype(np.float32)
Cs, Ct = np.array([[1]]).astype(np.float32), np.array([[1]]).astype(np.float32)
msb, mtb, Csb, Ctb = nx.from_numpy(ms, mt, Cs, Ct)
Wb_log, log = ot.gaussian.bures_wasserstein_distance(msb, mtb, Csb, Ctb, log=True)
Wb = ot.gaussian.bures_wasserstein_distance(msb, mtb, Csb, Ctb, log=False)
np.testing.assert_allclose(
nx.to_numpy(Wb_log), nx.to_numpy(Wb), rtol=1e-2, atol=1e-2
)
np.testing.assert_allclose(10, nx.to_numpy(Wb), rtol=1e-2, atol=1e-2)
@pytest.mark.parametrize("bias", [True, False])
def test_empirical_bures_wasserstein_distance(nx, bias):
ns = 400
nt = 400
rng = np.random.RandomState(10)
Xs = rng.normal(0, 1, ns)[:, np.newaxis]
Xt = rng.normal(10 * bias, 1, nt)[:, np.newaxis]
Xsb, Xtb = nx.from_numpy(Xs, Xt)
Wb_log, log = ot.gaussian.empirical_bures_wasserstein_distance(
Xsb, Xtb, log=True, bias=bias
)
Wb = ot.gaussian.empirical_bures_wasserstein_distance(
Xsb, Xtb, log=False, bias=bias
)
np.testing.assert_allclose(
nx.to_numpy(Wb_log), nx.to_numpy(Wb), rtol=1e-2, atol=1e-2
)
np.testing.assert_allclose(10 * bias, nx.to_numpy(Wb), rtol=1e-2, atol=1e-2)
def test_bures_wasserstein_barycenter(nx):
n = 50
k = 10
X = []
y = []
m = []
C = []
for _ in range(k):
X_, y_ = make_data_classif("3gauss", n)
m_ = np.mean(X_, axis=0)[None, :]
C_ = np.cov(X_.T)
X.append(X_)
y.append(y_)
m.append(m_)
C.append(C_)
m = np.array(m)
C = np.array(C)
X = nx.from_numpy(*X)
m = nx.from_numpy(m)
C = nx.from_numpy(C)
mblog, Cblog, log = ot.gaussian.bures_wasserstein_barycenter(m, C, log=True)
mb, Cb = ot.gaussian.bures_wasserstein_barycenter(m, C, log=False)
np.testing.assert_allclose(Cb, Cblog, rtol=1e-2, atol=1e-2)
np.testing.assert_allclose(mb, mblog, rtol=1e-2, atol=1e-2)
# Test weights argument
weights = nx.ones(k) / k
mbw, Cbw = ot.gaussian.bures_wasserstein_barycenter(
m, C, weights=weights, log=False
)
np.testing.assert_allclose(Cbw, Cb, rtol=1e-2, atol=1e-2)
# test with closed form for diagonal covariance matrices
Cdiag = [nx.diag(nx.diag(C[i])) for i in range(k)]
Cdiag = nx.stack(Cdiag, axis=0)
mbdiag, Cbdiag = ot.gaussian.bures_wasserstein_barycenter(m, Cdiag, log=False)
Cdiag_sqrt = [nx.sqrtm(C) for C in Cdiag]
Cdiag_sqrt = nx.stack(Cdiag_sqrt, axis=0)
Cdiag_mean = nx.mean(Cdiag_sqrt, axis=0)
Cdiag_cf = Cdiag_mean @ Cdiag_mean
np.testing.assert_allclose(Cbdiag, Cdiag_cf, rtol=1e-2, atol=1e-2)
@pytest.mark.parametrize("bias", [True, False])
def test_empirical_bures_wasserstein_barycenter(nx, bias):
n = 50
k = 10
X = []
y = []
for _ in range(k):
X_, y_ = make_data_classif("3gauss", n)
X.append(X_)
y.append(y_)
X = nx.from_numpy(*X)
mblog, Cblog, log = ot.gaussian.empirical_bures_wasserstein_barycenter(
X, log=True, bias=bias
)
mb, Cb = ot.gaussian.empirical_bures_wasserstein_barycenter(X, log=False, bias=bias)
np.testing.assert_allclose(Cb, Cblog, rtol=1e-2, atol=1e-2)
np.testing.assert_allclose(mb, mblog, rtol=1e-2, atol=1e-2)
@pytest.mark.parametrize("d_target", [1, 2, 3, 10])
def test_gaussian_gromov_wasserstein_distance(nx, d_target):
ns = 400
nt = 400
rng = np.random.RandomState(10)
Xs, ys = make_data_classif("3gauss", ns, random_state=rng)
Xt, yt = make_data_classif("3gauss2", nt, random_state=rng)
Xt = np.concatenate((Xt, rng.normal(0, 1, (nt, 8))), axis=1)
Xt = Xt[:, 0:d_target].reshape((nt, d_target))
ms = np.mean(Xs, axis=0)[None, :]
mt = np.mean(Xt, axis=0)[None, :]
Cs = np.cov(Xs.T)
Ct = np.cov(Xt.T).reshape((d_target, d_target))
Xsb, Xtb, msb, mtb, Csb, Ctb = nx.from_numpy(Xs, Xt, ms, mt, Cs, Ct)
Gb, log = ot.gaussian.gaussian_gromov_wasserstein_distance(Csb, Ctb, log=True)
Ge, log = ot.gaussian.empirical_gaussian_gromov_wasserstein_distance(
Xsb, Xtb, log=True
)
# no log
Ge0 = ot.gaussian.empirical_gaussian_gromov_wasserstein_distance(
Xsb, Xtb, log=False
)
np.testing.assert_allclose(nx.to_numpy(Gb), nx.to_numpy(Ge), rtol=1e-2, atol=1e-2)
np.testing.assert_allclose(nx.to_numpy(Ge), nx.to_numpy(Ge0), rtol=1e-2, atol=1e-2)
@pytest.mark.parametrize("d_target", [1, 2, 3, 10])
def test_gaussian_gromov_wasserstein_mapping(nx, d_target):
ns = 400
nt = 400
rng = np.random.RandomState(10)
Xs, ys = make_data_classif("3gauss", ns, random_state=rng)
Xt, yt = make_data_classif("3gauss2", nt, random_state=rng)
Xt = np.concatenate((Xt, rng.normal(0, 1, (nt, 8))), axis=1)
Xt = Xt[:, 0:d_target].reshape((nt, d_target))
ms = np.mean(Xs, axis=0)[None, :]
mt = np.mean(Xt, axis=0)[None, :]
Cs = np.cov(Xs.T)
Ct = np.cov(Xt.T).reshape((d_target, d_target))
Xsb, Xtb, msb, mtb, Csb, Ctb = nx.from_numpy(Xs, Xt, ms, mt, Cs, Ct)
A, b, log = ot.gaussian.gaussian_gromov_wasserstein_mapping(
msb, mtb, Csb, Ctb, log=True
)
Ae, be, loge = ot.gaussian.empirical_gaussian_gromov_wasserstein_mapping(
Xsb, Xtb, log=True
)
# no log + skewness
Ae0, be0 = ot.gaussian.empirical_gaussian_gromov_wasserstein_mapping(
Xsb, Xtb, log=False, sign_eigs="skewness"
)
Xst = nx.to_numpy(nx.dot(Xsb, A) + b)
Cst = np.cov(Xst.T)
np.testing.assert_allclose(nx.to_numpy(A), nx.to_numpy(Ae))
if d_target <= 2:
np.testing.assert_allclose(Ct, Cst)
# test the other way around (target to source)
Ai, bi, logi = ot.gaussian.gaussian_gromov_wasserstein_mapping(
mtb, msb, Ctb, Csb, log=True
)
Xtt = nx.to_numpy(nx.dot(Xtb, Ai) + bi)
Ctt = np.cov(Xtt.T)
if d_target >= 2:
np.testing.assert_allclose(Cs, Ctt)
|