Skip to content

Commit 3a51e49

Browse files
committed
Add specific tests
1 parent 64116e9 commit 3a51e49

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

hidimstat/test/test_adaptive_permutation_threshold.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import numpy as np
66
from numpy.testing import assert_almost_equal
7+
from sklearn.svm import SVR
78

89
from hidimstat.ada_svr import ada_svr
910
from hidimstat.scenario import multivariate_1D_simulation
1011
from hidimstat.stat_tools import pval_from_scale
12+
from hidimstat.permutation_test import permutation_test
1113

1214

1315
def test_ada_svr():
@@ -43,3 +45,54 @@ def test_ada_svr():
4345

4446
assert_almost_equal(pval[:support_size], expected[:support_size], decimal=1)
4547
assert_almost_equal(pval_corr[support_size:], expected[support_size:], decimal=1)
48+
49+
50+
def test_ada_svr_rcond():
51+
"""
52+
Testing the effect of rcond
53+
"""
54+
# create dataset
55+
X, y, beta, _ = multivariate_1D_simulation(
56+
n_samples=20,
57+
n_features=50,
58+
support_size=3,
59+
sigma=0.1,
60+
shuffle=False,
61+
seed=42,
62+
)
63+
X[:10] *= 1e-5
64+
beta_hat, scale = ada_svr(X, y)
65+
beta_hat_2, scale_2 = ada_svr(X, y, rcond=1e-15)
66+
assert np.max(np.abs(beta_hat - beta_hat_2)) > 1
67+
assert np.max(np.abs(scale - scale_2)) > 1
68+
69+
70+
def test_ada_svr_vs_permutation():
71+
"""
72+
Validate the adaptive permutation threshold procedure against a permutation
73+
test. The adaptive permutation threshold procedure should good approciation
74+
of the proba of the permutation test.
75+
"""
76+
# create dataset
77+
X, y, beta, _ = multivariate_1D_simulation(
78+
n_samples=10,
79+
n_features=100,
80+
support_size=1,
81+
sigma=0.1,
82+
shuffle=False,
83+
seed=42,
84+
)
85+
beta_hat, scale = ada_svr(X, y)
86+
# fit a SVR to get the coefficients
87+
estimator = SVR(kernel="linear", epsilon=0.0, gamma="scale", C=1.0)
88+
estimator.fit(X, y)
89+
beta_hat_svr = estimator.coef_
90+
91+
# compare that the coefficiants are the same that the one of SVR
92+
assert np.max(np.abs(beta_hat - beta_hat_svr.T[:, 0])) < 2e-4
93+
94+
proba = permutation_test(
95+
X, y, estimator=estimator, n_permutations=10000, n_jobs=8, seed=42, proba=True
96+
)
97+
assert np.max(np.abs(np.mean(proba, axis=0))) < 1e-3
98+
assert np.max(np.abs(scale - np.std(proba, axis=0)) / scale) < 1e-1

0 commit comments

Comments
 (0)