Debian Codegraph @

test/test_gaussian.py

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