| 1 |
"""Tests for module dr on Dimensionality Reduction""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import numpy as np |
| 10 |
import ot |
| 11 |
import pytest |
| 12 |
|
| 13 |
try: |
| 14 |
import ot.dr |
| 15 |
|
| 16 |
nogo = False |
| 17 |
except ImportError: |
| 18 |
nogo = True |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") |
| 22 |
def test_fda(): |
| 23 |
n_samples = 90 |
| 24 |
rng = np.random.RandomState(0) |
| 25 |
|
| 26 |
|
| 27 |
xs, ys = ot.datasets.make_data_classif("gaussrot", n_samples, random_state=rng) |
| 28 |
|
| 29 |
n_features_noise = 8 |
| 30 |
|
| 31 |
xs = np.hstack((xs, rng.randn(n_samples, n_features_noise))) |
| 32 |
|
| 33 |
p = 1 |
| 34 |
|
| 35 |
Pfda, projfda = ot.dr.fda(xs, ys, p) |
| 36 |
|
| 37 |
projfda(xs) |
| 38 |
|
| 39 |
np.testing.assert_allclose(np.sum(Pfda**2, 0), np.ones(p)) |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") |
| 43 |
def test_wda(): |
| 44 |
n_samples = 100 |
| 45 |
rng = np.random.RandomState(0) |
| 46 |
|
| 47 |
|
| 48 |
xs, ys = ot.datasets.make_data_classif("gaussrot", n_samples, random_state=rng) |
| 49 |
|
| 50 |
n_features_noise = 8 |
| 51 |
|
| 52 |
xs = np.hstack((xs, rng.randn(n_samples, n_features_noise))) |
| 53 |
|
| 54 |
p = 2 |
| 55 |
|
| 56 |
Pwda, projwda = ot.dr.wda(xs, ys, p, maxiter=10) |
| 57 |
|
| 58 |
projwda(xs) |
| 59 |
|
| 60 |
np.testing.assert_allclose(np.sum(Pwda**2, 0), np.ones(p)) |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") |
| 64 |
def test_wda_low_reg(): |
| 65 |
n_samples = 100 |
| 66 |
rng = np.random.RandomState(0) |
| 67 |
|
| 68 |
|
| 69 |
xs, ys = ot.datasets.make_data_classif("gaussrot", n_samples, random_state=rng) |
| 70 |
|
| 71 |
n_features_noise = 8 |
| 72 |
|
| 73 |
xs = np.hstack((xs, rng.randn(n_samples, n_features_noise))) |
| 74 |
|
| 75 |
p = 2 |
| 76 |
|
| 77 |
Pwda, projwda = ot.dr.wda( |
| 78 |
xs, ys, p, reg=0.01, maxiter=10, sinkhorn_method="sinkhorn_log" |
| 79 |
) |
| 80 |
|
| 81 |
projwda(xs) |
| 82 |
|
| 83 |
np.testing.assert_allclose(np.sum(Pwda**2, 0), np.ones(p)) |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") |
| 87 |
def test_wda_normalized(): |
| 88 |
n_samples = 100 |
| 89 |
rng = np.random.RandomState(0) |
| 90 |
|
| 91 |
|
| 92 |
xs, ys = ot.datasets.make_data_classif("gaussrot", n_samples, random_state=rng) |
| 93 |
|
| 94 |
n_features_noise = 8 |
| 95 |
|
| 96 |
xs = np.hstack((xs, rng.randn(n_samples, n_features_noise))) |
| 97 |
|
| 98 |
p = 2 |
| 99 |
|
| 100 |
P0 = rng.randn(10, p) |
| 101 |
P0 /= P0.sum(0, keepdims=True) |
| 102 |
|
| 103 |
Pwda, projwda = ot.dr.wda(xs, ys, p, maxiter=10, P0=P0, normalize=True) |
| 104 |
|
| 105 |
projwda(xs) |
| 106 |
|
| 107 |
np.testing.assert_allclose(np.sum(Pwda**2, 0), np.ones(p)) |
| 108 |
|
| 109 |
|
| 110 |
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") |
| 111 |
def test_prw(): |
| 112 |
d = 100 |
| 113 |
n = 100 |
| 114 |
k = 3 |
| 115 |
dim = 3 |
| 116 |
|
| 117 |
def fragmented_hypercube(n, d, dim, rng): |
| 118 |
assert dim <= d |
| 119 |
assert dim >= 1 |
| 120 |
assert dim == int(dim) |
| 121 |
|
| 122 |
a = (1.0 / n) * np.ones(n) |
| 123 |
b = (1.0 / n) * np.ones(n) |
| 124 |
|
| 125 |
|
| 126 |
X = rng.uniform(-1, 1, size=(n, d)) |
| 127 |
|
| 128 |
|
| 129 |
tmp_y = rng.uniform(-1, 1, size=(n, d)) |
| 130 |
Y = tmp_y + 2 * np.sign(tmp_y) * np.array(dim * [1] + (d - dim) * [0]) |
| 131 |
return a, b, X, Y |
| 132 |
|
| 133 |
rng = np.random.RandomState(42) |
| 134 |
a, b, X, Y = fragmented_hypercube(n, d, dim, rng) |
| 135 |
|
| 136 |
tau = 0.002 |
| 137 |
reg = 0.2 |
| 138 |
|
| 139 |
pi, U = ot.dr.projection_robust_wasserstein( |
| 140 |
X, Y, a, b, tau, reg=reg, k=k, maxiter=1000, verbose=1 |
| 141 |
) |
| 142 |
|
| 143 |
U0 = rng.randn(d, k) |
| 144 |
U0, _ = np.linalg.qr(U0) |
| 145 |
|
| 146 |
pi, U = ot.dr.projection_robust_wasserstein( |
| 147 |
X, Y, a, b, tau, U0=U0, reg=reg, k=k, maxiter=1000, verbose=1 |
| 148 |
) |
| 149 |
|
| 150 |
|
| 151 |
@pytest.mark.skipif(nogo, reason="Missing modules (autograd or pymanopt)") |
| 152 |
def test_ewca(): |
| 153 |
d = 5 |
| 154 |
n_samples = 50 |
| 155 |
k = 3 |
| 156 |
rng = np.random.RandomState(0) |
| 157 |
|
| 158 |
|
| 159 |
A = rng.normal(size=(d, d)) |
| 160 |
Q, _ = np.linalg.qr(A) |
| 161 |
D = rng.normal(size=d) |
| 162 |
D = (D / np.linalg.norm(D)) ** 4 |
| 163 |
cov = Q @ np.diag(D) @ Q.T |
| 164 |
X = rng.multivariate_normal(np.zeros(d), cov, size=n_samples) |
| 165 |
X = X - X.mean(0, keepdims=True) |
| 166 |
assert X.shape == (n_samples, d) |
| 167 |
|
| 168 |
|
| 169 |
pi, U = ot.dr.ewca( |
| 170 |
X, reg=0.01, method="BCD", k=k, verbose=1, sinkhorn_method="sinkhorn_log" |
| 171 |
) |
| 172 |
assert pi.shape == (n_samples, n_samples) |
| 173 |
assert (pi >= 0).all() |
| 174 |
assert np.allclose(pi.sum(0), 1 / n_samples, atol=1e-3) |
| 175 |
assert np.allclose(pi.sum(1), 1 / n_samples, atol=1e-3) |
| 176 |
assert U.shape == (d, k) |
| 177 |
assert np.allclose(U.T @ U, np.eye(k), atol=1e-3) |
| 178 |
|
| 179 |
|
| 180 |
U_first_eigvec = np.linalg.svd(X.T, full_matrices=False)[0][:, :k] |
| 181 |
_, cos, _ = np.linalg.svd(U.T @ U_first_eigvec, full_matrices=False) |
| 182 |
assert np.allclose(cos, np.ones(k), atol=1e-3) |
| 183 |
|
| 184 |
|
| 185 |
pi, U = ot.dr.ewca( |
| 186 |
X, reg=0.01, method="MM", k=k, verbose=1, sinkhorn_method="sinkhorn_log" |
| 187 |
) |
| 188 |
assert pi.shape == (n_samples, n_samples) |
| 189 |
assert (pi >= 0).all() |
| 190 |
assert np.allclose(pi.sum(0), 1 / n_samples, atol=1e-3) |
| 191 |
assert np.allclose(pi.sum(1), 1 / n_samples, atol=1e-3) |
| 192 |
assert U.shape == (d, k) |
| 193 |
assert np.allclose(U.T @ U, np.eye(k), atol=1e-3) |
| 194 |
|
| 195 |
|
| 196 |
U_first_eigvec = np.linalg.svd(X.T, full_matrices=False)[0][:, :k] |
| 197 |
_, cos, _ = np.linalg.svd(U.T @ U_first_eigvec, full_matrices=False) |
| 198 |
assert np.allclose(cos, np.ones(k), atol=1e-3) |
| 199 |
|
| 200 |
|
| 201 |
pi, U = ot.dr.ewca( |
| 202 |
X, reg=100000, method="MM", k=k, verbose=1, sinkhorn_method="sinkhorn_log" |
| 203 |
) |
| 204 |
|
| 205 |
|
| 206 |
U_last_eigvec = np.linalg.svd(X.T, full_matrices=False)[0][:, -k:] |
| 207 |
_, cos, _ = np.linalg.svd(U.T @ U_last_eigvec, full_matrices=False) |
| 208 |
assert np.allclose(cos, np.ones(k), atol=1e-3) |