|
4 | 4 |
|
5 | 5 | import numpy as np
|
6 | 6 | from numpy.testing import assert_almost_equal
|
| 7 | +from sklearn.svm import SVR |
7 | 8 |
|
8 | 9 | from hidimstat.ada_svr import ada_svr
|
9 | 10 | from hidimstat.scenario import multivariate_1D_simulation
|
10 | 11 | from hidimstat.stat_tools import pval_from_scale
|
| 12 | +from hidimstat.permutation_test import permutation_test |
11 | 13 |
|
12 | 14 |
|
13 | 15 | def test_ada_svr():
|
@@ -43,3 +45,54 @@ def test_ada_svr():
|
43 | 45 |
|
44 | 46 | assert_almost_equal(pval[:support_size], expected[:support_size], decimal=1)
|
45 | 47 | 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