Skip to content

Commit d3e16d4

Browse files
committed
TST: enhance kendall_dis tests with random data and tie/no-tie scenarios
1 parent 844f9cf commit d3e16d4

1 file changed

Lines changed: 77 additions & 22 deletions

File tree

tests/xsf_tests/test_kendall_dis.cpp

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,86 @@
22
#include <xsf/stats.h>
33

44
/*
5-
# Reference values computed with scipy.stats._hypotests._pval_cvm_2samp_exact
6-
5+
# Reference values computed with scipy.stats._kendall_dis
76
import numpy as np
87
from scipy.stats._stats import _kendall_dis
98
10-
assert np.isclose(
11-
0,
12-
_kendall_dis(np.array([1, 2, 3, 4]), np.array([1, 2, 3, 4])),
13-
)
14-
assert np.isclose(
15-
6,
16-
_kendall_dis(np.array([1, 2, 3, 4]), np.array([4, 3, 2, 1])),
17-
)
18-
assert np.isclose(
19-
1,
20-
_kendall_dis(np.array([1, 2, 3, 4]), np.array([1, 3, 2, 4])),
21-
)
9+
rng = np.random.default_rng(123456789)
10+
n = 150
11+
x_no_ties = rng.permutation(n) + 1
12+
y_no_ties = rng.permutation(n) + 1
13+
x_with_ties = rng.integers(1, n + 1, size=n)
14+
y_with_ties = rng.integers(1, n + 1, size=n)
15+
16+
# check "no_ties" arrays have no ties and "with_ties" arrays have ties
17+
assert not len(np.unique(x_no_ties)) < len(x_no_ties)
18+
assert len(np.unique(x_with_ties)) < len(x_with_ties)
19+
assert not len(np.unique(y_no_ties)) < len(y_no_ties)
20+
assert len(np.unique(y_with_ties)) < len(y_with_ties)
21+
22+
for key_x, x in {"no_ties": x_no_ties, "with_ties": x_with_ties}.items():
23+
for key_y, y in {"no_ties": y_no_ties, "with_ties": y_with_ties}.items():
24+
print(f"x {key_x} and y {key_y}: disc = {_kendall_dis(x, y)}")
2225
*/
2326
TEST_CASE("kendall_dis test", "[kendall_dis][xsf_tests]") {
24-
using test_case = std::tuple<std::vector<intptr_t>, std::vector<intptr_t>, int64_t>;
25-
auto [x, y, expected] = GENERATE(
26-
test_case{{1, 2, 3, 4}, {1, 2, 3, 4}, 0}, test_case{{1, 2, 3, 4}, {4, 3, 2, 1}, 6},
27-
test_case{{1, 2, 3, 4}, {1, 3, 2, 4}, 1}
28-
);
29-
const auto result = xsf::kendall_dis(x, y);
30-
CAPTURE(x, y, result, expected);
31-
REQUIRE(result == expected);
27+
std::vector<intptr_t> x_no_ties = {
28+
81, 147, 93, 88, 65, 140, 89, 139, 72, 17, 43, 79, 19, 35, 101, 6, 53, 18, 149, 120, 90, 26,
29+
115, 42, 103, 98, 97, 96, 84, 54, 32, 102, 27, 108, 8, 68, 123, 94, 64, 134, 22, 124, 44, 41,
30+
50, 117, 116, 69, 138, 58, 91, 85, 21, 37, 49, 33, 80, 130, 75, 132, 83, 15, 51, 82, 127, 24,
31+
95, 114, 56, 109, 59, 34, 150, 119, 133, 107, 14, 4, 77, 31, 100, 92, 137, 122, 128, 136, 145, 55,
32+
48, 63, 29, 36, 105, 76, 5, 125, 38, 141, 113, 39, 45, 11, 148, 71, 111, 66, 28, 25, 110, 52,
33+
112, 118, 62, 74, 16, 3, 144, 67, 87, 57, 7, 70, 104, 30, 131, 86, 73, 9, 23, 142, 106, 60,
34+
143, 78, 129, 126, 99, 46, 10, 12, 2, 1, 121, 20, 40, 47, 146, 135, 13, 61
35+
};
36+
std::vector<intptr_t> y_no_ties = {
37+
39, 8, 58, 40, 55, 91, 57, 79, 117, 149, 29, 147, 150, 60, 93, 138, 36, 112, 122, 145, 137, 34,
38+
87, 19, 126, 26, 102, 86, 41, 105, 111, 146, 76, 143, 140, 136, 32, 18, 75, 67, 10, 17, 25, 22,
39+
119, 134, 43, 77, 14, 84, 101, 80, 103, 144, 46, 6, 124, 73, 38, 1, 68, 49, 74, 133, 114, 12,
40+
116, 118, 92, 63, 54, 106, 5, 121, 82, 33, 109, 69, 96, 16, 97, 95, 89, 61, 35, 27, 20, 37,
41+
56, 127, 108, 141, 142, 104, 135, 30, 13, 65, 45, 130, 83, 51, 23, 78, 44, 59, 110, 52, 81, 129,
42+
128, 2, 15, 53, 4, 132, 21, 64, 123, 148, 48, 139, 115, 100, 72, 71, 24, 90, 120, 9, 88, 98,
43+
113, 7, 70, 131, 66, 42, 62, 125, 31, 28, 3, 50, 107, 85, 99, 47, 11, 94
44+
};
45+
std::vector<intptr_t> x_with_ties = {
46+
45, 34, 80, 111, 115, 17, 129, 24, 29, 16, 98, 133, 59, 71, 131, 99, 41, 123, 44, 38, 42, 106,
47+
144, 82, 89, 59, 32, 75, 33, 99, 13, 84, 96, 88, 28, 85, 141, 101, 123, 65, 55, 70, 78, 124,
48+
4, 8, 83, 140, 24, 86, 40, 19, 120, 44, 111, 77, 105, 97, 94, 34, 46, 74, 119, 38, 41, 148,
49+
27, 145, 88, 88, 56, 30, 118, 35, 52, 123, 34, 97, 6, 15, 140, 27, 99, 54, 104, 18, 103, 102,
50+
12, 140, 134, 129, 90, 143, 95, 43, 70, 102, 22, 64, 62, 135, 147, 46, 123, 147, 55, 118, 70, 23,
51+
43, 41, 65, 134, 10, 29, 124, 105, 110, 119, 104, 102, 34, 26, 3, 15, 16, 52, 45, 24, 79, 49,
52+
89, 36, 149, 15, 113, 58, 108, 147, 28, 76, 99, 47, 95, 55, 146, 39, 42, 50
53+
};
54+
std::vector<intptr_t> y_with_ties = {
55+
97, 89, 31, 48, 54, 30, 120, 48, 61, 116, 35, 77, 33, 4, 139, 46, 52, 61, 104, 25, 109, 128,
56+
44, 77, 1, 28, 87, 43, 36, 139, 41, 76, 40, 82, 93, 49, 59, 110, 70, 110, 11, 130, 88, 65,
57+
27, 1, 60, 70, 98, 126, 103, 53, 109, 109, 133, 136, 25, 117, 36, 45, 20, 7, 89, 52, 129, 39,
58+
6, 93, 132, 120, 42, 66, 148, 141, 94, 106, 127, 86, 116, 41, 31, 47, 63, 23, 111, 48, 63, 108,
59+
120, 48, 46, 72, 18, 147, 65, 5, 59, 52, 111, 64, 20, 23, 105, 99, 92, 42, 107, 82, 16, 87,
60+
65, 37, 87, 31, 111, 15, 5, 76, 125, 91, 127, 30, 67, 100, 9, 136, 106, 68, 14, 74, 143, 97,
61+
86, 18, 28, 51, 23, 145, 133, 28, 94, 84, 72, 46, 103, 68, 12, 40, 80, 138
62+
};
63+
64+
// x_no_ties and y_no_ties
65+
const int64_t res_no_no = xsf::kendall_dis(x_no_ties, y_no_ties);
66+
const int64_t expected_no_no = 5943;
67+
CAPTURE(x_no_ties, y_no_ties, res_no_no, expected_no_no);
68+
REQUIRE(res_no_no == expected_no_no);
69+
70+
// x_no_ties and y_with_ties
71+
const int64_t res_no_with = xsf::kendall_dis(x_no_ties, y_with_ties);
72+
const int64_t expected_no_with = 5444;
73+
CAPTURE(x_no_ties, y_with_ties, res_no_with, expected_no_with);
74+
REQUIRE(res_no_with == expected_no_with);
75+
76+
// x_with_ties and y_no_ties
77+
const int64_t res_with_no = xsf::kendall_dis(x_with_ties, y_no_ties);
78+
const int64_t expected_with_no = 5942;
79+
CAPTURE(x_with_ties, y_no_ties, res_with_no, expected_with_no);
80+
REQUIRE(res_with_no == expected_with_no);
81+
82+
// x_with_ties and y_with_ties
83+
const int64_t res_with_with = xsf::kendall_dis(x_with_ties, y_with_ties);
84+
const int64_t expected_with_with = 5443;
85+
CAPTURE(x_with_ties, y_with_ties, res_with_with, expected_with_with);
86+
REQUIRE(res_with_with == expected_with_with);
3287
}

0 commit comments

Comments
 (0)