techprotrade commited on
Commit
2c7ccbf
·
verified ·
1 Parent(s): 6f64c75

Add loan-advisor-agent (part 7)

Browse files
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_odds_ratio.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import numpy as np
3
+ from numpy.testing import assert_equal, assert_allclose
4
+ from .._discrete_distns import nchypergeom_fisher, hypergeom
5
+ from scipy.stats._odds_ratio import odds_ratio
6
+ from .data.fisher_exact_results_from_r import data
7
+
8
+
9
+ class TestOddsRatio:
10
+
11
+ @pytest.mark.parametrize('parameters, rresult', data)
12
+ def test_results_from_r(self, parameters, rresult):
13
+ alternative = parameters.alternative.replace('.', '-')
14
+ result = odds_ratio(parameters.table)
15
+ # The results computed by R are not very accurate.
16
+ if result.statistic < 400:
17
+ or_rtol = 5e-4
18
+ ci_rtol = 2e-2
19
+ else:
20
+ or_rtol = 5e-2
21
+ ci_rtol = 1e-1
22
+ assert_allclose(result.statistic,
23
+ rresult.conditional_odds_ratio, rtol=or_rtol)
24
+ ci = result.confidence_interval(parameters.confidence_level,
25
+ alternative)
26
+ assert_allclose((ci.low, ci.high), rresult.conditional_odds_ratio_ci,
27
+ rtol=ci_rtol)
28
+
29
+ # Also do a self-check for the conditional odds ratio.
30
+ # With the computed conditional odds ratio as the noncentrality
31
+ # parameter of the noncentral hypergeometric distribution with
32
+ # parameters table.sum(), table[0].sum(), and table[:,0].sum() as
33
+ # total, ngood and nsample, respectively, the mean of the distribution
34
+ # should equal table[0, 0].
35
+ cor = result.statistic
36
+ table = np.array(parameters.table)
37
+ total = table.sum()
38
+ ngood = table[0].sum()
39
+ nsample = table[:, 0].sum()
40
+ # nchypergeom_fisher does not allow the edge cases where the
41
+ # noncentrality parameter is 0 or inf, so handle those values
42
+ # separately here.
43
+ if cor == 0:
44
+ nchg_mean = hypergeom.support(total, ngood, nsample)[0]
45
+ elif cor == np.inf:
46
+ nchg_mean = hypergeom.support(total, ngood, nsample)[1]
47
+ else:
48
+ nchg_mean = nchypergeom_fisher.mean(total, ngood, nsample, cor)
49
+ assert_allclose(nchg_mean, table[0, 0], rtol=1e-13)
50
+
51
+ # Check that the confidence interval is correct.
52
+ alpha = 1 - parameters.confidence_level
53
+ if alternative == 'two-sided':
54
+ if ci.low > 0:
55
+ sf = nchypergeom_fisher.sf(table[0, 0] - 1,
56
+ total, ngood, nsample, ci.low)
57
+ assert_allclose(sf, alpha/2, rtol=1e-11)
58
+ if np.isfinite(ci.high):
59
+ cdf = nchypergeom_fisher.cdf(table[0, 0],
60
+ total, ngood, nsample, ci.high)
61
+ assert_allclose(cdf, alpha/2, rtol=1e-11)
62
+ elif alternative == 'less':
63
+ if np.isfinite(ci.high):
64
+ cdf = nchypergeom_fisher.cdf(table[0, 0],
65
+ total, ngood, nsample, ci.high)
66
+ assert_allclose(cdf, alpha, rtol=1e-11)
67
+ else:
68
+ # alternative == 'greater'
69
+ if ci.low > 0:
70
+ sf = nchypergeom_fisher.sf(table[0, 0] - 1,
71
+ total, ngood, nsample, ci.low)
72
+ assert_allclose(sf, alpha, rtol=1e-11)
73
+
74
+ @pytest.mark.parametrize('table', [
75
+ [[0, 0], [5, 10]],
76
+ [[5, 10], [0, 0]],
77
+ [[0, 5], [0, 10]],
78
+ [[5, 0], [10, 0]],
79
+ ])
80
+ def test_row_or_col_zero(self, table):
81
+ result = odds_ratio(table)
82
+ assert_equal(result.statistic, np.nan)
83
+ ci = result.confidence_interval()
84
+ assert_equal((ci.low, ci.high), (0, np.inf))
85
+
86
+ @pytest.mark.parametrize("case",
87
+ [[0.95, 'two-sided', 0.4879913, 2.635883],
88
+ [0.90, 'two-sided', 0.5588516, 2.301663]])
89
+ def test_sample_odds_ratio_ci(self, case):
90
+ # Compare the sample odds ratio confidence interval to the R function
91
+ # oddsratio.wald from the epitools package, e.g.
92
+ # > library(epitools)
93
+ # > table = matrix(c(10, 20, 41, 93), nrow=2, ncol=2, byrow=TRUE)
94
+ # > result = oddsratio.wald(table)
95
+ # > result$measure
96
+ # odds ratio with 95% C.I.
97
+ # Predictor estimate lower upper
98
+ # Exposed1 1.000000 NA NA
99
+ # Exposed2 1.134146 0.4879913 2.635883
100
+
101
+ confidence_level, alternative, ref_low, ref_high = case
102
+ table = [[10, 20], [41, 93]]
103
+ result = odds_ratio(table, kind='sample')
104
+ assert_allclose(result.statistic, 1.134146, rtol=1e-6)
105
+ ci = result.confidence_interval(confidence_level, alternative)
106
+ assert_allclose([ci.low, ci.high], [ref_low, ref_high], rtol=1e-6)
107
+
108
+ @pytest.mark.slow
109
+ @pytest.mark.parametrize('alternative', ['less', 'greater', 'two-sided'])
110
+ def test_sample_odds_ratio_one_sided_ci(self, alternative):
111
+ # can't find a good reference for one-sided CI, so bump up the sample
112
+ # size and compare against the conditional odds ratio CI
113
+ table = [[1000, 2000], [4100, 9300]]
114
+ res = odds_ratio(table, kind='sample')
115
+ ref = odds_ratio(table, kind='conditional')
116
+ assert_allclose(res.statistic, ref.statistic, atol=1e-5)
117
+ assert_allclose(res.confidence_interval(alternative=alternative),
118
+ ref.confidence_interval(alternative=alternative),
119
+ atol=2e-3)
120
+
121
+ @pytest.mark.parametrize('kind', ['sample', 'conditional'])
122
+ @pytest.mark.parametrize('bad_table', [123, "foo", [10, 11, 12]])
123
+ def test_invalid_table_shape(self, kind, bad_table):
124
+ with pytest.raises(ValueError, match="Invalid shape"):
125
+ odds_ratio(bad_table, kind=kind)
126
+
127
+ def test_invalid_table_type(self):
128
+ with pytest.raises(ValueError, match='must be an array of integers'):
129
+ odds_ratio([[1.0, 3.4], [5.0, 9.9]])
130
+
131
+ def test_negative_table_values(self):
132
+ with pytest.raises(ValueError, match='must be nonnegative'):
133
+ odds_ratio([[1, 2], [3, -4]])
134
+
135
+ def test_invalid_kind(self):
136
+ with pytest.raises(ValueError, match='`kind` must be'):
137
+ odds_ratio([[10, 20], [30, 14]], kind='magnetoreluctance')
138
+
139
+ def test_invalid_alternative(self):
140
+ result = odds_ratio([[5, 10], [2, 32]])
141
+ with pytest.raises(ValueError, match='`alternative` must be'):
142
+ result.confidence_interval(alternative='depleneration')
143
+
144
+ @pytest.mark.parametrize('level', [-0.5, 1.5])
145
+ def test_invalid_confidence_level(self, level):
146
+ result = odds_ratio([[5, 10], [2, 32]])
147
+ with pytest.raises(ValueError, match='must be between 0 and 1'):
148
+ result.confidence_interval(confidence_level=level)
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_qmc.py ADDED
@@ -0,0 +1,1534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from collections import Counter
3
+ from itertools import combinations, product
4
+
5
+ import pytest
6
+ import numpy as np
7
+ from numpy.testing import (assert_allclose, assert_equal, assert_array_equal,
8
+ assert_array_less)
9
+
10
+ from scipy.spatial import distance
11
+ from scipy.stats import shapiro
12
+ from scipy.stats._sobol import _test_find_index, _test_low_0_bit
13
+ from scipy.stats import qmc
14
+ from scipy.stats._qmc import (
15
+ van_der_corput, n_primes, primes_from_2_to,
16
+ update_discrepancy, QMCEngine, _l1_norm,
17
+ _perturb_discrepancy, _lloyd_centroidal_voronoi_tessellation
18
+ )
19
+
20
+
21
+ class TestUtils:
22
+ def test_scale(self):
23
+ # 1d scalar
24
+ space = [[0], [1], [0.5]]
25
+ out = [[-2], [6], [2]]
26
+ scaled_space = qmc.scale(space, l_bounds=-2, u_bounds=6)
27
+
28
+ assert_allclose(scaled_space, out)
29
+
30
+ # 2d space
31
+ space = [[0, 0], [1, 1], [0.5, 0.5]]
32
+ bounds = np.array([[-2, 0], [6, 5]])
33
+ out = [[-2, 0], [6, 5], [2, 2.5]]
34
+
35
+ scaled_space = qmc.scale(space, l_bounds=bounds[0], u_bounds=bounds[1])
36
+
37
+ assert_allclose(scaled_space, out)
38
+
39
+ scaled_back_space = qmc.scale(scaled_space, l_bounds=bounds[0],
40
+ u_bounds=bounds[1], reverse=True)
41
+ assert_allclose(scaled_back_space, space)
42
+
43
+ # broadcast
44
+ space = [[0, 0, 0], [1, 1, 1], [0.5, 0.5, 0.5]]
45
+ l_bounds, u_bounds = 0, [6, 5, 3]
46
+ out = [[0, 0, 0], [6, 5, 3], [3, 2.5, 1.5]]
47
+
48
+ scaled_space = qmc.scale(space, l_bounds=l_bounds, u_bounds=u_bounds)
49
+
50
+ assert_allclose(scaled_space, out)
51
+
52
+ def test_scale_random(self):
53
+ rng = np.random.default_rng(317589836511269190194010915937762468165)
54
+ sample = rng.random((30, 10))
55
+ a = -rng.random(10) * 10
56
+ b = rng.random(10) * 10
57
+ scaled = qmc.scale(sample, a, b, reverse=False)
58
+ unscaled = qmc.scale(scaled, a, b, reverse=True)
59
+ assert_allclose(unscaled, sample)
60
+
61
+ def test_scale_errors(self):
62
+ with pytest.raises(ValueError, match=r"Sample is not a 2D array"):
63
+ space = [0, 1, 0.5]
64
+ qmc.scale(space, l_bounds=-2, u_bounds=6)
65
+
66
+ with pytest.raises(ValueError, match=r"Bounds are not consistent"):
67
+ space = [[0, 0], [1, 1], [0.5, 0.5]]
68
+ bounds = np.array([[-2, 6], [6, 5]])
69
+ qmc.scale(space, l_bounds=bounds[0], u_bounds=bounds[1])
70
+
71
+ with pytest.raises(ValueError, match=r"'l_bounds' and 'u_bounds'"
72
+ r" must be broadcastable"):
73
+ space = [[0, 0], [1, 1], [0.5, 0.5]]
74
+ l_bounds, u_bounds = [-2, 0, 2], [6, 5]
75
+ qmc.scale(space, l_bounds=l_bounds, u_bounds=u_bounds)
76
+
77
+ with pytest.raises(ValueError, match=r"'l_bounds' and 'u_bounds'"
78
+ r" must be broadcastable"):
79
+ space = [[0, 0], [1, 1], [0.5, 0.5]]
80
+ bounds = np.array([[-2, 0, 2], [6, 5, 5]])
81
+ qmc.scale(space, l_bounds=bounds[0], u_bounds=bounds[1])
82
+
83
+ with pytest.raises(ValueError, match=r"Sample is not in unit "
84
+ r"hypercube"):
85
+ space = [[0, 0], [1, 1.5], [0.5, 0.5]]
86
+ bounds = np.array([[-2, 0], [6, 5]])
87
+ qmc.scale(space, l_bounds=bounds[0], u_bounds=bounds[1])
88
+
89
+ with pytest.raises(ValueError, match=r"Sample is out of bounds"):
90
+ out = [[-2, 0], [6, 5], [8, 2.5]]
91
+ bounds = np.array([[-2, 0], [6, 5]])
92
+ qmc.scale(out, l_bounds=bounds[0], u_bounds=bounds[1],
93
+ reverse=True)
94
+
95
+ def test_discrepancy(self):
96
+ space_1 = np.array([[1, 3], [2, 6], [3, 2], [4, 5], [5, 1], [6, 4]])
97
+ space_1 = (2.0 * space_1 - 1.0) / (2.0 * 6.0)
98
+ space_2 = np.array([[1, 5], [2, 4], [3, 3], [4, 2], [5, 1], [6, 6]])
99
+ space_2 = (2.0 * space_2 - 1.0) / (2.0 * 6.0)
100
+
101
+ # From Fang et al. Design and modeling for computer experiments, 2006
102
+ assert_allclose(qmc.discrepancy(space_1), 0.0081, atol=1e-4)
103
+ assert_allclose(qmc.discrepancy(space_2), 0.0105, atol=1e-4)
104
+
105
+ # From Zhou Y.-D. et al. Mixture discrepancy for quasi-random point
106
+ # sets. Journal of Complexity, 29 (3-4), pp. 283-301, 2013.
107
+ # Example 4 on Page 298
108
+ sample = np.array([[2, 1, 1, 2, 2, 2],
109
+ [1, 2, 2, 2, 2, 2],
110
+ [2, 1, 1, 1, 1, 1],
111
+ [1, 1, 1, 1, 2, 2],
112
+ [1, 2, 2, 2, 1, 1],
113
+ [2, 2, 2, 2, 1, 1],
114
+ [2, 2, 2, 1, 2, 2]])
115
+ sample = (2.0 * sample - 1.0) / (2.0 * 2.0)
116
+
117
+ assert_allclose(qmc.discrepancy(sample, method='MD'), 2.5000,
118
+ atol=1e-4)
119
+ assert_allclose(qmc.discrepancy(sample, method='WD'), 1.3680,
120
+ atol=1e-4)
121
+ assert_allclose(qmc.discrepancy(sample, method='CD'), 0.3172,
122
+ atol=1e-4)
123
+
124
+ # From Tim P. et al. Minimizing the L2 and Linf star discrepancies
125
+ # of a single point in the unit hypercube. JCAM, 2005
126
+ # Table 1 on Page 283
127
+ for dim in [2, 4, 8, 16, 32, 64]:
128
+ ref = np.sqrt(3**(-dim))
129
+ assert_allclose(qmc.discrepancy(np.array([[1]*dim]),
130
+ method='L2-star'), ref)
131
+
132
+ def test_discrepancy_errors(self):
133
+ sample = np.array([[1, 3], [2, 6], [3, 2], [4, 5], [5, 1], [6, 4]])
134
+
135
+ with pytest.raises(
136
+ ValueError, match=r"Sample is not in unit hypercube"
137
+ ):
138
+ qmc.discrepancy(sample)
139
+
140
+ with pytest.raises(ValueError, match=r"Sample is not a 2D array"):
141
+ qmc.discrepancy([1, 3])
142
+
143
+ sample = [[0, 0], [1, 1], [0.5, 0.5]]
144
+ with pytest.raises(ValueError, match=r"'toto' is not a valid ..."):
145
+ qmc.discrepancy(sample, method="toto")
146
+
147
+ def test_discrepancy_parallel(self, monkeypatch):
148
+ sample = np.array([[2, 1, 1, 2, 2, 2],
149
+ [1, 2, 2, 2, 2, 2],
150
+ [2, 1, 1, 1, 1, 1],
151
+ [1, 1, 1, 1, 2, 2],
152
+ [1, 2, 2, 2, 1, 1],
153
+ [2, 2, 2, 2, 1, 1],
154
+ [2, 2, 2, 1, 2, 2]])
155
+ sample = (2.0 * sample - 1.0) / (2.0 * 2.0)
156
+
157
+ assert_allclose(qmc.discrepancy(sample, method='MD', workers=8),
158
+ 2.5000,
159
+ atol=1e-4)
160
+ assert_allclose(qmc.discrepancy(sample, method='WD', workers=8),
161
+ 1.3680,
162
+ atol=1e-4)
163
+ assert_allclose(qmc.discrepancy(sample, method='CD', workers=8),
164
+ 0.3172,
165
+ atol=1e-4)
166
+
167
+ # From Tim P. et al. Minimizing the L2 and Linf star discrepancies
168
+ # of a single point in the unit hypercube. JCAM, 2005
169
+ # Table 1 on Page 283
170
+ for dim in [2, 4, 8, 16, 32, 64]:
171
+ ref = np.sqrt(3 ** (-dim))
172
+ assert_allclose(qmc.discrepancy(np.array([[1] * dim]),
173
+ method='L2-star', workers=-1), ref)
174
+
175
+ monkeypatch.setattr(os, 'cpu_count', lambda: None)
176
+ with pytest.raises(NotImplementedError, match="Cannot determine the"):
177
+ qmc.discrepancy(sample, workers=-1)
178
+
179
+ with pytest.raises(ValueError, match="Invalid number of workers..."):
180
+ qmc.discrepancy(sample, workers=-2)
181
+
182
+ def test_geometric_discrepancy_errors(self):
183
+ sample = np.array([[1, 3], [2, 6], [3, 2], [4, 5], [5, 1], [6, 4]])
184
+
185
+ with pytest.raises(ValueError, match=r"Sample is not in unit hypercube"):
186
+ qmc.geometric_discrepancy(sample)
187
+
188
+ with pytest.raises(ValueError, match=r"Sample is not a 2D array"):
189
+ qmc.geometric_discrepancy([1, 3])
190
+
191
+ sample = [[0, 0], [1, 1], [0.5, 0.5]]
192
+ with pytest.raises(ValueError, match=r"'toto' is not a valid ..."):
193
+ qmc.geometric_discrepancy(sample, method="toto")
194
+
195
+ sample = np.array([[0, 0], [0, 0], [0, 1]])
196
+ with pytest.warns(UserWarning, match="Sample contains duplicate points."):
197
+ qmc.geometric_discrepancy(sample)
198
+
199
+ sample = np.array([[0.5, 0.5]])
200
+ with pytest.raises(ValueError, match="Sample must contain at least two points"):
201
+ qmc.geometric_discrepancy(sample)
202
+
203
+ def test_geometric_discrepancy(self):
204
+ sample = np.array([[0, 0], [1, 1]])
205
+ assert_allclose(qmc.geometric_discrepancy(sample), np.sqrt(2))
206
+ assert_allclose(qmc.geometric_discrepancy(sample, method="mst"), np.sqrt(2))
207
+
208
+ sample = np.array([[0, 0], [0, 1], [0.5, 1]])
209
+ assert_allclose(qmc.geometric_discrepancy(sample), 0.5)
210
+ assert_allclose(qmc.geometric_discrepancy(sample, method="mst"), 0.75)
211
+
212
+ sample = np.array([[0, 0], [0.25, 0.25], [1, 1]])
213
+ assert_allclose(qmc.geometric_discrepancy(sample), np.sqrt(2) / 4)
214
+ assert_allclose(qmc.geometric_discrepancy(sample, method="mst"), np.sqrt(2) / 2)
215
+ assert_allclose(qmc.geometric_discrepancy(sample, metric="chebyshev"), 0.25)
216
+ assert_allclose(
217
+ qmc.geometric_discrepancy(sample, method="mst", metric="chebyshev"), 0.5
218
+ )
219
+
220
+ rng = np.random.default_rng(191468432622931918890291693003068437394)
221
+ sample = qmc.LatinHypercube(d=3, rng=rng).random(50)
222
+ assert_allclose(qmc.geometric_discrepancy(sample), 0.05106012076093356)
223
+ assert_allclose(
224
+ qmc.geometric_discrepancy(sample, method='mst'), 0.19704396643366182
225
+ )
226
+
227
+ @pytest.mark.xfail(
228
+ reason="minimum_spanning_tree ignores zero distances (#18892)",
229
+ strict=True,
230
+ )
231
+ @pytest.mark.thread_unsafe
232
+ def test_geometric_discrepancy_mst_with_zero_distances(self):
233
+ sample = np.array([[0, 0], [0, 0], [0, 1]])
234
+ assert_allclose(qmc.geometric_discrepancy(sample, method='mst'), 0.5)
235
+
236
+ def test_update_discrepancy(self):
237
+ # From Fang et al. Design and modeling for computer experiments, 2006
238
+ space_1 = np.array([[1, 3], [2, 6], [3, 2], [4, 5], [5, 1], [6, 4]])
239
+ space_1 = (2.0 * space_1 - 1.0) / (2.0 * 6.0)
240
+
241
+ disc_init = qmc.discrepancy(space_1[:-1], iterative=True)
242
+ disc_iter = update_discrepancy(space_1[-1], space_1[:-1], disc_init)
243
+
244
+ assert_allclose(disc_iter, 0.0081, atol=1e-4)
245
+
246
+ # n<d
247
+ rng = np.random.default_rng(241557431858162136881731220526394276199)
248
+ space_1 = rng.random((4, 10))
249
+
250
+ disc_ref = qmc.discrepancy(space_1)
251
+ disc_init = qmc.discrepancy(space_1[:-1], iterative=True)
252
+ disc_iter = update_discrepancy(space_1[-1], space_1[:-1], disc_init)
253
+
254
+ assert_allclose(disc_iter, disc_ref, atol=1e-4)
255
+
256
+ # errors
257
+ with pytest.raises(ValueError, match=r"Sample is not in unit "
258
+ r"hypercube"):
259
+ update_discrepancy(space_1[-1], space_1[:-1] + 1, disc_init)
260
+
261
+ with pytest.raises(ValueError, match=r"Sample is not a 2D array"):
262
+ update_discrepancy(space_1[-1], space_1[0], disc_init)
263
+
264
+ x_new = [1, 3]
265
+ with pytest.raises(ValueError, match=r"x_new is not in unit "
266
+ r"hypercube"):
267
+ update_discrepancy(x_new, space_1[:-1], disc_init)
268
+
269
+ x_new = [[0.5, 0.5]]
270
+ with pytest.raises(ValueError, match=r"x_new is not a 1D array"):
271
+ update_discrepancy(x_new, space_1[:-1], disc_init)
272
+
273
+ x_new = [0.3, 0.1, 0]
274
+ with pytest.raises(ValueError, match=r"x_new and sample must be "
275
+ r"broadcastable"):
276
+ update_discrepancy(x_new, space_1[:-1], disc_init)
277
+
278
+ def test_perm_discrepancy(self):
279
+ rng = np.random.default_rng(46449423132557934943847369749645759997)
280
+ qmc_gen = qmc.LatinHypercube(5, rng=rng)
281
+ sample = qmc_gen.random(10)
282
+ disc = qmc.discrepancy(sample)
283
+
284
+ for i in range(100):
285
+ row_1 = rng.integers(10)
286
+ row_2 = rng.integers(10)
287
+ col = rng.integers(5)
288
+
289
+ disc = _perturb_discrepancy(sample, row_1, row_2, col, disc)
290
+ sample[row_1, col], sample[row_2, col] = (
291
+ sample[row_2, col], sample[row_1, col])
292
+ disc_reference = qmc.discrepancy(sample)
293
+ assert_allclose(disc, disc_reference)
294
+
295
+ def test_discrepancy_alternative_implementation(self):
296
+ """Alternative definitions from Matt Haberland."""
297
+
298
+ def disc_c2(x):
299
+ n, s = x.shape
300
+ xij = x
301
+ disc1 = np.sum(np.prod((1
302
+ + 1/2*np.abs(xij-0.5)
303
+ - 1/2*np.abs(xij-0.5)**2), axis=1))
304
+ xij = x[None, :, :]
305
+ xkj = x[:, None, :]
306
+ disc2 = np.sum(np.sum(np.prod(1
307
+ + 1/2*np.abs(xij - 0.5)
308
+ + 1/2*np.abs(xkj - 0.5)
309
+ - 1/2*np.abs(xij - xkj), axis=2),
310
+ axis=0))
311
+ return (13/12)**s - 2/n * disc1 + 1/n**2*disc2
312
+
313
+ def disc_wd(x):
314
+ n, s = x.shape
315
+ xij = x[None, :, :]
316
+ xkj = x[:, None, :]
317
+ disc = np.sum(np.sum(np.prod(3/2
318
+ - np.abs(xij - xkj)
319
+ + np.abs(xij - xkj)**2, axis=2),
320
+ axis=0))
321
+ return -(4/3)**s + 1/n**2 * disc
322
+
323
+ def disc_md(x):
324
+ n, s = x.shape
325
+ xij = x
326
+ disc1 = np.sum(np.prod((5/3
327
+ - 1/4*np.abs(xij-0.5)
328
+ - 1/4*np.abs(xij-0.5)**2), axis=1))
329
+ xij = x[None, :, :]
330
+ xkj = x[:, None, :]
331
+ disc2 = np.sum(np.sum(np.prod(15/8
332
+ - 1/4*np.abs(xij - 0.5)
333
+ - 1/4*np.abs(xkj - 0.5)
334
+ - 3/4*np.abs(xij - xkj)
335
+ + 1/2*np.abs(xij - xkj)**2,
336
+ axis=2), axis=0))
337
+ return (19/12)**s - 2/n * disc1 + 1/n**2*disc2
338
+
339
+ def disc_star_l2(x):
340
+ n, s = x.shape
341
+ return np.sqrt(
342
+ 3 ** (-s) - 2 ** (1 - s) / n
343
+ * np.sum(np.prod(1 - x ** 2, axis=1))
344
+ + np.sum([
345
+ np.prod(1 - np.maximum(x[k, :], x[j, :]))
346
+ for k in range(n) for j in range(n)
347
+ ]) / n ** 2
348
+ )
349
+
350
+ rng = np.random.default_rng(117065081482921065782761407107747179201)
351
+ sample = rng.random((30, 10))
352
+
353
+ disc_curr = qmc.discrepancy(sample, method='CD')
354
+ disc_alt = disc_c2(sample)
355
+ assert_allclose(disc_curr, disc_alt)
356
+
357
+ disc_curr = qmc.discrepancy(sample, method='WD')
358
+ disc_alt = disc_wd(sample)
359
+ assert_allclose(disc_curr, disc_alt)
360
+
361
+ disc_curr = qmc.discrepancy(sample, method='MD')
362
+ disc_alt = disc_md(sample)
363
+ assert_allclose(disc_curr, disc_alt)
364
+
365
+ disc_curr = qmc.discrepancy(sample, method='L2-star')
366
+ disc_alt = disc_star_l2(sample)
367
+ assert_allclose(disc_curr, disc_alt)
368
+
369
+ def test_n_primes(self):
370
+ primes = n_primes(10)
371
+ assert primes[-1] == 29
372
+
373
+ primes = n_primes(168)
374
+ assert primes[-1] == 997
375
+
376
+ primes = n_primes(350)
377
+ assert primes[-1] == 2357
378
+
379
+ def test_primes(self):
380
+ primes = primes_from_2_to(50)
381
+ out = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
382
+ assert_allclose(primes, out)
383
+
384
+
385
+ class TestVDC:
386
+ def test_van_der_corput(self):
387
+ sample = van_der_corput(10)
388
+ out = [0.0, 0.5, 0.25, 0.75, 0.125, 0.625,
389
+ 0.375, 0.875, 0.0625, 0.5625]
390
+ assert_allclose(sample, out)
391
+
392
+ sample = van_der_corput(10, workers=4)
393
+ assert_allclose(sample, out)
394
+
395
+ sample = van_der_corput(10, workers=8)
396
+ assert_allclose(sample, out)
397
+
398
+ sample = van_der_corput(7, start_index=3)
399
+ assert_allclose(sample, out[3:])
400
+
401
+ def test_van_der_corput_scramble(self):
402
+ rng = 338213789010180879520345496831675783177
403
+ out = van_der_corput(10, scramble=True, rng=rng)
404
+
405
+ sample = van_der_corput(7, start_index=3, scramble=True, rng=rng)
406
+ assert_allclose(sample, out[3:])
407
+
408
+ sample = van_der_corput(
409
+ 7, start_index=3, scramble=True, rng=rng, workers=4
410
+ )
411
+ assert_allclose(sample, out[3:])
412
+
413
+ sample = van_der_corput(
414
+ 7, start_index=3, scramble=True, rng=rng, workers=8
415
+ )
416
+ assert_allclose(sample, out[3:])
417
+
418
+ def test_invalid_base_error(self):
419
+ with pytest.raises(ValueError, match=r"'base' must be at least 2"):
420
+ van_der_corput(10, base=1)
421
+
422
+
423
+ class RandomEngine(qmc.QMCEngine):
424
+ def __init__(self, d, optimization=None, rng=None):
425
+ super().__init__(d=d, optimization=optimization, rng=rng)
426
+
427
+ def _random(self, n=1, *, workers=1):
428
+ sample = self.rng.random((n, self.d))
429
+ return sample
430
+
431
+
432
+ def test_subclassing_QMCEngine():
433
+ engine = RandomEngine(2, rng=175180605424926556207367152557812293274)
434
+
435
+ sample_1 = engine.random(n=5)
436
+ sample_2 = engine.random(n=7)
437
+ assert engine.num_generated == 12
438
+
439
+ # reset and re-sample
440
+ engine.reset()
441
+ assert engine.num_generated == 0
442
+
443
+ sample_1_test = engine.random(n=5)
444
+ assert_equal(sample_1, sample_1_test)
445
+
446
+ # repeat reset and fast forward
447
+ engine.reset()
448
+ engine.fast_forward(n=5)
449
+ sample_2_test = engine.random(n=7)
450
+
451
+ assert_equal(sample_2, sample_2_test)
452
+ assert engine.num_generated == 12
453
+
454
+
455
+ def test_raises():
456
+ # input validation
457
+ with pytest.raises(ValueError, match=r"d must be a non-negative integer"):
458
+ RandomEngine((2,))
459
+
460
+ with pytest.raises(ValueError, match=r"d must be a non-negative integer"):
461
+ RandomEngine(-1)
462
+
463
+ msg = r"'u_bounds' and 'l_bounds' must be integers"
464
+ with pytest.raises(ValueError, match=msg):
465
+ engine = RandomEngine(1)
466
+ engine.integers(l_bounds=1, u_bounds=1.1)
467
+
468
+
469
+ def test_integers():
470
+ engine = RandomEngine(1, rng=231195739755290648063853336582377368684)
471
+
472
+ # basic tests
473
+ sample = engine.integers(1, n=10)
474
+ assert_equal(np.unique(sample), [0])
475
+
476
+ assert sample.dtype == np.dtype('int64')
477
+
478
+ sample = engine.integers(1, n=10, endpoint=True)
479
+ assert_equal(np.unique(sample), [0, 1])
480
+
481
+ low = -5
482
+ high = 7
483
+
484
+ # scaling logic
485
+ engine.reset()
486
+ ref_sample = engine.random(20)
487
+ ref_sample = ref_sample * (high - low) + low
488
+ ref_sample = np.floor(ref_sample).astype(np.int64)
489
+
490
+ engine.reset()
491
+ sample = engine.integers(low, u_bounds=high, n=20, endpoint=False)
492
+
493
+ assert_equal(sample, ref_sample)
494
+
495
+ # up to bounds, no less, no more
496
+ sample = engine.integers(low, u_bounds=high, n=100, endpoint=False)
497
+ assert_equal((sample.min(), sample.max()), (low, high-1))
498
+
499
+ sample = engine.integers(low, u_bounds=high, n=100, endpoint=True)
500
+ assert_equal((sample.min(), sample.max()), (low, high))
501
+
502
+
503
+ def test_integers_nd():
504
+ d = 10
505
+ rng = np.random.default_rng(3716505122102428560615700415287450951)
506
+ low = rng.integers(low=-5, high=-1, size=d)
507
+ high = rng.integers(low=1, high=5, size=d, endpoint=True)
508
+ engine = RandomEngine(d, rng=rng)
509
+
510
+ sample = engine.integers(low, u_bounds=high, n=100, endpoint=False)
511
+ assert_equal(sample.min(axis=0), low)
512
+ assert_equal(sample.max(axis=0), high-1)
513
+
514
+ sample = engine.integers(low, u_bounds=high, n=100, endpoint=True)
515
+ assert_equal(sample.min(axis=0), low)
516
+ assert_equal(sample.max(axis=0), high)
517
+
518
+
519
+ class QMCEngineTests:
520
+ """Generic tests for QMC engines."""
521
+ qmce = NotImplemented
522
+ can_scramble = NotImplemented
523
+ unscramble_nd = NotImplemented
524
+ scramble_nd = NotImplemented
525
+
526
+ scramble = [True, False]
527
+ ids = ["Scrambled", "Unscrambled"]
528
+
529
+ def engine(
530
+ self, scramble: bool,
531
+ rng=170382760648021597650530316304495310428,
532
+ **kwargs
533
+ ) -> QMCEngine:
534
+ # preserve use of `seed` during SPEC 7 transition because
535
+ # some tests rely on behavior with integer `seed` (which is
536
+ # different from behavior with integer `rng`)
537
+ if self.can_scramble:
538
+ return self.qmce(scramble=scramble, seed=rng, **kwargs)
539
+ else:
540
+ if scramble:
541
+ pytest.skip()
542
+ else:
543
+ return self.qmce(seed=rng, **kwargs)
544
+
545
+ def reference(self, scramble: bool) -> np.ndarray:
546
+ return self.scramble_nd if scramble else self.unscramble_nd
547
+
548
+ @pytest.mark.parametrize("scramble", scramble, ids=ids)
549
+ def test_0dim(self, scramble):
550
+ engine = self.engine(d=0, scramble=scramble)
551
+ sample = engine.random(4)
552
+ assert_array_equal(np.empty((4, 0)), sample)
553
+
554
+ @pytest.mark.parametrize("scramble", scramble, ids=ids)
555
+ def test_0sample(self, scramble):
556
+ engine = self.engine(d=2, scramble=scramble)
557
+ sample = engine.random(0)
558
+ assert_array_equal(np.empty((0, 2)), sample)
559
+
560
+ @pytest.mark.parametrize("scramble", scramble, ids=ids)
561
+ def test_1sample(self, scramble):
562
+ engine = self.engine(d=2, scramble=scramble)
563
+ sample = engine.random(1)
564
+ assert (1, 2) == sample.shape
565
+
566
+ @pytest.mark.parametrize("scramble", scramble, ids=ids)
567
+ def test_bounds(self, scramble):
568
+ engine = self.engine(d=100, scramble=scramble)
569
+ sample = engine.random(512)
570
+ assert np.all(sample >= 0)
571
+ assert np.all(sample <= 1)
572
+
573
+ @pytest.mark.parametrize("scramble", scramble, ids=ids)
574
+ def test_sample(self, scramble):
575
+ ref_sample = self.reference(scramble=scramble)
576
+ engine = self.engine(d=2, scramble=scramble)
577
+ sample = engine.random(n=len(ref_sample))
578
+
579
+ assert_allclose(sample, ref_sample, atol=1e-1)
580
+ assert engine.num_generated == len(ref_sample)
581
+
582
+ @pytest.mark.parametrize("scramble", scramble, ids=ids)
583
+ def test_continuing(self, scramble):
584
+ engine = self.engine(d=2, scramble=scramble)
585
+ ref_sample = engine.random(n=8)
586
+
587
+ engine = self.engine(d=2, scramble=scramble)
588
+
589
+ n_half = len(ref_sample) // 2
590
+
591
+ _ = engine.random(n=n_half)
592
+ sample = engine.random(n=n_half)
593
+ assert_allclose(sample, ref_sample[n_half:], atol=1e-1)
594
+
595
+ @pytest.mark.parametrize("scramble", scramble, ids=ids)
596
+ @pytest.mark.parametrize(
597
+ "rng",
598
+ (
599
+ 170382760648021597650530316304495310428,
600
+ lambda: np.random.default_rng(170382760648021597650530316304495310428),
601
+ pytest.param(None, marks=pytest.mark.thread_unsafe),
602
+ ),
603
+ )
604
+ def test_reset(self, scramble, rng):
605
+ if callable(rng):
606
+ # Initialize local RNG here to make it thread-local in pytest-run-parallel
607
+ rng = rng()
608
+ engine = self.engine(d=2, scramble=scramble, rng=rng)
609
+ ref_sample = engine.random(n=8)
610
+
611
+ engine.reset()
612
+ assert engine.num_generated == 0
613
+
614
+ sample = engine.random(n=8)
615
+ assert_allclose(sample, ref_sample)
616
+
617
+ @pytest.mark.parametrize("scramble", scramble, ids=ids)
618
+ def test_fast_forward(self, scramble):
619
+ engine = self.engine(d=2, scramble=scramble)
620
+ ref_sample = engine.random(n=8)
621
+
622
+ engine = self.engine(d=2, scramble=scramble)
623
+
624
+ engine.fast_forward(4)
625
+ sample = engine.random(n=4)
626
+
627
+ assert_allclose(sample, ref_sample[4:], atol=1e-1)
628
+
629
+ # alternate fast forwarding with sampling
630
+ engine.reset()
631
+ even_draws = []
632
+ for i in range(8):
633
+ if i % 2 == 0:
634
+ even_draws.append(engine.random())
635
+ else:
636
+ engine.fast_forward(1)
637
+ assert_allclose(
638
+ ref_sample[[i for i in range(8) if i % 2 == 0]],
639
+ np.concatenate(even_draws),
640
+ atol=1e-5
641
+ )
642
+
643
+ @pytest.mark.parametrize("scramble", [True])
644
+ def test_distribution(self, scramble):
645
+ d = 50
646
+ engine = self.engine(d=d, scramble=scramble)
647
+ sample = engine.random(1024)
648
+ assert_allclose(
649
+ np.mean(sample, axis=0), np.repeat(0.5, d), atol=1e-2
650
+ )
651
+ assert_allclose(
652
+ np.percentile(sample, 25, axis=0), np.repeat(0.25, d), atol=1e-2
653
+ )
654
+ assert_allclose(
655
+ np.percentile(sample, 75, axis=0), np.repeat(0.75, d), atol=1e-2
656
+ )
657
+
658
+ def test_raises_optimizer(self):
659
+ message = r"'toto' is not a valid optimization method"
660
+ with pytest.raises(ValueError, match=message):
661
+ self.engine(d=1, scramble=False, optimization="toto")
662
+
663
+ @pytest.mark.parametrize(
664
+ "optimization,metric",
665
+ [
666
+ ("random-CD", qmc.discrepancy),
667
+ ("lloyd", lambda sample: -_l1_norm(sample))]
668
+ )
669
+ def test_optimizers(self, optimization, metric):
670
+ engine = self.engine(d=2, scramble=False)
671
+ sample_ref = engine.random(n=64)
672
+ metric_ref = metric(sample_ref)
673
+
674
+ optimal_ = self.engine(d=2, scramble=False, optimization=optimization)
675
+ sample_ = optimal_.random(n=64)
676
+ metric_ = metric(sample_)
677
+
678
+ assert metric_ < metric_ref
679
+
680
+ def test_consume_prng_state(self):
681
+ rng = np.random.default_rng(0xa29cabb11cfdf44ff6cac8bec254c2a0)
682
+ sample = []
683
+ for i in range(3):
684
+ engine = self.engine(d=2, scramble=True, rng=rng)
685
+ sample.append(engine.random(4))
686
+
687
+ with pytest.raises(AssertionError, match="Arrays are not equal"):
688
+ assert_equal(sample[0], sample[1])
689
+ with pytest.raises(AssertionError, match="Arrays are not equal"):
690
+ assert_equal(sample[0], sample[2])
691
+
692
+
693
+ class TestHalton(QMCEngineTests):
694
+ qmce = qmc.Halton
695
+ can_scramble = True
696
+ # theoretical values known from Van der Corput
697
+ unscramble_nd = np.array([[0, 0], [1 / 2, 1 / 3],
698
+ [1 / 4, 2 / 3], [3 / 4, 1 / 9],
699
+ [1 / 8, 4 / 9], [5 / 8, 7 / 9],
700
+ [3 / 8, 2 / 9], [7 / 8, 5 / 9]])
701
+ # theoretical values unknown: convergence properties checked
702
+ scramble_nd = np.array([[0.50246036, 0.93382481],
703
+ [0.00246036, 0.26715815],
704
+ [0.75246036, 0.60049148],
705
+ [0.25246036, 0.8227137 ],
706
+ [0.62746036, 0.15604704],
707
+ [0.12746036, 0.48938037],
708
+ [0.87746036, 0.71160259],
709
+ [0.37746036, 0.04493592]])
710
+
711
+ def test_workers(self):
712
+ ref_sample = self.reference(scramble=True)
713
+ engine = self.engine(d=2, scramble=True)
714
+ sample = engine.random(n=len(ref_sample), workers=8)
715
+
716
+ assert_allclose(sample, ref_sample, atol=1e-3)
717
+
718
+ # worker + integers
719
+ engine.reset()
720
+ ref_sample = engine.integers(10)
721
+ engine.reset()
722
+ sample = engine.integers(10, workers=8)
723
+ assert_equal(sample, ref_sample)
724
+
725
+
726
+ class TestLHS(QMCEngineTests):
727
+ qmce = qmc.LatinHypercube
728
+ can_scramble = True
729
+
730
+ def test_continuing(self, *args):
731
+ pytest.skip("Not applicable: not a sequence.")
732
+
733
+ def test_fast_forward(self, *args):
734
+ pytest.skip("Not applicable: not a sequence.")
735
+
736
+ def test_sample(self, *args):
737
+ pytest.skip("Not applicable: the value of reference sample is"
738
+ " implementation dependent.")
739
+
740
+ @pytest.mark.parametrize("strength", [1, 2])
741
+ @pytest.mark.parametrize("scramble", [False, True])
742
+ @pytest.mark.parametrize("optimization", [None, "random-CD"])
743
+ def test_sample_stratified(self, optimization, scramble, strength):
744
+ rng = np.random.default_rng(37511836202578819870665127532742111260)
745
+ p = 5
746
+ n = p**2
747
+ d = 6
748
+
749
+ engine = qmc.LatinHypercube(d=d, scramble=scramble,
750
+ strength=strength,
751
+ optimization=optimization,
752
+ rng=rng)
753
+ sample = engine.random(n=n)
754
+ assert sample.shape == (n, d)
755
+ assert engine.num_generated == n
756
+
757
+ # centering stratifies samples in the middle of equal segments:
758
+ # * inter-sample distance is constant in 1D sub-projections
759
+ # * after ordering, columns are equal
760
+ expected1d = (np.arange(n) + 0.5) / n
761
+ expected = np.broadcast_to(expected1d, (d, n)).T
762
+ assert np.any(sample != expected)
763
+
764
+ sorted_sample = np.sort(sample, axis=0)
765
+ tol = 0.5 / n if scramble else 0
766
+
767
+ assert_allclose(sorted_sample, expected, atol=tol)
768
+ assert np.any(sample - expected > tol)
769
+
770
+ if strength == 2 and optimization is None:
771
+ unique_elements = np.arange(p)
772
+ desired = set(product(unique_elements, unique_elements))
773
+
774
+ for i, j in combinations(range(engine.d), 2):
775
+ samples_2d = sample[:, [i, j]]
776
+ res = (samples_2d * p).astype(int)
777
+ res_set = {tuple(row) for row in res}
778
+ assert_equal(res_set, desired)
779
+
780
+ def test_optimizer_1d(self):
781
+ # discrepancy measures are invariant under permuting factors and runs
782
+ engine = self.engine(d=1, scramble=False)
783
+ sample_ref = engine.random(n=64)
784
+
785
+ optimal_ = self.engine(d=1, scramble=False, optimization="random-CD")
786
+ sample_ = optimal_.random(n=64)
787
+
788
+ assert_array_equal(sample_ref, sample_)
789
+
790
+ def test_raises(self):
791
+ message = r"not a valid strength"
792
+ with pytest.raises(ValueError, match=message):
793
+ qmc.LatinHypercube(1, strength=3)
794
+
795
+ message = r"n is not the square of a prime number"
796
+ with pytest.raises(ValueError, match=message):
797
+ engine = qmc.LatinHypercube(d=2, strength=2)
798
+ engine.random(16)
799
+
800
+ message = r"n is not the square of a prime number"
801
+ with pytest.raises(ValueError, match=message):
802
+ engine = qmc.LatinHypercube(d=2, strength=2)
803
+ engine.random(5) # because int(sqrt(5)) would result in 2
804
+
805
+ message = r"n is too small for d"
806
+ with pytest.raises(ValueError, match=message):
807
+ engine = qmc.LatinHypercube(d=5, strength=2)
808
+ engine.random(9)
809
+
810
+
811
+ class TestSobol(QMCEngineTests):
812
+ qmce = qmc.Sobol
813
+ can_scramble = True
814
+ # theoretical values from Joe Kuo2010
815
+ unscramble_nd = np.array([[0., 0.],
816
+ [0.5, 0.5],
817
+ [0.75, 0.25],
818
+ [0.25, 0.75],
819
+ [0.375, 0.375],
820
+ [0.875, 0.875],
821
+ [0.625, 0.125],
822
+ [0.125, 0.625]])
823
+
824
+ # theoretical values unknown: convergence properties checked
825
+ scramble_nd = np.array([[0.25331921, 0.41371179],
826
+ [0.8654213, 0.9821167],
827
+ [0.70097554, 0.03664616],
828
+ [0.18027647, 0.60895735],
829
+ [0.10521339, 0.21897069],
830
+ [0.53019685, 0.66619033],
831
+ [0.91122276, 0.34580743],
832
+ [0.45337471, 0.78912079]])
833
+
834
+ def test_warning(self):
835
+ with pytest.warns(UserWarning, match=r"The balance properties of "
836
+ r"Sobol' points"):
837
+ engine = qmc.Sobol(1)
838
+ engine.random(10)
839
+
840
+ def test_random_base2(self):
841
+ engine = qmc.Sobol(2, scramble=False)
842
+ sample = engine.random_base2(2)
843
+ assert_array_equal(self.unscramble_nd[:4], sample)
844
+
845
+ # resampling still having N=2**n
846
+ sample = engine.random_base2(2)
847
+ assert_array_equal(self.unscramble_nd[4:8], sample)
848
+
849
+ # resampling again but leading to N!=2**n
850
+ with pytest.raises(ValueError, match=r"The balance properties of "
851
+ r"Sobol' points"):
852
+ engine.random_base2(2)
853
+
854
+ def test_raise(self):
855
+ with pytest.raises(ValueError, match=r"Maximum supported "
856
+ r"dimensionality"):
857
+ qmc.Sobol(qmc.Sobol.MAXDIM + 1)
858
+
859
+ with pytest.raises(ValueError, match=r"Maximum supported "
860
+ r"'bits' is 64"):
861
+ qmc.Sobol(1, bits=65)
862
+
863
+ def test_high_dim(self):
864
+ engine = qmc.Sobol(1111, scramble=False)
865
+ count1 = Counter(engine.random().flatten().tolist())
866
+ count2 = Counter(engine.random().flatten().tolist())
867
+ assert_equal(count1, Counter({0.0: 1111}))
868
+ assert_equal(count2, Counter({0.5: 1111}))
869
+
870
+ @pytest.mark.parametrize("bits", [2, 3])
871
+ def test_bits(self, bits):
872
+ engine = qmc.Sobol(2, scramble=False, bits=bits)
873
+ ns = 2**bits
874
+ sample = engine.random(ns)
875
+ assert_array_equal(self.unscramble_nd[:ns], sample)
876
+
877
+ with pytest.raises(ValueError, match="increasing `bits`"):
878
+ engine.random()
879
+
880
+ def test_64bits(self):
881
+ engine = qmc.Sobol(2, scramble=False, bits=64)
882
+ sample = engine.random(8)
883
+ assert_array_equal(self.unscramble_nd, sample)
884
+
885
+
886
+ class TestLow0Bit:
887
+ def test_examples(self):
888
+ test_vector = [
889
+ # from low_0_bit's docstring
890
+ (0b0000, 1),
891
+ (0b0001, 2),
892
+ (0b0010, 1),
893
+ (0b0101, 2),
894
+ (0b0111, 4),
895
+ # gh-23409
896
+ (2 ** 32 - 1, 33),
897
+ (2 ** 32, 1),
898
+ (2 ** 33 - 1, 34),
899
+ (2 ** 64 - 1, 65),
900
+ ]
901
+ for in_, out in test_vector:
902
+ assert_equal(_test_low_0_bit(in_), out)
903
+
904
+
905
+ class TestPoisson(QMCEngineTests):
906
+ qmce = qmc.PoissonDisk
907
+ can_scramble = False
908
+
909
+ def test_bounds(self, *args):
910
+ pytest.skip("Too costly in memory.")
911
+
912
+ def test_fast_forward(self, *args):
913
+ pytest.skip("Not applicable: recursive process.")
914
+
915
+ def test_sample(self, *args):
916
+ pytest.skip("Not applicable: the value of reference sample is"
917
+ " implementation dependent.")
918
+
919
+ def test_continuing(self, *args):
920
+ # can continue a sampling, but will not preserve the same order
921
+ # because candidates are lost, so we will not select the same center
922
+ radius = 0.05
923
+ ns = 6
924
+ engine = self.engine(d=2, radius=radius, scramble=False)
925
+
926
+ sample_init = engine.random(n=ns)
927
+ assert len(sample_init) <= ns
928
+ assert l2_norm(sample_init) >= radius
929
+
930
+ sample_continued = engine.random(n=ns)
931
+ assert len(sample_continued) <= ns
932
+ assert l2_norm(sample_continued) >= radius
933
+
934
+ sample = np.concatenate([sample_init, sample_continued], axis=0)
935
+ assert len(sample) <= ns * 2
936
+ assert l2_norm(sample) >= radius
937
+
938
+ def test_mindist(self):
939
+ rng = np.random.default_rng(132074951149370773672162394161442690287)
940
+ ns = 50
941
+
942
+ low, high = 0.08, 0.2
943
+ radii = (high - low) * rng.random(5) + low
944
+
945
+ dimensions = [1, 3, 4]
946
+ hypersphere_methods = ["volume", "surface"]
947
+
948
+ gen = product(dimensions, radii, hypersphere_methods)
949
+
950
+ for d, radius, hypersphere in gen:
951
+ engine = self.qmce(
952
+ d=d, radius=radius, hypersphere=hypersphere, rng=rng
953
+ )
954
+ sample = engine.random(ns)
955
+
956
+ assert len(sample) <= ns
957
+ assert l2_norm(sample) >= radius
958
+
959
+ @pytest.mark.parametrize("l_bounds, u_bounds", # add bounds to test gh-22819
960
+ [(None, None), ([-5, -5], [5, 5]), ([1.1, 1.1], [5.1, 5.1])])
961
+ def test_fill_space(self, l_bounds, u_bounds):
962
+ radius = 0.2
963
+ engine = self.qmce(d=2, radius=radius, l_bounds=l_bounds, u_bounds=u_bounds)
964
+
965
+ sample = engine.fill_space()
966
+ # circle packing problem is np complex
967
+ assert l2_norm(sample) >= radius
968
+
969
+ def test_bounds_shift_scale(self):
970
+ # test a reasonable property: as long as shape of region is a hypercube,
971
+ # bounds just shift and scale the sample.
972
+ radius = 0.2 # arbitrary parameters
973
+ shift = np.asarray([-2.1, 3.4])
974
+ scale = 2.6
975
+
976
+ rng = np.random.default_rng(8504196751)
977
+ engine = self.qmce(d=2, radius=radius*scale, rng=rng,
978
+ l_bounds=shift, u_bounds=shift + scale)
979
+ res = engine.fill_space()
980
+
981
+ rng = np.random.default_rng(8504196751)
982
+ engine = self.qmce(d=2, radius=radius, rng=rng)
983
+ ref = engine.fill_space()
984
+
985
+ # circle packing problem is np complex
986
+ assert l2_norm(res) >= radius
987
+ assert_allclose(res, ref*scale + shift)
988
+
989
+ @pytest.mark.parametrize("l_bounds", [[-1, -2, -1], [1, 2, 1]])
990
+ def test_sample_inside_lower_bounds(self, l_bounds):
991
+ radius = 0.2
992
+ u_bounds=[3, 3, 2]
993
+ engine = self.qmce(
994
+ d=3, radius=radius, l_bounds=l_bounds, u_bounds=u_bounds
995
+ )
996
+ sample = engine.random(30)
997
+
998
+ for point in sample:
999
+ assert_array_less(point, u_bounds)
1000
+ assert_array_less(l_bounds, point)
1001
+
1002
+ @pytest.mark.parametrize("u_bounds", [[-1, -2, -1], [1, 2, 1]])
1003
+ def test_sample_inside_upper_bounds(self, u_bounds):
1004
+ radius = 0.2
1005
+ l_bounds=[-3, -3, -2]
1006
+ engine = self.qmce(
1007
+ d=3, radius=radius, l_bounds=l_bounds, u_bounds=u_bounds
1008
+ )
1009
+ sample = engine.random(30)
1010
+
1011
+ for point in sample:
1012
+ assert_array_less(point, u_bounds)
1013
+ assert_array_less(l_bounds, point)
1014
+
1015
+ def test_inconsistent_bound_value(self):
1016
+ radius = 0.2
1017
+ l_bounds=[3, 2, 1]
1018
+ u_bounds=[-1, -2, -1]
1019
+ with pytest.raises(
1020
+ ValueError,
1021
+ match="Bounds are not consistent 'l_bounds' < 'u_bounds'"):
1022
+ self.qmce(d=3, radius=radius, l_bounds=l_bounds, u_bounds=u_bounds)
1023
+
1024
+ @pytest.mark.parametrize("u_bounds", [[-1, -2, -1], [-1, -2]])
1025
+ @pytest.mark.parametrize("l_bounds", [[3, 2]])
1026
+ def test_inconsistent_bounds(self, u_bounds, l_bounds):
1027
+ radius = 0.2
1028
+ with pytest.raises(
1029
+ ValueError,
1030
+ match="'l_bounds' and 'u_bounds' must be broadcastable and respect"
1031
+ " the sample dimension"):
1032
+ self.qmce(
1033
+ d=3, radius=radius,
1034
+ l_bounds=l_bounds, u_bounds=u_bounds
1035
+ )
1036
+
1037
+ def test_raises(self):
1038
+ message = r"'toto' is not a valid hypersphere sampling"
1039
+ with pytest.raises(ValueError, match=message):
1040
+ qmc.PoissonDisk(1, hypersphere="toto")
1041
+
1042
+
1043
+ class TestMultinomialQMC:
1044
+ def test_validations(self):
1045
+ # negative Ps
1046
+ p = np.array([0.12, 0.26, -0.05, 0.35, 0.22])
1047
+ with pytest.raises(ValueError, match=r"Elements of pvals must "
1048
+ r"be non-negative."):
1049
+ qmc.MultinomialQMC(p, n_trials=10)
1050
+
1051
+ # sum of P too large
1052
+ p = np.array([0.12, 0.26, 0.1, 0.35, 0.22])
1053
+ message = r"Elements of pvals must sum to 1."
1054
+ with pytest.raises(ValueError, match=message):
1055
+ qmc.MultinomialQMC(p, n_trials=10)
1056
+
1057
+ p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
1058
+
1059
+ message = r"Dimension of `engine` must be 1."
1060
+ with pytest.raises(ValueError, match=message):
1061
+ qmc.MultinomialQMC(p, n_trials=10, engine=qmc.Sobol(d=2))
1062
+
1063
+ message = r"`engine` must be an instance of..."
1064
+ with pytest.raises(ValueError, match=message):
1065
+ qmc.MultinomialQMC(p, n_trials=10, engine=np.random.default_rng())
1066
+
1067
+ @pytest.mark.filterwarnings('ignore::UserWarning')
1068
+ def test_MultinomialBasicDraw(self):
1069
+ rng = np.random.default_rng(6955663962957011631562466584467607969)
1070
+ p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
1071
+ n_trials = 100
1072
+ expected = np.atleast_2d(n_trials * p).astype(int)
1073
+ # preserve use of legacy keyword during SPEC 7 transition
1074
+ engine = qmc.MultinomialQMC(p, n_trials=n_trials, seed=rng)
1075
+ assert_allclose(engine.random(1), expected, atol=1)
1076
+
1077
+ def test_MultinomialDistribution(self):
1078
+ rng = np.random.default_rng(77797854505813727292048130876699859000)
1079
+ p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
1080
+ engine = qmc.MultinomialQMC(p, n_trials=8192, rng=rng)
1081
+ draws = engine.random(1)
1082
+ assert_allclose(draws / np.sum(draws), np.atleast_2d(p), atol=1e-4)
1083
+
1084
+ def test_FindIndex(self):
1085
+ p_cumulative = np.array([0.1, 0.4, 0.45, 0.6, 0.75, 0.9, 0.99, 1.0])
1086
+ size = len(p_cumulative)
1087
+ assert_equal(_test_find_index(p_cumulative, size, 0.0), 0)
1088
+ assert_equal(_test_find_index(p_cumulative, size, 0.4), 2)
1089
+ assert_equal(_test_find_index(p_cumulative, size, 0.44999), 2)
1090
+ assert_equal(_test_find_index(p_cumulative, size, 0.45001), 3)
1091
+ assert_equal(_test_find_index(p_cumulative, size, 1.0), size - 1)
1092
+
1093
+ @pytest.mark.filterwarnings('ignore::UserWarning')
1094
+ def test_other_engine(self):
1095
+ # same as test_MultinomialBasicDraw with different engine
1096
+ rng = np.random.default_rng(283753519042773243071753037669078065412)
1097
+ p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
1098
+ n_trials = 100
1099
+ expected = np.atleast_2d(n_trials * p).astype(int)
1100
+ base_engine = qmc.Sobol(1, scramble=True, rng=rng)
1101
+ engine = qmc.MultinomialQMC(p, n_trials=n_trials, engine=base_engine,
1102
+ rng=rng)
1103
+ assert_allclose(engine.random(1), expected, atol=1)
1104
+
1105
+
1106
+ class TestNormalQMC:
1107
+ def test_NormalQMC(self):
1108
+ # d = 1
1109
+ engine = qmc.MultivariateNormalQMC(mean=np.zeros(1))
1110
+ samples = engine.random()
1111
+ assert_equal(samples.shape, (1, 1))
1112
+ samples = engine.random(n=5)
1113
+ assert_equal(samples.shape, (5, 1))
1114
+ # d = 2
1115
+ engine = qmc.MultivariateNormalQMC(mean=np.zeros(2))
1116
+ samples = engine.random()
1117
+ assert_equal(samples.shape, (1, 2))
1118
+ samples = engine.random(n=5)
1119
+ assert_equal(samples.shape, (5, 2))
1120
+
1121
+ def test_NormalQMCInvTransform(self):
1122
+ # d = 1
1123
+ engine = qmc.MultivariateNormalQMC(
1124
+ mean=np.zeros(1), inv_transform=True)
1125
+ samples = engine.random()
1126
+ assert_equal(samples.shape, (1, 1))
1127
+ samples = engine.random(n=5)
1128
+ assert_equal(samples.shape, (5, 1))
1129
+ # d = 2
1130
+ engine = qmc.MultivariateNormalQMC(
1131
+ mean=np.zeros(2), inv_transform=True)
1132
+ samples = engine.random()
1133
+ assert_equal(samples.shape, (1, 2))
1134
+ samples = engine.random(n=5)
1135
+ assert_equal(samples.shape, (5, 2))
1136
+
1137
+ def test_NormalQMCSeeded(self):
1138
+ # test even dimension
1139
+ rng = np.random.default_rng(274600237797326520096085022671371676017)
1140
+ # preserve use of legacy keyword during SPEC 7 transition
1141
+ engine = qmc.MultivariateNormalQMC(
1142
+ mean=np.zeros(2), inv_transform=False, seed=rng)
1143
+ samples = engine.random(n=2)
1144
+ samples_expected = np.array([[-0.932001, -0.522923],
1145
+ [-1.477655, 0.846851]])
1146
+ assert_allclose(samples, samples_expected, atol=1e-4)
1147
+
1148
+ # test odd dimension
1149
+ rng = np.random.default_rng(274600237797326520096085022671371676017)
1150
+ engine = qmc.MultivariateNormalQMC(
1151
+ mean=np.zeros(3), inv_transform=False, rng=rng)
1152
+ samples = engine.random(n=2)
1153
+ samples_expected = np.array([[-0.932001, -0.522923, 0.036578],
1154
+ [-1.778011, 0.912428, -0.065421]])
1155
+ assert_allclose(samples, samples_expected, atol=1e-4)
1156
+
1157
+ # same test with another engine
1158
+ rng = np.random.default_rng(274600237797326520096085022671371676017)
1159
+ base_engine = qmc.Sobol(4, scramble=True, rng=rng)
1160
+ engine = qmc.MultivariateNormalQMC(
1161
+ mean=np.zeros(3), inv_transform=False,
1162
+ engine=base_engine, rng=rng
1163
+ )
1164
+ samples = engine.random(n=2)
1165
+ samples_expected = np.array([[-0.932001, -0.522923, 0.036578],
1166
+ [-1.778011, 0.912428, -0.065421]])
1167
+ assert_allclose(samples, samples_expected, atol=1e-4)
1168
+
1169
+ def test_NormalQMCSeededInvTransform(self):
1170
+ # test even dimension
1171
+ rng = np.random.default_rng(288527772707286126646493545351112463929)
1172
+ engine = qmc.MultivariateNormalQMC(
1173
+ mean=np.zeros(2), rng=rng, inv_transform=True)
1174
+ samples = engine.random(n=2)
1175
+ samples_expected = np.array([[-0.913237, -0.964026],
1176
+ [0.255904, 0.003068]])
1177
+ assert_allclose(samples, samples_expected, atol=1e-4)
1178
+
1179
+ # test odd dimension
1180
+ rng = np.random.default_rng(288527772707286126646493545351112463929)
1181
+ engine = qmc.MultivariateNormalQMC(
1182
+ mean=np.zeros(3), rng=rng, inv_transform=True)
1183
+ samples = engine.random(n=2)
1184
+ samples_expected = np.array([[-0.913237, -0.964026, 0.355501],
1185
+ [0.699261, 2.90213 , -0.6418]])
1186
+ assert_allclose(samples, samples_expected, atol=1e-4)
1187
+
1188
+ def test_other_engine(self):
1189
+ for d in (0, 1, 2):
1190
+ base_engine = qmc.Sobol(d=d, scramble=False)
1191
+ engine = qmc.MultivariateNormalQMC(mean=np.zeros(d),
1192
+ engine=base_engine,
1193
+ inv_transform=True)
1194
+ samples = engine.random()
1195
+ assert_equal(samples.shape, (1, d))
1196
+
1197
+ def test_NormalQMCShapiro(self):
1198
+ rng = np.random.default_rng(13242)
1199
+ engine = qmc.MultivariateNormalQMC(mean=np.zeros(2), rng=rng)
1200
+ samples = engine.random(n=256)
1201
+ assert all(np.abs(samples.mean(axis=0)) < 1e-2)
1202
+ assert all(np.abs(samples.std(axis=0) - 1) < 1e-2)
1203
+ # perform Shapiro-Wilk test for normality
1204
+ for i in (0, 1):
1205
+ _, pval = shapiro(samples[:, i])
1206
+ assert pval > 0.9
1207
+ # make sure samples are uncorrelated
1208
+ cov = np.cov(samples.transpose())
1209
+ assert np.abs(cov[0, 1]) < 1e-2
1210
+
1211
+ def test_NormalQMCShapiroInvTransform(self):
1212
+ rng = np.random.default_rng(32344554)
1213
+ engine = qmc.MultivariateNormalQMC(
1214
+ mean=np.zeros(2), inv_transform=True, rng=rng)
1215
+ samples = engine.random(n=256)
1216
+ assert all(np.abs(samples.mean(axis=0)) < 1e-2)
1217
+ assert all(np.abs(samples.std(axis=0) - 1) < 1e-2)
1218
+ # perform Shapiro-Wilk test for normality
1219
+ for i in (0, 1):
1220
+ _, pval = shapiro(samples[:, i])
1221
+ assert pval > 0.9
1222
+ # make sure samples are uncorrelated
1223
+ cov = np.cov(samples.transpose())
1224
+ assert np.abs(cov[0, 1]) < 1e-2
1225
+
1226
+
1227
+ class TestMultivariateNormalQMC:
1228
+
1229
+ def test_validations(self):
1230
+
1231
+ message = r"Dimension of `engine` must be consistent"
1232
+ with pytest.raises(ValueError, match=message):
1233
+ qmc.MultivariateNormalQMC([0], engine=qmc.Sobol(d=2))
1234
+
1235
+ message = r"Dimension of `engine` must be consistent"
1236
+ with pytest.raises(ValueError, match=message):
1237
+ qmc.MultivariateNormalQMC([0, 0, 0], engine=qmc.Sobol(d=4))
1238
+
1239
+ message = r"`engine` must be an instance of..."
1240
+ with pytest.raises(ValueError, match=message):
1241
+ qmc.MultivariateNormalQMC([0, 0], engine=np.random.default_rng())
1242
+
1243
+ message = r"Covariance matrix not PSD."
1244
+ with pytest.raises(ValueError, match=message):
1245
+ qmc.MultivariateNormalQMC([0, 0], [[1, 2], [2, 1]])
1246
+
1247
+ message = r"Covariance matrix is not symmetric."
1248
+ with pytest.raises(ValueError, match=message):
1249
+ qmc.MultivariateNormalQMC([0, 0], [[1, 0], [2, 1]])
1250
+
1251
+ message = r"Dimension mismatch between mean and covariance."
1252
+ with pytest.raises(ValueError, match=message):
1253
+ qmc.MultivariateNormalQMC([0], [[1, 0], [0, 1]])
1254
+
1255
+ def test_MultivariateNormalQMCNonPD(self):
1256
+ # try with non-pd but psd cov; should work
1257
+ engine = qmc.MultivariateNormalQMC(
1258
+ [0, 0, 0], [[1, 0, 1], [0, 1, 1], [1, 1, 2]],
1259
+ )
1260
+ assert engine._corr_matrix is not None
1261
+
1262
+ def test_MultivariateNormalQMC(self):
1263
+ # d = 1 scalar
1264
+ engine = qmc.MultivariateNormalQMC(mean=0, cov=5)
1265
+ samples = engine.random()
1266
+ assert_equal(samples.shape, (1, 1))
1267
+ samples = engine.random(n=5)
1268
+ assert_equal(samples.shape, (5, 1))
1269
+
1270
+ # d = 2 list
1271
+ engine = qmc.MultivariateNormalQMC(mean=[0, 1], cov=[[1, 0], [0, 1]])
1272
+ samples = engine.random()
1273
+ assert_equal(samples.shape, (1, 2))
1274
+ samples = engine.random(n=5)
1275
+ assert_equal(samples.shape, (5, 2))
1276
+
1277
+ # d = 3 np.array
1278
+ mean = np.array([0, 1, 2])
1279
+ cov = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
1280
+ engine = qmc.MultivariateNormalQMC(mean, cov)
1281
+ samples = engine.random()
1282
+ assert_equal(samples.shape, (1, 3))
1283
+ samples = engine.random(n=5)
1284
+ assert_equal(samples.shape, (5, 3))
1285
+
1286
+ def test_MultivariateNormalQMCInvTransform(self):
1287
+ # d = 1 scalar
1288
+ engine = qmc.MultivariateNormalQMC(mean=0, cov=5, inv_transform=True)
1289
+ samples = engine.random()
1290
+ assert_equal(samples.shape, (1, 1))
1291
+ samples = engine.random(n=5)
1292
+ assert_equal(samples.shape, (5, 1))
1293
+
1294
+ # d = 2 list
1295
+ engine = qmc.MultivariateNormalQMC(
1296
+ mean=[0, 1], cov=[[1, 0], [0, 1]], inv_transform=True,
1297
+ )
1298
+ samples = engine.random()
1299
+ assert_equal(samples.shape, (1, 2))
1300
+ samples = engine.random(n=5)
1301
+ assert_equal(samples.shape, (5, 2))
1302
+
1303
+ # d = 3 np.array
1304
+ mean = np.array([0, 1, 2])
1305
+ cov = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
1306
+ engine = qmc.MultivariateNormalQMC(mean, cov, inv_transform=True)
1307
+ samples = engine.random()
1308
+ assert_equal(samples.shape, (1, 3))
1309
+ samples = engine.random(n=5)
1310
+ assert_equal(samples.shape, (5, 3))
1311
+
1312
+ def test_MultivariateNormalQMCSeeded(self):
1313
+ # test even dimension
1314
+ rng = np.random.default_rng(180182791534511062935571481899241825000)
1315
+ a = rng.standard_normal((2, 2))
1316
+ A = a @ a.transpose() + np.diag(rng.random(2))
1317
+ engine = qmc.MultivariateNormalQMC(np.array([0, 0]), A,
1318
+ inv_transform=False, rng=rng)
1319
+ samples = engine.random(n=2)
1320
+ samples_expected = np.array([[-0.64419, -0.882413],
1321
+ [0.837199, 2.045301]])
1322
+ assert_allclose(samples, samples_expected, atol=1e-4)
1323
+
1324
+ # test odd dimension
1325
+ rng = np.random.default_rng(180182791534511062935571481899241825000)
1326
+ a = rng.standard_normal((3, 3))
1327
+ A = a @ a.transpose() + np.diag(rng.random(3))
1328
+ engine = qmc.MultivariateNormalQMC(np.array([0, 0, 0]), A,
1329
+ inv_transform=False, rng=rng)
1330
+ samples = engine.random(n=2)
1331
+ samples_expected = np.array([[-0.693853, -1.265338, -0.088024],
1332
+ [1.620193, 2.679222, 0.457343]])
1333
+ assert_allclose(samples, samples_expected, atol=1e-4)
1334
+
1335
+ def test_MultivariateNormalQMCSeededInvTransform(self):
1336
+ # test even dimension
1337
+ rng = np.random.default_rng(224125808928297329711992996940871155974)
1338
+ a = rng.standard_normal((2, 2))
1339
+ A = a @ a.transpose() + np.diag(rng.random(2))
1340
+ engine = qmc.MultivariateNormalQMC(
1341
+ np.array([0, 0]), A, rng=rng, inv_transform=True
1342
+ )
1343
+ samples = engine.random(n=2)
1344
+ samples_expected = np.array([[0.682171, -3.114233],
1345
+ [-0.098463, 0.668069]])
1346
+ assert_allclose(samples, samples_expected, atol=1e-4)
1347
+
1348
+ # test odd dimension
1349
+ rng = np.random.default_rng(224125808928297329711992996940871155974)
1350
+ a = rng.standard_normal((3, 3))
1351
+ A = a @ a.transpose() + np.diag(rng.random(3))
1352
+ engine = qmc.MultivariateNormalQMC(
1353
+ np.array([0, 0, 0]), A, rng=rng, inv_transform=True
1354
+ )
1355
+ samples = engine.random(n=2)
1356
+ samples_expected = np.array([[0.988061, -1.644089, -0.877035],
1357
+ [-1.771731, 1.096988, 2.024744]])
1358
+ assert_allclose(samples, samples_expected, atol=1e-4)
1359
+
1360
+ def test_MultivariateNormalQMCShapiro(self):
1361
+ # test the standard case
1362
+ rng = np.random.default_rng(188960007281846377164494575845971640)
1363
+ engine = qmc.MultivariateNormalQMC(
1364
+ mean=[0, 0], cov=[[1, 0], [0, 1]], rng=rng
1365
+ )
1366
+ samples = engine.random(n=256)
1367
+ assert all(np.abs(samples.mean(axis=0)) < 1e-2)
1368
+ assert all(np.abs(samples.std(axis=0) - 1) < 1e-2)
1369
+ # perform Shapiro-Wilk test for normality
1370
+ for i in (0, 1):
1371
+ _, pval = shapiro(samples[:, i])
1372
+ assert pval > 0.9
1373
+ # make sure samples are uncorrelated
1374
+ cov = np.cov(samples.transpose())
1375
+ assert np.abs(cov[0, 1]) < 1e-2
1376
+
1377
+ # test the correlated, non-zero mean case
1378
+ engine = qmc.MultivariateNormalQMC(
1379
+ mean=[1.0, 2.0], cov=[[1.5, 0.5], [0.5, 1.5]], rng=rng
1380
+ )
1381
+ samples = engine.random(n=256)
1382
+ assert all(np.abs(samples.mean(axis=0) - [1, 2]) < 1e-2)
1383
+ assert all(np.abs(samples.std(axis=0) - np.sqrt(1.5)) < 1e-2)
1384
+ # perform Shapiro-Wilk test for normality
1385
+ for i in (0, 1):
1386
+ _, pval = shapiro(samples[:, i])
1387
+ assert pval > 0.9
1388
+ # check covariance
1389
+ cov = np.cov(samples.transpose())
1390
+ assert np.abs(cov[0, 1] - 0.5) < 1e-2
1391
+
1392
+ def test_MultivariateNormalQMCShapiroInvTransform(self):
1393
+ # test the standard case
1394
+ rng = np.random.default_rng(200089821034563288698994840831440331329)
1395
+ engine = qmc.MultivariateNormalQMC(
1396
+ mean=[0, 0], cov=[[1, 0], [0, 1]], rng=rng, inv_transform=True
1397
+ )
1398
+ samples = engine.random(n=256)
1399
+ assert all(np.abs(samples.mean(axis=0)) < 1e-2)
1400
+ assert all(np.abs(samples.std(axis=0) - 1) < 1e-2)
1401
+ # perform Shapiro-Wilk test for normality
1402
+ for i in (0, 1):
1403
+ _, pval = shapiro(samples[:, i])
1404
+ assert pval > 0.9
1405
+ # make sure samples are uncorrelated
1406
+ cov = np.cov(samples.transpose())
1407
+ assert np.abs(cov[0, 1]) < 1e-2
1408
+
1409
+ # test the correlated, non-zero mean case
1410
+ engine = qmc.MultivariateNormalQMC(
1411
+ mean=[1.0, 2.0],
1412
+ cov=[[1.5, 0.5], [0.5, 1.5]],
1413
+ rng=rng,
1414
+ inv_transform=True,
1415
+ )
1416
+ samples = engine.random(n=256)
1417
+ assert all(np.abs(samples.mean(axis=0) - [1, 2]) < 1e-2)
1418
+ assert all(np.abs(samples.std(axis=0) - np.sqrt(1.5)) < 1e-2)
1419
+ # perform Shapiro-Wilk test for normality
1420
+ for i in (0, 1):
1421
+ _, pval = shapiro(samples[:, i])
1422
+ assert pval > 0.9
1423
+ # check covariance
1424
+ cov = np.cov(samples.transpose())
1425
+ assert np.abs(cov[0, 1] - 0.5) < 1e-2
1426
+
1427
+ def test_MultivariateNormalQMCDegenerate(self):
1428
+ # X, Y iid standard Normal and Z = X + Y, random vector (X, Y, Z)
1429
+ rng = np.random.default_rng(16320637417581448357869821654290448620)
1430
+ engine = qmc.MultivariateNormalQMC(
1431
+ mean=[0.0, 0.0, 0.0],
1432
+ cov=[[1.0, 0.0, 1.0], [0.0, 1.0, 1.0], [1.0, 1.0, 2.0]],
1433
+ rng=rng,
1434
+ )
1435
+ samples = engine.random(n=512)
1436
+ assert all(np.abs(samples.mean(axis=0)) < 1e-2)
1437
+ assert np.abs(np.std(samples[:, 0]) - 1) < 1e-2
1438
+ assert np.abs(np.std(samples[:, 1]) - 1) < 1e-2
1439
+ assert np.abs(np.std(samples[:, 2]) - np.sqrt(2)) < 1e-2
1440
+ for i in (0, 1, 2):
1441
+ _, pval = shapiro(samples[:, i])
1442
+ assert pval > 0.8
1443
+ cov = np.cov(samples.transpose())
1444
+ assert np.abs(cov[0, 1]) < 1e-2
1445
+ assert np.abs(cov[0, 2] - 1) < 1e-2
1446
+ # check to see if X + Y = Z almost exactly
1447
+ assert all(np.abs(samples[:, 0] + samples[:, 1] - samples[:, 2])
1448
+ < 1e-5)
1449
+
1450
+
1451
+ class TestLloyd:
1452
+ def test_lloyd(self):
1453
+ # quite sensible seed as it can go up before going further down
1454
+ rng = np.random.RandomState(1809831)
1455
+ sample = rng.uniform(0, 1, size=(128, 2))
1456
+ base_l1 = _l1_norm(sample)
1457
+ base_l2 = l2_norm(sample)
1458
+
1459
+ for _ in range(4):
1460
+ sample_lloyd = _lloyd_centroidal_voronoi_tessellation(
1461
+ sample, maxiter=1,
1462
+ )
1463
+ curr_l1 = _l1_norm(sample_lloyd)
1464
+ curr_l2 = l2_norm(sample_lloyd)
1465
+
1466
+ # higher is better for the distance measures
1467
+ assert base_l1 < curr_l1
1468
+ assert base_l2 < curr_l2
1469
+
1470
+ base_l1 = curr_l1
1471
+ base_l2 = curr_l2
1472
+
1473
+ sample = sample_lloyd
1474
+
1475
+ def test_lloyd_non_mutating(self):
1476
+ """
1477
+ Verify that the input samples are not mutated in place and that they do
1478
+ not share memory with the output.
1479
+ """
1480
+ sample_orig = np.array([[0.1, 0.1],
1481
+ [0.1, 0.2],
1482
+ [0.2, 0.1],
1483
+ [0.2, 0.2]])
1484
+ sample_copy = sample_orig.copy()
1485
+ new_sample = _lloyd_centroidal_voronoi_tessellation(
1486
+ sample=sample_orig
1487
+ )
1488
+ assert_allclose(sample_orig, sample_copy)
1489
+ assert not np.may_share_memory(sample_orig, new_sample)
1490
+
1491
+ def test_lloyd_errors(self):
1492
+ with pytest.raises(ValueError, match=r"`sample` is not a 2D array"):
1493
+ sample = [0, 1, 0.5]
1494
+ _lloyd_centroidal_voronoi_tessellation(sample)
1495
+
1496
+ msg = r"`sample` dimension is not >= 2"
1497
+ with pytest.raises(ValueError, match=msg):
1498
+ sample = [[0], [0.4], [1]]
1499
+ _lloyd_centroidal_voronoi_tessellation(sample)
1500
+
1501
+ msg = r"`sample` is not in unit hypercube"
1502
+ with pytest.raises(ValueError, match=msg):
1503
+ sample = [[-1.1, 0], [0.1, 0.4], [1, 2]]
1504
+ _lloyd_centroidal_voronoi_tessellation(sample)
1505
+
1506
+
1507
+ # mindist
1508
+ def l2_norm(sample):
1509
+ return distance.pdist(sample).min()
1510
+
1511
+
1512
+ @pytest.mark.parametrize('engine', [qmc.Halton, qmc.Sobol,
1513
+ qmc.LatinHypercube, qmc.PoissonDisk])
1514
+ def test_deterministic(engine):
1515
+ seed_number = 2359834584
1516
+
1517
+ rng = np.random.RandomState(seed_number)
1518
+ res1 = engine(d=1, seed=rng).random(4)
1519
+ rng = np.random.RandomState(seed_number)
1520
+ res2 = engine(d=1, seed=rng).random(4)
1521
+ assert_equal(res1, res2)
1522
+
1523
+ rng = np.random.default_rng(seed_number)
1524
+ res1 = engine(d=1, seed=rng).random(4)
1525
+ res2 = engine(d=1, rng=seed_number).random(4)
1526
+ assert_equal(res1, res2)
1527
+ rng = np.random.default_rng(seed_number)
1528
+ res3 = engine(d=1, rng=rng).random(4)
1529
+ assert_equal(res2, res1)
1530
+ assert_equal(res3, res1)
1531
+
1532
+ message = "got multiple values for argument now known as `rng`"
1533
+ with pytest.raises(TypeError, match=message):
1534
+ engine(d=1, rng=seed_number, seed=seed_number)
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_quantile.py ADDED
@@ -0,0 +1,412 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import numpy as np
3
+
4
+ from scipy import stats
5
+ from scipy.stats._quantile import _xp_searchsorted
6
+ from scipy._lib._array_api import (
7
+ xp_default_dtype,
8
+ is_numpy,
9
+ is_torch,
10
+ is_jax,
11
+ make_xp_test_case,
12
+ SCIPY_ARRAY_API,
13
+ xp_size,
14
+ xp_copy,
15
+ )
16
+ from scipy._lib._array_api_no_0d import xp_assert_close, xp_assert_equal
17
+ from scipy._lib._util import _apply_over_batch
18
+
19
+ skip_xp_backends = pytest.mark.skip_xp_backends
20
+
21
+ lazy_xp_modules = [stats]
22
+
23
+ @_apply_over_batch(('x', 1), ('p', 1))
24
+ def quantile_reference_last_axis(x, p, nan_policy, method):
25
+ if nan_policy == 'omit':
26
+ x = x[~np.isnan(x)]
27
+ p_mask = np.isnan(p)
28
+ p = p.copy()
29
+ p[p_mask] = 0.5
30
+ if method == 'harrell-davis':
31
+ # hdquantiles returns masked element if length along axis is 1 (bug)
32
+ res = (np.full_like(p, x[0]) if x.size == 1
33
+ else stats.mstats.hdquantiles(x, p).data)
34
+ elif method.startswith('round'):
35
+ res = winsor_reference_1d(np.sort(x), p, method)
36
+ else:
37
+ res = np.quantile(x, p, method=method)
38
+
39
+ res = np.asarray(res)
40
+ if nan_policy == 'propagate' and np.any(np.isnan(x)):
41
+ res[:] = np.nan
42
+
43
+ res[p_mask] = np.nan
44
+ return res
45
+
46
+
47
+ @np.vectorize(excluded={0, 2}) # type: ignore[call-arg]
48
+ def winsor_reference_1d(y, p, method):
49
+ # Adapted directly from the documentation
50
+ # Note: `y` is the sorted data array
51
+ n = len(y)
52
+ if method == 'round_nearest':
53
+ j = int(np.round(p * n) if p < 0.5 else np.round(n * p - 1))
54
+ elif method == 'round_outward':
55
+ j = int(np.floor(p * n) if p < 0.5 else np.ceil(n * p - 1))
56
+ elif method == 'round_inward':
57
+ j = int(np.ceil(p * n) if p < 0.5 else np.floor(n * p - 1))
58
+ return y[j]
59
+
60
+
61
+ def quantile_reference(x, p, *, axis, nan_policy, keepdims, method):
62
+ x, p = np.moveaxis(x, axis, -1), np.moveaxis(np.atleast_1d(p), axis, -1)
63
+ res = quantile_reference_last_axis(x, p, nan_policy, method)
64
+ res = np.moveaxis(res, -1, axis)
65
+ if not keepdims:
66
+ res = np.squeeze(res, axis=axis)
67
+ return res
68
+
69
+
70
+ @make_xp_test_case(stats.quantile)
71
+ class TestQuantile:
72
+
73
+ def test_input_validation(self, xp):
74
+ x = xp.asarray([1, 2, 3])
75
+ p = xp.asarray(0.5)
76
+
77
+ message = "`x` must have real dtype."
78
+ with pytest.raises(ValueError, match=message):
79
+ stats.quantile(xp.asarray([True, False]), p)
80
+ with pytest.raises(ValueError):
81
+ stats.quantile(xp.asarray([1+1j, 2]), p)
82
+
83
+ message = "`p` must have real floating dtype."
84
+ with pytest.raises(ValueError, match=message):
85
+ stats.quantile(x, xp.asarray([0, 1]))
86
+
87
+ message = "`weights` must have real dtype."
88
+ with pytest.raises(ValueError, match=message):
89
+ stats.quantile(x, p, weights=xp.astype(x, xp.complex64))
90
+
91
+ message = "`axis` must be an integer or None."
92
+ with pytest.raises(ValueError, match=message):
93
+ stats.quantile(x, p, axis=0.5)
94
+ with pytest.raises(ValueError, match=message):
95
+ stats.quantile(x, p, axis=(0, -1))
96
+
97
+ message = "`axis` is not compatible with the shapes of the inputs."
98
+ with pytest.raises(ValueError, match=message):
99
+ stats.quantile(x, p, axis=2)
100
+
101
+ if not is_jax(xp): # no data-dependent input validation for lazy arrays
102
+ message = "The input contains nan values"
103
+ with pytest.raises(ValueError, match=message):
104
+ stats.quantile(xp.asarray([xp.nan, 1, 2]), p, nan_policy='raise')
105
+
106
+ message = "`method` must be one of..."
107
+ with pytest.raises(ValueError, match=message):
108
+ stats.quantile(x, p, method='a duck')
109
+
110
+ message = "`method='harrell-davis'` does not support `weights`."
111
+ with pytest.raises(ValueError, match=message):
112
+ stats.quantile(x, p, weights=x, method='harrell-davis')
113
+
114
+ message = "`method='round_nearest'` does not support `weights`."
115
+ with pytest.raises(ValueError, match=message):
116
+ stats.quantile(x, p, weights=x, method='round_nearest')
117
+
118
+ message = "If specified, `keepdims` must be True or False."
119
+ with pytest.raises(ValueError, match=message):
120
+ stats.quantile(x, p, keepdims=42)
121
+
122
+ message = "`keepdims` may be False only if the length of `p` along `axis` is 1."
123
+ with pytest.raises(ValueError, match=message):
124
+ stats.quantile(x, xp.asarray([0.5, 0.6]), keepdims=False)
125
+
126
+
127
+ def _get_weights_x_rep(self, x, axis, rng):
128
+ x = np.swapaxes(x, axis, -1)
129
+ ndim = x.ndim
130
+ x = np.atleast_2d(x)
131
+ counts = rng.integers(10, size=x.shape[-1], dtype=np.int32)
132
+ x_rep = []
133
+ weights = []
134
+ for x_ in x:
135
+ counts_ = rng.permuted(counts)
136
+ x_rep.append(np.repeat(x_, counts_))
137
+ weights.append(counts_)
138
+ x_rep, weights = np.stack(x_rep), np.stack(weights)
139
+ if ndim < 2:
140
+ x_rep, weights = np.squeeze(x_rep, axis=0), np.squeeze(weights, axis=0)
141
+ x_rep, weights = np.swapaxes(x_rep, -1, axis), np.swapaxes(weights, -1, axis)
142
+ weights = np.asarray(weights, dtype=x.dtype)
143
+ return weights, x_rep
144
+
145
+
146
+ @skip_xp_backends(cpu_only=True, reason="PyTorch doesn't have `betainc`.",
147
+ exceptions=['cupy'])
148
+ @pytest.mark.parametrize('method',
149
+ ['inverted_cdf', 'averaged_inverted_cdf', 'closest_observation',
150
+ 'hazen', 'interpolated_inverted_cdf', 'linear',
151
+ 'median_unbiased', 'normal_unbiased', 'weibull',
152
+ 'harrell-davis', 'round_nearest', 'round_outward', 'round_inward',
153
+ '_lower', '_higher', '_midpoint', '_nearest'])
154
+ @pytest.mark.parametrize('shape_x, shape_p, axis',
155
+ [(10, None, -1), (10, 10, -1), (10, (2, 3), -1), ((10, 2), None, 0)])
156
+ @pytest.mark.parametrize('weights', [False, True])
157
+ def test_against_reference(self, method, shape_x, shape_p, axis, weights, xp):
158
+ # Test all methods with various data shapes
159
+ if weights and (method.startswith('_') or method.startswith('round')
160
+ or method=='harrell-davis'):
161
+ pytest.skip('`weights` not supported by private (legacy) methods.')
162
+ dtype = xp_default_dtype(xp)
163
+ rng = np.random.default_rng(23458924568734956)
164
+ x = rng.random(size=shape_x)
165
+ p = rng.random(size=shape_p)
166
+
167
+ if weights:
168
+ weights, x_rep = self._get_weights_x_rep(x, axis, rng)
169
+ else:
170
+ weights, x_rep = None, x
171
+
172
+ ref = quantile_reference(
173
+ x_rep, p, method=method[1:] if method.startswith('_') else method,
174
+ axis=axis, nan_policy='propagate', keepdims=shape_p is not None)
175
+
176
+ x, p = xp.asarray(x, dtype=dtype), xp.asarray(p, dtype=dtype)
177
+ weights = weights if weights is None else xp.asarray(weights, dtype=dtype)
178
+ res = stats.quantile(x, p, method=method, weights=weights, axis=axis)
179
+
180
+ xp_assert_close(res, xp.asarray(ref, dtype=dtype))
181
+
182
+ @pytest.mark.filterwarnings("ignore:torch.searchsorted:UserWarning")
183
+ @skip_xp_backends(cpu_only=True, reason="PyTorch doesn't have `betainc`.",
184
+ exceptions=['cupy', 'jax.numpy'])
185
+ @pytest.mark.parametrize('axis', [0, 1])
186
+ @pytest.mark.parametrize('keepdims', [False, True])
187
+ @pytest.mark.parametrize('nan_policy', ['omit', 'propagate', 'marray'])
188
+ @pytest.mark.parametrize('dtype', ['float32', 'float64'])
189
+ @pytest.mark.parametrize('method', ['linear', 'harrell-davis', 'round_nearest'])
190
+ @pytest.mark.parametrize('weights', [False, True])
191
+ def test_against_reference_2(self, axis, keepdims, nan_policy,
192
+ dtype, method, weights, xp):
193
+ # Test some methods with various combinations of arguments
194
+ if is_jax(xp) and nan_policy == 'marray': # mdhaber/marray#146
195
+ pytest.skip("`marray` currently incompatible with JAX")
196
+ if weights and method in {'harrell-davis', 'round_nearest'}:
197
+ pytest.skip("These methods don't yet support weights")
198
+ rng = np.random.default_rng(23458924568734956)
199
+ shape = (5, 6)
200
+ x = rng.random(size=shape).astype(dtype)
201
+ p = rng.random(size=shape).astype(dtype)
202
+ mask = rng.random(size=shape) > 0.8
203
+ assert np.any(mask)
204
+ x[mask] = np.nan
205
+ if not keepdims:
206
+ p = np.mean(p, axis=axis, keepdims=True)
207
+
208
+ # inject p = 0 and p = 1 to test edge cases
209
+ # Currently would fail with CuPy/JAX (cupy/cupy#8934, jax-ml/jax#21900);
210
+ # remove the `if` when those are resolved.
211
+ if is_numpy(xp):
212
+ p0 = p.ravel()
213
+ p0[1] = 0.
214
+ p0[-2] = 1.
215
+
216
+ dtype = getattr(xp, dtype)
217
+
218
+ if weights:
219
+ weights, x_rep = self._get_weights_x_rep(x, axis, rng)
220
+ weights = weights if weights is None else xp.asarray(weights)
221
+ else:
222
+ weights, x_rep = None, x
223
+
224
+ if nan_policy == 'marray':
225
+ if not SCIPY_ARRAY_API:
226
+ pytest.skip("MArray is only available if SCIPY_ARRAY_API=1")
227
+ if weights is not None:
228
+ pytest.skip("MArray is not yet compatible with weights")
229
+ marray = pytest.importorskip('marray')
230
+ kwargs = dict(axis=axis, keepdims=keepdims, method=method)
231
+ mxp = marray._get_namespace(xp)
232
+ x_mp = mxp.asarray(x, mask=mask)
233
+ weights = weights if weights is None else mxp.asarray(weights)
234
+ res = stats.quantile(x_mp, mxp.asarray(p), weights=weights, **kwargs)
235
+ ref = quantile_reference(x_rep, p, nan_policy='omit', **kwargs)
236
+ xp_assert_close(res.data, xp.asarray(ref, dtype=dtype))
237
+ return
238
+
239
+ kwargs = dict(axis=axis, keepdims=keepdims,
240
+ nan_policy=nan_policy, method=method)
241
+ res = stats.quantile(xp.asarray(x), xp.asarray(p), weights=weights, **kwargs)
242
+ ref = quantile_reference(x_rep, p, **kwargs)
243
+ xp_assert_close(res, xp.asarray(ref, dtype=dtype))
244
+
245
+ def test_integer_input_output_dtype(self, xp):
246
+ res = stats.quantile(xp.arange(10, dtype=xp.int64), 0.5)
247
+ assert res.dtype == xp_default_dtype(xp)
248
+
249
+ @pytest.mark.parametrize('x, p, ref, kwargs',
250
+ [([], 0.5, np.nan, {}),
251
+ ([1, 2, 3], [-1, 0, 1, 1.5, np.nan], [np.nan, 1, 3, np.nan, np.nan], {}),
252
+ ([1, 2, 3], [], [], {}),
253
+ ([[np.nan, 2]], 0.5, [np.nan, 2], {'nan_policy': 'omit'}),
254
+ ([[], []], 0.5, np.full(2, np.nan), {'axis': -1}),
255
+ ([[], []], 0.5, np.zeros((0,)), {'axis': 0, 'keepdims': False}),
256
+ ([[], []], 0.5, np.zeros((1, 0)), {'axis': 0, 'keepdims': True}),
257
+ ([], [0.5, 0.6], np.full(2, np.nan), {}),
258
+ (np.arange(1, 28).reshape((3, 3, 3)), 0.5, [[[14.]]],
259
+ {'axis': None, 'keepdims': True}),
260
+ ([[1, 2], [3, 4]], [0.25, 0.5, 0.75], [[1.75, 2.5, 3.25]],
261
+ {'axis': None, 'keepdims': True}),
262
+ # Known issue:
263
+ # ([1, 2, 3], 0.5, 2., {'weights': [0, 0, 0]})
264
+ # See https://github.com/scipy/scipy/pull/23941#issuecomment-3503554361
265
+ ])
266
+ def test_edge_cases(self, x, p, ref, kwargs, xp):
267
+ default_dtype = xp_default_dtype(xp)
268
+ x, p, ref = xp.asarray(x), xp.asarray(p), xp.asarray(ref, dtype=default_dtype)
269
+ res = stats.quantile(x, p, **kwargs)
270
+ xp_assert_equal(res, ref)
271
+
272
+ @pytest.mark.parametrize('axis', [0, 1, 2])
273
+ @pytest.mark.parametrize('keepdims', [False, True])
274
+ def test_size_0(self, axis, keepdims, xp):
275
+ shape = [3, 4, 0]
276
+ out_shape = shape.copy()
277
+ if keepdims:
278
+ out_shape[axis] = 1
279
+ else:
280
+ out_shape.pop(axis)
281
+ res = stats.quantile(xp.zeros(tuple(shape)), 0.5, axis=axis, keepdims=keepdims)
282
+ assert res.shape == tuple(out_shape)
283
+
284
+ @pytest.mark.parametrize('method',
285
+ ['inverted_cdf', 'averaged_inverted_cdf', 'closest_observation',
286
+ '_lower', '_higher', '_midpoint', '_nearest'])
287
+ def test_transition(self, method, xp):
288
+ # test that values of discontinuous estimators are correct when
289
+ # p*n + m - 1 is integral.
290
+ if method == 'closest_observation' and np.__version__ < '2.0.1':
291
+ pytest.skip('Bug in np.quantile (numpy/numpy#26656) fixed in 2.0.1')
292
+ x = np.arange(8., dtype=np.float64)
293
+ p = np.arange(0, 1.03125, 0.03125)
294
+ res = stats.quantile(xp.asarray(x), xp.asarray(p), method=method)
295
+ ref = np.quantile(x, p, method=method[1:] if method.startswith('_') else method)
296
+ xp_assert_equal(res, xp.asarray(ref, dtype=xp.float64))
297
+
298
+ @pytest.mark.parametrize('zero_weights', [False, True])
299
+ def test_weights_against_numpy(self, zero_weights, xp):
300
+ if is_numpy(xp) and xp.__version__ < "2.0":
301
+ pytest.skip('`weights` not supported by NumPy < 2.0.')
302
+ dtype = xp_default_dtype(xp)
303
+ rng = np.random.default_rng(85468924398205602)
304
+ method = 'inverted_cdf'
305
+ x = rng.random(size=100)
306
+ weights = rng.random(size=100)
307
+ if zero_weights:
308
+ weights[weights < 0.5] = 0
309
+ p = np.linspace(0., 1., 300)
310
+ res = stats.quantile(xp.asarray(x, dtype=dtype), xp.asarray(p, dtype=dtype),
311
+ method=method, weights=xp.asarray(weights, dtype=dtype))
312
+ ref = np.quantile(x, p, method=method, weights=weights)
313
+ xp_assert_close(res, xp.asarray(ref, dtype=dtype))
314
+
315
+ @pytest.mark.parametrize('method',
316
+ ['inverted_cdf', 'averaged_inverted_cdf', 'closest_observation', 'hazen',
317
+ 'interpolated_inverted_cdf', 'linear','median_unbiased', 'normal_unbiased',
318
+ 'weibull'])
319
+ def test_zero_weights(self, method, xp):
320
+ rng = np.random.default_rng(85468924398205602)
321
+
322
+ # test 1-D versus eliminating zero-weighted values
323
+ n = 100
324
+ x = xp.asarray(rng.random(size=n))
325
+ x0 = xp_copy(x)
326
+ p = xp.asarray(rng.random(size=n))
327
+ i_zero = xp.asarray(rng.random(size=n) < 0.1)
328
+ weights = xp.asarray(rng.random(size=n))
329
+ weights = xp.where(i_zero, 0., weights)
330
+ res = stats.quantile(x, p, weights=weights, method=method)
331
+ ref = stats.quantile(x[~i_zero], p, weights=weights[~i_zero], method=method)
332
+ xp_assert_close(res, ref)
333
+ xp_assert_equal(x, x0) # no input mutation
334
+
335
+ # test multi-D versus `nan_policy='omit'`
336
+ shape = (5, 100)
337
+ x = xp.asarray(rng.random(size=shape))
338
+ x0 = xp_copy(x)
339
+ p = xp.asarray(rng.random(size=shape))
340
+ i_zero = xp.asarray(rng.random(size=shape) < 0.1)
341
+ weights = xp.asarray(rng.random(size=shape))
342
+ x_nanned = xp.where(i_zero, xp.nan, x)
343
+ weights_zeroed = xp.where(i_zero, 0., weights)
344
+ res = stats.quantile(x, p, weights=weights_zeroed, method=method, axis=-1)
345
+ ref = stats.quantile(x_nanned, p, weights=weights,
346
+ nan_policy='omit', method=method, axis=-1)
347
+ xp_assert_close(res, ref)
348
+ xp_assert_equal(x, x0) # no input mutation
349
+
350
+ @pytest.mark.filterwarnings("ignore:torch.searchsorted:UserWarning")
351
+ @pytest.mark.parametrize('method',
352
+ ['inverted_cdf', 'averaged_inverted_cdf', 'closest_observation', 'hazen',
353
+ 'interpolated_inverted_cdf', 'linear','median_unbiased', 'normal_unbiased',
354
+ 'weibull'])
355
+ @pytest.mark.parametrize('shape', [50, (50, 3)])
356
+ def test_unity_weights(self, method, shape, xp):
357
+ # Check that result is unchanged if all weights are `1.0`
358
+ rng = np.random.default_rng(28546892439820560)
359
+ x = xp.asarray(rng.random(size=shape))
360
+ p = xp.asarray(rng.random(size=shape))
361
+ weights = xp.ones_like(x)
362
+ res = stats.quantile(x, p, weights=weights, method=method)
363
+ ref = stats.quantile(x, p, method=method)
364
+ xp_assert_close(res, ref)
365
+
366
+
367
+ @_apply_over_batch(('a', 1), ('v', 1))
368
+ def np_searchsorted(a, v, side):
369
+ return np.searchsorted(a, v, side=side)
370
+
371
+
372
+ @make_xp_test_case(_xp_searchsorted)
373
+ class Test_XPSearchsorted:
374
+ @pytest.mark.parametrize('side', ['left', 'right'])
375
+ @pytest.mark.parametrize('ties', [False, True])
376
+ @pytest.mark.parametrize('shape', [0, 1, 2, 10, 11, 1000, 10001,
377
+ (2, 0), (0, 2), (2, 10), (2, 3, 11)])
378
+ @pytest.mark.parametrize('nans_x', [False, True])
379
+ @pytest.mark.parametrize('infs_x', [False, True])
380
+ def test_nd(self, side, ties, shape, nans_x, infs_x, xp):
381
+ if nans_x and is_torch(xp):
382
+ pytest.skip('torch sorts NaNs differently')
383
+ rng = np.random.default_rng(945298725498274853)
384
+ if ties:
385
+ x = rng.integers(5, size=shape)
386
+ else:
387
+ x = rng.random(shape)
388
+ # float32 is to accommodate JAX - nextafter with `float64` is too small?
389
+ x = np.asarray(x, dtype=np.float32)
390
+ xr = np.nextafter(x, np.inf)
391
+ xl = np.nextafter(x, -np.inf)
392
+ x_ = np.asarray([-np.inf, np.inf, np.nan])
393
+ x_ = np.broadcast_to(x_, x.shape[:-1] + (3,))
394
+ y = rng.permuted(np.concatenate((xl, x, xr, x_), axis=-1), axis=-1)
395
+ if nans_x:
396
+ mask = rng.random(shape) < 0.1
397
+ x[mask] = np.nan
398
+ if infs_x:
399
+ mask = rng.random(shape) < 0.1
400
+ x[mask] = -np.inf
401
+ mask = rng.random(shape) > 0.9
402
+ x[mask] = np.inf
403
+ x = np.sort(x, axis=-1)
404
+ x, y = np.asarray(x, dtype=np.float64), np.asarray(y, dtype=np.float64)
405
+ xp_default_int = xp.asarray(1).dtype
406
+ if xp_size(x) == 0 and x.ndim > 0 and x.shape[-1] != 0:
407
+ ref = xp.empty(x.shape[:-1] + (y.shape[-1],), dtype=xp_default_int)
408
+ else:
409
+ ref = xp.asarray(np_searchsorted(x, y, side=side), dtype=xp_default_int)
410
+ x, y = xp.asarray(x), xp.asarray(y)
411
+ res = _xp_searchsorted(x, y, side=side)
412
+ xp_assert_equal(res, ref)
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_rank.py ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from numpy.testing import assert_equal, assert_array_equal
3
+ import pytest
4
+
5
+ from scipy import stats
6
+ from scipy.conftest import skip_xp_invalid_arg
7
+ from scipy.stats import rankdata, tiecorrect
8
+ from scipy._lib._array_api import xp_assert_equal, make_xp_test_case
9
+
10
+ skip_xp_backends = pytest.mark.skip_xp_backends
11
+
12
+ class TestTieCorrect:
13
+
14
+ def test_empty(self):
15
+ """An empty array requires no correction, should return 1.0."""
16
+ ranks = np.array([], dtype=np.float64)
17
+ c = tiecorrect(ranks)
18
+ assert_equal(c, 1.0)
19
+
20
+ def test_one(self):
21
+ """A single element requires no correction, should return 1.0."""
22
+ ranks = np.array([1.0], dtype=np.float64)
23
+ c = tiecorrect(ranks)
24
+ assert_equal(c, 1.0)
25
+
26
+ def test_no_correction(self):
27
+ """Arrays with no ties require no correction."""
28
+ ranks = np.arange(2.0)
29
+ c = tiecorrect(ranks)
30
+ assert_equal(c, 1.0)
31
+ ranks = np.arange(3.0)
32
+ c = tiecorrect(ranks)
33
+ assert_equal(c, 1.0)
34
+
35
+ def test_basic(self):
36
+ """Check a few basic examples of the tie correction factor."""
37
+ # One tie of two elements
38
+ ranks = np.array([1.0, 2.5, 2.5])
39
+ c = tiecorrect(ranks)
40
+ T = 2.0
41
+ N = ranks.size
42
+ expected = 1.0 - (T**3 - T) / (N**3 - N)
43
+ assert_equal(c, expected)
44
+
45
+ # One tie of two elements (same as above, but tie is not at the end)
46
+ ranks = np.array([1.5, 1.5, 3.0])
47
+ c = tiecorrect(ranks)
48
+ T = 2.0
49
+ N = ranks.size
50
+ expected = 1.0 - (T**3 - T) / (N**3 - N)
51
+ assert_equal(c, expected)
52
+
53
+ # One tie of three elements
54
+ ranks = np.array([1.0, 3.0, 3.0, 3.0])
55
+ c = tiecorrect(ranks)
56
+ T = 3.0
57
+ N = ranks.size
58
+ expected = 1.0 - (T**3 - T) / (N**3 - N)
59
+ assert_equal(c, expected)
60
+
61
+ # Two ties, lengths 2 and 3.
62
+ ranks = np.array([1.5, 1.5, 4.0, 4.0, 4.0])
63
+ c = tiecorrect(ranks)
64
+ T1 = 2.0
65
+ T2 = 3.0
66
+ N = ranks.size
67
+ expected = 1.0 - ((T1**3 - T1) + (T2**3 - T2)) / (N**3 - N)
68
+ assert_equal(c, expected)
69
+
70
+ def test_overflow(self):
71
+ ntie, k = 2000, 5
72
+ a = np.repeat(np.arange(k), ntie)
73
+ n = a.size # ntie * k
74
+ out = tiecorrect(rankdata(a))
75
+ assert_equal(out, 1.0 - k * (ntie**3 - ntie) / float(n**3 - n))
76
+
77
+
78
+ @make_xp_test_case(stats.rankdata)
79
+ class TestRankData:
80
+
81
+ def desired_dtype(self, method='average', has_nans=False, *, xp):
82
+ if has_nans:
83
+ return xp.asarray(1.).dtype
84
+ return xp.asarray(1.).dtype if method=='average' else xp.asarray(1).dtype
85
+
86
+ def test_empty(self, xp):
87
+ """stats.rankdata of empty array should return an empty array."""
88
+ a = xp.asarray([], dtype=xp.int64)
89
+ r = rankdata(a)
90
+ xp_assert_equal(r, xp.asarray([], dtype=self.desired_dtype(xp=xp)))
91
+
92
+ def test_list(self):
93
+ # test that NumPy still accepts lists
94
+ r = rankdata([])
95
+ assert_array_equal(r, np.array([]))
96
+
97
+ r = rankdata([40, 10, 30, 10, 50])
98
+ assert_equal(r, [4.0, 1.5, 3.0, 1.5, 5.0])
99
+
100
+ @pytest.mark.parametrize("shape", [(0, 1, 2)])
101
+ @pytest.mark.parametrize("axis", [None, *range(3)])
102
+ def test_empty_multidim(self, shape, axis, xp):
103
+ a = xp.empty(shape, dtype=xp.int64)
104
+ r = rankdata(a, axis=axis)
105
+ expected_shape = (0,) if axis is None else shape
106
+ xp_assert_equal(r, xp.empty(expected_shape, dtype=self.desired_dtype(xp=xp)))
107
+
108
+ def test_one(self, xp):
109
+ """Check stats.rankdata with an array of length 1."""
110
+ data = [100]
111
+ a = xp.asarray(data, dtype=xp.int64)
112
+ r = rankdata(a)
113
+ xp_assert_equal(r, xp.asarray([1.0], dtype=self.desired_dtype(xp=xp)))
114
+
115
+ def test_basic(self, xp):
116
+ """Basic tests of stats.rankdata."""
117
+ desired_dtype = self.desired_dtype(xp=xp)
118
+
119
+ data = [100, 10, 50]
120
+ expected = xp.asarray([3.0, 1.0, 2.0], dtype=desired_dtype)
121
+ a = xp.asarray(data, dtype=xp.int64)
122
+ r = rankdata(a)
123
+ xp_assert_equal(r, expected)
124
+
125
+ data = [40, 10, 30, 10, 50]
126
+ expected = xp.asarray([4.0, 1.5, 3.0, 1.5, 5.0], dtype=desired_dtype)
127
+ a = xp.asarray(data, dtype=xp.int64)
128
+ r = rankdata(a)
129
+ xp_assert_equal(r, expected)
130
+
131
+ data = [20, 20, 20, 10, 10, 10]
132
+ expected = xp.asarray([5.0, 5.0, 5.0, 2.0, 2.0, 2.0], dtype=desired_dtype)
133
+ a = xp.asarray(data, dtype=xp.int64)
134
+ r = rankdata(a)
135
+ xp_assert_equal(r, expected)
136
+
137
+ # # The docstring states explicitly that the argument is flattened.
138
+ a2d = xp.reshape(a, (2, 3))
139
+ r = rankdata(a2d)
140
+ xp_assert_equal(r, expected)
141
+
142
+ @skip_xp_invalid_arg
143
+ def test_rankdata_object_string(self):
144
+
145
+ def min_rank(a):
146
+ return [1 + sum(i < j for i in a) for j in a]
147
+
148
+ def max_rank(a):
149
+ return [sum(i <= j for i in a) for j in a]
150
+
151
+ def ordinal_rank(a):
152
+ return min_rank([(x, i) for i, x in enumerate(a)])
153
+
154
+ def average_rank(a):
155
+ return [(i + j) / 2.0 for i, j in zip(min_rank(a), max_rank(a))]
156
+
157
+ def dense_rank(a):
158
+ b = np.unique(a)
159
+ return [1 + sum(i < j for i in b) for j in a]
160
+
161
+ rankf = dict(min=min_rank, max=max_rank, ordinal=ordinal_rank,
162
+ average=average_rank, dense=dense_rank)
163
+
164
+ def check_ranks(a):
165
+ for method in 'min', 'max', 'dense', 'ordinal', 'average':
166
+ out = rankdata(a, method=method)
167
+ assert_array_equal(out, rankf[method](a))
168
+
169
+ val = ['foo', 'bar', 'qux', 'xyz', 'abc', 'efg', 'ace', 'qwe', 'qaz']
170
+ check_ranks(np.random.choice(val, 200))
171
+ check_ranks(np.random.choice(val, 200).astype('object'))
172
+
173
+ val = np.array([0, 1, 2, 2.718, 3, 3.141], dtype='object')
174
+ check_ranks(np.random.choice(val, 200).astype('object'))
175
+
176
+ @pytest.mark.skip_xp_backends("torch", reason="`take_along_axis` fails with uint64")
177
+ def test_large_uint(self, xp):
178
+ data = xp.asarray([2**60, 2**60+1], dtype=xp.uint64)
179
+ r = rankdata(data)
180
+ xp_assert_equal(r, xp.asarray([1.0, 2.0], dtype=self.desired_dtype(xp=xp)))
181
+
182
+ def test_large_int(self, xp):
183
+ data = xp.asarray([2**60, 2**60+1], dtype=xp.int64)
184
+ r = rankdata(data)
185
+ xp_assert_equal(r, xp.asarray([1.0, 2.0], dtype=self.desired_dtype(xp=xp)))
186
+
187
+ data = xp.asarray([2**60, -2**60+1], dtype=xp.int64)
188
+ r = rankdata(data)
189
+ xp_assert_equal(r, xp.asarray([2.0, 1.0], dtype=self.desired_dtype(xp=xp)))
190
+
191
+ @pytest.mark.parametrize('n', [10000, 100000, 1000000])
192
+ def test_big_tie(self, n, xp):
193
+ data = xp.ones(n)
194
+ r = rankdata(data)
195
+ expected_rank = 0.5 * (n + 1)
196
+ ref = xp.asarray(expected_rank * data, dtype=self.desired_dtype(xp=xp))
197
+ xp_assert_equal(r, ref)
198
+
199
+ def test_axis(self, xp):
200
+ data = xp.asarray([[0, 2, 1], [4, 2, 2]])
201
+
202
+ expected0 = xp.asarray([[1., 1.5, 1.], [2., 1.5, 2.]])
203
+ r0 = rankdata(data, axis=0)
204
+ xp_assert_equal(r0, expected0)
205
+
206
+ expected1 = xp.asarray([[1., 3., 2.], [3., 1.5, 1.5]])
207
+ r1 = rankdata(data, axis=1)
208
+ xp_assert_equal(r1, expected1)
209
+
210
+ methods= ["average", "min", "max", "dense", "ordinal"]
211
+
212
+ @pytest.mark.parametrize("axis", [0, 1])
213
+ @pytest.mark.parametrize("method", methods)
214
+ def test_size_0_axis(self, axis, method, xp):
215
+ shape = (3, 0)
216
+ desired_dtype = self.desired_dtype(method, xp=xp)
217
+ data = xp.zeros(shape)
218
+ r = rankdata(data, method=method, axis=axis)
219
+ assert_equal(r.shape, shape)
220
+ assert_equal(r.dtype, desired_dtype)
221
+ xp_assert_equal(r, xp.empty(shape, dtype=desired_dtype))
222
+
223
+ @pytest.mark.parametrize('axis', range(3))
224
+ @pytest.mark.parametrize('method', methods)
225
+ def test_nan_policy_omit_3d(self, axis, method):
226
+ shape = (20, 21, 22)
227
+ rng = np.random.RandomState(23983242)
228
+
229
+ a = rng.random(size=shape)
230
+ i = rng.random(size=shape) < 0.4
231
+ j = rng.random(size=shape) < 0.1
232
+ k = rng.random(size=shape) < 0.1
233
+ a[i] = np.nan
234
+ a[j] = -np.inf
235
+ a[k] - np.inf
236
+
237
+ def rank_1d_omit(a, method):
238
+ out = np.zeros_like(a)
239
+ i = np.isnan(a)
240
+ a_compressed = a[~i]
241
+ res = rankdata(a_compressed, method)
242
+ out[~i] = res
243
+ out[i] = np.nan
244
+ return out
245
+
246
+ def rank_omit(a, method, axis):
247
+ return np.apply_along_axis(lambda a: rank_1d_omit(a, method),
248
+ axis, a)
249
+
250
+ res = rankdata(a, method, axis=axis, nan_policy='omit')
251
+ res0 = rank_omit(a, method, axis=axis)
252
+
253
+ assert_array_equal(res, res0)
254
+
255
+ def test_nan_policy_2d_axis_none(self):
256
+ # 2 2d-array test with axis=None
257
+ data = [[0, np.nan, 3],
258
+ [4, 2, np.nan],
259
+ [1, 2, 2]]
260
+ assert_array_equal(rankdata(data, axis=None, nan_policy='omit'),
261
+ [1., np.nan, 6., 7., 4., np.nan, 2., 4., 4.])
262
+ assert_array_equal(rankdata(data, axis=None, nan_policy='propagate'),
263
+ [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
264
+ np.nan, np.nan, np.nan])
265
+
266
+ def test_nan_policy_raise(self):
267
+ # 1 1d-array test
268
+ data = [0, 2, 3, -2, np.nan, np.nan]
269
+ with pytest.raises(ValueError, match="The input contains nan"):
270
+ rankdata(data, nan_policy='raise')
271
+
272
+ # 2 2d-array test
273
+ data = [[0, np.nan, 3],
274
+ [4, 2, np.nan],
275
+ [np.nan, 2, 2]]
276
+
277
+ with pytest.raises(ValueError, match="The input contains nan"):
278
+ rankdata(data, axis=0, nan_policy="raise")
279
+
280
+ with pytest.raises(ValueError, match="The input contains nan"):
281
+ rankdata(data, axis=1, nan_policy="raise")
282
+
283
+ def test_nan_policy_propagate(self):
284
+ # 1 1d-array test
285
+ data = [0, 2, 3, -2, np.nan, np.nan]
286
+ assert_array_equal(rankdata(data, nan_policy='propagate'),
287
+ [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
288
+
289
+ # 2 2d-array test
290
+ data = [[0, np.nan, 3],
291
+ [4, 2, np.nan],
292
+ [1, 2, 2]]
293
+ assert_array_equal(rankdata(data, axis=0, nan_policy='propagate'),
294
+ [[1, np.nan, np.nan],
295
+ [3, np.nan, np.nan],
296
+ [2, np.nan, np.nan]])
297
+ assert_array_equal(rankdata(data, axis=1, nan_policy='propagate'),
298
+ [[np.nan, np.nan, np.nan],
299
+ [np.nan, np.nan, np.nan],
300
+ [1, 2.5, 2.5]])
301
+
302
+ _rankdata_cases = (
303
+ # values, method, expected
304
+ ([], 'average', []),
305
+ ([], 'min', []),
306
+ ([], 'max', []),
307
+ ([], 'dense', []),
308
+ ([], 'ordinal', []),
309
+ #
310
+ ([100], 'average', [1.0]),
311
+ ([100], 'min', [1.0]),
312
+ ([100], 'max', [1.0]),
313
+ ([100], 'dense', [1.0]),
314
+ ([100], 'ordinal', [1.0]),
315
+ #
316
+ ([100, 100, 100], 'average', [2.0, 2.0, 2.0]),
317
+ ([100, 100, 100], 'min', [1.0, 1.0, 1.0]),
318
+ ([100, 100, 100], 'max', [3.0, 3.0, 3.0]),
319
+ ([100, 100, 100], 'dense', [1.0, 1.0, 1.0]),
320
+ ([100, 100, 100], 'ordinal', [1.0, 2.0, 3.0]),
321
+ #
322
+ ([100, 300, 200], 'average', [1.0, 3.0, 2.0]),
323
+ ([100, 300, 200], 'min', [1.0, 3.0, 2.0]),
324
+ ([100, 300, 200], 'max', [1.0, 3.0, 2.0]),
325
+ ([100, 300, 200], 'dense', [1.0, 3.0, 2.0]),
326
+ ([100, 300, 200], 'ordinal', [1.0, 3.0, 2.0]),
327
+ #
328
+ ([100, 200, 300, 200], 'average', [1.0, 2.5, 4.0, 2.5]),
329
+ ([100, 200, 300, 200], 'min', [1.0, 2.0, 4.0, 2.0]),
330
+ ([100, 200, 300, 200], 'max', [1.0, 3.0, 4.0, 3.0]),
331
+ ([100, 200, 300, 200], 'dense', [1.0, 2.0, 3.0, 2.0]),
332
+ ([100, 200, 300, 200], 'ordinal', [1.0, 2.0, 4.0, 3.0]),
333
+ #
334
+ ([100, 200, 300, 200, 100], 'average', [1.5, 3.5, 5.0, 3.5, 1.5]),
335
+ ([100, 200, 300, 200, 100], 'min', [1.0, 3.0, 5.0, 3.0, 1.0]),
336
+ ([100, 200, 300, 200, 100], 'max', [2.0, 4.0, 5.0, 4.0, 2.0]),
337
+ ([100, 200, 300, 200, 100], 'dense', [1.0, 2.0, 3.0, 2.0, 1.0]),
338
+ ([100, 200, 300, 200, 100], 'ordinal', [1.0, 3.0, 5.0, 4.0, 2.0]),
339
+ #
340
+ ([10] * 30, 'ordinal', np.arange(1.0, 31.0)),
341
+ )
342
+
343
+ @pytest.mark.parametrize('case', _rankdata_cases)
344
+ def test_cases(self, case, xp):
345
+ values, method, expected = case
346
+ r = rankdata(xp.asarray(values), method=method)
347
+ ref = xp.asarray(expected, dtype=self.desired_dtype(method, xp=xp))
348
+ xp_assert_equal(r, ref)
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_relative_risk.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import numpy as np
3
+ from numpy.testing import assert_allclose, assert_equal
4
+ from scipy.stats.contingency import relative_risk
5
+
6
+
7
+ # Test just the calculation of the relative risk, including edge
8
+ # cases that result in a relative risk of 0, inf or nan.
9
+ @pytest.mark.parametrize(
10
+ 'exposed_cases, exposed_total, control_cases, control_total, expected_rr',
11
+ [(1, 4, 3, 8, 0.25 / 0.375),
12
+ (0, 10, 5, 20, 0),
13
+ (0, 10, 0, 20, np.nan),
14
+ (5, 15, 0, 20, np.inf)]
15
+ )
16
+ def test_relative_risk(exposed_cases, exposed_total,
17
+ control_cases, control_total, expected_rr):
18
+ result = relative_risk(exposed_cases, exposed_total,
19
+ control_cases, control_total)
20
+ assert_allclose(result.relative_risk, expected_rr, rtol=1e-13)
21
+
22
+
23
+ def test_relative_risk_confidence_interval():
24
+ result = relative_risk(exposed_cases=16, exposed_total=128,
25
+ control_cases=24, control_total=256)
26
+ rr = result.relative_risk
27
+ ci = result.confidence_interval(confidence_level=0.95)
28
+ # The corresponding calculation in R using the epitools package.
29
+ #
30
+ # > library(epitools)
31
+ # > c <- matrix(c(232, 112, 24, 16), nrow=2)
32
+ # > result <- riskratio(c)
33
+ # > result$measure
34
+ # risk ratio with 95% C.I.
35
+ # Predictor estimate lower upper
36
+ # Exposed1 1.000000 NA NA
37
+ # Exposed2 1.333333 0.7347317 2.419628
38
+ #
39
+ # The last line is the result that we want.
40
+ assert_allclose(rr, 4/3)
41
+ assert_allclose((ci.low, ci.high), (0.7347317, 2.419628), rtol=5e-7)
42
+
43
+
44
+ def test_relative_risk_ci_conflevel0():
45
+ result = relative_risk(exposed_cases=4, exposed_total=12,
46
+ control_cases=5, control_total=30)
47
+ rr = result.relative_risk
48
+ assert_allclose(rr, 2.0, rtol=1e-14)
49
+ ci = result.confidence_interval(0)
50
+ assert_allclose((ci.low, ci.high), (2.0, 2.0), rtol=1e-12)
51
+
52
+
53
+ def test_relative_risk_ci_conflevel1():
54
+ result = relative_risk(exposed_cases=4, exposed_total=12,
55
+ control_cases=5, control_total=30)
56
+ ci = result.confidence_interval(1)
57
+ assert_equal((ci.low, ci.high), (0, np.inf))
58
+
59
+
60
+ def test_relative_risk_ci_edge_cases_00():
61
+ result = relative_risk(exposed_cases=0, exposed_total=12,
62
+ control_cases=0, control_total=30)
63
+ assert_equal(result.relative_risk, np.nan)
64
+ ci = result.confidence_interval()
65
+ assert_equal((ci.low, ci.high), (np.nan, np.nan))
66
+
67
+
68
+ def test_relative_risk_ci_edge_cases_01():
69
+ result = relative_risk(exposed_cases=0, exposed_total=12,
70
+ control_cases=1, control_total=30)
71
+ assert_equal(result.relative_risk, 0)
72
+ ci = result.confidence_interval()
73
+ assert_equal((ci.low, ci.high), (0.0, np.nan))
74
+
75
+
76
+ def test_relative_risk_ci_edge_cases_10():
77
+ result = relative_risk(exposed_cases=1, exposed_total=12,
78
+ control_cases=0, control_total=30)
79
+ assert_equal(result.relative_risk, np.inf)
80
+ ci = result.confidence_interval()
81
+ assert_equal((ci.low, ci.high), (np.nan, np.inf))
82
+
83
+
84
+ @pytest.mark.parametrize('ec, et, cc, ct', [(0, 0, 10, 20),
85
+ (-1, 10, 1, 5),
86
+ (1, 10, 0, 0),
87
+ (1, 10, -1, 4)])
88
+ def test_relative_risk_bad_value(ec, et, cc, ct):
89
+ with pytest.raises(ValueError, match="must be an integer not less than"):
90
+ relative_risk(ec, et, cc, ct)
91
+
92
+
93
+ def test_relative_risk_bad_type():
94
+ with pytest.raises(TypeError, match="must be an integer"):
95
+ relative_risk(1, 10, 2.0, 40)
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_resampling.py ADDED
@@ -0,0 +1,2046 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+
3
+ import pytest
4
+
5
+ import numpy as np
6
+ from numpy.testing import assert_allclose, assert_equal
7
+
8
+ from scipy._lib._util import rng_integers
9
+ from scipy._lib._array_api import (is_numpy, make_xp_test_case, xp_default_dtype,
10
+ xp_size, array_namespace, _xp_copy_to_numpy)
11
+ from scipy._lib._array_api_no_0d import xp_assert_close, xp_assert_equal
12
+ from scipy._lib import array_api_extra as xpx
13
+ from scipy import stats, special
14
+ from scipy.fft.tests.test_fftlog import skip_xp_backends
15
+ from scipy.optimize import root
16
+
17
+ from scipy.stats import bootstrap, monte_carlo_test, permutation_test, power
18
+ import scipy.stats._resampling as _resampling
19
+
20
+
21
+ @make_xp_test_case(bootstrap)
22
+ class TestBootstrap:
23
+ @skip_xp_backends('numpy', reason='NumPy does not raise')
24
+ def test_bootstrap_iv_other(self, xp):
25
+ message = f"When using array library {xp.__name__}"
26
+ with pytest.raises(TypeError, match=message):
27
+ bootstrap((xp.asarray([1, 2, 3]),), lambda x: xp.mean(x))
28
+
29
+ def test_bootstrap_iv(self, xp):
30
+ sample = xp.asarray([1, 2, 3])
31
+
32
+ message = "`data` must contain at least one sample."
33
+ with pytest.raises(ValueError, match=message):
34
+ bootstrap(tuple(), xp.mean)
35
+
36
+ message = "each sample in `data` must contain two or more observations..."
37
+ with pytest.raises(ValueError, match=message):
38
+ bootstrap((sample, xp.asarray([1])), xp.mean)
39
+
40
+ message = ("When `paired is True`, all samples must have the same length ")
41
+ with pytest.raises(ValueError, match=message):
42
+ bootstrap((sample, xp.asarray([1, 2, 3, 4])), xp.mean, paired=True)
43
+
44
+ message = "`vectorized` must be `True`, `False`, or `None`."
45
+ with pytest.raises(ValueError, match=message):
46
+ bootstrap(sample, xp.mean, vectorized='ekki')
47
+
48
+ message = "`axis` must be an integer."
49
+ with pytest.raises(ValueError, match=message):
50
+ bootstrap((sample,), xp.mean, axis=1.5)
51
+
52
+ message = "could not convert string to float"
53
+ with pytest.raises(ValueError, match=message):
54
+ bootstrap((sample,), xp.mean, confidence_level='ni')
55
+
56
+ message = "`n_resamples` must be a non-negative integer."
57
+ with pytest.raises(ValueError, match=message):
58
+ bootstrap((sample,), xp.mean, n_resamples=-1000)
59
+
60
+ message = "`n_resamples` must be a non-negative integer."
61
+ with pytest.raises(ValueError, match=message):
62
+ bootstrap((sample,), xp.mean, n_resamples=1000.5)
63
+
64
+ message = "`batch` must be a positive integer or None."
65
+ with pytest.raises(ValueError, match=message):
66
+ bootstrap((sample,), xp.mean, batch=-1000)
67
+
68
+ message = "`batch` must be a positive integer or None."
69
+ with pytest.raises(ValueError, match=message):
70
+ bootstrap((sample,), xp.mean, batch=1000.5)
71
+
72
+ message = "`method` must be in"
73
+ with pytest.raises(ValueError, match=message):
74
+ bootstrap((sample,), xp.mean, method='ekki')
75
+
76
+ message = "`bootstrap_result` must have attribute `bootstrap_distribution'"
77
+ with pytest.raises(ValueError, match=message):
78
+ bootstrap((sample,), xp.mean, bootstrap_result=10)
79
+
80
+ message = "Either `bootstrap_result.bootstrap_distribution.size`"
81
+ with pytest.raises(ValueError, match=message):
82
+ bootstrap((sample,), xp.mean, n_resamples=0)
83
+
84
+ message = "SeedSequence expects int or sequence of ints"
85
+ with pytest.raises(TypeError, match=message):
86
+ bootstrap((sample,), xp.mean, rng='herring')
87
+
88
+ @pytest.mark.parametrize("method", ['basic', 'percentile', 'BCa'])
89
+ @pytest.mark.parametrize("axis", [0, 1, 2])
90
+ def test_bootstrap_batch(self, method, axis, xp):
91
+ # for one-sample statistics, batch size shouldn't affect the result
92
+ rng = np.random.RandomState(0)
93
+
94
+ x = rng.rand(10, 11, 12)
95
+ # SPEC-007 leave one call with random_state to ensure it still works
96
+ res1 = bootstrap((xp.asarray(x),), xp.mean, batch=None, method=method,
97
+ random_state=0, axis=axis, n_resamples=100)
98
+ rng = np.random.RandomState(0)
99
+ res2 = bootstrap((xp.asarray(x),), xp.mean, batch=10, method=method,
100
+ axis=axis, n_resamples=100, random_state=rng)
101
+
102
+ xp_assert_equal(res2.confidence_interval.low, res1.confidence_interval.low)
103
+ xp_assert_equal(res2.confidence_interval.high, res1.confidence_interval.high)
104
+ xp_assert_equal(res2.standard_error, res1.standard_error)
105
+
106
+ @pytest.mark.parametrize("method", ['basic', 'percentile', 'BCa'])
107
+ def test_bootstrap_paired(self, method, xp):
108
+ # test that `paired` works as expected
109
+ rng = np.random.RandomState(0)
110
+ n = 100
111
+ x = xp.asarray(rng.rand(n))
112
+ y = xp.asarray(rng.rand(n))
113
+
114
+ def my_statistic(x, y, axis=-1):
115
+ return xp.mean((x-y)**2, axis=axis)
116
+
117
+ def my_paired_statistic(i, axis=-1):
118
+ a = x[i]
119
+ b = y[i]
120
+ res = my_statistic(a, b)
121
+ return res
122
+
123
+ i = xp.arange(x.shape[0])
124
+
125
+ res1 = bootstrap((i,), my_paired_statistic, rng=0)
126
+ res2 = bootstrap((x, y), my_statistic, paired=True, rng=0)
127
+
128
+ xp_assert_close(res1.confidence_interval.low, res2.confidence_interval.low)
129
+ xp_assert_close(res1.confidence_interval.high, res2.confidence_interval.high)
130
+ xp_assert_close(res1.standard_error, res2.standard_error)
131
+
132
+ @pytest.mark.parametrize("method", ['basic', 'percentile', 'BCa'])
133
+ @pytest.mark.parametrize("axis", [0, 1, 2])
134
+ @pytest.mark.parametrize("paired", [True, False])
135
+ def test_bootstrap_vectorized(self, method, axis, paired, xp):
136
+ # test that paired is vectorized as expected: when samples are tiled,
137
+ # CI and standard_error of each axis-slice is the same as those of the
138
+ # original 1d sample
139
+
140
+ rng = np.random.RandomState(0)
141
+
142
+ def my_statistic(x, y, z, axis=-1):
143
+ return xp.mean(x, axis=axis) + xp.mean(y, axis=axis) + xp.mean(z, axis=axis)
144
+
145
+ shape = 10, 11, 12
146
+ n_samples = shape[axis]
147
+
148
+ x = rng.rand(n_samples)
149
+ y = rng.rand(n_samples)
150
+ z = rng.rand(n_samples)
151
+ x, y, z = xp.asarray(x), xp.asarray(y), xp.asarray(z)
152
+
153
+ res1 = bootstrap((x, y, z), my_statistic, paired=paired, method=method,
154
+ rng=0, axis=0, n_resamples=100)
155
+ assert (res1.bootstrap_distribution.shape
156
+ == res1.standard_error.shape + (100,))
157
+
158
+ reshape = [1, 1, 1]
159
+ reshape[axis] = n_samples
160
+ reshape = tuple(reshape)
161
+ x = xp.broadcast_to(xp.reshape(x, reshape), shape)
162
+ y = xp.broadcast_to(xp.reshape(y, reshape), shape)
163
+ z = xp.broadcast_to(xp.reshape(z, reshape), shape)
164
+ res2 = bootstrap((x, y, z), my_statistic, paired=paired, method=method,
165
+ rng=0, axis=axis, n_resamples=100)
166
+
167
+ result_shape = list(shape)
168
+ result_shape.pop(axis)
169
+
170
+ ref_ci_low = xp.broadcast_to(res2.confidence_interval.low, result_shape)
171
+ ref_ci_high = xp.broadcast_to(res2.confidence_interval.high, result_shape)
172
+ ref_ci_standard_error = xp.broadcast_to(res2.standard_error, result_shape)
173
+
174
+ xp_assert_close(res2.confidence_interval.low, ref_ci_low)
175
+ xp_assert_close(res2.confidence_interval.high, ref_ci_high)
176
+ xp_assert_close(res2.standard_error, ref_ci_standard_error)
177
+
178
+ @pytest.mark.slow
179
+ @pytest.mark.xfail_on_32bit("MemoryError with BCa observed in CI")
180
+ @pytest.mark.parametrize("method", ['basic', 'percentile', 'BCa'])
181
+ def test_bootstrap_against_theory(self, method, xp):
182
+ # based on https://www.statology.org/confidence-intervals-python/
183
+ rng = np.random.default_rng(2442101192988600726)
184
+ data = stats.norm.rvs(loc=5, scale=2, size=5000, random_state=rng)
185
+ alpha = 0.95
186
+ dist = stats.t(df=len(data)-1, loc=np.mean(data), scale=stats.sem(data))
187
+ ref_low, ref_high = dist.interval(confidence=alpha)
188
+ ref_se = dist.std()
189
+
190
+ config = dict(data=(xp.asarray(data),), statistic=xp.mean, n_resamples=5000,
191
+ method=method, rng=rng)
192
+ res = bootstrap(**config, confidence_level=alpha)
193
+
194
+ xp_assert_close(res.confidence_interval.low, xp.asarray(ref_low), rtol=5e-4)
195
+ xp_assert_close(res.confidence_interval.high, xp.asarray(ref_high), rtol=5e-4)
196
+ xp_assert_close(res.standard_error, xp.asarray(ref_se), atol=3e-4)
197
+
198
+ config.update(dict(n_resamples=0, bootstrap_result=res))
199
+ res = bootstrap(**config, confidence_level=alpha, alternative='less')
200
+ xp_assert_close(res.confidence_interval.high,
201
+ xp.asarray(dist.ppf(alpha)), rtol=5e-4)
202
+
203
+ config.update(dict(n_resamples=0, bootstrap_result=res))
204
+ res = bootstrap(**config, confidence_level=alpha, alternative='greater')
205
+ xp_assert_close(res.confidence_interval.low,
206
+ xp.asarray(dist.ppf(1-alpha)), rtol=5e-4)
207
+
208
+ tests_R = [("basic", 23.77, 79.12),
209
+ ("percentile", 28.86, 84.21),
210
+ ("BCa", 32.31, 91.43)]
211
+
212
+ @pytest.mark.parametrize("method, ref_low, ref_high", tests_R)
213
+ def test_bootstrap_against_R(self, method, ref_low, ref_high, xp):
214
+ # Compare against R's "boot" library
215
+ # library(boot)
216
+
217
+ # stat <- function (x, a) {
218
+ # mean(x[a])
219
+ # }
220
+
221
+ # x <- c(10, 12, 12.5, 12.5, 13.9, 15, 21, 22,
222
+ # 23, 34, 50, 81, 89, 121, 134, 213)
223
+
224
+ # # Use a large value so we get a few significant digits for the CI.
225
+ # n = 1000000
226
+ # bootresult = boot(x, stat, n)
227
+ # result <- boot.ci(bootresult)
228
+ # print(result)
229
+ x = xp.asarray([10, 12, 12.5, 12.5, 13.9, 15, 21, 22,
230
+ 23, 34, 50, 81, 89, 121, 134, 213])
231
+ res = bootstrap((x,), xp.mean, n_resamples=1000000, method=method, rng=0)
232
+ xp_assert_close(res.confidence_interval.low, xp.asarray(ref_low), rtol=0.005)
233
+ xp_assert_close(res.confidence_interval.high, xp.asarray(ref_high), rtol=0.005)
234
+
235
+ def test_multisample_BCa_against_R(self, xp):
236
+ # Because bootstrap is stochastic, it's tricky to test against reference
237
+ # behavior. Here, we show that SciPy's BCa CI matches R wboot's BCa CI
238
+ # much more closely than the other SciPy CIs do.
239
+
240
+ # arbitrary skewed data
241
+ x = xp.asarray([0.75859206, 0.5910282, -0.4419409, -0.36654601,
242
+ 0.34955357, -1.38835871, 0.76735821])
243
+ y = xp.asarray([1.41186073, 0.49775975, 0.08275588, 0.24086388,
244
+ 0.03567057, 0.52024419, 0.31966611, 1.32067634])
245
+
246
+ # a multi-sample statistic for which the BCa CI tends to be different
247
+ # from the other CIs
248
+ def statistic(x, y, axis):
249
+ s1 = stats.skew(x, axis=axis)
250
+ s2 = stats.skew(y, axis=axis)
251
+ return s1 - s2
252
+
253
+ # compute confidence intervals using each method
254
+ rng = np.random.default_rng(468865032284792692)
255
+
256
+ res_basic = stats.bootstrap((x, y), statistic, method='basic',
257
+ batch=100, rng=rng)
258
+ res_percent = stats.bootstrap((x, y), statistic, method='percentile',
259
+ batch=100, rng=rng)
260
+ res_bca = stats.bootstrap((x, y), statistic, method='bca',
261
+ batch=100, rng=rng)
262
+
263
+ # compute midpoints so we can compare just one number for each
264
+ mid_basic = xp.mean(xp.stack(res_basic.confidence_interval))
265
+ mid_percent = xp.mean(xp.stack(res_percent.confidence_interval))
266
+ mid_bca = xp.mean(xp.stack(res_bca.confidence_interval))
267
+
268
+ # reference for BCA CI computed using R wboot package:
269
+ # library(wBoot)
270
+ # library(moments)
271
+
272
+ # x = c(0.75859206, 0.5910282, -0.4419409, -0.36654601,
273
+ # 0.34955357, -1.38835871, 0.76735821)
274
+ # y = c(1.41186073, 0.49775975, 0.08275588, 0.24086388,
275
+ # 0.03567057, 0.52024419, 0.31966611, 1.32067634)
276
+
277
+ # twoskew <- function(x1, y1) {skewness(x1) - skewness(y1)}
278
+ # boot.two.bca(x, y, skewness, conf.level = 0.95,
279
+ # R = 9999, stacked = FALSE)
280
+ mid_wboot = -1.5519
281
+
282
+ # compute percent difference relative to wboot BCA method
283
+ diff_basic = (mid_basic - mid_wboot)/abs(mid_wboot)
284
+ diff_percent = (mid_percent - mid_wboot)/abs(mid_wboot)
285
+ diff_bca = (mid_bca - mid_wboot)/abs(mid_wboot)
286
+
287
+ # SciPy's BCa CI midpoint is much closer than that of the other methods
288
+ assert diff_basic < -0.15
289
+ assert diff_percent > 0.15
290
+ assert abs(diff_bca) < 0.03
291
+
292
+ def test_BCa_acceleration_against_reference(self, xp):
293
+ # Compare the (deterministic) acceleration parameter for a multi-sample
294
+ # problem against a reference value. The example is from [1], but Efron's
295
+ # value seems inaccurate. Straightforward code for computing the
296
+ # reference acceleration (0.011008228344026734) is available at:
297
+ # https://github.com/scipy/scipy/pull/16455#issuecomment-1193400981
298
+
299
+ y = xp.asarray([10., 27., 31., 40., 46., 50., 52., 104., 146.])
300
+ z = xp.asarray([16., 23., 38., 94., 99., 141., 197.])
301
+
302
+ def statistic(z, y, axis=0):
303
+ return xp.mean(z, axis=axis) - xp.mean(y, axis=axis)
304
+
305
+ data = [z, y]
306
+ res = stats.bootstrap(data, statistic)
307
+
308
+ axis = -1
309
+ alpha = 0.95
310
+ theta_hat_b = res.bootstrap_distribution
311
+ batch = 100
312
+ _, _, a_hat = _resampling._bca_interval(data, statistic, axis, alpha,
313
+ theta_hat_b, batch, xp)
314
+ xp_assert_close(a_hat, xp.asarray(0.011008228344026734))
315
+
316
+ tests_against_itself_1samp = {"basic": 1789,
317
+ "percentile": 1790,
318
+ "BCa": 1789}
319
+
320
+ @pytest.mark.slow
321
+ @pytest.mark.parametrize("method, expected",
322
+ tests_against_itself_1samp.items())
323
+ def test_bootstrap_against_itself_1samp(self, method, expected, xp):
324
+ # The expected values in this test were generated using bootstrap
325
+ # to check for unintended changes in behavior. The test also makes sure
326
+ # that bootstrap works with multi-sample statistics and that the
327
+ # `axis` argument works as expected / function is vectorized.
328
+ rng = np.random.default_rng(9123847)
329
+
330
+ n = 100 # size of sample
331
+ n_resamples = 999 # number of bootstrap resamples used to form each CI
332
+ confidence_level = 0.9
333
+
334
+ # The true mean is 5
335
+ dist = stats.norm(loc=5, scale=1)
336
+ stat_true = float(dist.mean())
337
+
338
+ # Do the same thing 2000 times. (The code is fully vectorized.)
339
+ n_replications = 2000
340
+ data = dist.rvs(size=(n_replications, n), random_state=rng)
341
+ res = bootstrap((xp.asarray(data),),
342
+ statistic=xp.mean,
343
+ confidence_level=confidence_level,
344
+ n_resamples=n_resamples,
345
+ batch=50,
346
+ method=method,
347
+ axis=-1,
348
+ rng=rng)
349
+ ci = res.confidence_interval
350
+
351
+ # ci contains vectors of lower and upper confidence interval bounds
352
+ ci_contains_true = xp.count_nonzero((ci[0] < stat_true) & (stat_true < ci[1]))
353
+ assert ci_contains_true == expected
354
+
355
+ # ci_contains_true is not inconsistent with confidence_level
356
+ pvalue = stats.binomtest(int(ci_contains_true), n_replications,
357
+ confidence_level).pvalue
358
+ assert pvalue > 0.1
359
+
360
+ tests_against_itself_2samp = {"basic": 892,
361
+ "percentile": 890}
362
+
363
+ @pytest.mark.slow
364
+ @pytest.mark.parametrize("method, expected",
365
+ tests_against_itself_2samp.items())
366
+ def test_bootstrap_against_itself_2samp(self, method, expected, xp):
367
+ # The expected values in this test were generated using bootstrap
368
+ # to check for unintended changes in behavior. The test also makes sure
369
+ # that bootstrap works with multi-sample statistics and that the
370
+ # `axis` argument works as expected / function is vectorized.
371
+ rng = np.random.RandomState(0)
372
+
373
+ n1 = 100 # size of sample 1
374
+ n2 = 120 # size of sample 2
375
+ n_resamples = 999 # number of bootstrap resamples used to form each CI
376
+ confidence_level = 0.9
377
+
378
+ # The statistic we're interested in is the difference in means
379
+ def my_stat(data1, data2, axis=-1):
380
+ mean1 = xp.mean(data1, axis=axis)
381
+ mean2 = xp.mean(data2, axis=axis)
382
+ return mean1 - mean2
383
+
384
+ # The true difference in the means is -0.1
385
+ dist1 = stats.norm(loc=0, scale=1)
386
+ dist2 = stats.norm(loc=0.1, scale=1)
387
+ stat_true = float(dist1.mean() - dist2.mean())
388
+
389
+ # Do the same thing 1000 times. (The code is fully vectorized.)
390
+ n_replications = 1000
391
+ data1 = dist1.rvs(size=(n_replications, n1), random_state=rng)
392
+ data2 = dist2.rvs(size=(n_replications, n2), random_state=rng)
393
+ res = bootstrap((xp.asarray(data1), xp.asarray(data2)),
394
+ statistic=my_stat,
395
+ confidence_level=confidence_level,
396
+ n_resamples=n_resamples,
397
+ batch=50,
398
+ method=method,
399
+ axis=-1,
400
+ random_state=rng)
401
+ ci = res.confidence_interval
402
+
403
+ # ci contains vectors of lower and upper confidence interval bounds
404
+ ci_contains_true = xp.count_nonzero((ci[0] < stat_true) & (stat_true < ci[1]))
405
+ assert ci_contains_true == expected
406
+
407
+ # ci_contains_true is not inconsistent with confidence_level
408
+ pvalue = stats.binomtest(int(ci_contains_true), n_replications,
409
+ confidence_level).pvalue
410
+ assert pvalue > 0.1
411
+
412
+ @pytest.mark.parametrize("method", ["basic", "percentile", "BCa"])
413
+ @pytest.mark.parametrize("axis", [0, 1])
414
+ @pytest.mark.parametrize("dtype", ['float32', 'float64'])
415
+ def test_bootstrap_vectorized_3samp(self, method, axis, dtype, xp):
416
+ def statistic(*data, axis=0):
417
+ # an arbitrary, vectorized statistic
418
+ return sum(xp.mean(sample, axis=axis) for sample in data)
419
+
420
+ def statistic_1d(*data):
421
+ # the same statistic, not vectorized
422
+ for sample in data:
423
+ assert sample.ndim == 1
424
+ return np.asarray(sum(sample.mean() for sample in data), dtype=dtype)
425
+
426
+ rng = np.random.RandomState(0)
427
+ x = rng.rand(4, 5).astype(dtype)
428
+ y = rng.rand(4, 5).astype(dtype)
429
+ z = rng.rand(4, 5).astype(dtype)
430
+ res1 = bootstrap((xp.asarray(x), xp.asarray(y), xp.asarray(z)),
431
+ statistic, vectorized=True, axis=axis, n_resamples=100,
432
+ method=method, rng=0)
433
+ res2 = bootstrap((x, y, z), statistic_1d, vectorized=False,
434
+ axis=axis, n_resamples=100, method=method, rng=0)
435
+
436
+ rtol = 1e-6 if dtype == 'float32' else 1e-14
437
+ xp_assert_close(res1.confidence_interval.low,
438
+ xp.asarray(res2.confidence_interval.low), rtol=rtol)
439
+ xp_assert_close(res1.confidence_interval.high,
440
+ xp.asarray(res2.confidence_interval.high), rtol=rtol)
441
+ xp_assert_close(res1.standard_error, xp.asarray(res2.standard_error), rtol=rtol)
442
+
443
+ @pytest.mark.xfail_on_32bit("Failure is not concerning; see gh-14107")
444
+ @pytest.mark.parametrize("method", ["basic", "percentile", "BCa"])
445
+ @pytest.mark.parametrize("axis", [0, 1])
446
+ def test_bootstrap_vectorized_1samp(self, method, axis, xp):
447
+ def statistic(x, axis=0):
448
+ # an arbitrary, vectorized statistic
449
+ return xp.mean(x, axis=axis)
450
+
451
+ def statistic_1d(x):
452
+ # the same statistic, not vectorized
453
+ assert x.ndim == 1
454
+ return x.mean(axis=0)
455
+
456
+ rng = np.random.default_rng(7939952824)
457
+ x = rng.random((2, 3))
458
+ res1 = bootstrap((xp.asarray(x),), statistic, vectorized=True, axis=axis,
459
+ n_resamples=100, batch=None, method=method, rng=0)
460
+ res2 = bootstrap((x,), statistic_1d, vectorized=False, axis=axis,
461
+ n_resamples=100, batch=10, method=method, rng=0)
462
+ xp_assert_close(res1.confidence_interval.low,
463
+ xp.asarray(res2.confidence_interval.low))
464
+ xp_assert_close(res1.confidence_interval.high,
465
+ xp.asarray(res2.confidence_interval.high))
466
+ xp_assert_close(res1.standard_error, xp.asarray(res2.standard_error))
467
+
468
+ @pytest.mark.parametrize("method", ["basic", "percentile", "BCa"])
469
+ def test_bootstrap_degenerate(self, method, xp):
470
+ data = xp.full((35,), 10000.)
471
+ if method == "BCa":
472
+ with np.errstate(invalid='ignore'):
473
+ msg = "The BCa confidence interval cannot be calculated"
474
+ with pytest.warns(stats.DegenerateDataWarning, match=msg):
475
+ res = bootstrap([data, ], xp.mean, method=method)
476
+ xp_assert_equal(res.confidence_interval.low, xp.asarray(xp.nan))
477
+ xp_assert_equal(res.confidence_interval.high, xp.asarray(xp.nan))
478
+ else:
479
+ res = bootstrap([data, ], xp.mean, method=method)
480
+ xp_assert_equal(res.confidence_interval.low, xp.asarray(10000.))
481
+ xp_assert_equal(res.confidence_interval.high, xp.asarray(10000.))
482
+ xp_assert_equal(res.standard_error, xp.asarray(0.))
483
+
484
+ @pytest.mark.parametrize("method", ["BCa", "basic", "percentile"])
485
+ def test_bootstrap_gh15678(self, method, xp):
486
+ # Check that gh-15678 is fixed: when statistic function returned a Python
487
+ # float, method="BCa" failed when trying to add a dimension to the float
488
+ rng = np.random.default_rng(354645618886684)
489
+ dist = stats.norm(loc=2, scale=4)
490
+ data = dist.rvs(size=100, random_state=rng)
491
+ res = bootstrap((xp.asarray(data),), stats.skew, method=method, n_resamples=100,
492
+ rng=np.random.default_rng(9563))
493
+ # this always worked because np.apply_along_axis returns NumPy data type
494
+ ref = bootstrap((data,), stats.skew, method=method, n_resamples=100,
495
+ rng=np.random.default_rng(9563), vectorized=False)
496
+ xp_assert_close(res.confidence_interval.low,
497
+ xp.asarray(ref.confidence_interval.low))
498
+ xp_assert_close(res.confidence_interval.high,
499
+ xp.asarray(ref.confidence_interval.high))
500
+ xp_assert_close(res.standard_error, xp.asarray(ref.standard_error))
501
+
502
+ def test_bootstrap_min(self, xp):
503
+ # Check that gh-15883 is fixed: percentileofscore should
504
+ # behave according to the 'mean' behavior and not trigger nan for BCa
505
+ rng = np.random.default_rng(1891289180021102)
506
+ dist = stats.norm(loc=2, scale=4)
507
+ data = dist.rvs(size=100, random_state=rng)
508
+ true_min = np.min(data)
509
+ data = xp.asarray(data)
510
+ res = bootstrap((data,), xp.min, method="BCa", n_resamples=100,
511
+ rng=np.random.default_rng(3942))
512
+ xp_assert_equal(res.confidence_interval.low, xp.asarray(true_min))
513
+ res2 = bootstrap((-data,), xp.max, method="BCa", n_resamples=100,
514
+ rng=np.random.default_rng(3942))
515
+ xp_assert_close(-res.confidence_interval.low, res2.confidence_interval.high)
516
+ xp_assert_close(-res.confidence_interval.high, res2.confidence_interval.low)
517
+
518
+ @pytest.mark.parametrize("additional_resamples", [0, 1000])
519
+ def test_re_bootstrap(self, additional_resamples, xp):
520
+ # Test behavior of parameter `bootstrap_result`
521
+ rng = np.random.default_rng(8958153316228384)
522
+ x = rng.random(size=100)
523
+
524
+ n1 = 1000
525
+ n2 = additional_resamples
526
+ n3 = n1 + additional_resamples
527
+
528
+ rng = np.random.default_rng(296689032789913033)
529
+ res = stats.bootstrap((xp.asarray(x),), xp.mean, n_resamples=n1, rng=rng,
530
+ confidence_level=0.95, method='percentile')
531
+ res = stats.bootstrap((xp.asarray(x),), xp.mean, n_resamples=n2, rng=rng,
532
+ confidence_level=0.90, method='BCa',
533
+ bootstrap_result=res)
534
+
535
+ rng = np.random.default_rng(296689032789913033)
536
+ ref = stats.bootstrap((xp.asarray(x),), xp.mean, n_resamples=n3, rng=rng,
537
+ confidence_level=0.90, method='BCa')
538
+
539
+ xp_assert_close(res.confidence_interval.low,
540
+ xp.asarray(ref.confidence_interval.low),
541
+ rtol=1e-14)
542
+ xp_assert_close(res.confidence_interval.high,
543
+ xp.asarray(ref.confidence_interval.high),
544
+ rtol=1e-14)
545
+ xp_assert_close(res.standard_error, xp.asarray(ref.standard_error), rtol=1e-14)
546
+
547
+ @pytest.mark.xfail_on_32bit("Sensitive to machine precision")
548
+ @pytest.mark.parametrize("method", ['basic', 'percentile', 'BCa'])
549
+ def test_bootstrap_alternative(self, method, xp):
550
+ rng = np.random.default_rng(5894822712842015040)
551
+ dist = stats.norm(loc=2, scale=4)
552
+ data = (xp.asarray(dist.rvs(size=(100), random_state=rng)),)
553
+
554
+ config = dict(data=data, statistic=xp.std, rng=rng, axis=-1)
555
+ t = stats.bootstrap(**config, confidence_level=0.9)
556
+
557
+ config.update(dict(n_resamples=0, bootstrap_result=t))
558
+ l = stats.bootstrap(**config, confidence_level=0.95, alternative='less')
559
+ g = stats.bootstrap(**config, confidence_level=0.95, alternative='greater')
560
+
561
+ xp_assert_close(l.confidence_interval.high, t.confidence_interval.high,
562
+ rtol=1e-14)
563
+ xp_assert_close(g.confidence_interval.low, t.confidence_interval.low,
564
+ rtol=1e-14)
565
+ assert xp.isinf(l.confidence_interval.low) and l.confidence_interval.low < 0
566
+ assert xp.isinf(g.confidence_interval.high) and g.confidence_interval.high > 0
567
+
568
+ with pytest.raises(ValueError, match='`alternative` must be one of'):
569
+ stats.bootstrap(**config, alternative='ekki-ekki')
570
+
571
+ def test_jackknife_resample(self, xp):
572
+ shape = 3, 4, 5, 6
573
+ rng = np.random.default_rng(5274950392)
574
+ x = rng.random(size=shape)
575
+ y = next(_resampling._jackknife_resample(xp.asarray(x), xp=xp))
576
+
577
+ for i in range(shape[-1]):
578
+ # each resample is indexed along second to last axis
579
+ # (last axis is the one the statistic will be taken over / consumed)
580
+ slc = y[..., i, :]
581
+ expected = np.delete(x, i, axis=-1)
582
+
583
+ xp_assert_equal(slc, xp.asarray(expected))
584
+
585
+ y2 = list(_resampling._jackknife_resample(xp.asarray(x), batch=2, xp=xp))
586
+ xp_assert_equal(xp.concat(y2, axis=-2), y)
587
+
588
+ @pytest.mark.skip_xp_backends("array_api_strict",
589
+ reason="Test uses ... + fancy indexing")
590
+ @pytest.mark.parametrize("rng_name", ["RandomState", "default_rng"])
591
+ def test_bootstrap_resample(self, rng_name, xp):
592
+ rng_gen = getattr(np.random, rng_name)
593
+ rng1 = rng_gen(3949441460)
594
+ rng2 = rng_gen(3949441460)
595
+
596
+ n_resamples = 10
597
+ shape = 3, 4, 5, 6
598
+
599
+ rng = np.random.default_rng(5274950392)
600
+ x = xp.asarray(rng.random(shape))
601
+ y = _resampling._bootstrap_resample(x, n_resamples, rng=rng1, xp=xp)
602
+
603
+ for i in range(n_resamples):
604
+ # each resample is indexed along second to last axis
605
+ # (last axis is the one the statistic will be taken over / consumed)
606
+ slc = y[..., i, :]
607
+
608
+ js = xp.asarray(rng_integers(rng2, 0, shape[-1], shape[-1]))
609
+ expected = x[..., js]
610
+
611
+ xp_assert_equal(slc, expected)
612
+
613
+ @pytest.mark.parametrize("score", [0, 0.5, 1])
614
+ @pytest.mark.parametrize("axis", [0, 1, 2])
615
+ def test_percentile_of_score(self, score, axis, xp):
616
+ shape = 10, 20, 30
617
+ rng = np.random.default_rng(5903363153)
618
+ x = rng.random(shape)
619
+ dtype = xp_default_dtype(xp)
620
+ p = _resampling._percentile_of_score(xp.asarray(x, dtype=dtype),
621
+ xp.asarray(score, dtype=dtype),
622
+ axis=-1, xp=xp)
623
+
624
+ def vectorized_pos(a, score, axis):
625
+ return np.apply_along_axis(stats.percentileofscore, axis, a, score)
626
+
627
+ p2 = vectorized_pos(x, score, axis=-1)/100
628
+
629
+ xp_assert_close(p, xp.asarray(p2, dtype=dtype), rtol=1e-15)
630
+
631
+ @pytest.mark.parametrize("axis", [0, 1, 2])
632
+ def test_vectorize_statistic(self, axis):
633
+ # test that _vectorize_statistic vectorizes a statistic along `axis`
634
+ # only used with NumPy arrays
635
+
636
+ def statistic(*data, axis):
637
+ # an arbitrary, vectorized statistic
638
+ return sum(sample.mean(axis) for sample in data)
639
+
640
+ def statistic_1d(*data):
641
+ # the same statistic, not vectorized
642
+ for sample in data:
643
+ assert sample.ndim == 1
644
+ return statistic(*data, axis=0)
645
+
646
+ # vectorize the non-vectorized statistic
647
+ statistic2 = _resampling._vectorize_statistic(statistic_1d)
648
+
649
+ rng = np.random.RandomState(0)
650
+ x = rng.rand(4, 5, 6)
651
+ y = rng.rand(4, 1, 6)
652
+ z = rng.rand(1, 5, 6)
653
+
654
+ res1 = statistic(x, y, z, axis=axis)
655
+ res2 = statistic2(x, y, z, axis=axis)
656
+ assert_allclose(res1, res2)
657
+
658
+ @pytest.mark.slow
659
+ @pytest.mark.parametrize("method", ["basic", "percentile", "BCa"])
660
+ def test_vector_valued_statistic(self, method, xp):
661
+ # Generate 95% confidence interval around MLE of normal distribution
662
+ # parameters. Repeat 100 times, each time on sample of size 100.
663
+ # Check that confidence interval contains true parameters ~95 times.
664
+ # Confidence intervals are estimated and stochastic; a test failure
665
+ # does not necessarily indicate that something is wrong. More important
666
+ # than values of `counts` below is that the shapes of the outputs are
667
+ # correct.
668
+
669
+ rng = np.random.default_rng(2196847219)
670
+ params = 1, 0.5
671
+ sample = xp.asarray(rng.normal(*params, size=(100, 100)))
672
+
673
+ def statistic(data, axis):
674
+ return xp.stack([xp.mean(data, axis=axis),
675
+ xp.std(data, axis=axis, correction=1)])
676
+
677
+ res = bootstrap((sample,), statistic, method=method, axis=-1,
678
+ n_resamples=9999, batch=200, random_state=rng)
679
+
680
+ params = xp.asarray([1, 0.5])
681
+ counts = xp.count_nonzero((res.confidence_interval.low.T < params)
682
+ & (res.confidence_interval.high.T > params),
683
+ axis=0)
684
+ assert xp.all(counts >= 90)
685
+ assert xp.all(counts <= 100)
686
+ assert res.confidence_interval.low.shape == (2, 100)
687
+ assert res.confidence_interval.high.shape == (2, 100)
688
+ assert res.standard_error.shape == (2, 100)
689
+ assert res.bootstrap_distribution.shape == (2, 100, 9999)
690
+
691
+ @pytest.mark.slow
692
+ @pytest.mark.filterwarnings('ignore::RuntimeWarning')
693
+ def test_vector_valued_statistic_gh17715(self):
694
+ # gh-17715 reported a mistake introduced in the extension of BCa to
695
+ # multi-sample statistics; a `len` should have been `.shape[-1]`. Check
696
+ # that this is resolved.
697
+ # If this is resolved for NumPy, it's resolved for the rest,
698
+ # let's only run this for NumPy.
699
+
700
+ rng = np.random.default_rng(141921000979291141)
701
+
702
+ def concordance(x, y, axis):
703
+ xm = x.mean(axis)
704
+ ym = y.mean(axis)
705
+ cov = ((x - xm[..., None]) * (y - ym[..., None])).mean(axis)
706
+ return (2 * cov) / (x.var(axis) + y.var(axis) + (xm - ym) ** 2)
707
+
708
+ def statistic(tp, tn, fp, fn, axis):
709
+ actual = tp + fp
710
+ expected = tp + fn
711
+ return np.nan_to_num(concordance(actual, expected, axis))
712
+
713
+ def statistic_extradim(*args, axis):
714
+ return statistic(*args, axis)[np.newaxis, ...]
715
+
716
+ data = [[4, 0, 0, 2], # (tp, tn, fp, fn)
717
+ [2, 1, 2, 1],
718
+ [0, 6, 0, 0],
719
+ [0, 6, 3, 0],
720
+ [0, 8, 1, 0]]
721
+ data = np.array(data).T
722
+
723
+ res = bootstrap(data, statistic_extradim, rng=rng, paired=True)
724
+ ref = bootstrap(data, statistic, rng=rng, paired=True)
725
+ assert_allclose(res.confidence_interval.low[0],
726
+ ref.confidence_interval.low, atol=1e-15)
727
+ assert_allclose(res.confidence_interval.high[0],
728
+ ref.confidence_interval.high, atol=1e-15)
729
+
730
+ # torch doesn't have T distribution CDF and can't fall back to NumPy on GPU
731
+ @pytest.mark.skip_xp_backends(np_only=True, exceptions=['cupy'])
732
+ def test_gh_20850(self, xp):
733
+ rng = np.random.default_rng(2085020850)
734
+ x = xp.asarray(rng.random((10, 2)))
735
+ y = xp.asarray(rng.random((11, 2)))
736
+ def statistic(x, y, axis):
737
+ return stats.ttest_ind(x, y, axis=axis).statistic
738
+
739
+ # The shapes do *not* need to be the same along axis
740
+ stats.bootstrap((x, y), statistic)
741
+ stats.bootstrap((x.T, y.T), statistic, axis=1)
742
+ # But even when the shapes *are* the same along axis, the lengths
743
+ # along other dimensions have to be the same (or `bootstrap` warns).
744
+ message = "Array shapes are incompatible for broadcasting."
745
+ with pytest.raises(ValueError, match=message):
746
+ stats.bootstrap((x, y[:10, 0]), statistic) # this won't work after 1.16
747
+ stats.bootstrap((x, y[:10, 0:1]), statistic) # this will
748
+ stats.bootstrap((x.T, y.T[0:1, :10]), statistic, axis=1) # this will
749
+
750
+ # --- Test Monte Carlo Hypothesis Test --- #
751
+
752
+ @make_xp_test_case(monte_carlo_test)
753
+ class TestMonteCarloHypothesisTest:
754
+ atol = 2.5e-2 # for comparing p-value
755
+
756
+ def get_rvs(self, rvs_in, rs, dtype=None, xp=np):
757
+ return lambda *args, **kwds: xp.asarray(rvs_in(*args, random_state=rs, **kwds),
758
+ dtype=dtype)
759
+
760
+ def get_statistic(self, xp):
761
+ def statistic(x, axis):
762
+ m = xp.mean(x, axis=axis)
763
+ v = xp.var(x, axis=axis, correction=1)
764
+ n = x.shape[axis]
765
+ return m / (v/n)**0.5
766
+ # return stats.ttest_1samp(x, popmean=0., axis=axis).statistic)
767
+ return statistic
768
+
769
+ def test_input_validation(self, xp):
770
+ # test that the appropriate error messages are raised for invalid input
771
+
772
+ data = xp.asarray([1., 2., 3.])
773
+ def stat(x, axis=None):
774
+ return xp.mean(x, axis=axis)
775
+
776
+ message = "Array shapes are incompatible for broadcasting."
777
+ temp = (xp.zeros((2, 5)), xp.zeros((3, 5)))
778
+ rvs = (stats.norm.rvs, stats.norm.rvs)
779
+ with pytest.raises(ValueError, match=message):
780
+ monte_carlo_test(temp, rvs, lambda x, y, axis: 1, axis=-1)
781
+
782
+ message = "`axis` must be an integer."
783
+ with pytest.raises(ValueError, match=message):
784
+ monte_carlo_test(data, stats.norm.rvs, stat, axis=1.5)
785
+
786
+ message = "`vectorized` must be `True`, `False`, or `None`."
787
+ with pytest.raises(ValueError, match=message):
788
+ monte_carlo_test(data, stats.norm.rvs, stat, vectorized=1.5)
789
+
790
+ message = "`rvs` must be callable or sequence of callables."
791
+ with pytest.raises(TypeError, match=message):
792
+ monte_carlo_test(data, None, stat)
793
+ with pytest.raises(TypeError, match=message):
794
+ temp = xp.asarray([[1., 2.], [3., 4.]])
795
+ monte_carlo_test(temp, [lambda x: x, None], stat)
796
+
797
+ message = "If `rvs` is a sequence..."
798
+ with pytest.raises(ValueError, match=message):
799
+ temp = xp.asarray([[1., 2., 3.]])
800
+ monte_carlo_test(temp, [lambda x: x, lambda x: x], stat)
801
+
802
+ message = "`statistic` must be callable."
803
+ with pytest.raises(TypeError, match=message):
804
+ monte_carlo_test(data, stats.norm.rvs, None)
805
+
806
+ message = "`n_resamples` must be a positive integer."
807
+ with pytest.raises(ValueError, match=message):
808
+ monte_carlo_test(data, stats.norm.rvs, stat, n_resamples=-1000)
809
+
810
+ message = "`n_resamples` must be a positive integer."
811
+ with pytest.raises(ValueError, match=message):
812
+ monte_carlo_test(data, stats.norm.rvs, stat, n_resamples=1000.5)
813
+
814
+ message = "`batch` must be a positive integer or None."
815
+ with pytest.raises(ValueError, match=message):
816
+ monte_carlo_test(data, stats.norm.rvs, stat, batch=-1000)
817
+
818
+ message = "`batch` must be a positive integer or None."
819
+ with pytest.raises(ValueError, match=message):
820
+ monte_carlo_test(data, stats.norm.rvs, stat, batch=1000.5)
821
+
822
+ message = "`alternative` must be in..."
823
+ with pytest.raises(ValueError, match=message):
824
+ monte_carlo_test(data, stats.norm.rvs, stat, alternative='ekki')
825
+
826
+ # *If* this raises a value error, make sure it has the intended message
827
+ message = "Signature inspection of statistic"
828
+ def rvs(size):
829
+ return xp.asarray(stats.norm.rvs(size=size))
830
+ try:
831
+ monte_carlo_test(data, rvs, xp.mean)
832
+ except ValueError as e:
833
+ assert str(e).startswith(message)
834
+
835
+ def test_input_validation_xp(self, xp):
836
+ def non_vectorized_statistic(x):
837
+ return xp.mean(x)
838
+
839
+ message = "`statistic` must be vectorized..."
840
+ sample = xp.asarray([1., 2., 3.])
841
+ if is_numpy(xp):
842
+ monte_carlo_test(sample, stats.norm.rvs, non_vectorized_statistic)
843
+ return
844
+
845
+ with pytest.raises(ValueError, match=message):
846
+ monte_carlo_test(sample, stats.norm.rvs, non_vectorized_statistic)
847
+ with pytest.raises(ValueError, match=message):
848
+ monte_carlo_test(sample, stats.norm.rvs, xp.mean, vectorized=False)
849
+
850
+ @pytest.mark.xslow
851
+ def test_batch(self, xp):
852
+ # make sure that the `batch` parameter is respected by checking the
853
+ # maximum batch size provided in calls to `statistic`
854
+ rng = np.random.default_rng(23492340193)
855
+ x = xp.asarray(rng.standard_normal(size=10))
856
+
857
+ def statistic(x, axis):
858
+ batch_size = 1 if x.ndim == 1 else x.shape[0]
859
+ statistic.batch_size = max(batch_size, statistic.batch_size)
860
+ statistic.counter += 1
861
+ return self.get_statistic(xp)(x, axis=axis)
862
+ statistic.counter = 0
863
+ statistic.batch_size = 0
864
+
865
+ kwds = {'sample': x, 'statistic': statistic,
866
+ 'n_resamples': 1000, 'vectorized': True}
867
+
868
+ kwds['rvs'] = self.get_rvs(stats.norm.rvs, np.random.default_rng(328423), xp=xp)
869
+ res1 = monte_carlo_test(batch=1, **kwds)
870
+ assert_equal(statistic.counter, 1001)
871
+ assert_equal(statistic.batch_size, 1)
872
+
873
+ kwds['rvs'] = self.get_rvs(stats.norm.rvs, np.random.default_rng(328423), xp=xp)
874
+ statistic.counter = 0
875
+ res2 = monte_carlo_test(batch=50, **kwds)
876
+ assert_equal(statistic.counter, 21)
877
+ assert_equal(statistic.batch_size, 50)
878
+
879
+ kwds['rvs'] = self.get_rvs(stats.norm.rvs, np.random.default_rng(328423), xp=xp)
880
+ statistic.counter = 0
881
+ res3 = monte_carlo_test(**kwds)
882
+ assert_equal(statistic.counter, 2)
883
+ assert_equal(statistic.batch_size, 1000)
884
+
885
+ xp_assert_equal(res1.pvalue, res3.pvalue)
886
+ xp_assert_equal(res2.pvalue, res3.pvalue)
887
+
888
+ @pytest.mark.parametrize('axis', range(-3, 3))
889
+ def test_axis_dtype(self, axis, xp):
890
+ # test that Nd-array samples are handled correctly for valid values
891
+ # of the `axis` parameter; also make sure non-default dtype is maintained
892
+ rng = np.random.default_rng(2389234)
893
+ size = [2, 3, 4]
894
+ size[axis] = 100
895
+
896
+ # Determine non-default dtype
897
+ dtype_default = xp.asarray(1.).dtype
898
+ dtype_str = 'float32'if ("64" in str(dtype_default)) else 'float64'
899
+ dtype_np = getattr(np, dtype_str)
900
+ dtype = getattr(xp, dtype_str)
901
+
902
+ # ttest_1samp is CPU array-API compatible, but it would be good to
903
+ # include CuPy in this test. We'll perform ttest_1samp with a
904
+ # NumPy array, but all the rest with be done with fully array-API
905
+ # compatible code.
906
+ x = rng.standard_normal(size=size, dtype=dtype_np)
907
+ expected = stats.ttest_1samp(x, popmean=0., axis=axis)
908
+
909
+ x = xp.asarray(x, dtype=dtype)
910
+ statistic = self.get_statistic(xp)
911
+ rvs = self.get_rvs(stats.norm.rvs, rng, dtype=dtype, xp=xp)
912
+
913
+ res = monte_carlo_test(x, rvs, statistic, vectorized=True,
914
+ n_resamples=20000, axis=axis)
915
+
916
+ ref_statistic = xp.asarray(expected.statistic, dtype=dtype)
917
+ ref_pvalue = xp.asarray(expected.pvalue, dtype=dtype)
918
+ xp_assert_close(res.statistic, ref_statistic)
919
+ xp_assert_close(res.pvalue, ref_pvalue, atol=self.atol)
920
+
921
+ @pytest.mark.parametrize('alternative', ("two-sided", "less", "greater"))
922
+ def test_alternative(self, alternative, xp):
923
+ # test that `alternative` is working as expected
924
+ rng = np.random.default_rng(65723433)
925
+
926
+ x = rng.standard_normal(size=30)
927
+ ref = stats.ttest_1samp(x, 0., alternative=alternative)
928
+
929
+ x = xp.asarray(x)
930
+ statistic = self.get_statistic(xp)
931
+ rvs = self.get_rvs(stats.norm.rvs, rng, xp=xp)
932
+
933
+ res = monte_carlo_test(x, rvs, statistic, alternative=alternative)
934
+
935
+ xp_assert_close(res.statistic, xp.asarray(ref.statistic))
936
+ xp_assert_close(res.pvalue, xp.asarray(ref.pvalue), atol=self.atol)
937
+
938
+
939
+ # Tests below involve statistics that are not yet array-API compatible.
940
+ # They can be converted when the statistics are converted.
941
+ @pytest.mark.slow
942
+ @pytest.mark.parametrize('alternative', ("less", "greater"))
943
+ @pytest.mark.parametrize('a', np.linspace(-0.5, 0.5, 5)) # skewness
944
+ def test_against_ks_1samp(self, alternative, a):
945
+ # test that monte_carlo_test can reproduce pvalue of ks_1samp
946
+ rng = np.random.default_rng(65723433)
947
+
948
+ x = stats.skewnorm.rvs(a=a, size=30, random_state=rng)
949
+ expected = stats.ks_1samp(x, stats.norm.cdf, alternative=alternative)
950
+
951
+ def statistic1d(x):
952
+ return stats.ks_1samp(x, stats.norm.cdf, mode='asymp',
953
+ alternative=alternative).statistic
954
+
955
+ norm_rvs = self.get_rvs(stats.norm.rvs, rng)
956
+ res = monte_carlo_test(x, norm_rvs, statistic1d,
957
+ n_resamples=1000, vectorized=False,
958
+ alternative=alternative)
959
+
960
+ assert_allclose(res.statistic, expected.statistic)
961
+ if alternative == 'greater':
962
+ assert_allclose(res.pvalue, expected.pvalue, atol=self.atol)
963
+ elif alternative == 'less':
964
+ assert_allclose(1-res.pvalue, expected.pvalue, atol=self.atol)
965
+
966
+ @pytest.mark.parametrize('hypotest', (stats.skewtest, stats.kurtosistest))
967
+ @pytest.mark.parametrize('alternative', ("less", "greater", "two-sided"))
968
+ @pytest.mark.parametrize('a', np.linspace(-2, 2, 5)) # skewness
969
+ def test_against_normality_tests(self, hypotest, alternative, a):
970
+ # test that monte_carlo_test can reproduce pvalue of normality tests
971
+ rng = np.random.default_rng(85723405)
972
+
973
+ x = stats.skewnorm.rvs(a=a, size=150, random_state=rng)
974
+ expected = hypotest(x, alternative=alternative)
975
+
976
+ def statistic(x, axis):
977
+ return hypotest(x, axis=axis).statistic
978
+
979
+ norm_rvs = self.get_rvs(stats.norm.rvs, rng)
980
+ res = monte_carlo_test(x, norm_rvs, statistic, vectorized=True,
981
+ alternative=alternative)
982
+
983
+ assert_allclose(res.statistic, expected.statistic)
984
+ assert_allclose(res.pvalue, expected.pvalue, atol=self.atol)
985
+
986
+ @pytest.mark.parametrize('a', np.arange(-2, 3)) # skewness parameter
987
+ def test_against_normaltest(self, a):
988
+ # test that monte_carlo_test can reproduce pvalue of normaltest
989
+ rng = np.random.default_rng(12340513)
990
+
991
+ x = stats.skewnorm.rvs(a=a, size=150, random_state=rng)
992
+ expected = stats.normaltest(x)
993
+
994
+ def statistic(x, axis):
995
+ return stats.normaltest(x, axis=axis).statistic
996
+
997
+ norm_rvs = self.get_rvs(stats.norm.rvs, rng)
998
+ res = monte_carlo_test(x, norm_rvs, statistic, vectorized=True,
999
+ alternative='greater')
1000
+
1001
+ assert_allclose(res.statistic, expected.statistic)
1002
+ assert_allclose(res.pvalue, expected.pvalue, atol=self.atol)
1003
+
1004
+ @pytest.mark.xslow
1005
+ @pytest.mark.parametrize('a', np.linspace(-0.5, 0.5, 5)) # skewness
1006
+ def test_against_cramervonmises(self, a):
1007
+ # test that monte_carlo_test can reproduce pvalue of cramervonmises
1008
+ rng = np.random.default_rng(234874135)
1009
+
1010
+ x = stats.skewnorm.rvs(a=a, size=30, random_state=rng)
1011
+ expected = stats.cramervonmises(x, stats.norm.cdf)
1012
+
1013
+ def statistic1d(x):
1014
+ return stats.cramervonmises(x, stats.norm.cdf).statistic
1015
+
1016
+ norm_rvs = self.get_rvs(stats.norm.rvs, rng)
1017
+ res = monte_carlo_test(x, norm_rvs, statistic1d,
1018
+ n_resamples=1000, vectorized=False,
1019
+ alternative='greater')
1020
+
1021
+ assert_allclose(res.statistic, expected.statistic)
1022
+ assert_allclose(res.pvalue, expected.pvalue, atol=self.atol)
1023
+
1024
+ @pytest.mark.slow
1025
+ @pytest.mark.parametrize('dist_name', ['norm', 'logistic'])
1026
+ @pytest.mark.parametrize('target_statistic', [0.6, 0.7, 0.8])
1027
+ def test_against_anderson(self, dist_name, target_statistic):
1028
+ # test that monte_carlo_test can reproduce results of `anderson`.
1029
+
1030
+ # find the skewness for which the sample statistic is within the range of
1031
+ # values tubulated by `anderson`
1032
+
1033
+ def fun(a):
1034
+ rng = np.random.default_rng(394295467)
1035
+ x = stats.tukeylambda.rvs(a, size=100, random_state=rng)
1036
+ expected = stats.anderson(x, dist_name, method='interpolate')
1037
+ return expected.statistic - target_statistic
1038
+ with warnings.catch_warnings():
1039
+ warnings.simplefilter("ignore", RuntimeWarning)
1040
+ sol = root(fun, x0=0)
1041
+ assert sol.success
1042
+
1043
+ # get the significance level (p-value) associated with that critical
1044
+ # value
1045
+ a = sol.x[0]
1046
+ rng = np.random.default_rng(394295467)
1047
+ x = stats.tukeylambda.rvs(a, size=100, random_state=rng)
1048
+ expected = stats.anderson(x, dist_name, method='interpolate')
1049
+ expected_stat = expected.statistic
1050
+ expected_p = expected.pvalue
1051
+
1052
+ # perform equivalent Monte Carlo test and compare results
1053
+ def statistic1d(x):
1054
+ return stats.anderson(x, dist_name, method='interpolate').statistic
1055
+
1056
+ dist_rvs = self.get_rvs(getattr(stats, dist_name).rvs, rng)
1057
+ with warnings.catch_warnings():
1058
+ warnings.simplefilter("ignore", RuntimeWarning)
1059
+ res = monte_carlo_test(x, dist_rvs,
1060
+ statistic1d, n_resamples=1000,
1061
+ vectorized=False, alternative='greater')
1062
+
1063
+ assert_allclose(res.statistic, expected_stat)
1064
+ assert_allclose(res.pvalue, expected_p, atol=2*self.atol)
1065
+
1066
+ def test_p_never_zero(self):
1067
+ # Use biased estimate of p-value to ensure that p-value is never zero
1068
+ # per monte_carlo_test reference [1]
1069
+ rng = np.random.default_rng(2190176673029737545)
1070
+ x = np.zeros(100)
1071
+ res = monte_carlo_test(x, rng.random, np.mean,
1072
+ vectorized=True, alternative='less')
1073
+ assert res.pvalue == 0.0001
1074
+
1075
+ def test_against_ttest_ind(self):
1076
+ # test that `monte_carlo_test` can reproduce results of `ttest_ind`.
1077
+ rng = np.random.default_rng(219017667302737545)
1078
+ data = rng.random(size=(2, 5)), rng.random(size=7) # broadcastable
1079
+ rvs = rng.normal, rng.normal
1080
+ def statistic(x, y, axis):
1081
+ return stats.ttest_ind(x, y, axis=axis).statistic
1082
+
1083
+ res = stats.monte_carlo_test(data, rvs, statistic, axis=-1)
1084
+ ref = stats.ttest_ind(data[0], [data[1]], axis=-1)
1085
+ assert_allclose(res.statistic, ref.statistic)
1086
+ assert_allclose(res.pvalue, ref.pvalue, rtol=2e-2)
1087
+
1088
+ def test_against_f_oneway(self):
1089
+ # test that `monte_carlo_test` can reproduce results of `f_oneway`.
1090
+ rng = np.random.default_rng(219017667302737545)
1091
+ data = (rng.random(size=(2, 100)), rng.random(size=(2, 101)),
1092
+ rng.random(size=(2, 102)), rng.random(size=(2, 103)))
1093
+ rvs = rng.normal, rng.normal, rng.normal, rng.normal
1094
+
1095
+ def statistic(*args, axis):
1096
+ return stats.f_oneway(*args, axis=axis).statistic
1097
+
1098
+ res = stats.monte_carlo_test(data, rvs, statistic, axis=-1,
1099
+ alternative='greater')
1100
+ ref = stats.f_oneway(*data, axis=-1)
1101
+
1102
+ assert_allclose(res.statistic, ref.statistic)
1103
+ assert_allclose(res.pvalue, ref.pvalue, atol=1e-2)
1104
+
1105
+ @pytest.mark.fail_slow(2)
1106
+ @pytest.mark.xfail_on_32bit("Statistic may not depend on sample order on 32-bit")
1107
+ def test_finite_precision_statistic(self):
1108
+ # Some statistics return numerically distinct values when the values
1109
+ # should be equal in theory. Test that `monte_carlo_test` accounts
1110
+ # for this in some way.
1111
+ rng = np.random.default_rng(2549824598234528)
1112
+ n_resamples = 9999
1113
+ def rvs(size):
1114
+ return 1. * stats.bernoulli(p=0.333).rvs(size=size, random_state=rng)
1115
+
1116
+ x = rvs(100)
1117
+ res = stats.monte_carlo_test(x, rvs, np.var, alternative='less',
1118
+ n_resamples=n_resamples)
1119
+ # show that having a tolerance matters
1120
+ c0 = np.sum(res.null_distribution <= res.statistic)
1121
+ c1 = np.sum(res.null_distribution <= res.statistic*(1+1e-15))
1122
+ assert c0 != c1
1123
+ assert res.pvalue == (c1 + 1)/(n_resamples + 1)
1124
+
1125
+
1126
+ @make_xp_test_case(power)
1127
+ class TestPower:
1128
+ def xp_normal(self, rng, *, xp, dtype=None):
1129
+ dtype = xp_default_dtype(xp) if dtype is None else dtype
1130
+ return lambda size: xp.asarray(rng.normal(size=size), dtype=dtype)
1131
+
1132
+ def test_input_validation(self, xp):
1133
+ # test that the appropriate error messages are raised for invalid input
1134
+ rng = np.random.default_rng(8519895914314711673)
1135
+
1136
+ test = stats.ttest_ind
1137
+ rvs = (self.xp_normal(rng, xp=xp), self.xp_normal(rng, xp=xp))
1138
+ n_observations = (xp.asarray(10), xp.asarray(12))
1139
+
1140
+ message = "`vectorized` must be `True`, `False`, or `None`."
1141
+ with pytest.raises(ValueError, match=message):
1142
+ power(test, rvs, n_observations, vectorized=1.5)
1143
+
1144
+ message = "`rvs` must be callable or sequence of callables."
1145
+ with pytest.raises(TypeError, match=message):
1146
+ power(test, None, n_observations)
1147
+ with pytest.raises(TypeError, match=message):
1148
+ power(test, (self.xp_normal(rng, xp=xp), 'ekki'), n_observations)
1149
+
1150
+ message = "If `rvs` is a sequence..."
1151
+ with pytest.raises(ValueError, match=message):
1152
+ power(test, (self.xp_normal(rng, xp=xp),), n_observations)
1153
+ with pytest.raises(ValueError, match=message):
1154
+ power(test, rvs, (10,))
1155
+
1156
+ message = "`significance` must contain floats between 0 and 1."
1157
+ with pytest.raises(ValueError, match=message):
1158
+ power(test, rvs, n_observations, significance=2)
1159
+ with pytest.raises(ValueError, match=message):
1160
+ power(test, rvs, n_observations, significance=xp.linspace(-1, 1, 50))
1161
+
1162
+ message = "`kwargs` must be a dictionary"
1163
+ with pytest.raises(TypeError, match=message):
1164
+ power(test, rvs, n_observations, kwargs=(1, 2, 3))
1165
+
1166
+ message = "not be broadcast|Chunks do not add|Incompatible shapes"
1167
+ with pytest.raises((ValueError, RuntimeError), match=message):
1168
+ power(test, rvs, (xp.asarray([10, 11]), xp.asarray([12, 13, 14])))
1169
+ with pytest.raises((ValueError, RuntimeError), match=message):
1170
+ power(test, rvs, (xp.asarray([10, 11]), xp.asarray([12, 13])),
1171
+ kwargs={'x': xp.asarray([1, 2, 3])})
1172
+
1173
+ message = "`test` must be callable"
1174
+ with pytest.raises(TypeError, match=message):
1175
+ power(None, rvs, n_observations)
1176
+
1177
+ message = "`n_resamples` must be a positive integer"
1178
+ with pytest.raises(ValueError, match=message):
1179
+ power(test, rvs, n_observations, n_resamples=-10)
1180
+ with pytest.raises(ValueError, match=message):
1181
+ power(test, rvs, n_observations, n_resamples=10.5)
1182
+
1183
+ message = "`batch` must be a positive integer"
1184
+ with pytest.raises(ValueError, match=message):
1185
+ power(test, rvs, n_observations, batch=-10)
1186
+ with pytest.raises(ValueError, match=message):
1187
+ power(test, rvs, n_observations, batch=10.5)
1188
+
1189
+ @pytest.mark.slow
1190
+ def test_batch(self, xp):
1191
+ # make sure that the `batch` parameter is respected by checking the
1192
+ # maximum batch size provided in calls to `test`
1193
+ rng = np.random.default_rng(23492340193)
1194
+
1195
+ def test(x, axis):
1196
+ batch_size = 1 if x.ndim == 1 else x.shape[0]
1197
+ test.batch_size = max(batch_size, test.batch_size)
1198
+ test.counter += 1
1199
+ return stats.ttest_1samp(x, xp.asarray(0.), axis=axis).pvalue
1200
+ test.counter = 0
1201
+ test.batch_size = 0
1202
+
1203
+ kwds = dict(test=test,
1204
+ n_observations=xp.asarray(10),
1205
+ n_resamples=1000)
1206
+
1207
+ rng = np.random.default_rng(23492340193)
1208
+ res1 = power(**kwds, rvs=self.xp_normal(rng, xp=xp), batch=1)
1209
+ assert test.counter == 1000
1210
+ assert test.batch_size == 1
1211
+
1212
+ rng = np.random.default_rng(23492340193)
1213
+ test.counter = 0
1214
+ res2 = power(**kwds, rvs=self.xp_normal(rng, xp=xp), batch=50)
1215
+ assert test.counter == 20
1216
+ assert test.batch_size == 50
1217
+
1218
+ rng = np.random.default_rng(23492340193)
1219
+ test.counter = 0
1220
+ res3 = power(**kwds, rvs=self.xp_normal(rng, xp=xp), batch=1000)
1221
+ assert test.counter == 1
1222
+ assert test.batch_size == 1000
1223
+
1224
+ xp_assert_equal(res1.power, res3.power)
1225
+ xp_assert_equal(res2.power, res3.power)
1226
+
1227
+ @pytest.mark.slow
1228
+ def test_vectorization(self, xp):
1229
+ # Test that `power` is vectorized as expected
1230
+ rng = np.random.default_rng(25495254834552)
1231
+ alternatives = {-1: 'less', 0:'two-sided', 1: 'greater'}
1232
+
1233
+ # Single vectorized call
1234
+ popmeans = xp.asarray([0, 0.2])
1235
+ def test(x, alternative, axis=-1):
1236
+ # ensure that popmeans axis is zeroth and orthogonal to the rest
1237
+ popmeans_expanded = xpx.expand_dims(popmeans,
1238
+ axis=tuple(range(1, x.ndim + 1)))
1239
+ alternative = alternatives[int(alternative)]
1240
+ return stats.ttest_1samp(x, popmeans_expanded, alternative=alternative,
1241
+ axis=axis)
1242
+
1243
+ # nx and kwargs broadcast against one another
1244
+ nx = xp.asarray([10, 15, 20, 50, 100])[:, xp.newaxis]
1245
+ kwargs = {'alternative': xp.asarray([-1, 0, 1])}
1246
+
1247
+ # This dimension is added to the beginning
1248
+ significance = xp.asarray([0.01, 0.025, 0.05, 0.1])
1249
+ res = stats.power(test, self.xp_normal(rng, xp=xp), nx,
1250
+ significance=significance, kwargs=kwargs)
1251
+
1252
+ # Looping over all combinations
1253
+ ref = []
1254
+ for significance_i in significance:
1255
+ for i in range(nx.shape[0]):
1256
+ nx_i = nx[i, ...]
1257
+ for alternative_i in kwargs['alternative']:
1258
+ for j in range(popmeans.shape[0]):
1259
+ popmean_j = popmeans[j]
1260
+ def test2(x, axis=-1):
1261
+ return stats.ttest_1samp(x, popmean_j, axis=axis,
1262
+ alternative=alternatives[int(alternative_i)])
1263
+
1264
+ tmp = stats.power(test2, self.xp_normal(rng, xp=xp), nx_i,
1265
+ significance=significance_i)
1266
+ ref.append(tmp.power)
1267
+ ref = xp.reshape(xp.stack(ref), res.power.shape)
1268
+
1269
+ # Show that results are similar
1270
+ xp_assert_close(res.power, ref, rtol=2e-2, atol=1e-2)
1271
+
1272
+ @pytest.mark.skip_xp_backends(cpu_only=True,
1273
+ exceptions=['cupy', 'jax.numpy', 'dask.array'])
1274
+ def test_ttest_ind_null(self, xp):
1275
+ # Check that the p-values of `ttest_ind` are uniformly distributed under
1276
+ # the null hypothesis
1277
+ rng = np.random.default_rng(254952548345528)
1278
+
1279
+ test = stats.ttest_ind
1280
+ n_observations = (xp.asarray(rng.integers(10, 100, size=(10))),
1281
+ xp.asarray(rng.integers(10, 100, size=(10))))
1282
+ rvs = self.xp_normal(rng, xp=xp), self.xp_normal(rng, xp=xp)
1283
+ significance = xp.asarray([0.01, 0.05, 0.1])
1284
+ res = stats.power(test, rvs, n_observations, significance=significance)
1285
+ significance = xp.broadcast_to(significance[:, xp.newaxis], res.power.shape)
1286
+ xp_assert_close(res.power, significance, atol=1e-2)
1287
+
1288
+ @pytest.mark.skip_xp_backends('array_api_strict',
1289
+ reason='currently combines integer and float arrays')
1290
+ @pytest.mark.skip_xp_backends(cpu_only=True,
1291
+ exceptions=['cupy', 'jax.numpy', 'dask.array'])
1292
+ def test_ttest_1samp_power(self, xp):
1293
+ # Check simulated ttest_1samp power against reference
1294
+ rng = np.random.default_rng(254952548345528)
1295
+
1296
+ # Reference values computed with statmodels
1297
+ # import numpy as np
1298
+ # from statsmodels.stats.power import tt_solve_power
1299
+ # tt_solve_power = np.vectorize(tt_solve_power)
1300
+ # tt_solve_power([0.1, 0.5, 0.9], [[10], [20]], [[[0.01]], [[0.05]]])
1301
+ ref = [[[0.0126515 , 0.10269751, 0.40415802],
1302
+ [0.01657775, 0.29734608, 0.86228288]],
1303
+ [[0.0592903 , 0.29317561, 0.71718121],
1304
+ [0.07094116, 0.56450441, 0.96815163]]]
1305
+
1306
+ kwargs = {'popmean': xp.asarray([0.1, 0.5, 0.9])}
1307
+ n_observations = xp.asarray([[10], [20]])
1308
+ significance = xp.asarray([0.01, 0.05])
1309
+ res = stats.power(stats.ttest_1samp, self.xp_normal(rng, xp=xp), n_observations,
1310
+ significance=significance, kwargs=kwargs)
1311
+ xp_assert_close(res.power, xp.asarray(ref), atol=1e-2)
1312
+
1313
+
1314
+ @make_xp_test_case(permutation_test)
1315
+ class TestPermutationTest:
1316
+
1317
+ rtol = 1e-14
1318
+
1319
+ def setup_method(self):
1320
+ self.rng = np.random.default_rng(7170559330470561044)
1321
+
1322
+ # -- Input validation -- #
1323
+ def test_permutation_test_iv(self, xp):
1324
+
1325
+ def stat(x, y, axis):
1326
+ return stats.ttest_ind((x, y), axis).statistic
1327
+
1328
+ data = (xp.asarray([1, 2, 3]), xp.asarray([1, 2, 3]))
1329
+
1330
+ message = "each sample in `data` must contain two or more ..."
1331
+ with pytest.raises(ValueError, match=message):
1332
+ permutation_test((data[0], xp.asarray([0])), stat)
1333
+
1334
+ message = "`data` must be a tuple containing at least two samples"
1335
+ with pytest.raises(ValueError, match=message):
1336
+ permutation_test((1,), stat)
1337
+ with pytest.raises(TypeError, match=message):
1338
+ permutation_test(1, stat)
1339
+
1340
+ message = "`axis` must be an integer."
1341
+ with pytest.raises(ValueError, match=message):
1342
+ permutation_test(data, stat, axis=1.5)
1343
+
1344
+ message = "`permutation_type` must be in..."
1345
+ with pytest.raises(ValueError, match=message):
1346
+ permutation_test(data, stat,
1347
+ permutation_type="ekki")
1348
+
1349
+ message = "`vectorized` must be `True`, `False`, or `None`."
1350
+ with pytest.raises(ValueError, match=message):
1351
+ permutation_test(data, stat, vectorized=1.5)
1352
+
1353
+ message = "`n_resamples` must be a positive integer."
1354
+ with pytest.raises(ValueError, match=message):
1355
+ permutation_test(data, stat, n_resamples=-1000)
1356
+
1357
+ message = "`n_resamples` must be a positive integer."
1358
+ with pytest.raises(ValueError, match=message):
1359
+ permutation_test(data, stat, n_resamples=1000.5)
1360
+
1361
+ message = "`batch` must be a positive integer or None."
1362
+ with pytest.raises(ValueError, match=message):
1363
+ permutation_test(data, stat, batch=-1000)
1364
+
1365
+ message = "`batch` must be a positive integer or None."
1366
+ with pytest.raises(ValueError, match=message):
1367
+ permutation_test(data, stat, batch=1000.5)
1368
+
1369
+ message = "`alternative` must be in..."
1370
+ with pytest.raises(ValueError, match=message):
1371
+ permutation_test(data, stat, alternative='ekki')
1372
+
1373
+ message = "SeedSequence expects int or sequence of ints"
1374
+ with pytest.raises(TypeError, match=message):
1375
+ permutation_test(data, stat, rng='herring')
1376
+
1377
+ # -- Test Parameters -- #
1378
+ # SPEC-007 leave one call with seed to check it still works
1379
+ @pytest.mark.parametrize('random_state', [np.random.RandomState,
1380
+ np.random.default_rng])
1381
+ @pytest.mark.parametrize('permutation_type',
1382
+ ['pairings', 'samples', 'independent'])
1383
+ def test_batch(self, permutation_type, random_state, xp):
1384
+ # make sure that the `batch` parameter is respected by checking the
1385
+ # maximum batch size provided in calls to `statistic`
1386
+ x = xp.asarray(self.rng.random(10))
1387
+ y = xp.asarray(self.rng.random(10))
1388
+
1389
+ def statistic(x, y, axis):
1390
+ batch_size = 1 if x.ndim == 1 else x.shape[0]
1391
+ statistic.batch_size = max(batch_size, statistic.batch_size)
1392
+ statistic.counter += 1
1393
+ return xp.mean(x, axis=axis) - xp.mean(y, axis=axis)
1394
+ statistic.counter = 0
1395
+ statistic.batch_size = 0
1396
+
1397
+ kwds = {'n_resamples': 1000, 'permutation_type': permutation_type,
1398
+ 'vectorized': True}
1399
+ res1 = stats.permutation_test((x, y), statistic, batch=1,
1400
+ random_state=random_state(0), **kwds)
1401
+ assert statistic.counter == 1001
1402
+ assert statistic.batch_size == 1
1403
+
1404
+ statistic.counter = 0
1405
+ res2 = stats.permutation_test((x, y), statistic, batch=50,
1406
+ random_state=random_state(0), **kwds)
1407
+ assert statistic.counter == 21
1408
+ assert statistic.batch_size == 50
1409
+
1410
+ statistic.counter = 0
1411
+ res3 = stats.permutation_test((x, y), statistic, batch=1000,
1412
+ random_state=random_state(0), **kwds)
1413
+ assert statistic.counter == 2
1414
+ assert statistic.batch_size == 1000
1415
+
1416
+ xp_assert_equal(res1.pvalue, res3.pvalue)
1417
+ xp_assert_equal(res2.pvalue, res3.pvalue)
1418
+
1419
+ # SPEC-007 leave at least one call with seed to check it still works
1420
+ @pytest.mark.parametrize('random_state', [np.random.RandomState,
1421
+ np.random.default_rng])
1422
+ @pytest.mark.parametrize('permutation_type, exact_size',
1423
+ [('pairings', special.factorial(3)**2),
1424
+ ('samples', 2**3),
1425
+ ('independent', special.binom(6, 3))])
1426
+ def test_permutations(self, permutation_type, exact_size, random_state, xp):
1427
+ # make sure that the `permutations` parameter is respected by checking
1428
+ # the size of the null distribution
1429
+ x = xp.asarray(self.rng.random(3))
1430
+ y = xp.asarray(self.rng.random(3))
1431
+
1432
+ def statistic(x, y, axis):
1433
+ return xp.mean(x, axis=axis) - xp.mean(y, axis=axis)
1434
+
1435
+ kwds = {'permutation_type': permutation_type,
1436
+ 'vectorized': True}
1437
+ res = stats.permutation_test((x, y), statistic, n_resamples=3,
1438
+ random_state=random_state(0), **kwds)
1439
+ assert xp_size(res.null_distribution) == 3
1440
+
1441
+ res = stats.permutation_test((x, y), statistic, **kwds)
1442
+ assert xp_size(res.null_distribution) == exact_size
1443
+
1444
+ # -- Randomized Permutation Tests -- #
1445
+
1446
+ # To get reasonable accuracy, these next three tests are somewhat slow.
1447
+ # Originally, I had them passing for all combinations of permutation type,
1448
+ # alternative, and RNG, but that takes too long for CI. Instead, split
1449
+ # into three tests, each testing a particular combination of the three
1450
+ # parameters.
1451
+
1452
+ def test_randomized_test_against_exact_both(self, xp):
1453
+ # check that the randomized and exact tests agree to reasonable
1454
+ # precision for permutation_type='both
1455
+
1456
+ alternative, rng = 'less', 0
1457
+
1458
+ nx, ny, permutations = 8, 9, 24000
1459
+ assert special.binom(nx + ny, nx) > permutations
1460
+
1461
+ rng = np.random.default_rng(8235259808)
1462
+ x = xp.asarray(rng.standard_normal(size=nx))
1463
+ y = xp.asarray(rng.standard_normal(size=ny))
1464
+ data = x, y
1465
+
1466
+ def statistic(x, y, axis):
1467
+ return xp.mean(x, axis=axis) - xp.mean(y, axis=axis)
1468
+
1469
+ kwds = {'vectorized': True, 'permutation_type': 'independent',
1470
+ 'batch': 100, 'alternative': alternative, 'rng': rng}
1471
+ res = permutation_test(data, statistic, n_resamples=permutations, **kwds)
1472
+ res2 = permutation_test(data, statistic, n_resamples=xp.inf, **kwds)
1473
+
1474
+ assert res.statistic == res2.statistic
1475
+ xp_assert_close(res.pvalue, res2.pvalue, atol=1e-2)
1476
+
1477
+ @pytest.mark.slow()
1478
+ def test_randomized_test_against_exact_samples(self, xp):
1479
+ # check that the randomized and exact tests agree to reasonable
1480
+ # precision for permutation_type='samples'
1481
+
1482
+ alternative, rng = 'greater', None
1483
+
1484
+ nx, ny, permutations = 15, 15, 32000
1485
+ assert 2**nx > permutations
1486
+
1487
+ rng = np.random.default_rng(8235259808)
1488
+ x = xp.asarray(rng.standard_normal(size=nx))
1489
+ y = xp.asarray(rng.standard_normal(size=ny))
1490
+ data = x, y
1491
+
1492
+ def statistic(x, y, axis):
1493
+ return xp.mean(x - y, axis=axis)
1494
+
1495
+ kwds = {'vectorized': True, 'permutation_type': 'samples',
1496
+ 'batch': 100, 'alternative': alternative, 'rng': rng}
1497
+ res = permutation_test(data, statistic, n_resamples=permutations, **kwds)
1498
+ res2 = permutation_test(data, statistic, n_resamples=xp.inf, **kwds)
1499
+
1500
+ assert res.statistic == res2.statistic
1501
+ xp_assert_close(res.pvalue, res2.pvalue, atol=1e-2)
1502
+
1503
+ # I only need to skip torch on GPU because it doesn't have betaincc for pearsonr
1504
+ @pytest.mark.skip_xp_backends(cpu_only=True, exceptions=['cupy', 'jax.numpy'])
1505
+ def test_randomized_test_against_exact_pairings(self, xp):
1506
+ # check that the randomized and exact tests agree to reasonable
1507
+ # precision for permutation_type='pairings'
1508
+
1509
+ alternative, rng = 'two-sided', self.rng
1510
+
1511
+ nx, ny, permutations = 8, 8, 40000
1512
+ assert special.factorial(nx) > permutations
1513
+
1514
+ rng = np.random.default_rng(8235259808)
1515
+ x = xp.asarray(rng.standard_normal(size=nx))
1516
+ y = xp.asarray(rng.standard_normal(size=ny))
1517
+ data = [x]
1518
+
1519
+ def statistic(x, axis):
1520
+ return stats.pearsonr(x, y, axis=axis).statistic
1521
+
1522
+ kwds = {'vectorized': True, 'permutation_type': 'samples',
1523
+ 'batch': 100, 'alternative': alternative, 'rng': rng}
1524
+ res = permutation_test(data, statistic, n_resamples=permutations, **kwds)
1525
+ res2 = permutation_test(data, statistic, n_resamples=xp.inf, **kwds)
1526
+
1527
+ assert res.statistic == res2.statistic
1528
+ xp_assert_close(res.pvalue, res2.pvalue, atol=1e-2)
1529
+
1530
+ # -- Independent (Unpaired) Sample Tests -- #
1531
+
1532
+ @pytest.mark.skip_xp_backends(eager_only=True) # TODO: change to jax_jit=False
1533
+ @pytest.mark.parametrize('alternative', ("less", "greater", "two-sided"))
1534
+ def test_against_ks_2samp(self, alternative, xp):
1535
+ x = self.rng.normal(size=4, scale=1)
1536
+ y = self.rng.normal(size=5, loc=3, scale=3)
1537
+
1538
+ expected = stats.ks_2samp(x, y, alternative=alternative, mode='exact')
1539
+
1540
+ def statistic(x, y, axis):
1541
+ # todo: use `xp` as backend when `ks_2samp` is translated to array API
1542
+ x, y = _xp_copy_to_numpy(x), _xp_copy_to_numpy(y)
1543
+ res = stats.ks_2samp(x, y, axis=axis, mode='asymp', alternative=alternative)
1544
+ res = xp.asarray(res.statistic)
1545
+ return res[()] if res.ndim == 0 else res
1546
+
1547
+ # ks_2samp is always a one-tailed 'greater' test
1548
+ # it's the statistic that changes (D+ vs D- vs max(D+, D-))
1549
+ x, y = xp.asarray(x), xp.asarray(y)
1550
+ res = permutation_test((x, y), statistic, n_resamples=np.inf,
1551
+ alternative='greater', rng=self.rng)
1552
+
1553
+ xp_assert_close(res.statistic, xp.asarray(expected.statistic), rtol=self.rtol)
1554
+ xp_assert_close(res.pvalue, xp.asarray(expected.pvalue), rtol=self.rtol)
1555
+
1556
+ @pytest.mark.skip_xp_backends(eager_only=True) # TODO: change to jax_jit=False
1557
+ @pytest.mark.parametrize('alternative', ("less", "greater", "two-sided"))
1558
+ def test_against_ansari(self, alternative, xp):
1559
+ x = self.rng.normal(size=4, scale=1)
1560
+ y = self.rng.normal(size=5, scale=3)
1561
+
1562
+ # ansari has a different convention for 'alternative'
1563
+ alternative_correspondence = {"less": "greater",
1564
+ "greater": "less",
1565
+ "two-sided": "two-sided"}
1566
+ alternative_scipy = alternative_correspondence[alternative]
1567
+ expected = stats.ansari(x, y, alternative=alternative_scipy)
1568
+
1569
+ def statistic(x, y, axis):
1570
+ # todo: use `xp` as backend when `ansari` is translated to array API
1571
+ x, y = _xp_copy_to_numpy(x), _xp_copy_to_numpy(y)
1572
+ res = stats.ansari(x, y, axis=axis)
1573
+ res = xp.asarray(res.statistic)
1574
+ return res[()] if res.ndim == 0 else res
1575
+
1576
+ x, y = xp.asarray(x), xp.asarray(y)
1577
+ res = permutation_test((x, y), statistic, n_resamples=np.inf,
1578
+ alternative=alternative, rng=self.rng)
1579
+
1580
+ xp_assert_close(res.statistic, xp.asarray(expected.statistic), rtol=self.rtol)
1581
+ xp_assert_close(res.pvalue, xp.asarray(expected.pvalue), rtol=self.rtol)
1582
+
1583
+ @skip_xp_backends('cupy', reason='needs mannwhitneyu')
1584
+ @skip_xp_backends(eager_only=True) # mannwhitneyu does input validation
1585
+ @pytest.mark.parametrize('alternative', ("less", "greater", "two-sided"))
1586
+ def test_against_mannwhitneyu(self, alternative, xp):
1587
+ x = stats.uniform.rvs(size=(3, 5, 2), loc=0, random_state=self.rng)
1588
+ y = stats.uniform.rvs(size=(3, 5, 2), loc=0.05, random_state=self.rng)
1589
+
1590
+ expected = stats.mannwhitneyu(x, y, axis=1, alternative=alternative)
1591
+
1592
+ def statistic(x, y, axis):
1593
+ return stats.mannwhitneyu(x, y, axis=axis, method='asymptotic').statistic
1594
+
1595
+ x, y = xp.asarray(x), xp.asarray(y)
1596
+ res = permutation_test((x, y), statistic, vectorized=True,
1597
+ n_resamples=xp.inf, alternative=alternative,
1598
+ axis=1, rng=self.rng)
1599
+
1600
+ xp_assert_close(res.statistic, xp.asarray(expected.statistic), rtol=self.rtol)
1601
+ xp_assert_close(res.pvalue, xp.asarray(expected.pvalue), rtol=self.rtol)
1602
+
1603
+ @skip_xp_backends('cupy', reason='needs cramervonmises_2samp')
1604
+ @skip_xp_backends(eager_only=True) # cramervonmises_2samp does input validation
1605
+ @skip_xp_backends(cpu_only=True) # torch doesn't have `kv`
1606
+ def test_against_cvm(self, xp):
1607
+ x = stats.norm.rvs(size=4, scale=1, random_state=self.rng)
1608
+ y = stats.norm.rvs(size=5, loc=3, scale=3, random_state=self.rng)
1609
+
1610
+ expected = stats.cramervonmises_2samp(x, y, method='exact')
1611
+
1612
+ def statistic(x, y, axis):
1613
+ res = stats.cramervonmises_2samp(x, y, axis=axis, method='asymptotic')
1614
+ return res.statistic
1615
+
1616
+ # cramervonmises_2samp has only one alternative, greater
1617
+ x, y = xp.asarray(x), xp.asarray(y)
1618
+ res = permutation_test((x, y), statistic, n_resamples=np.inf,
1619
+ alternative='greater', rng=self.rng)
1620
+
1621
+ xp_assert_close(res.statistic, xp.asarray(expected.statistic), rtol=self.rtol)
1622
+ xp_assert_close(res.pvalue, xp.asarray(expected.pvalue), rtol=self.rtol)
1623
+
1624
+ @skip_xp_backends('cupy', reason='needs kruskal')
1625
+ @skip_xp_backends(eager_only=True) # kruskal does input validation
1626
+ @pytest.mark.parametrize('axis', (-1, 2))
1627
+ def test_vectorized_nsamp_ptype_both(self, axis, xp):
1628
+ # statistic only available for NumPy
1629
+
1630
+ # Test that permutation_test with permutation_type='independent' works
1631
+ # properly for a 3-sample statistic with nd array samples of different
1632
+ # (but compatible) shapes and ndims. Show that exact permutation test
1633
+ # and random permutation tests approximate SciPy's asymptotic pvalues
1634
+ # and that exact and random permutation test results are even closer
1635
+ # to one another (than they are to the asymptotic results).
1636
+
1637
+ # Three samples, different (but compatible) shapes with different ndims
1638
+ rng = np.random.default_rng(6709265303529651545)
1639
+ x = rng.random(size=(3))
1640
+ y = rng.random(size=(1, 3, 2))
1641
+ z = rng.random(size=(2, 1, 4))
1642
+ data = (x, y, z)
1643
+
1644
+ expected = stats.kruskal(*data, axis=axis)
1645
+
1646
+ # Define the statistic (and pvalue for comparison)
1647
+ def statistic(*data, axis):
1648
+ return stats.kruskal(*data, axis=axis).statistic
1649
+
1650
+ # Calculate exact and randomized permutation results
1651
+ kwds = {'axis': axis, 'alternative': 'greater',
1652
+ 'permutation_type': 'independent', 'rng': rng}
1653
+ data = [xp.asarray(data_) for data_ in data]
1654
+ res = permutation_test(data, statistic, n_resamples=xp.inf, **kwds)
1655
+ res2 = permutation_test(data, statistic, n_resamples=1000, **kwds)
1656
+
1657
+ # Check results
1658
+ xp_assert_close(res.statistic, xp.asarray(expected.statistic), rtol=self.rtol*5)
1659
+ xp_assert_close(res.statistic, res2.statistic, rtol=self.rtol*5)
1660
+ xp_assert_close(res.pvalue, xp.asarray(expected.pvalue), atol=6e-2)
1661
+ xp_assert_close(res.pvalue, res2.pvalue, atol=3e-2)
1662
+
1663
+ # -- Paired-Sample Tests -- #
1664
+
1665
+ @pytest.mark.skip_xp_backends(eager_only=True) # TODO: change to jax_jit=False
1666
+ @pytest.mark.parametrize('alternative', ("less", "greater", "two-sided"))
1667
+ def test_against_wilcoxon(self, alternative, xp):
1668
+ x = stats.uniform.rvs(size=(3, 6, 2), loc=0, random_state=self.rng)
1669
+ y = stats.uniform.rvs(size=(3, 6, 2), loc=0.05, random_state=self.rng)
1670
+ expected = stats.wilcoxon(x, y, alternative=alternative, axis=1)
1671
+
1672
+ # We'll check both 1- and 2-sample versions of the same test;
1673
+ # we expect identical results to wilcoxon in all cases.
1674
+ def statistic_1samp_1d(z, axis):
1675
+ # todo: use `xp` as backend when `wilcoxon` is translated to array API
1676
+ # 'less' ensures we get the same of two statistics every time
1677
+ z = _xp_copy_to_numpy(z)
1678
+ res = stats.wilcoxon(z, alternative='less', axis=axis)
1679
+ res = xp.asarray(res.statistic)
1680
+ return res[()] if res.ndim == 0 else res
1681
+
1682
+ def statistic_2samp_1d(x, y, axis):
1683
+ # todo: use `xp` as backend when `wilcoxon` is translated to array API
1684
+ x, y = _xp_copy_to_numpy(x), _xp_copy_to_numpy(y)
1685
+ res = stats.wilcoxon(x, y, alternative='less', axis=axis)
1686
+ res = xp.asarray(res.statistic)
1687
+ return res[()] if res.ndim == 0 else res
1688
+
1689
+ x, y = xp.asarray(x), xp.asarray(y)
1690
+ kwds = {'axis': 1, 'alternative': alternative, 'permutation_type': 'samples',
1691
+ 'rng': self.rng, 'n_resamples': np.inf}
1692
+ res1 = permutation_test((x-y,), statistic_1samp_1d, **kwds)
1693
+ res2 = permutation_test((x, y), statistic_2samp_1d, **kwds)
1694
+
1695
+ # `wilcoxon` returns a different statistic with 'two-sided'
1696
+ xp_assert_close(res1.statistic, res2.statistic, rtol=self.rtol)
1697
+ if alternative != 'two-sided':
1698
+ xp_assert_close(res2.statistic, xp.asarray(expected.statistic),
1699
+ rtol=self.rtol)
1700
+
1701
+ xp_assert_close(res2.pvalue, xp.asarray(expected.pvalue), rtol=self.rtol)
1702
+ xp_assert_close(res1.pvalue, res2.pvalue, rtol=self.rtol)
1703
+
1704
+ @pytest.mark.parametrize('alternative', ("less", "greater", "two-sided"))
1705
+ def test_against_binomtest(self, alternative, xp):
1706
+
1707
+ x = self.rng.integers(0, 2, size=10)
1708
+ x[x == 0] = -1
1709
+ # More naturally, the test would flip elements between 0 and one.
1710
+ # However, permutation_test will flip the _signs_ of the elements.
1711
+ # So we have to work with +1/-1 instead of 1/0.
1712
+
1713
+ def statistic(x, axis=0):
1714
+ xp_ = array_namespace(x)
1715
+ return xp_.count_nonzero(x > 0, axis=axis)
1716
+
1717
+ k, n, p = statistic(x), 10, 0.5
1718
+ expected = stats.binomtest(k, n, p, alternative=alternative)
1719
+
1720
+ res = stats.permutation_test((xp.asarray(x, dtype=xp.float64),), statistic,
1721
+ vectorized=True,
1722
+ permutation_type='samples', n_resamples=xp.inf,
1723
+ rng=self.rng, alternative=alternative)
1724
+ xp_assert_close(res.pvalue, xp.asarray(expected.pvalue), rtol=self.rtol)
1725
+
1726
+ # -- Exact Association Tests -- #
1727
+
1728
+ @pytest.mark.skip_xp_backends(eager_only=True) # TODO: change to jax_jit=False
1729
+ def test_against_kendalltau(self, xp):
1730
+ x = self.rng.normal(size=6)
1731
+ y = x + self.rng.normal(size=6)
1732
+
1733
+ expected = stats.kendalltau(x, y, method='exact')
1734
+
1735
+ def statistic(x, axis):
1736
+ # todo: use `xp` as backend when `kendalltau` is translated to array API
1737
+ x = _xp_copy_to_numpy(x)
1738
+ res = stats.kendalltau(x, y, method='asymptotic', axis=axis)
1739
+ res = xp.asarray(res.statistic)
1740
+ return res[()] if res.ndim == 0 else res
1741
+
1742
+ # kendalltau currently has only one alternative, two-sided
1743
+ x = xp.asarray(x)
1744
+ res = permutation_test((x,), statistic, permutation_type='pairings',
1745
+ n_resamples=np.inf, rng=self.rng)
1746
+
1747
+ xp_assert_close(res.statistic, xp.asarray(expected.statistic), rtol=self.rtol)
1748
+ xp_assert_close(res.pvalue, xp.asarray(expected.pvalue), rtol=self.rtol)
1749
+
1750
+ @pytest.mark.parametrize('alternative', ('less', 'greater', 'two-sided'))
1751
+ def test_against_fisher_exact(self, alternative, xp):
1752
+ # x and y are binary random variables with some dependence
1753
+ rng = np.random.default_rng(6235696159000529929)
1754
+ x = (rng.random(7) > 0.6).astype(float)
1755
+ y = (rng.random(7) + 0.25*x > 0.6).astype(float)
1756
+ tab = stats.contingency.crosstab(x, y)[1]
1757
+
1758
+ x, y = xp.asarray(x), xp.asarray(y)
1759
+ def statistic(x, axis):
1760
+ return xp.count_nonzero((x == 1) & (y == 1), axis=axis)
1761
+
1762
+ res = permutation_test((x,), statistic, permutation_type='pairings',
1763
+ n_resamples=xp.inf, alternative=alternative,
1764
+ rng=rng)
1765
+ res2 = stats.fisher_exact(tab, alternative=alternative)
1766
+
1767
+ xp_assert_close(res.pvalue, xp.asarray(res2.pvalue, dtype=x.dtype))
1768
+
1769
+ @pytest.mark.xslow()
1770
+ @pytest.mark.parametrize('axis', (-2, 1))
1771
+ def test_vectorized_nsamp_ptype_samples(self, axis):
1772
+ # statistic only available for NumPy, and it's a pain to vectorize
1773
+
1774
+ # Test that permutation_test with permutation_type='samples' works
1775
+ # properly for a 3-sample statistic with nd array samples of different
1776
+ # (but compatible) shapes and ndims. Show that exact permutation test
1777
+ # reproduces SciPy's exact pvalue and that random permutation test
1778
+ # approximates it.
1779
+
1780
+ x = self.rng.random(size=(2, 4, 3))
1781
+ y = self.rng.random(size=(1, 4, 3))
1782
+ z = self.rng.random(size=(2, 4, 1))
1783
+ x = stats.rankdata(x, axis=axis)
1784
+ y = stats.rankdata(y, axis=axis)
1785
+ z = stats.rankdata(z, axis=axis)
1786
+ y = y[0] # to check broadcast with different ndim
1787
+ data = (x, y, z)
1788
+
1789
+ def statistic1d(*data):
1790
+ return stats.page_trend_test(data, ranked=True,
1791
+ method='asymptotic').statistic
1792
+
1793
+ def pvalue1d(*data):
1794
+ return stats.page_trend_test(data, ranked=True,
1795
+ method='exact').pvalue
1796
+
1797
+ statistic = _resampling._vectorize_statistic(statistic1d)
1798
+ pvalue = _resampling._vectorize_statistic(pvalue1d)
1799
+
1800
+ expected_statistic = statistic(*np.broadcast_arrays(*data), axis=axis)
1801
+ expected_pvalue = pvalue(*np.broadcast_arrays(*data), axis=axis)
1802
+
1803
+ # Let's forgive this use of an integer seed, please.
1804
+ kwds = {'vectorized': False, 'axis': axis, 'alternative': 'greater',
1805
+ 'permutation_type': 'pairings', 'rng': 0}
1806
+ res = permutation_test(data, statistic1d, n_resamples=np.inf, **kwds)
1807
+ res2 = permutation_test(data, statistic1d, n_resamples=5000, **kwds)
1808
+
1809
+ assert_allclose(res.statistic, expected_statistic, rtol=self.rtol)
1810
+ assert_allclose(res.statistic, res2.statistic, rtol=self.rtol)
1811
+ assert_allclose(res.pvalue, expected_pvalue, rtol=self.rtol)
1812
+ assert_allclose(res.pvalue, res2.pvalue, atol=3e-2)
1813
+
1814
+ # -- Test Against External References -- #
1815
+
1816
+ tie_case_1 = {'x': [1, 2, 3, 4], 'y': [1.5, 2, 2.5],
1817
+ 'expected_less': 0.2000000000,
1818
+ 'expected_2sided': 0.4, # 2*expected_less
1819
+ 'expected_Pr_gte_S_mean': 0.3428571429, # see note below
1820
+ 'expected_statistic': 7.5,
1821
+ 'expected_avg': 9.142857, 'expected_std': 1.40698}
1822
+ tie_case_2 = {'x': [111, 107, 100, 99, 102, 106, 109, 108],
1823
+ 'y': [107, 108, 106, 98, 105, 103, 110, 105, 104],
1824
+ 'expected_less': 0.1555738379,
1825
+ 'expected_2sided': 0.3111476758,
1826
+ 'expected_Pr_gte_S_mean': 0.2969971205, # see note below
1827
+ 'expected_statistic': 32.5,
1828
+ 'expected_avg': 38.117647, 'expected_std': 5.172124}
1829
+
1830
+ @pytest.mark.skip_xp_backends(eager_only=True) # TODO: change to jax_jit=False
1831
+ @pytest.mark.xslow() # only the second case is slow, really
1832
+ @pytest.mark.parametrize('case', (tie_case_1, tie_case_2))
1833
+ def test_with_ties(self, case, xp):
1834
+ """
1835
+ Results above from SAS PROC NPAR1WAY, e.g.
1836
+
1837
+ DATA myData;
1838
+ INPUT X Y;
1839
+ CARDS;
1840
+ 1 1
1841
+ 1 2
1842
+ 1 3
1843
+ 1 4
1844
+ 2 1.5
1845
+ 2 2
1846
+ 2 2.5
1847
+ ods graphics on;
1848
+ proc npar1way AB data=myData;
1849
+ class X;
1850
+ EXACT;
1851
+ run;
1852
+ ods graphics off;
1853
+
1854
+ Note: SAS provides Pr >= |S-Mean|, which is different from our
1855
+ definition of a two-sided p-value.
1856
+
1857
+ """
1858
+
1859
+ x = case['x']
1860
+ y = case['y']
1861
+
1862
+ expected_statistic = xp.asarray(case['expected_statistic'])
1863
+ expected_less = xp.asarray(case['expected_less'])
1864
+ expected_2sided = xp.asarray(case['expected_2sided'])
1865
+ expected_Pr_gte_S_mean = xp.asarray(case['expected_Pr_gte_S_mean'])
1866
+ expected_avg = xp.asarray(case['expected_avg'])
1867
+ expected_std = xp.asarray(case['expected_std'])
1868
+
1869
+ def statistic(x, y, axis):
1870
+ # todo: use `xp` as backend when `ansari` is translated to array API
1871
+ x, y = _xp_copy_to_numpy(x), _xp_copy_to_numpy(y)
1872
+ res = stats.ansari(x, y, axis=axis)
1873
+ res = xp.asarray(res.statistic)
1874
+ return res[()] if res.ndim == 0 else res
1875
+
1876
+ dtype = xp_default_dtype(xp)
1877
+ x, y = xp.asarray(x, dtype=dtype), xp.asarray(y, dtype=dtype)
1878
+ with warnings.catch_warnings():
1879
+ warnings.filterwarnings(
1880
+ "ignore", "Ties preclude use of exact statistic", UserWarning)
1881
+ res = permutation_test((x, y), statistic, n_resamples=np.inf,
1882
+ alternative='less')
1883
+ res2 = permutation_test((x, y), statistic, n_resamples=np.inf,
1884
+ alternative='two-sided')
1885
+
1886
+ xp_assert_close(res.statistic, expected_statistic, rtol=self.rtol)
1887
+ xp_assert_close(res.pvalue, expected_less, atol=1e-10)
1888
+ xp_assert_close(res2.pvalue, expected_2sided, atol=1e-10)
1889
+ xp_assert_close(xp.mean(res2.null_distribution), expected_avg, rtol=1e-6)
1890
+ xp_assert_close(xp.std(res2.null_distribution), expected_std, rtol=1e-6)
1891
+
1892
+ # SAS provides Pr >= |S-Mean|; might as well check against that, too
1893
+ S = res.statistic
1894
+ mean = xp.mean(res.null_distribution)
1895
+ n = res.null_distribution.shape[0]
1896
+ Pr_gte_S_mean = xp.astype(xp.count_nonzero(
1897
+ xp.abs(res.null_distribution-mean) >= xp.abs(S-mean)), S.dtype) / n
1898
+ xp_assert_close(Pr_gte_S_mean, expected_Pr_gte_S_mean)
1899
+
1900
+ @pytest.mark.slow
1901
+ @pytest.mark.parametrize('alternative, expected_pvalue',
1902
+ (('less', 0.9708333333333),
1903
+ ('greater', 0.05138888888889),
1904
+ ('two-sided', 0.1027777777778)))
1905
+ # I only need to skip torch on GPU because it doesn't have betaincc for pearsonr
1906
+ @pytest.mark.skip_xp_backends(cpu_only=True, exceptions=['cupy', 'jax.numpy'])
1907
+ @pytest.mark.skip_xp_backends(eager_only=True) # TODO: change to jax_jit=False
1908
+ def test_against_spearmanr_in_R(self, alternative, expected_pvalue, xp):
1909
+ """
1910
+ Results above from R cor.test, e.g.
1911
+
1912
+ options(digits=16)
1913
+ x <- c(1.76405235, 0.40015721, 0.97873798,
1914
+ 2.2408932, 1.86755799, -0.97727788)
1915
+ y <- c(2.71414076, 0.2488, 0.87551913,
1916
+ 2.6514917, 2.01160156, 0.47699563)
1917
+ cor.test(x, y, method = "spearm", alternative = "t")
1918
+ """
1919
+ # data comes from
1920
+ # np.random.seed(0)
1921
+ # x = stats.norm.rvs(size=6)
1922
+ # y = x + stats.norm.rvs(size=6)
1923
+ x = xp.asarray([1.76405235, 0.40015721, 0.97873798,
1924
+ 2.2408932, 1.86755799, -0.97727788])
1925
+ y = xp.asarray([2.71414076, 0.2488, 0.87551913,
1926
+ 2.6514917, 2.01160156, 0.47699563])
1927
+ expected_statistic = 0.7714285714285715
1928
+
1929
+ y = xp.asarray(stats.rankdata(_xp_copy_to_numpy(y)))
1930
+ def statistic(x, axis):
1931
+ # `spearmanr` is not array api compatible, but `pearsonr` is. So for now
1932
+ # use _xp_copy_to_numpy just for ranking so we can run this test w/ CuPy.
1933
+ # TODO: use `xp` as backend when cupy works with `rankdata`
1934
+ x = xp.asarray(stats.rankdata(_xp_copy_to_numpy(x), axis=axis))
1935
+ return stats.pearsonr(x, y, axis=axis).statistic
1936
+
1937
+ res = permutation_test((x,), statistic, permutation_type='pairings',
1938
+ n_resamples=xp.inf, alternative=alternative)
1939
+
1940
+ xp_assert_close(res.statistic, xp.asarray(expected_statistic), rtol=self.rtol)
1941
+ xp_assert_close(res.pvalue, xp.asarray(expected_pvalue), atol=1e-13)
1942
+
1943
+ @pytest.mark.parametrize("batch", (-1, 0))
1944
+ def test_batch_generator_iv(self, batch):
1945
+ with pytest.raises(ValueError, match="`batch` must be positive."):
1946
+ list(_resampling._batch_generator([1, 2, 3], batch))
1947
+
1948
+ batch_generator_cases = [(range(0), 3, []),
1949
+ (range(6), 3, [[0, 1, 2], [3, 4, 5]]),
1950
+ (range(8), 3, [[0, 1, 2], [3, 4, 5], [6, 7]])]
1951
+
1952
+ @pytest.mark.parametrize("iterable, batch, expected",
1953
+ batch_generator_cases)
1954
+ def test_batch_generator(self, iterable, batch, expected):
1955
+ got = list(_resampling._batch_generator(iterable, batch))
1956
+ assert got == expected
1957
+
1958
+ @pytest.mark.fail_slow(2)
1959
+ # I only need to skip torch on GPU because it doesn't have betaincc for pearsonr
1960
+ @pytest.mark.skip_xp_backends(cpu_only=True, exceptions=['cupy', 'jax.numpy'])
1961
+ def test_finite_precision_statistic(self, xp):
1962
+ # Some statistics return numerically distinct values when the values
1963
+ # should be equal in theory. Test that `permutation_test` accounts
1964
+ # for this in some way.
1965
+ x = xp.asarray([1., 2., 4., 3.], dtype=xp.float64)
1966
+ y = xp.asarray([2., 4., 6., 8.], dtype=xp.float64)
1967
+
1968
+ def statistic(x, y, axis):
1969
+ return stats.pearsonr(x, y, axis=axis)[0]
1970
+
1971
+ res = stats.permutation_test((x, y), statistic,
1972
+ permutation_type='pairings')
1973
+ r, pvalue, null = res.statistic, res.pvalue, res.null_distribution
1974
+
1975
+ correct_p = 2 * float(xp.count_nonzero(null >= r - 1e-14)) / null.shape[0]
1976
+ assert pvalue == correct_p == 1/3
1977
+ # Compare against other exact correlation tests using R corr.test
1978
+ # options(digits=16)
1979
+ # x = c(1, 2, 4, 3)
1980
+ # y = c(2, 4, 6, 8)
1981
+ # cor.test(x, y, alternative = "t", method = "spearman") # 0.333333333
1982
+ # cor.test(x, y, alternative = "t", method = "kendall") # 0.333333333
1983
+
1984
+
1985
+ def test_all_partitions_concatenated():
1986
+ # make sure that _all_paritions_concatenated produces the correct number
1987
+ # of partitions of the data into samples of the given sizes and that
1988
+ # all are unique
1989
+ n = np.array([3, 2, 4], dtype=int)
1990
+ nc = np.cumsum(n)
1991
+
1992
+ all_partitions = set()
1993
+ counter = 0
1994
+ for partition_concatenated in _resampling._all_partitions_concatenated(n):
1995
+ counter += 1
1996
+ partitioning = np.split(partition_concatenated, nc[:-1])
1997
+ all_partitions.add(tuple([frozenset(i) for i in partitioning]))
1998
+
1999
+ expected = np.prod([special.binom(sum(n[i:]), sum(n[i+1:]))
2000
+ for i in range(len(n)-1)])
2001
+
2002
+ assert_equal(counter, expected)
2003
+ assert_equal(len(all_partitions), expected)
2004
+
2005
+
2006
+ @pytest.mark.parametrize('fun_name',
2007
+ ['bootstrap', 'permutation_test', 'monte_carlo_test'])
2008
+ def test_parameter_vectorized(fun_name):
2009
+ # Check that parameter `vectorized` is working as desired for all
2010
+ # resampling functions. Results don't matter; just don't fail asserts.
2011
+ rng = np.random.default_rng(75245098234592)
2012
+ sample = rng.random(size=10)
2013
+
2014
+ def rvs(size): # needed by `monte_carlo_test`
2015
+ return stats.norm.rvs(size=size, random_state=rng)
2016
+
2017
+ fun_options = {'bootstrap': {'data': (sample,), 'rng': rng,
2018
+ 'method': 'percentile'},
2019
+ 'permutation_test': {'data': (sample,), 'rng': rng,
2020
+ 'permutation_type': 'samples'},
2021
+ 'monte_carlo_test': {'sample': sample, 'rvs': rvs}}
2022
+ common_options = {'n_resamples': 100}
2023
+
2024
+ fun = getattr(stats, fun_name)
2025
+ options = fun_options[fun_name]
2026
+ options.update(common_options)
2027
+
2028
+ def statistic(x, axis):
2029
+ assert x.ndim > 1 or np.array_equal(x, sample)
2030
+ return np.mean(x, axis=axis)
2031
+ fun(statistic=statistic, vectorized=None, **options)
2032
+ fun(statistic=statistic, vectorized=True, **options)
2033
+
2034
+ def statistic(x):
2035
+ assert x.ndim == 1
2036
+ return np.mean(x)
2037
+ fun(statistic=statistic, vectorized=None, **options)
2038
+ fun(statistic=statistic, vectorized=False, **options)
2039
+
2040
+
2041
+ class TestMonteCarloMethod:
2042
+ def test_rvs_and_random_state(self):
2043
+ message = "Use of `rvs` and `rng` are mutually exclusive."
2044
+ rng = np.random.default_rng(34982345)
2045
+ with pytest.raises(ValueError, match=message):
2046
+ stats.MonteCarloMethod(rvs=rng.random, rng=rng)
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_sampling.py ADDED
@@ -0,0 +1,1448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import threading
2
+ import pickle
3
+ import pytest
4
+ from copy import deepcopy
5
+ import platform
6
+ import sys
7
+ import math
8
+ import warnings
9
+
10
+ import numpy as np
11
+ from numpy.testing import assert_allclose, assert_equal
12
+ from scipy.stats.sampling import (
13
+ TransformedDensityRejection,
14
+ DiscreteAliasUrn,
15
+ DiscreteGuideTable,
16
+ NumericalInversePolynomial,
17
+ NumericalInverseHermite,
18
+ RatioUniforms,
19
+ SimpleRatioUniforms,
20
+ UNURANError
21
+ )
22
+ from pytest import raises as assert_raises
23
+ from scipy import stats
24
+ from scipy import special
25
+ from scipy.stats import chisquare, cramervonmises
26
+ from scipy.stats._distr_params import distdiscrete, distcont
27
+ from scipy._lib._util import check_random_state
28
+
29
+
30
+ # common test data: this data can be shared between all the tests.
31
+
32
+
33
+ # Normal distribution shared between all the continuous methods
34
+ class StandardNormal:
35
+ def pdf(self, x):
36
+ # normalization constant needed for NumericalInverseHermite
37
+ return 1./np.sqrt(2.*np.pi) * np.exp(-0.5 * x*x)
38
+
39
+ def dpdf(self, x):
40
+ return 1./np.sqrt(2.*np.pi) * -x * np.exp(-0.5 * x*x)
41
+
42
+ def cdf(self, x):
43
+ return special.ndtr(x)
44
+
45
+
46
+ all_methods = [
47
+ ("TransformedDensityRejection", {"dist": StandardNormal()}),
48
+ ("DiscreteAliasUrn", {"dist": [0.02, 0.18, 0.8]}),
49
+ ("DiscreteGuideTable", {"dist": [0.02, 0.18, 0.8]}),
50
+ ("NumericalInversePolynomial", {"dist": StandardNormal()}),
51
+ ("NumericalInverseHermite", {"dist": StandardNormal()}),
52
+ ("SimpleRatioUniforms", {"dist": StandardNormal(), "mode": 0})
53
+ ]
54
+
55
+ if (sys.implementation.name == 'pypy'
56
+ and sys.implementation.version < (7, 3, 10)):
57
+ # changed in PyPy for v7.3.10
58
+ floaterr = r"unsupported operand type for float\(\): 'list'"
59
+ else:
60
+ floaterr = r"must be real number, not list"
61
+ # Make sure an internal error occurs in UNU.RAN when invalid callbacks are
62
+ # passed. Moreover, different generators throw different error messages.
63
+ # So, in case of an `UNURANError`, we do not validate the error message.
64
+ bad_pdfs_common = [
65
+ # Negative PDF
66
+ (lambda x: -x, UNURANError, r"..."),
67
+ # Returning wrong type
68
+ (lambda x: [], TypeError, floaterr),
69
+ # Undefined name inside the function
70
+ (lambda x: foo, NameError, r"name 'foo' is not defined"), # type: ignore[name-defined] # noqa: F821, E501
71
+ # Infinite value returned => Overflow error.
72
+ (lambda x: np.inf, UNURANError, r"..."),
73
+ # NaN value => internal error in UNU.RAN
74
+ (lambda x: np.nan, UNURANError, r"..."),
75
+ # signature of PDF wrong
76
+ (lambda: 1.0, TypeError, r"takes 0 positional arguments but 1 was given")
77
+ ]
78
+
79
+
80
+ # same approach for dpdf
81
+ bad_dpdf_common = [
82
+ # Infinite value returned.
83
+ (lambda x: np.inf, UNURANError, r"..."),
84
+ # NaN value => internal error in UNU.RAN
85
+ (lambda x: np.nan, UNURANError, r"..."),
86
+ # Returning wrong type
87
+ (lambda x: [], TypeError, floaterr),
88
+ # Undefined name inside the function
89
+ (lambda x: foo, NameError, r"name 'foo' is not defined"), # type: ignore[name-defined] # noqa: F821, E501
90
+ # signature of dPDF wrong
91
+ (lambda: 1.0, TypeError, r"takes 0 positional arguments but 1 was given")
92
+ ]
93
+
94
+
95
+ # same approach for logpdf
96
+ bad_logpdfs_common = [
97
+ # Returning wrong type
98
+ (lambda x: [], TypeError, floaterr),
99
+ # Undefined name inside the function
100
+ (lambda x: foo, NameError, r"name 'foo' is not defined"), # type: ignore[name-defined] # noqa: F821, E501
101
+ # Infinite value returned => Overflow error.
102
+ (lambda x: np.inf, UNURANError, r"..."),
103
+ # NaN value => internal error in UNU.RAN
104
+ (lambda x: np.nan, UNURANError, r"..."),
105
+ # signature of logpdf wrong
106
+ (lambda: 1.0, TypeError, r"takes 0 positional arguments but 1 was given")
107
+ ]
108
+
109
+
110
+ bad_pv_common = [
111
+ ([], r"must contain at least one element"),
112
+ ([[1.0, 0.0]], r"wrong number of dimensions \(expected 1, got 2\)"),
113
+ ([0.2, 0.4, np.nan, 0.8], r"must contain only finite / non-nan values"),
114
+ ([0.2, 0.4, np.inf, 0.8], r"must contain only finite / non-nan values"),
115
+ ([0.0, 0.0], r"must contain at least one non-zero value"),
116
+ ]
117
+
118
+
119
+ # size of the domains is incorrect
120
+ bad_sized_domains = [
121
+ # > 2 elements in the domain
122
+ ((1, 2, 3), ValueError, r"must be a length 2 tuple"),
123
+ # empty domain
124
+ ((), ValueError, r"must be a length 2 tuple")
125
+ ]
126
+
127
+ # domain values are incorrect
128
+ bad_domains = [
129
+ ((2, 1), UNURANError, r"left >= right"),
130
+ ((1, 1), UNURANError, r"left >= right"),
131
+ ]
132
+
133
+ # infinite and nan values present in domain.
134
+ inf_nan_domains = [
135
+ # left >= right
136
+ ((10, 10), UNURANError, r"left >= right"),
137
+ ((np.inf, np.inf), UNURANError, r"left >= right"),
138
+ ((-np.inf, -np.inf), UNURANError, r"left >= right"),
139
+ ((np.inf, -np.inf), UNURANError, r"left >= right"),
140
+ # Also include nans in some of the domains.
141
+ ((-np.inf, np.nan), ValueError, r"only non-nan values"),
142
+ ((np.nan, np.inf), ValueError, r"only non-nan values")
143
+ ]
144
+
145
+ # `nan` values present in domain. Some distributions don't support
146
+ # infinite tails, so don't mix the nan values with infinities.
147
+ nan_domains = [
148
+ ((0, np.nan), ValueError, r"only non-nan values"),
149
+ ((np.nan, np.nan), ValueError, r"only non-nan values")
150
+ ]
151
+
152
+
153
+ # all the methods should throw errors for nan, bad sized, and bad valued
154
+ # domains.
155
+ @pytest.mark.parametrize("domain, err, msg",
156
+ bad_domains + bad_sized_domains +
157
+ nan_domains) # type: ignore[operator]
158
+ @pytest.mark.parametrize("method, kwargs", all_methods)
159
+ def test_bad_domain(domain, err, msg, method, kwargs):
160
+ Method = getattr(stats.sampling, method)
161
+ with pytest.raises(err, match=msg):
162
+ Method(**kwargs, domain=domain)
163
+
164
+
165
+ @pytest.mark.parametrize("method, kwargs", all_methods)
166
+ def test_random_state(method, kwargs):
167
+ Method = getattr(stats.sampling, method)
168
+
169
+ # simple seed that works for any version of NumPy
170
+ seed = 123
171
+ rng1 = Method(**kwargs, random_state=seed)
172
+ rng2 = Method(**kwargs, random_state=seed)
173
+ assert_equal(rng1.rvs(100), rng2.rvs(100))
174
+
175
+ # global seed
176
+ rng = np.random.RandomState(123)
177
+ rng1 = Method(**kwargs)
178
+ rvs1 = rng1.rvs(100, random_state=rng)
179
+ np.random.seed(None) # valid use of np.random.seed
180
+ rng2 = Method(**kwargs, random_state=123)
181
+ rvs2 = rng2.rvs(100)
182
+ assert_equal(rvs1, rvs2)
183
+
184
+ # Generator seed for new NumPy
185
+ # when a RandomState is given, it should take the bitgen_t
186
+ # member of the class and create a Generator instance.
187
+ seed1 = np.random.RandomState(np.random.MT19937(123))
188
+ seed2 = np.random.Generator(np.random.MT19937(123))
189
+ rng1 = Method(**kwargs, random_state=seed1)
190
+ rng2 = Method(**kwargs, random_state=seed2)
191
+ assert_equal(rng1.rvs(100), rng2.rvs(100))
192
+
193
+
194
+ def test_set_random_state():
195
+ rng1 = TransformedDensityRejection(StandardNormal(), random_state=123)
196
+ rng2 = TransformedDensityRejection(StandardNormal())
197
+ rng2.set_random_state(123)
198
+ assert_equal(rng1.rvs(100), rng2.rvs(100))
199
+ rng = TransformedDensityRejection(StandardNormal(), random_state=123)
200
+ rvs1 = rng.rvs(100)
201
+ rng.set_random_state(123)
202
+ rvs2 = rng.rvs(100)
203
+ assert_equal(rvs1, rvs2)
204
+
205
+
206
+ def test_threading_behaviour():
207
+ # Test if the API is thread-safe.
208
+ # This verifies if the lock mechanism and the use of `PyErr_Occurred`
209
+ # is correct.
210
+ errors = {"err1": None, "err2": None}
211
+
212
+ class Distribution:
213
+ def __init__(self, pdf_msg):
214
+ self.pdf_msg = pdf_msg
215
+
216
+ def pdf(self, x):
217
+ if 49.9 < x < 50.0:
218
+ raise ValueError(self.pdf_msg)
219
+ return x
220
+
221
+ def dpdf(self, x):
222
+ return 1
223
+
224
+ def func1():
225
+ dist = Distribution('foo')
226
+ rng = TransformedDensityRejection(dist, domain=(10, 100),
227
+ random_state=12)
228
+ try:
229
+ rng.rvs(100000)
230
+ except ValueError as e:
231
+ errors['err1'] = e.args[0]
232
+
233
+ def func2():
234
+ dist = Distribution('bar')
235
+ rng = TransformedDensityRejection(dist, domain=(10, 100),
236
+ random_state=2)
237
+ try:
238
+ rng.rvs(100000)
239
+ except ValueError as e:
240
+ errors['err2'] = e.args[0]
241
+
242
+ t1 = threading.Thread(target=func1)
243
+ t2 = threading.Thread(target=func2)
244
+
245
+ t1.start()
246
+ t2.start()
247
+
248
+ t1.join()
249
+ t2.join()
250
+
251
+ assert errors['err1'] == 'foo'
252
+ assert errors['err2'] == 'bar'
253
+
254
+
255
+ @pytest.mark.parametrize("method, kwargs", all_methods)
256
+ def test_pickle(method, kwargs):
257
+ Method = getattr(stats.sampling, method)
258
+ rng1 = Method(**kwargs, random_state=123)
259
+ obj = pickle.dumps(rng1)
260
+ rng2 = pickle.loads(obj)
261
+ assert_equal(rng1.rvs(100), rng2.rvs(100))
262
+
263
+
264
+ @pytest.mark.parametrize("size", [None, 0, (0, ), 1, (10, 3), (2, 3, 4, 5),
265
+ (0, 0), (0, 1)])
266
+ def test_rvs_size(size):
267
+ # As the `rvs` method is present in the base class and shared between
268
+ # all the classes, we can just test with one of the methods.
269
+ rng = TransformedDensityRejection(StandardNormal())
270
+ if size is None:
271
+ assert np.isscalar(rng.rvs(size))
272
+ else:
273
+ if np.isscalar(size):
274
+ size = (size, )
275
+ assert rng.rvs(size).shape == size
276
+
277
+
278
+ def test_with_scipy_distribution():
279
+ # test if the setup works with SciPy's rv_frozen distributions
280
+ dist = stats.norm()
281
+ urng = np.random.default_rng(0)
282
+ rng = NumericalInverseHermite(dist, random_state=urng)
283
+ u = np.linspace(0, 1, num=100)
284
+ check_cont_samples(rng, dist, dist.stats())
285
+ assert_allclose(dist.ppf(u), rng.ppf(u))
286
+ # test if it works with `loc` and `scale`
287
+ dist = stats.norm(loc=10., scale=5.)
288
+ rng = NumericalInverseHermite(dist, random_state=urng)
289
+ check_cont_samples(rng, dist, dist.stats())
290
+ assert_allclose(dist.ppf(u), rng.ppf(u))
291
+ # check for discrete distributions
292
+ dist = stats.binom(10, 0.2)
293
+ rng = DiscreteAliasUrn(dist, random_state=urng)
294
+ domain = dist.support()
295
+ pv = dist.pmf(np.arange(domain[0], domain[1]+1))
296
+ check_discr_samples(rng, pv, dist.stats())
297
+
298
+
299
+ def check_cont_samples(rng, dist, mv_ex, rtol=1e-7, atol=1e-1):
300
+ rvs = rng.rvs(100000)
301
+ mv = rvs.mean(), rvs.var()
302
+ # test the moments only if the variance is finite
303
+ if np.isfinite(mv_ex[1]):
304
+ assert_allclose(mv, mv_ex, rtol=rtol, atol=atol)
305
+ # Cramer Von Mises test for goodness-of-fit
306
+ rvs = rng.rvs(500)
307
+ dist.cdf = np.vectorize(dist.cdf)
308
+ pval = cramervonmises(rvs, dist.cdf).pvalue
309
+ assert pval > 0.1
310
+
311
+
312
+ def check_discr_samples(rng, pv, mv_ex, rtol=1e-3, atol=1e-1):
313
+ rvs = rng.rvs(100000)
314
+ # test if the first few moments match
315
+ mv = rvs.mean(), rvs.var()
316
+ assert_allclose(mv, mv_ex, rtol=rtol, atol=atol)
317
+ # normalize
318
+ pv = pv / pv.sum()
319
+ # chi-squared test for goodness-of-fit
320
+ obs_freqs = np.zeros_like(pv)
321
+ _, freqs = np.unique(rvs, return_counts=True)
322
+ freqs = freqs / freqs.sum()
323
+ obs_freqs[:freqs.size] = freqs
324
+ pval = chisquare(obs_freqs, pv).pvalue
325
+ assert pval > 0.1
326
+
327
+
328
+ def test_warning_center_not_in_domain():
329
+ # UNURAN will warn if the center provided or the one computed w/o the
330
+ # domain is outside of the domain
331
+ msg = "102 : center moved into domain of distribution"
332
+ with pytest.warns(RuntimeWarning, match=msg):
333
+ NumericalInversePolynomial(StandardNormal(), center=0, domain=(3, 5))
334
+ with pytest.warns(RuntimeWarning, match=msg):
335
+ NumericalInversePolynomial(StandardNormal(), domain=(3, 5))
336
+
337
+
338
+ @pytest.mark.parametrize('method', ["SimpleRatioUniforms",
339
+ "NumericalInversePolynomial",
340
+ "TransformedDensityRejection"])
341
+ def test_error_mode_not_in_domain(method):
342
+ # UNURAN raises an error if the mode is not in the domain
343
+ # the behavior is different compared to the case that center is not in the
344
+ # domain. mode is supposed to be the exact value, center can be an
345
+ # approximate value
346
+ Method = getattr(stats.sampling, method)
347
+ msg = "17 : mode not in domain"
348
+ with pytest.raises(UNURANError, match=msg):
349
+ Method(StandardNormal(), mode=0, domain=(3, 5))
350
+
351
+
352
+ @pytest.mark.parametrize('method', ["NumericalInverseHermite",
353
+ "NumericalInversePolynomial"])
354
+ class TestQRVS:
355
+ def test_input_validation(self, method):
356
+ match = "`qmc_engine` must be an instance of..."
357
+ with pytest.raises(ValueError, match=match):
358
+ Method = getattr(stats.sampling, method)
359
+ gen = Method(StandardNormal())
360
+ gen.qrvs(qmc_engine=0)
361
+
362
+ # issues with QMCEngines and old NumPy
363
+ Method = getattr(stats.sampling, method)
364
+ gen = Method(StandardNormal())
365
+
366
+ match = "`d` must be consistent with dimension of `qmc_engine`."
367
+ with pytest.raises(ValueError, match=match):
368
+ gen.qrvs(d=3, qmc_engine=stats.qmc.Halton(2))
369
+
370
+ qrngs = [None, stats.qmc.Sobol(1, seed=0), stats.qmc.Halton(3, seed=0)]
371
+ # `size=None` should not add anything to the shape, `size=1` should
372
+ sizes = [(None, tuple()), (1, (1,)), (4, (4,)),
373
+ ((4,), (4,)), ((2, 4), (2, 4))] # type: ignore
374
+ # Neither `d=None` nor `d=1` should add anything to the shape
375
+ ds = [(None, tuple()), (1, tuple()), (3, (3,))]
376
+
377
+ @pytest.mark.parametrize('qrng', qrngs)
378
+ @pytest.mark.parametrize('size_in, size_out', sizes)
379
+ @pytest.mark.parametrize('d_in, d_out', ds)
380
+ @pytest.mark.thread_unsafe(reason="fails in parallel")
381
+ def test_QRVS_shape_consistency(self, qrng, size_in, size_out,
382
+ d_in, d_out, method):
383
+ w32 = sys.platform == "win32" and platform.architecture()[0] == "32bit"
384
+ if w32 and method == "NumericalInversePolynomial":
385
+ pytest.xfail("NumericalInversePolynomial.qrvs fails for Win "
386
+ "32-bit")
387
+
388
+ dist = StandardNormal()
389
+ Method = getattr(stats.sampling, method)
390
+ gen = Method(dist)
391
+
392
+ # If d and qrng.d are inconsistent, an error is raised
393
+ if d_in is not None and qrng is not None and qrng.d != d_in:
394
+ match = "`d` must be consistent with dimension of `qmc_engine`."
395
+ with pytest.raises(ValueError, match=match):
396
+ gen.qrvs(size_in, d=d_in, qmc_engine=qrng)
397
+ return
398
+
399
+ # Sometimes d is really determined by qrng
400
+ if d_in is None and qrng is not None and qrng.d != 1:
401
+ d_out = (qrng.d,)
402
+
403
+ shape_expected = size_out + d_out
404
+
405
+ qrng2 = deepcopy(qrng)
406
+ qrvs = gen.qrvs(size=size_in, d=d_in, qmc_engine=qrng)
407
+ if size_in is not None:
408
+ assert qrvs.shape == shape_expected
409
+
410
+ if qrng2 is not None:
411
+ uniform = qrng2.random(np.prod(size_in) or 1)
412
+ qrvs2 = stats.norm.ppf(uniform).reshape(shape_expected)
413
+ assert_allclose(qrvs, qrvs2, atol=1e-12)
414
+
415
+ def test_QRVS_size_tuple(self, method):
416
+ # QMCEngine samples are always of shape (n, d). When `size` is a tuple,
417
+ # we set `n = prod(size)` in the call to qmc_engine.random, transform
418
+ # the sample, and reshape it to the final dimensions. When we reshape,
419
+ # we need to be careful, because the _columns_ of the sample returned
420
+ # by a QMCEngine are "independent"-ish, but the elements within the
421
+ # columns are not. We need to make sure that this doesn't get mixed up
422
+ # by reshaping: qrvs[..., i] should remain "independent"-ish of
423
+ # qrvs[..., i+1], but the elements within qrvs[..., i] should be
424
+ # transformed from the same low-discrepancy sequence.
425
+
426
+ dist = StandardNormal()
427
+ Method = getattr(stats.sampling, method)
428
+ gen = Method(dist)
429
+
430
+ size = (3, 4)
431
+ d = 5
432
+ qrng = stats.qmc.Halton(d, seed=0)
433
+ qrng2 = stats.qmc.Halton(d, seed=0)
434
+
435
+ uniform = qrng2.random(np.prod(size))
436
+
437
+ qrvs = gen.qrvs(size=size, d=d, qmc_engine=qrng)
438
+ qrvs2 = stats.norm.ppf(uniform)
439
+
440
+ for i in range(d):
441
+ sample = qrvs[..., i]
442
+ sample2 = qrvs2[:, i].reshape(size)
443
+ assert_allclose(sample, sample2, atol=1e-12)
444
+
445
+
446
+ class TestTransformedDensityRejection:
447
+ # Simple Custom Distribution
448
+ class dist0:
449
+ def pdf(self, x):
450
+ return 3/4 * (1-x*x)
451
+
452
+ def dpdf(self, x):
453
+ return 3/4 * (-2*x)
454
+
455
+ def cdf(self, x):
456
+ return 3/4 * (x - x**3/3 + 2/3)
457
+
458
+ def support(self):
459
+ return -1, 1
460
+
461
+ # Standard Normal Distribution
462
+ class dist1:
463
+ def pdf(self, x):
464
+ return stats.norm._pdf(x / 0.1)
465
+
466
+ def dpdf(self, x):
467
+ return -x / 0.01 * stats.norm._pdf(x / 0.1)
468
+
469
+ def cdf(self, x):
470
+ return stats.norm._cdf(x / 0.1)
471
+
472
+ # pdf with piecewise linear function as transformed density
473
+ # with T = -1/sqrt with shift. Taken from UNU.RAN test suite
474
+ # (from file t_tdr_ps.c)
475
+ class dist2:
476
+ def __init__(self, shift):
477
+ self.shift = shift
478
+
479
+ def pdf(self, x):
480
+ x -= self.shift
481
+ y = 1. / (abs(x) + 1.)
482
+ return 0.5 * y * y
483
+
484
+ def dpdf(self, x):
485
+ x -= self.shift
486
+ y = 1. / (abs(x) + 1.)
487
+ y = y * y * y
488
+ return y if (x < 0.) else -y
489
+
490
+ def cdf(self, x):
491
+ x -= self.shift
492
+ if x <= 0.:
493
+ return 0.5 / (1. - x)
494
+ else:
495
+ return 1. - 0.5 / (1. + x)
496
+
497
+ dists = [dist0(), dist1(), dist2(0.), dist2(10000.)]
498
+
499
+ # exact mean and variance of the distributions in the list dists
500
+ mv0 = [0., 4./15.]
501
+ mv1 = [0., 0.01]
502
+ mv2 = [0., np.inf]
503
+ mv3 = [10000., np.inf]
504
+ mvs = [mv0, mv1, mv2, mv3]
505
+
506
+ @pytest.mark.parametrize("dist, mv_ex",
507
+ zip(dists, mvs))
508
+ @pytest.mark.thread_unsafe(reason="deadlocks for unknown reasons")
509
+ def test_basic(self, dist, mv_ex):
510
+ with warnings.catch_warnings():
511
+ # filter the warnings thrown by UNU.RAN
512
+ warnings.simplefilter("ignore", RuntimeWarning)
513
+ rng = TransformedDensityRejection(dist, random_state=42)
514
+ check_cont_samples(rng, dist, mv_ex)
515
+
516
+ # PDF 0 everywhere => bad construction points
517
+ bad_pdfs = [(lambda x: 0, UNURANError, r"50 : bad construction points.")]
518
+ bad_pdfs += bad_pdfs_common # type: ignore[arg-type]
519
+
520
+ @pytest.mark.parametrize("pdf, err, msg", bad_pdfs)
521
+ def test_bad_pdf(self, pdf, err, msg):
522
+ class dist:
523
+ pass
524
+ dist.pdf = pdf
525
+ dist.dpdf = lambda x: 1 # an arbitrary dPDF
526
+ with pytest.raises(err, match=msg):
527
+ TransformedDensityRejection(dist)
528
+
529
+ @pytest.mark.parametrize("dpdf, err, msg", bad_dpdf_common)
530
+ def test_bad_dpdf(self, dpdf, err, msg):
531
+ class dist:
532
+ pass
533
+ dist.pdf = lambda x: x
534
+ dist.dpdf = dpdf
535
+ with pytest.raises(err, match=msg):
536
+ TransformedDensityRejection(dist, domain=(1, 10))
537
+
538
+ # test domains with inf + nan in them. need to write a custom test for
539
+ # this because not all methods support infinite tails.
540
+ @pytest.mark.parametrize("domain, err, msg", inf_nan_domains)
541
+ def test_inf_nan_domains(self, domain, err, msg):
542
+ with pytest.raises(err, match=msg):
543
+ TransformedDensityRejection(StandardNormal(), domain=domain)
544
+
545
+ @pytest.mark.parametrize("construction_points", [-1, 0, 0.1])
546
+ def test_bad_construction_points_scalar(self, construction_points):
547
+ with pytest.raises(ValueError, match=r"`construction_points` must be "
548
+ r"a positive integer."):
549
+ TransformedDensityRejection(
550
+ StandardNormal(), construction_points=construction_points
551
+ )
552
+
553
+ def test_bad_construction_points_array(self):
554
+ # empty array
555
+ construction_points = []
556
+ with pytest.raises(ValueError, match=r"`construction_points` must "
557
+ r"either be a "
558
+ r"scalar or a non-empty array."):
559
+ TransformedDensityRejection(
560
+ StandardNormal(), construction_points=construction_points
561
+ )
562
+
563
+ # construction_points not monotonically increasing
564
+ construction_points = [1, 1, 1, 1, 1, 1]
565
+ with pytest.warns(RuntimeWarning, match=r"33 : starting points not "
566
+ r"strictly monotonically "
567
+ r"increasing"):
568
+ TransformedDensityRejection(
569
+ StandardNormal(), construction_points=construction_points
570
+ )
571
+
572
+ # construction_points containing nans
573
+ construction_points = [np.nan, np.nan, np.nan]
574
+ with pytest.raises(UNURANError, match=r"50 : bad construction "
575
+ r"points."):
576
+ TransformedDensityRejection(
577
+ StandardNormal(), construction_points=construction_points
578
+ )
579
+
580
+ # construction_points out of domain
581
+ construction_points = [-10, 10]
582
+ with pytest.warns(RuntimeWarning, match=r"50 : starting point out of "
583
+ r"domain"):
584
+ TransformedDensityRejection(
585
+ StandardNormal(), domain=(-3, 3),
586
+ construction_points=construction_points
587
+ )
588
+
589
+ @pytest.mark.parametrize("c", [-1., np.nan, np.inf, 0.1, 1.])
590
+ def test_bad_c(self, c):
591
+ msg = r"`c` must either be -0.5 or 0."
592
+ with pytest.raises(ValueError, match=msg):
593
+ TransformedDensityRejection(StandardNormal(), c=-1.)
594
+
595
+ u = [np.linspace(0, 1, num=1000), [], [[]], [np.nan],
596
+ [-np.inf, np.nan, np.inf], 0,
597
+ [[np.nan, 0.5, 0.1], [0.2, 0.4, np.inf], [-2, 3, 4]]]
598
+
599
+ @pytest.mark.parametrize("u", u)
600
+ def test_ppf_hat(self, u):
601
+ # Increase the `max_squeeze_hat_ratio` so the ppf_hat is more
602
+ # accurate.
603
+ rng = TransformedDensityRejection(StandardNormal(),
604
+ max_squeeze_hat_ratio=0.9999)
605
+ # Older versions of NumPy throw RuntimeWarnings for comparisons
606
+ # with nan.
607
+ with warnings.catch_warnings():
608
+ msg = "invalid value encountered in "
609
+ warnings.filterwarnings("ignore", msg + "greater", RuntimeWarning)
610
+ warnings.filterwarnings("ignore", msg + "greater_equal", RuntimeWarning)
611
+ warnings.filterwarnings("ignore", msg + "less", RuntimeWarning)
612
+ warnings.filterwarnings("ignore", msg + "less_equal", RuntimeWarning)
613
+ res = rng.ppf_hat(u)
614
+ expected = stats.norm.ppf(u)
615
+ assert_allclose(res, expected, rtol=1e-3, atol=1e-5)
616
+ assert res.shape == expected.shape
617
+
618
+ def test_bad_dist(self):
619
+ # Empty distribution
620
+ class dist:
621
+ ...
622
+
623
+ msg = r"`pdf` required but not found."
624
+ with pytest.raises(ValueError, match=msg):
625
+ TransformedDensityRejection(dist)
626
+
627
+ # dPDF not present in dist
628
+ class dist:
629
+ pdf = lambda x: 1-x*x # noqa: E731
630
+
631
+ msg = r"`dpdf` required but not found."
632
+ with pytest.raises(ValueError, match=msg):
633
+ TransformedDensityRejection(dist)
634
+
635
+
636
+ class TestDiscreteAliasUrn:
637
+ # DAU fails on these probably because of large domains and small
638
+ # computation errors in PMF. Mean/SD match but chi-squared test fails.
639
+ basic_fail_dists = {
640
+ 'nchypergeom_fisher', # numerical errors on tails
641
+ 'nchypergeom_wallenius', # numerical errors on tails
642
+ 'randint' # fails on 32-bit ubuntu
643
+ }
644
+
645
+ @pytest.mark.parametrize("distname, params", distdiscrete)
646
+ def test_basic(self, distname, params):
647
+ if distname in self.basic_fail_dists:
648
+ msg = ("DAU fails on these probably because of large domains "
649
+ "and small computation errors in PMF.")
650
+ pytest.skip(msg)
651
+ if not isinstance(distname, str):
652
+ dist = distname
653
+ else:
654
+ dist = getattr(stats, distname)
655
+ dist = dist(*params)
656
+ domain = dist.support()
657
+ if not np.isfinite(domain[1] - domain[0]):
658
+ # DAU only works with finite domain. So, skip the distributions
659
+ # with infinite tails.
660
+ pytest.skip("DAU only works with a finite domain.")
661
+ k = np.arange(domain[0], domain[1]+1)
662
+ pv = dist.pmf(k)
663
+ mv_ex = dist.stats('mv')
664
+ rng = DiscreteAliasUrn(dist, random_state=42)
665
+ check_discr_samples(rng, pv, mv_ex)
666
+
667
+ # Can't use bad_pmf_common here as we evaluate PMF early on to avoid
668
+ # unhelpful errors from UNU.RAN.
669
+ bad_pmf = [
670
+ # inf returned
671
+ (lambda x: np.inf, ValueError,
672
+ r"must contain only finite / non-nan values"),
673
+ # nan returned
674
+ (lambda x: np.nan, ValueError,
675
+ r"must contain only finite / non-nan values"),
676
+ # all zeros
677
+ (lambda x: 0.0, ValueError,
678
+ r"must contain at least one non-zero value"),
679
+ # Undefined name inside the function
680
+ (lambda x: foo, NameError, # type: ignore[name-defined] # noqa: F821
681
+ r"name 'foo' is not defined"),
682
+ # Returning wrong type.
683
+ (lambda x: [], ValueError,
684
+ r"setting an array element with a sequence."),
685
+ # probabilities < 0
686
+ (lambda x: -x, UNURANError,
687
+ r"50 : probability < 0"),
688
+ # signature of PMF wrong
689
+ (lambda: 1.0, TypeError,
690
+ r"takes 0 positional arguments but 1 was given")
691
+ ]
692
+
693
+ @pytest.mark.parametrize("pmf, err, msg", bad_pmf)
694
+ def test_bad_pmf(self, pmf, err, msg):
695
+ class dist:
696
+ pass
697
+ dist.pmf = pmf
698
+ with pytest.raises(err, match=msg):
699
+ DiscreteAliasUrn(dist, domain=(1, 10))
700
+
701
+ @pytest.mark.parametrize("pv", [[0.18, 0.02, 0.8],
702
+ [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]])
703
+ def test_sampling_with_pv(self, pv):
704
+ pv = np.asarray(pv, dtype=np.float64)
705
+ rng = DiscreteAliasUrn(pv, random_state=123)
706
+ rng.rvs(100_000)
707
+ pv = pv / pv.sum()
708
+ variates = np.arange(0, len(pv))
709
+ # test if the first few moments match
710
+ m_expected = np.average(variates, weights=pv)
711
+ v_expected = np.average((variates - m_expected) ** 2, weights=pv)
712
+ mv_expected = m_expected, v_expected
713
+ check_discr_samples(rng, pv, mv_expected)
714
+
715
+ @pytest.mark.parametrize("pv, msg", bad_pv_common)
716
+ def test_bad_pv(self, pv, msg):
717
+ with pytest.raises(ValueError, match=msg):
718
+ DiscreteAliasUrn(pv)
719
+
720
+ # DAU doesn't support infinite tails. So, it should throw an error when
721
+ # inf is present in the domain.
722
+ inf_domain = [(-np.inf, np.inf), (np.inf, np.inf), (-np.inf, -np.inf),
723
+ (0, np.inf), (-np.inf, 0)]
724
+
725
+ @pytest.mark.parametrize("domain", inf_domain)
726
+ def test_inf_domain(self, domain):
727
+ with pytest.raises(ValueError, match=r"must be finite"):
728
+ DiscreteAliasUrn(stats.binom(10, 0.2), domain=domain)
729
+
730
+ def test_bad_urn_factor(self):
731
+ with pytest.warns(RuntimeWarning, match=r"relative urn size < 1."):
732
+ DiscreteAliasUrn([0.5, 0.5], urn_factor=-1)
733
+
734
+ def test_bad_args(self):
735
+ msg = (r"`domain` must be provided when the "
736
+ r"probability vector is not available.")
737
+
738
+ class dist:
739
+ def pmf(self, x):
740
+ return x
741
+
742
+ with pytest.raises(ValueError, match=msg):
743
+ DiscreteAliasUrn(dist)
744
+
745
+ def test_gh19359(self):
746
+ pv = special.softmax(np.ones((1533,)))
747
+ rng = DiscreteAliasUrn(pv, random_state=42)
748
+ # check the correctness
749
+ check_discr_samples(rng, pv, (1532 / 2, (1532**2 - 1) / 12),
750
+ rtol=5e-3)
751
+
752
+
753
+ class TestNumericalInversePolynomial:
754
+ # Simple Custom Distribution
755
+ class dist0:
756
+ def pdf(self, x):
757
+ return 3/4 * (1-x*x)
758
+
759
+ def cdf(self, x):
760
+ return 3/4 * (x - x**3/3 + 2/3)
761
+
762
+ def support(self):
763
+ return -1, 1
764
+
765
+ # Standard Normal Distribution
766
+ class dist1:
767
+ def pdf(self, x):
768
+ return stats.norm._pdf(x / 0.1)
769
+
770
+ def cdf(self, x):
771
+ return stats.norm._cdf(x / 0.1)
772
+
773
+ # Sin 2 distribution
774
+ # / 0.05 + 0.45*(1 +sin(2 Pi x)) if |x| <= 1
775
+ # f(x) = <
776
+ # \ 0 otherwise
777
+ # Taken from UNU.RAN test suite (from file t_pinv.c)
778
+ class dist2:
779
+ def pdf(self, x):
780
+ return 0.05 + 0.45 * (1 + np.sin(2*np.pi*x))
781
+
782
+ def cdf(self, x):
783
+ return (0.05*(x + 1) +
784
+ 0.9*(1. + 2.*np.pi*(1 + x) - np.cos(2.*np.pi*x)) /
785
+ (4.*np.pi))
786
+
787
+ def support(self):
788
+ return -1, 1
789
+
790
+ # Sin 10 distribution
791
+ # / 0.05 + 0.45*(1 +sin(2 Pi x)) if |x| <= 5
792
+ # f(x) = <
793
+ # \ 0 otherwise
794
+ # Taken from UNU.RAN test suite (from file t_pinv.c)
795
+ class dist3:
796
+ def pdf(self, x):
797
+ return 0.2 * (0.05 + 0.45 * (1 + np.sin(2*np.pi*x)))
798
+
799
+ def cdf(self, x):
800
+ return x/10. + 0.5 + 0.09/(2*np.pi) * (np.cos(10*np.pi) -
801
+ np.cos(2*np.pi*x))
802
+
803
+ def support(self):
804
+ return -5, 5
805
+
806
+ dists = [dist0(), dist1(), dist2(), dist3()]
807
+
808
+ # exact mean and variance of the distributions in the list dists
809
+ mv0 = [0., 4./15.]
810
+ mv1 = [0., 0.01]
811
+ mv2 = [-0.45/np.pi, 2/3*0.5 - 0.45**2/np.pi**2]
812
+ mv3 = [-0.45/np.pi, 0.2 * 250/3 * 0.5 - 0.45**2/np.pi**2]
813
+ mvs = [mv0, mv1, mv2, mv3]
814
+
815
+ @pytest.mark.thread_unsafe(reason="deadlocks for unknown reasons")
816
+ @pytest.mark.parametrize("dist, mv_ex",
817
+ zip(dists, mvs))
818
+ def test_basic(self, dist, mv_ex):
819
+ rng = NumericalInversePolynomial(dist, random_state=42)
820
+ check_cont_samples(rng, dist, mv_ex)
821
+
822
+ @pytest.mark.xslow
823
+ @pytest.mark.parametrize("distname, params", distcont)
824
+ def test_basic_all_scipy_dists(self, distname, params):
825
+
826
+ very_slow_dists = ['anglit', 'gausshyper', 'kappa4',
827
+ 'ksone', 'kstwo', 'levy_l',
828
+ 'levy_stable', 'studentized_range',
829
+ 'trapezoid', 'triang', 'vonmises']
830
+ # for these distributions, some assertions fail due to minor
831
+ # numerical differences. They can be avoided either by changing
832
+ # the seed or by increasing the u_resolution.
833
+ fail_dists = ['chi2', 'fatiguelife', 'gibrat',
834
+ 'halfgennorm', 'lognorm', 'ncf',
835
+ 'ncx2', 'pareto', 't']
836
+ # for these distributions, skip the check for agreement between sample
837
+ # moments and true moments. We cannot expect them to pass due to the
838
+ # high variance of sample moments.
839
+ skip_sample_moment_check = ['rel_breitwigner']
840
+
841
+ if distname in very_slow_dists:
842
+ pytest.skip(f"PINV too slow for {distname}")
843
+ if distname in fail_dists:
844
+ pytest.skip(f"PINV fails for {distname}")
845
+ dist = (getattr(stats, distname)
846
+ if isinstance(distname, str)
847
+ else distname)
848
+ dist = dist(*params)
849
+ with warnings.catch_warnings():
850
+ warnings.simplefilter("ignore", RuntimeWarning)
851
+ rng = NumericalInversePolynomial(dist, random_state=42)
852
+ if distname in skip_sample_moment_check:
853
+ return
854
+ check_cont_samples(rng, dist, [dist.mean(), dist.var()])
855
+
856
+ @pytest.mark.parametrize("pdf, err, msg", bad_pdfs_common)
857
+ def test_bad_pdf(self, pdf, err, msg):
858
+ class dist:
859
+ pass
860
+ dist.pdf = pdf
861
+ with pytest.raises(err, match=msg):
862
+ NumericalInversePolynomial(dist, domain=[0, 5])
863
+
864
+ @pytest.mark.parametrize("logpdf, err, msg", bad_logpdfs_common)
865
+ def test_bad_logpdf(self, logpdf, err, msg):
866
+ class dist:
867
+ pass
868
+ dist.logpdf = logpdf
869
+ with pytest.raises(err, match=msg):
870
+ NumericalInversePolynomial(dist, domain=[0, 5])
871
+
872
+ # test domains with inf + nan in them. need to write a custom test for
873
+ # this because not all methods support infinite tails.
874
+ @pytest.mark.parametrize("domain, err, msg", inf_nan_domains)
875
+ def test_inf_nan_domains(self, domain, err, msg):
876
+ with pytest.raises(err, match=msg):
877
+ NumericalInversePolynomial(StandardNormal(), domain=domain)
878
+
879
+ u = [
880
+ # test if quantile 0 and 1 return -inf and inf respectively and check
881
+ # the correctness of the PPF for equidistant points between 0 and 1.
882
+ np.linspace(0, 1, num=10000),
883
+ # test the PPF method for empty arrays
884
+ [], [[]],
885
+ # test if nans and infs return nan result.
886
+ [np.nan], [-np.inf, np.nan, np.inf],
887
+ # test if a scalar is returned for a scalar input.
888
+ 0,
889
+ # test for arrays with nans, values greater than 1 and less than 0,
890
+ # and some valid values.
891
+ [[np.nan, 0.5, 0.1], [0.2, 0.4, np.inf], [-2, 3, 4]]
892
+ ]
893
+
894
+ @pytest.mark.parametrize("u", u)
895
+ def test_ppf(self, u):
896
+ dist = StandardNormal()
897
+ rng = NumericalInversePolynomial(dist, u_resolution=1e-14)
898
+ # Older versions of NumPy throw RuntimeWarnings for comparisons
899
+ # with nan.
900
+ with warnings.catch_warnings():
901
+ msg = "invalid value encountered in "
902
+ warnings.filterwarnings("ignore", msg + "greater", RuntimeWarning)
903
+ warnings.filterwarnings("ignore", msg + "greater_equal", RuntimeWarning)
904
+ warnings.filterwarnings("ignore", msg + "less", RuntimeWarning)
905
+ warnings.filterwarnings("ignore", msg + "less_equal", RuntimeWarning)
906
+ res = rng.ppf(u)
907
+ expected = stats.norm.ppf(u)
908
+ assert_allclose(res, expected, rtol=1e-11, atol=1e-11)
909
+ assert res.shape == expected.shape
910
+
911
+ x = [np.linspace(-10, 10, num=10000), [], [[]], [np.nan],
912
+ [-np.inf, np.nan, np.inf], 0,
913
+ [[np.nan, 0.5, 0.1], [0.2, 0.4, np.inf], [-np.inf, 3, 4]]]
914
+
915
+ @pytest.mark.parametrize("x", x)
916
+ def test_cdf(self, x):
917
+ dist = StandardNormal()
918
+ rng = NumericalInversePolynomial(dist, u_resolution=1e-14)
919
+ # Older versions of NumPy throw RuntimeWarnings for comparisons
920
+ # with nan.
921
+ with warnings.catch_warnings():
922
+ msg = "invalid value encountered in "
923
+ warnings.filterwarnings("ignore", msg + "greater", RuntimeWarning)
924
+ warnings.filterwarnings("ignore", msg + "greater_equal", RuntimeWarning)
925
+ warnings.filterwarnings("ignore", msg + "less", RuntimeWarning)
926
+ warnings.filterwarnings("ignore", msg + "less_equal", RuntimeWarning)
927
+ res = rng.cdf(x)
928
+ expected = stats.norm.cdf(x)
929
+ assert_allclose(res, expected, rtol=1e-11, atol=1e-11)
930
+ assert res.shape == expected.shape
931
+
932
+ @pytest.mark.slow
933
+ def test_u_error(self):
934
+ dist = StandardNormal()
935
+ rng = NumericalInversePolynomial(dist, u_resolution=1e-10)
936
+ max_error, mae = rng.u_error()
937
+ assert max_error < 1e-10
938
+ assert mae <= max_error
939
+ rng = NumericalInversePolynomial(dist, u_resolution=1e-14)
940
+ max_error, mae = rng.u_error()
941
+ assert max_error < 1e-14
942
+ assert mae <= max_error
943
+
944
+ bad_orders = [1, 4.5, 20, np.inf, np.nan]
945
+ bad_u_resolution = [1e-20, 1e-1, np.inf, np.nan]
946
+
947
+ @pytest.mark.parametrize("order", bad_orders)
948
+ def test_bad_orders(self, order):
949
+ dist = StandardNormal()
950
+
951
+ msg = r"`order` must be an integer in the range \[3, 17\]."
952
+ with pytest.raises(ValueError, match=msg):
953
+ NumericalInversePolynomial(dist, order=order)
954
+
955
+ @pytest.mark.parametrize("u_resolution", bad_u_resolution)
956
+ def test_bad_u_resolution(self, u_resolution):
957
+ msg = r"`u_resolution` must be between 1e-15 and 1e-5."
958
+ with pytest.raises(ValueError, match=msg):
959
+ NumericalInversePolynomial(StandardNormal(),
960
+ u_resolution=u_resolution)
961
+
962
+ def test_bad_args(self):
963
+
964
+ class BadDist:
965
+ def cdf(self, x):
966
+ return stats.norm._cdf(x)
967
+
968
+ dist = BadDist()
969
+ msg = r"Either of the methods `pdf` or `logpdf` must be specified"
970
+ with pytest.raises(ValueError, match=msg):
971
+ rng = NumericalInversePolynomial(dist)
972
+
973
+ dist = StandardNormal()
974
+ rng = NumericalInversePolynomial(dist)
975
+ msg = r"`sample_size` must be greater than or equal to 1000."
976
+ with pytest.raises(ValueError, match=msg):
977
+ rng.u_error(10)
978
+
979
+ class Distribution:
980
+ def pdf(self, x):
981
+ return np.exp(-0.5 * x*x)
982
+
983
+ dist = Distribution()
984
+ rng = NumericalInversePolynomial(dist)
985
+ msg = r"Exact CDF required but not found."
986
+ with pytest.raises(ValueError, match=msg):
987
+ rng.u_error()
988
+
989
+ def test_logpdf_pdf_consistency(self):
990
+ # 1. check that PINV works with pdf and logpdf only
991
+ # 2. check that generated ppf is the same (up to a small tolerance)
992
+
993
+ class MyDist:
994
+ pass
995
+
996
+ # create generator from dist with only pdf
997
+ dist_pdf = MyDist()
998
+ dist_pdf.pdf = lambda x: math.exp(-x*x/2)
999
+ rng1 = NumericalInversePolynomial(dist_pdf)
1000
+
1001
+ # create dist with only logpdf
1002
+ dist_logpdf = MyDist()
1003
+ dist_logpdf.logpdf = lambda x: -x*x/2
1004
+ rng2 = NumericalInversePolynomial(dist_logpdf)
1005
+
1006
+ q = np.linspace(1e-5, 1-1e-5, num=100)
1007
+ assert_allclose(rng1.ppf(q), rng2.ppf(q))
1008
+
1009
+
1010
+ class TestNumericalInverseHermite:
1011
+ # / (1 +sin(2 Pi x))/2 if |x| <= 1
1012
+ # f(x) = <
1013
+ # \ 0 otherwise
1014
+ # Taken from UNU.RAN test suite (from file t_hinv.c)
1015
+ class dist0:
1016
+ def pdf(self, x):
1017
+ return 0.5*(1. + np.sin(2.*np.pi*x))
1018
+
1019
+ def dpdf(self, x):
1020
+ return np.pi*np.cos(2.*np.pi*x)
1021
+
1022
+ def cdf(self, x):
1023
+ return (1. + 2.*np.pi*(1 + x) - np.cos(2.*np.pi*x)) / (4.*np.pi)
1024
+
1025
+ def support(self):
1026
+ return -1, 1
1027
+
1028
+ # / Max(sin(2 Pi x)),0)Pi/2 if -1 < x <0.5
1029
+ # f(x) = <
1030
+ # \ 0 otherwise
1031
+ # Taken from UNU.RAN test suite (from file t_hinv.c)
1032
+ class dist1:
1033
+ def pdf(self, x):
1034
+ if (x <= -0.5):
1035
+ return np.sin((2. * np.pi) * x) * 0.5 * np.pi
1036
+ if (x < 0.):
1037
+ return 0.
1038
+ if (x <= 0.5):
1039
+ return np.sin((2. * np.pi) * x) * 0.5 * np.pi
1040
+
1041
+ def dpdf(self, x):
1042
+ if (x <= -0.5):
1043
+ return np.cos((2. * np.pi) * x) * np.pi * np.pi
1044
+ if (x < 0.):
1045
+ return 0.
1046
+ if (x <= 0.5):
1047
+ return np.cos((2. * np.pi) * x) * np.pi * np.pi
1048
+
1049
+ def cdf(self, x):
1050
+ if (x <= -0.5):
1051
+ return 0.25 * (1 - np.cos((2. * np.pi) * x))
1052
+ if (x < 0.):
1053
+ return 0.5
1054
+ if (x <= 0.5):
1055
+ return 0.75 - 0.25 * np.cos((2. * np.pi) * x)
1056
+
1057
+ def support(self):
1058
+ return -1, 0.5
1059
+
1060
+ dists = [dist0(), dist1()]
1061
+
1062
+ # exact mean and variance of the distributions in the list dists
1063
+ mv0 = [-1/(2*np.pi), 1/3 - 1/(4*np.pi*np.pi)]
1064
+ mv1 = [-1/4, 3/8-1/(2*np.pi*np.pi) - 1/16]
1065
+ mvs = [mv0, mv1]
1066
+
1067
+ @pytest.mark.parametrize("dist, mv_ex",
1068
+ zip(dists, mvs))
1069
+ @pytest.mark.parametrize("order", [3, 5])
1070
+ @pytest.mark.thread_unsafe
1071
+ def test_basic(self, dist, mv_ex, order):
1072
+ rng = NumericalInverseHermite(dist, order=order, random_state=42)
1073
+ check_cont_samples(rng, dist, mv_ex)
1074
+
1075
+ # test domains with inf + nan in them. need to write a custom test for
1076
+ # this because not all methods support infinite tails.
1077
+ @pytest.mark.parametrize("domain, err, msg", inf_nan_domains)
1078
+ def test_inf_nan_domains(self, domain, err, msg):
1079
+ with pytest.raises(err, match=msg):
1080
+ NumericalInverseHermite(StandardNormal(), domain=domain)
1081
+
1082
+ def basic_test_all_scipy_dists(self, distname, shapes):
1083
+ slow_dists = {'ksone', 'kstwo', 'levy_stable', 'skewnorm'}
1084
+ fail_dists = {'beta', 'gausshyper', 'geninvgauss', 'ncf', 'nct',
1085
+ 'norminvgauss', 'genhyperbolic', 'studentized_range',
1086
+ 'vonmises', 'kappa4', 'invgauss', 'wald'}
1087
+
1088
+ if distname in slow_dists:
1089
+ pytest.skip("Distribution is too slow")
1090
+ if distname in fail_dists:
1091
+ # specific reasons documented in gh-13319
1092
+ # https://github.com/scipy/scipy/pull/13319#discussion_r626188955
1093
+ pytest.xfail("Fails - usually due to inaccurate CDF/PDF")
1094
+
1095
+ rng = np.random.default_rng(0)
1096
+
1097
+ dist = getattr(stats, distname)(*shapes)
1098
+ fni = NumericalInverseHermite(dist)
1099
+
1100
+ x = rng.random(10)
1101
+ p_tol = np.max(np.abs(dist.ppf(x)-fni.ppf(x))/np.abs(dist.ppf(x)))
1102
+ u_tol = np.max(np.abs(dist.cdf(fni.ppf(x)) - x))
1103
+
1104
+ assert p_tol < 1e-8
1105
+ assert u_tol < 1e-12
1106
+
1107
+ @pytest.mark.filterwarnings('ignore::RuntimeWarning')
1108
+ @pytest.mark.xslow
1109
+ @pytest.mark.parametrize(("distname", "shapes"), distcont)
1110
+ def test_basic_all_scipy_dists(self, distname, shapes):
1111
+ # if distname == "truncnorm":
1112
+ # pytest.skip("Tested separately")
1113
+ self.basic_test_all_scipy_dists(distname, shapes)
1114
+
1115
+ @pytest.mark.fail_slow(5)
1116
+ @pytest.mark.filterwarnings('ignore::RuntimeWarning')
1117
+ @pytest.mark.parallel_threads_limit(4) # Very slow
1118
+ def test_basic_truncnorm_gh17155(self):
1119
+ self.basic_test_all_scipy_dists("truncnorm", (0.1, 2))
1120
+
1121
+ def test_input_validation(self):
1122
+ match = r"`order` must be either 1, 3, or 5."
1123
+ with pytest.raises(ValueError, match=match):
1124
+ NumericalInverseHermite(StandardNormal(), order=2)
1125
+
1126
+ match = "`cdf` required but not found"
1127
+ with pytest.raises(ValueError, match=match):
1128
+ NumericalInverseHermite("norm")
1129
+
1130
+ match = "could not convert string to float"
1131
+ with pytest.raises(ValueError, match=match):
1132
+ NumericalInverseHermite(StandardNormal(),
1133
+ u_resolution='ekki')
1134
+
1135
+ rngs = [None, 0, np.random.RandomState(0)]
1136
+ rngs.append(np.random.default_rng(0)) # type: ignore
1137
+ sizes = [(None, tuple()), (8, (8,)), ((4, 5, 6), (4, 5, 6))]
1138
+
1139
+ @pytest.mark.parametrize('rng', rngs)
1140
+ @pytest.mark.parametrize('size_in, size_out', sizes)
1141
+ @pytest.mark.thread_unsafe
1142
+ def test_RVS(self, rng, size_in, size_out):
1143
+ dist = StandardNormal()
1144
+ fni = NumericalInverseHermite(dist)
1145
+
1146
+ rng2 = deepcopy(rng)
1147
+ rvs = fni.rvs(size=size_in, random_state=rng)
1148
+ if size_in is not None:
1149
+ assert rvs.shape == size_out
1150
+
1151
+ if rng2 is not None:
1152
+ rng2 = check_random_state(rng2)
1153
+ uniform = rng2.uniform(size=size_in)
1154
+ rvs2 = stats.norm.ppf(uniform)
1155
+ assert_allclose(rvs, rvs2)
1156
+
1157
+ def test_inaccurate_CDF(self):
1158
+ # CDF function with inaccurate tail cannot be inverted; see gh-13319
1159
+ # https://github.com/scipy/scipy/pull/13319#discussion_r626188955
1160
+ shapes = (2.3098496451481823, 0.6268795430096368)
1161
+ match = ("98 : one or more intervals very short; possibly due to "
1162
+ "numerical problems with a pole or very flat tail")
1163
+
1164
+ # fails with default tol
1165
+ with pytest.warns(RuntimeWarning, match=match):
1166
+ NumericalInverseHermite(stats.beta(*shapes))
1167
+
1168
+ # no error with coarser tol
1169
+ NumericalInverseHermite(stats.beta(*shapes), u_resolution=1e-8)
1170
+
1171
+ def test_custom_distribution(self):
1172
+ dist1 = StandardNormal()
1173
+ fni1 = NumericalInverseHermite(dist1)
1174
+
1175
+ dist2 = stats.norm()
1176
+ fni2 = NumericalInverseHermite(dist2)
1177
+
1178
+ assert_allclose(fni1.rvs(random_state=0), fni2.rvs(random_state=0))
1179
+
1180
+ u = [
1181
+ # check the correctness of the PPF for equidistant points between
1182
+ # 0.02 and 0.98.
1183
+ np.linspace(0., 1., num=10000),
1184
+ # test the PPF method for empty arrays
1185
+ [], [[]],
1186
+ # test if nans and infs return nan result.
1187
+ [np.nan], [-np.inf, np.nan, np.inf],
1188
+ # test if a scalar is returned for a scalar input.
1189
+ 0,
1190
+ # test for arrays with nans, values greater than 1 and less than 0,
1191
+ # and some valid values.
1192
+ [[np.nan, 0.5, 0.1], [0.2, 0.4, np.inf], [-2, 3, 4]]
1193
+ ]
1194
+
1195
+ @pytest.mark.parametrize("u", u)
1196
+ def test_ppf(self, u):
1197
+ dist = StandardNormal()
1198
+ rng = NumericalInverseHermite(dist, u_resolution=1e-12)
1199
+ # Older versions of NumPy throw RuntimeWarnings for comparisons
1200
+ # with nan.
1201
+ with warnings.catch_warnings():
1202
+ msg = "invalid value encountered in "
1203
+ warnings.filterwarnings("ignore", msg + "greater", RuntimeWarning)
1204
+ warnings.filterwarnings("ignore", msg + "greater_equal", RuntimeWarning)
1205
+ warnings.filterwarnings("ignore", msg + "less", RuntimeWarning)
1206
+ warnings.filterwarnings("ignore", msg + "less_equal", RuntimeWarning)
1207
+ res = rng.ppf(u)
1208
+ expected = stats.norm.ppf(u)
1209
+ assert_allclose(res, expected, rtol=1e-9, atol=3e-10)
1210
+ assert res.shape == expected.shape
1211
+
1212
+ @pytest.mark.slow
1213
+ def test_u_error(self):
1214
+ dist = StandardNormal()
1215
+ rng = NumericalInverseHermite(dist, u_resolution=1e-10)
1216
+ max_error, mae = rng.u_error()
1217
+ assert max_error < 1e-10
1218
+ assert mae <= max_error
1219
+ with warnings.catch_warnings():
1220
+ # ignore warning about u-resolution being too small.
1221
+ warnings.simplefilter("ignore", RuntimeWarning)
1222
+ rng = NumericalInverseHermite(dist, u_resolution=1e-14)
1223
+ max_error, mae = rng.u_error()
1224
+ assert max_error < 1e-14
1225
+ assert mae <= max_error
1226
+
1227
+
1228
+ class TestDiscreteGuideTable:
1229
+ basic_fail_dists = {
1230
+ 'nchypergeom_fisher', # numerical errors on tails
1231
+ 'nchypergeom_wallenius', # numerical errors on tails
1232
+ 'randint' # fails on 32-bit ubuntu
1233
+ }
1234
+
1235
+ def test_guide_factor_gt3_raises_warning(self):
1236
+ pv = [0.1, 0.3, 0.6]
1237
+ urng = np.random.default_rng()
1238
+ with pytest.warns(RuntimeWarning):
1239
+ DiscreteGuideTable(pv, random_state=urng, guide_factor=7)
1240
+
1241
+ def test_guide_factor_zero_raises_warning(self):
1242
+ pv = [0.1, 0.3, 0.6]
1243
+ urng = np.random.default_rng()
1244
+ with pytest.warns(RuntimeWarning):
1245
+ DiscreteGuideTable(pv, random_state=urng, guide_factor=0)
1246
+
1247
+ def test_negative_guide_factor_raises_warning(self):
1248
+ # This occurs from the UNU.RAN wrapper automatically.
1249
+ # however it already gives a useful warning
1250
+ # Here we just test that a warning is raised.
1251
+ pv = [0.1, 0.3, 0.6]
1252
+ urng = np.random.default_rng()
1253
+ with pytest.warns(RuntimeWarning):
1254
+ DiscreteGuideTable(pv, random_state=urng, guide_factor=-1)
1255
+
1256
+ @pytest.mark.parametrize("distname, params", distdiscrete)
1257
+ def test_basic(self, distname, params):
1258
+ if distname in self.basic_fail_dists:
1259
+ msg = ("DGT fails on these probably because of large domains "
1260
+ "and small computation errors in PMF.")
1261
+ pytest.skip(msg)
1262
+
1263
+ if not isinstance(distname, str):
1264
+ dist = distname
1265
+ else:
1266
+ dist = getattr(stats, distname)
1267
+
1268
+ dist = dist(*params)
1269
+ domain = dist.support()
1270
+
1271
+ if not np.isfinite(domain[1] - domain[0]):
1272
+ # DGT only works with finite domain. So, skip the distributions
1273
+ # with infinite tails.
1274
+ pytest.skip("DGT only works with a finite domain.")
1275
+
1276
+ k = np.arange(domain[0], domain[1]+1)
1277
+ pv = dist.pmf(k)
1278
+ mv_ex = dist.stats('mv')
1279
+ rng = DiscreteGuideTable(dist, random_state=42)
1280
+ check_discr_samples(rng, pv, mv_ex)
1281
+
1282
+ u = [
1283
+ # the correctness of the PPF for equidistant points between 0 and 1.
1284
+ np.linspace(0, 1, num=10000),
1285
+ # test the PPF method for empty arrays
1286
+ [], [[]],
1287
+ # test if nans and infs return nan result.
1288
+ [np.nan], [-np.inf, np.nan, np.inf],
1289
+ # test if a scalar is returned for a scalar input.
1290
+ 0,
1291
+ # test for arrays with nans, values greater than 1 and less than 0,
1292
+ # and some valid values.
1293
+ [[np.nan, 0.5, 0.1], [0.2, 0.4, np.inf], [-2, 3, 4]]
1294
+ ]
1295
+
1296
+ @pytest.mark.parametrize('u', u)
1297
+ def test_ppf(self, u):
1298
+ n, p = 4, 0.1
1299
+ dist = stats.binom(n, p)
1300
+ rng = DiscreteGuideTable(dist, random_state=42)
1301
+
1302
+ # Older versions of NumPy throw RuntimeWarnings for comparisons
1303
+ # with nan.
1304
+ with warnings.catch_warnings():
1305
+ msg = "invalid value encountered in "
1306
+ warnings.filterwarnings("ignore", msg + "greater", RuntimeWarning)
1307
+ warnings.filterwarnings("ignore", msg + "greater_equal", RuntimeWarning)
1308
+ warnings.filterwarnings("ignore", msg + "less", RuntimeWarning)
1309
+ warnings.filterwarnings("ignore", msg + "less_equal", RuntimeWarning)
1310
+ res = rng.ppf(u)
1311
+ expected = stats.binom.ppf(u, n, p)
1312
+ assert_equal(res.shape, expected.shape)
1313
+ assert_equal(res, expected)
1314
+
1315
+ @pytest.mark.parametrize("pv, msg", bad_pv_common)
1316
+ def test_bad_pv(self, pv, msg):
1317
+ with pytest.raises(ValueError, match=msg):
1318
+ DiscreteGuideTable(pv)
1319
+
1320
+ # DGT doesn't support infinite tails. So, it should throw an error when
1321
+ # inf is present in the domain.
1322
+ inf_domain = [(-np.inf, np.inf), (np.inf, np.inf), (-np.inf, -np.inf),
1323
+ (0, np.inf), (-np.inf, 0)]
1324
+
1325
+ @pytest.mark.parametrize("domain", inf_domain)
1326
+ def test_inf_domain(self, domain):
1327
+ with pytest.raises(ValueError, match=r"must be finite"):
1328
+ DiscreteGuideTable(stats.binom(10, 0.2), domain=domain)
1329
+
1330
+
1331
+ class TestSimpleRatioUniforms:
1332
+ # pdf with piecewise linear function as transformed density
1333
+ # with T = -1/sqrt with shift. Taken from UNU.RAN test suite
1334
+ # (from file t_srou.c)
1335
+ class dist:
1336
+ def __init__(self, shift):
1337
+ self.shift = shift
1338
+ self.mode = shift
1339
+
1340
+ def pdf(self, x):
1341
+ x -= self.shift
1342
+ y = 1. / (abs(x) + 1.)
1343
+ return 0.5 * y * y
1344
+
1345
+ def cdf(self, x):
1346
+ x -= self.shift
1347
+ if x <= 0.:
1348
+ return 0.5 / (1. - x)
1349
+ else:
1350
+ return 1. - 0.5 / (1. + x)
1351
+
1352
+ dists = [dist(0.), dist(10000.)]
1353
+
1354
+ # exact mean and variance of the distributions in the list dists
1355
+ mv1 = [0., np.inf]
1356
+ mv2 = [10000., np.inf]
1357
+ mvs = [mv1, mv2]
1358
+
1359
+ @pytest.mark.parametrize("dist, mv_ex",
1360
+ zip(dists, mvs))
1361
+ @pytest.mark.thread_unsafe
1362
+ def test_basic(self, dist, mv_ex):
1363
+ rng = SimpleRatioUniforms(dist, mode=dist.mode, random_state=42)
1364
+ check_cont_samples(rng, dist, mv_ex)
1365
+ rng = SimpleRatioUniforms(dist, mode=dist.mode,
1366
+ cdf_at_mode=dist.cdf(dist.mode),
1367
+ random_state=42)
1368
+ check_cont_samples(rng, dist, mv_ex)
1369
+
1370
+ # test domains with inf + nan in them. need to write a custom test for
1371
+ # this because not all methods support infinite tails.
1372
+ @pytest.mark.parametrize("domain, err, msg", inf_nan_domains)
1373
+ def test_inf_nan_domains(self, domain, err, msg):
1374
+ with pytest.raises(err, match=msg):
1375
+ SimpleRatioUniforms(StandardNormal(), domain=domain)
1376
+
1377
+ def test_bad_args(self):
1378
+ # pdf_area < 0
1379
+ with pytest.raises(ValueError, match=r"`pdf_area` must be > 0"):
1380
+ SimpleRatioUniforms(StandardNormal(), mode=0, pdf_area=-1)
1381
+
1382
+
1383
+ class TestRatioUniforms:
1384
+ def test_rv_generation(self):
1385
+ # use KS test to check distribution of rvs
1386
+ # normal distribution
1387
+ f = stats.norm.pdf
1388
+ v = np.sqrt(f(np.sqrt(2))) * np.sqrt(2)
1389
+ u = np.sqrt(f(0))
1390
+ gen = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=12345)
1391
+ assert_equal(stats.kstest(gen.rvs(2500), 'norm')[1] > 0.25, True)
1392
+
1393
+ # exponential distribution
1394
+ gen = RatioUniforms(lambda x: np.exp(-x), umax=1,
1395
+ vmin=0, vmax=2*np.exp(-1), random_state=12345)
1396
+ assert_equal(stats.kstest(gen.rvs(1000), 'expon')[1] > 0.25, True)
1397
+
1398
+ def test_shape(self):
1399
+ # test shape of return value depending on size parameter
1400
+ f = stats.norm.pdf
1401
+ v = np.sqrt(f(np.sqrt(2))) * np.sqrt(2)
1402
+ u = np.sqrt(f(0))
1403
+
1404
+ gen1 = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=1234)
1405
+ gen2 = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=1234)
1406
+ gen3 = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=1234)
1407
+ r1, r2, r3 = gen1.rvs(3), gen2.rvs((3,)), gen3.rvs((3, 1))
1408
+ assert_equal(r1, r2)
1409
+ assert_equal(r2, r3.flatten())
1410
+ assert_equal(r1.shape, (3,))
1411
+ assert_equal(r3.shape, (3, 1))
1412
+
1413
+ gen4 = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=12)
1414
+ gen5 = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=12)
1415
+ r4, r5 = gen4.rvs(size=(3, 3, 3)), gen5.rvs(size=27)
1416
+ assert_equal(r4.flatten(), r5)
1417
+ assert_equal(r4.shape, (3, 3, 3))
1418
+
1419
+ gen6 = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=1234)
1420
+ gen7 = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=1234)
1421
+ gen8 = RatioUniforms(f, umax=u, vmin=-v, vmax=v, random_state=1234)
1422
+ r6, r7, r8 = gen6.rvs(), gen7.rvs(1), gen8.rvs((1,))
1423
+ assert_equal(r6, r7)
1424
+ assert_equal(r7, r8)
1425
+
1426
+ def test_random_state(self):
1427
+ f = stats.norm.pdf
1428
+ v = np.sqrt(f(np.sqrt(2))) * np.sqrt(2)
1429
+ umax = np.sqrt(f(0))
1430
+ gen1 = RatioUniforms(f, umax=umax, vmin=-v, vmax=v, random_state=1234)
1431
+ r1 = gen1.rvs(10)
1432
+ rng = np.random.RandomState(1234)
1433
+ gen2 = RatioUniforms(f, umax=umax, vmin=-v, vmax=v, random_state=rng)
1434
+ r2 = gen2.rvs(10)
1435
+ assert_equal(r1, r2)
1436
+
1437
+ def test_exceptions(self):
1438
+ f = stats.norm.pdf
1439
+ # need vmin < vmax
1440
+ with assert_raises(ValueError, match="vmin must be smaller than vmax"):
1441
+ RatioUniforms(pdf=f, umax=1, vmin=3, vmax=1)
1442
+ with assert_raises(ValueError, match="vmin must be smaller than vmax"):
1443
+ RatioUniforms(pdf=f, umax=1, vmin=1, vmax=1)
1444
+ # need umax > 0
1445
+ with assert_raises(ValueError, match="umax must be positive"):
1446
+ RatioUniforms(pdf=f, umax=-1, vmin=1, vmax=3)
1447
+ with assert_raises(ValueError, match="umax must be positive"):
1448
+ RatioUniforms(pdf=f, umax=0, vmin=1, vmax=3)
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_sensitivity_analysis.py ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from numpy.testing import assert_allclose, assert_array_less
3
+ import pytest
4
+
5
+ from scipy import stats
6
+ from scipy.stats import sobol_indices
7
+ from scipy.stats._resampling import BootstrapResult
8
+ from scipy.stats._sensitivity_analysis import (
9
+ BootstrapSobolResult, f_ishigami, sample_AB, sample_A_B
10
+ )
11
+
12
+
13
+ @pytest.fixture(scope='session')
14
+ def ishigami_ref_indices():
15
+ """Reference values for Ishigami from Saltelli2007.
16
+
17
+ Chapter 4, exercise 5 pages 179-182.
18
+ """
19
+ a = 7.
20
+ b = 0.1
21
+
22
+ var = 0.5 + a**2/8 + b*np.pi**4/5 + b**2*np.pi**8/18
23
+ v1 = 0.5 + b*np.pi**4/5 + b**2*np.pi**8/50
24
+ v2 = a**2/8
25
+ v3 = 0
26
+ v12 = 0
27
+ # v13: mistake in the book, see other derivations e.g. in 10.1002/nme.4856
28
+ v13 = b**2*np.pi**8*8/225
29
+ v23 = 0
30
+
31
+ s_first = np.array([v1, v2, v3])/var
32
+ s_second = np.array([
33
+ [0., 0., v13],
34
+ [v12, 0., v23],
35
+ [v13, v23, 0.]
36
+ ])/var
37
+ s_total = s_first + s_second.sum(axis=1)
38
+
39
+ return s_first, s_total
40
+
41
+
42
+ def f_ishigami_vec(x):
43
+ """Output of shape (2, n)."""
44
+ res = f_ishigami(x)
45
+ return res, res
46
+
47
+
48
+ class TestSobolIndices:
49
+
50
+ dists = [
51
+ stats.uniform(loc=-np.pi, scale=2*np.pi) # type: ignore[attr-defined]
52
+ ] * 3
53
+
54
+ def test_sample_AB(self):
55
+ # (d, n)
56
+ A = np.array(
57
+ [[1, 4, 7, 10],
58
+ [2, 5, 8, 11],
59
+ [3, 6, 9, 12]]
60
+ )
61
+ B = A + 100
62
+ # (d, d, n)
63
+ ref = np.array(
64
+ [[[101, 104, 107, 110],
65
+ [2, 5, 8, 11],
66
+ [3, 6, 9, 12]],
67
+ [[1, 4, 7, 10],
68
+ [102, 105, 108, 111],
69
+ [3, 6, 9, 12]],
70
+ [[1, 4, 7, 10],
71
+ [2, 5, 8, 11],
72
+ [103, 106, 109, 112]]]
73
+ )
74
+ AB = sample_AB(A=A, B=B)
75
+ assert_allclose(AB, ref)
76
+
77
+ @pytest.mark.xslow
78
+ @pytest.mark.xfail_on_32bit("Can't create large array for test")
79
+ @pytest.mark.parametrize(
80
+ 'func',
81
+ [f_ishigami, pytest.param(f_ishigami_vec, marks=pytest.mark.slow)],
82
+ ids=['scalar', 'vector']
83
+ )
84
+ def test_ishigami(self, ishigami_ref_indices, func):
85
+ rng = np.random.default_rng(28631265345463262246170309650372465332)
86
+ res = sobol_indices(
87
+ func=func, n=4096,
88
+ dists=self.dists,
89
+ rng=rng
90
+ )
91
+
92
+ if func.__name__ == 'f_ishigami_vec':
93
+ ishigami_ref_indices = [
94
+ [ishigami_ref_indices[0], ishigami_ref_indices[0]],
95
+ [ishigami_ref_indices[1], ishigami_ref_indices[1]]
96
+ ]
97
+
98
+ assert_allclose(res.first_order, ishigami_ref_indices[0], atol=1e-2)
99
+ assert_allclose(res.total_order, ishigami_ref_indices[1], atol=1e-2)
100
+
101
+ assert res._bootstrap_result is None
102
+ bootstrap_res = res.bootstrap(n_resamples=99)
103
+ assert isinstance(bootstrap_res, BootstrapSobolResult)
104
+ assert isinstance(res._bootstrap_result, BootstrapResult)
105
+
106
+ assert res._bootstrap_result.confidence_interval.low.shape[0] == 2
107
+ assert res._bootstrap_result.confidence_interval.low[1].shape \
108
+ == res.first_order.shape
109
+
110
+ assert bootstrap_res.first_order.confidence_interval.low.shape \
111
+ == res.first_order.shape
112
+ assert bootstrap_res.total_order.confidence_interval.low.shape \
113
+ == res.total_order.shape
114
+
115
+ assert_array_less(
116
+ bootstrap_res.first_order.confidence_interval.low, res.first_order
117
+ )
118
+ assert_array_less(
119
+ res.first_order, bootstrap_res.first_order.confidence_interval.high
120
+ )
121
+ assert_array_less(
122
+ bootstrap_res.total_order.confidence_interval.low, res.total_order
123
+ )
124
+ assert_array_less(
125
+ res.total_order, bootstrap_res.total_order.confidence_interval.high
126
+ )
127
+
128
+ # call again to use previous results and change a param
129
+ assert isinstance(
130
+ res.bootstrap(confidence_level=0.9, n_resamples=99),
131
+ BootstrapSobolResult
132
+ )
133
+ assert isinstance(res._bootstrap_result, BootstrapResult)
134
+
135
+ def test_func_dict(self, ishigami_ref_indices):
136
+ rng = np.random.default_rng(28631265345463262246170309650372465332)
137
+ n = 4096
138
+ dists = [
139
+ stats.uniform(loc=-np.pi, scale=2*np.pi),
140
+ stats.uniform(loc=-np.pi, scale=2*np.pi),
141
+ stats.uniform(loc=-np.pi, scale=2*np.pi)
142
+ ]
143
+
144
+ A, B = sample_A_B(n=n, dists=dists, rng=rng)
145
+ AB = sample_AB(A=A, B=B)
146
+
147
+ func = {
148
+ 'f_A': f_ishigami(A).reshape(1, -1),
149
+ 'f_B': f_ishigami(B).reshape(1, -1),
150
+ 'f_AB': f_ishigami(AB).reshape((3, 1, -1))
151
+ }
152
+
153
+ # preserve use of old random_state during SPEC 7 transition
154
+ res = sobol_indices(
155
+ func=func, n=n,
156
+ dists=dists,
157
+ rng=rng
158
+ )
159
+ assert_allclose(res.first_order, ishigami_ref_indices[0], atol=1e-2)
160
+
161
+ res = sobol_indices(
162
+ func=func, n=n,
163
+ rng=rng
164
+ )
165
+ assert_allclose(res.first_order, ishigami_ref_indices[0], atol=1e-2)
166
+ # Ideally should be exactly equal but since f_ishigami
167
+ # uses floating point operations, so exact equality
168
+ # might not be possible (due to flakiness in computation).
169
+ # So, assert_allclose is used with default parameters
170
+ # Regression test for https://github.com/scipy/scipy/issues/21383
171
+ assert_allclose(f_ishigami(A).reshape(1, -1), func['f_A'])
172
+ assert_allclose(f_ishigami(B).reshape(1, -1), func['f_B'])
173
+ assert_allclose(f_ishigami(AB).reshape((3, 1, -1)), func['f_AB'])
174
+
175
+ def test_method(self, ishigami_ref_indices):
176
+ def jansen_sobol(f_A, f_B, f_AB):
177
+ """Jansen for S and Sobol' for St.
178
+
179
+ From Saltelli2010, table 2 formulations (c) and (e)."""
180
+ var = np.var([f_A, f_B], axis=(0, -1))
181
+
182
+ s = (var - 0.5*np.mean((f_B - f_AB)**2, axis=-1)) / var
183
+ st = np.mean(f_A*(f_A - f_AB), axis=-1) / var
184
+
185
+ return s.T, st.T
186
+
187
+ rng = np.random.default_rng(28631265345463262246170309650372465332)
188
+ res = sobol_indices(
189
+ func=f_ishigami, n=4096,
190
+ dists=self.dists,
191
+ method=jansen_sobol,
192
+ rng=rng
193
+ )
194
+
195
+ assert_allclose(res.first_order, ishigami_ref_indices[0], atol=1e-2)
196
+ assert_allclose(res.total_order, ishigami_ref_indices[1], atol=1e-2)
197
+
198
+ def jansen_sobol_typed(
199
+ f_A: np.ndarray, f_B: np.ndarray, f_AB: np.ndarray
200
+ ) -> tuple[np.ndarray, np.ndarray]:
201
+ return jansen_sobol(f_A, f_B, f_AB)
202
+
203
+ _ = sobol_indices(
204
+ func=f_ishigami, n=8,
205
+ dists=self.dists,
206
+ method=jansen_sobol_typed,
207
+ rng=rng
208
+ )
209
+
210
+ def test_normalization(self, ishigami_ref_indices):
211
+ rng = np.random.default_rng(28631265345463262246170309650372465332)
212
+ res = sobol_indices(
213
+ func=lambda x: f_ishigami(x) + 1000, n=4096,
214
+ dists=self.dists,
215
+ rng=rng
216
+ )
217
+
218
+ assert_allclose(res.first_order, ishigami_ref_indices[0], atol=1e-2)
219
+ assert_allclose(res.total_order, ishigami_ref_indices[1], atol=1e-2)
220
+
221
+ def test_constant_function(self, ishigami_ref_indices):
222
+
223
+ def f_ishigami_vec_const(x):
224
+ """Output of shape (3, n)."""
225
+ res = f_ishigami(x)
226
+ return res, res * 0 + 10, res
227
+
228
+ rng = np.random.default_rng(28631265345463262246170309650372465332)
229
+ res = sobol_indices(
230
+ func=f_ishigami_vec_const, n=4096,
231
+ dists=self.dists,
232
+ rng=rng
233
+ )
234
+
235
+ ishigami_vec_indices = [
236
+ [ishigami_ref_indices[0], [0, 0, 0], ishigami_ref_indices[0]],
237
+ [ishigami_ref_indices[1], [0, 0, 0], ishigami_ref_indices[1]]
238
+ ]
239
+
240
+ assert_allclose(res.first_order, ishigami_vec_indices[0], atol=1e-2)
241
+ assert_allclose(res.total_order, ishigami_vec_indices[1], atol=1e-2)
242
+
243
+ @pytest.mark.xfail_on_32bit("Can't create large array for test")
244
+ def test_more_converged(self, ishigami_ref_indices):
245
+ rng = np.random.default_rng(28631265345463262246170309650372465332)
246
+ res = sobol_indices(
247
+ func=f_ishigami, n=2**19, # 524288
248
+ dists=self.dists,
249
+ rng=rng
250
+ )
251
+
252
+ assert_allclose(res.first_order, ishigami_ref_indices[0], atol=1e-4)
253
+ assert_allclose(res.total_order, ishigami_ref_indices[1], atol=1e-4)
254
+
255
+ def test_raises(self):
256
+
257
+ message = r"Each distribution in `dists` must have method `ppf`"
258
+ with pytest.raises(ValueError, match=message):
259
+ sobol_indices(n=0, func=f_ishigami, dists="uniform")
260
+
261
+ with pytest.raises(ValueError, match=message):
262
+ sobol_indices(n=0, func=f_ishigami, dists=[lambda x: x])
263
+
264
+ message = r"The balance properties of Sobol'"
265
+ with pytest.raises(ValueError, match=message):
266
+ sobol_indices(n=7, func=f_ishigami, dists=[stats.uniform()])
267
+
268
+ with pytest.raises(ValueError, match=message):
269
+ sobol_indices(n=4.1, func=f_ishigami, dists=[stats.uniform()])
270
+
271
+ message = r"'toto' is not a valid 'method'"
272
+ with pytest.raises(ValueError, match=message):
273
+ sobol_indices(n=0, func=f_ishigami, method='toto')
274
+
275
+ message = r"must have the following signature"
276
+ with pytest.raises(ValueError, match=message):
277
+ sobol_indices(n=0, func=f_ishigami, method=lambda x: x)
278
+
279
+ message = r"'dists' must be defined when 'func' is a callable"
280
+ with pytest.raises(ValueError, match=message):
281
+ sobol_indices(n=0, func=f_ishigami)
282
+
283
+ def func_wrong_shape_output(x):
284
+ return x.reshape(-1, 1)
285
+
286
+ message = r"'func' output should have a shape"
287
+ with pytest.raises(ValueError, match=message):
288
+ sobol_indices(
289
+ n=2, func=func_wrong_shape_output, dists=[stats.uniform()]
290
+ )
291
+
292
+ message = r"When 'func' is a dictionary"
293
+ with pytest.raises(ValueError, match=message):
294
+ sobol_indices(
295
+ n=2, func={'f_A': [], 'f_AB': []}, dists=[stats.uniform()]
296
+ )
297
+
298
+ with pytest.raises(ValueError, match=message):
299
+ # f_B malformed
300
+ sobol_indices(
301
+ n=2,
302
+ func={'f_A': [1, 2], 'f_B': [3], 'f_AB': [5, 6, 7, 8]},
303
+ )
304
+
305
+ with pytest.raises(ValueError, match=message):
306
+ # f_AB malformed
307
+ sobol_indices(
308
+ n=2,
309
+ func={'f_A': [1, 2], 'f_B': [3, 4], 'f_AB': [5, 6, 7]},
310
+ )
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_stats.py ADDED
The diff for this file is too large to render. See raw diff
 
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_survival.py ADDED
@@ -0,0 +1,466 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import numpy as np
3
+ from numpy.testing import assert_equal, assert_allclose
4
+ from scipy import stats
5
+ from scipy.stats import _survival
6
+
7
+
8
+ def _kaplan_meier_reference(times, censored):
9
+ # This is a very straightforward implementation of the Kaplan-Meier
10
+ # estimator that does almost everything differently from the implementation
11
+ # in stats.ecdf.
12
+
13
+ # Begin by sorting the raw data. Note that the order of death and loss
14
+ # at a given time matters: death happens first. See [2] page 461:
15
+ # "These conventions may be paraphrased by saying that deaths recorded as
16
+ # of an age t are treated as if they occurred slightly before t, and losses
17
+ # recorded as of an age t are treated as occurring slightly after t."
18
+ # We implement this by sorting the data first by time, then by `censored`,
19
+ # (which is 0 when there is a death and 1 when there is only a loss).
20
+ dtype = [('time', float), ('censored', int)]
21
+ data = np.array([(t, d) for t, d in zip(times, censored)], dtype=dtype)
22
+ data = np.sort(data, order=('time', 'censored'))
23
+ times = data['time']
24
+ died = np.logical_not(data['censored'])
25
+
26
+ m = times.size
27
+ n = np.arange(m, 0, -1) # number at risk
28
+ sf = np.cumprod((n - died) / n)
29
+
30
+ # Find the indices of the *last* occurrence of unique times. The
31
+ # corresponding entries of `times` and `sf` are what we want.
32
+ _, indices = np.unique(times[::-1], return_index=True)
33
+ ref_times = times[-indices - 1]
34
+ ref_sf = sf[-indices - 1]
35
+ return ref_times, ref_sf
36
+
37
+
38
+ class TestSurvival:
39
+
40
+ @staticmethod
41
+ def get_random_sample(rng, n_unique):
42
+ # generate random sample
43
+ unique_times = rng.random(n_unique)
44
+ # convert to `np.int32` to resolve `np.repeat` failure in 32-bit CI
45
+ repeats = rng.integers(1, 4, n_unique).astype(np.int32)
46
+ times = rng.permuted(np.repeat(unique_times, repeats))
47
+ censored = rng.random(size=times.size) > rng.random()
48
+ sample = stats.CensoredData.right_censored(times, censored)
49
+ return sample, times, censored
50
+
51
+ def test_input_validation(self):
52
+ message = '`sample` must be a one-dimensional sequence.'
53
+ with pytest.raises(ValueError, match=message):
54
+ stats.ecdf([[1]])
55
+ with pytest.raises(ValueError, match=message):
56
+ stats.ecdf(1)
57
+
58
+ message = '`sample` must not contain nan'
59
+ with pytest.raises(ValueError, match=message):
60
+ stats.ecdf([np.nan])
61
+
62
+ message = 'Currently, only uncensored and right-censored data...'
63
+ with pytest.raises(NotImplementedError, match=message):
64
+ stats.ecdf(stats.CensoredData.left_censored([1], censored=[True]))
65
+
66
+ message = 'method` must be one of...'
67
+ res = stats.ecdf([1, 2, 3])
68
+ with pytest.raises(ValueError, match=message):
69
+ res.cdf.confidence_interval(method='ekki-ekki')
70
+ with pytest.raises(ValueError, match=message):
71
+ res.sf.confidence_interval(method='shrubbery')
72
+
73
+ message = 'confidence_level` must be a scalar between 0 and 1'
74
+ with pytest.raises(ValueError, match=message):
75
+ res.cdf.confidence_interval(-1)
76
+ with pytest.raises(ValueError, match=message):
77
+ res.sf.confidence_interval([0.5, 0.6])
78
+
79
+ message = 'The confidence interval is undefined at some observations.'
80
+ with pytest.warns(RuntimeWarning, match=message):
81
+ ci = res.cdf.confidence_interval()
82
+
83
+ message = 'Confidence interval bounds do not implement...'
84
+ with pytest.raises(NotImplementedError, match=message):
85
+ ci.low.confidence_interval()
86
+ with pytest.raises(NotImplementedError, match=message):
87
+ ci.high.confidence_interval()
88
+
89
+ def test_edge_cases(self):
90
+ res = stats.ecdf([])
91
+ assert_equal(res.cdf.quantiles, [])
92
+ assert_equal(res.cdf.probabilities, [])
93
+
94
+ res = stats.ecdf([1])
95
+ assert_equal(res.cdf.quantiles, [1])
96
+ assert_equal(res.cdf.probabilities, [1])
97
+
98
+ def test_unique(self):
99
+ # Example with unique observations; `stats.ecdf` ref. [1] page 80
100
+ sample = [6.23, 5.58, 7.06, 6.42, 5.20]
101
+ res = stats.ecdf(sample)
102
+ ref_x = np.sort(np.unique(sample))
103
+ ref_cdf = np.arange(1, 6) / 5
104
+ ref_sf = 1 - ref_cdf
105
+ assert_equal(res.cdf.quantiles, ref_x)
106
+ assert_equal(res.cdf.probabilities, ref_cdf)
107
+ assert_equal(res.sf.quantiles, ref_x)
108
+ assert_equal(res.sf.probabilities, ref_sf)
109
+
110
+ def test_nonunique(self):
111
+ # Example with non-unique observations; `stats.ecdf` ref. [1] page 82
112
+ sample = [0, 2, 1, 2, 3, 4]
113
+ res = stats.ecdf(sample)
114
+ ref_x = np.sort(np.unique(sample))
115
+ ref_cdf = np.array([1/6, 2/6, 4/6, 5/6, 1])
116
+ ref_sf = 1 - ref_cdf
117
+ assert_equal(res.cdf.quantiles, ref_x)
118
+ assert_equal(res.cdf.probabilities, ref_cdf)
119
+ assert_equal(res.sf.quantiles, ref_x)
120
+ assert_equal(res.sf.probabilities, ref_sf)
121
+
122
+ def test_evaluate_methods(self):
123
+ # Test CDF and SF `evaluate` methods
124
+ rng = np.random.default_rng(1162729143302572461)
125
+ sample, _, _ = self.get_random_sample(rng, 15)
126
+ res = stats.ecdf(sample)
127
+ x = res.cdf.quantiles
128
+ xr = x + np.diff(x, append=x[-1]+1)/2 # right shifted points
129
+
130
+ assert_equal(res.cdf.evaluate(x), res.cdf.probabilities)
131
+ assert_equal(res.cdf.evaluate(xr), res.cdf.probabilities)
132
+ assert_equal(res.cdf.evaluate(x[0]-1), 0) # CDF starts at 0
133
+ assert_equal(res.cdf.evaluate([-np.inf, np.inf]), [0, 1])
134
+
135
+ assert_equal(res.sf.evaluate(x), res.sf.probabilities)
136
+ assert_equal(res.sf.evaluate(xr), res.sf.probabilities)
137
+ assert_equal(res.sf.evaluate(x[0]-1), 1) # SF starts at 1
138
+ assert_equal(res.sf.evaluate([-np.inf, np.inf]), [1, 0])
139
+
140
+ # ref. [1] page 91
141
+ t1 = [37, 43, 47, 56, 60, 62, 71, 77, 80, 81] # times
142
+ d1 = [0, 0, 1, 1, 0, 0, 0, 1, 1, 1] # 1 means deaths (not censored)
143
+ r1 = [1, 1, 0.875, 0.75, 0.75, 0.75, 0.75, 0.5, 0.25, 0] # reference SF
144
+
145
+ # https://sphweb.bumc.bu.edu/otlt/mph-modules/bs/bs704_survival/BS704_Survival5.html
146
+ t2 = [8, 12, 26, 14, 21, 27, 8, 32, 20, 40]
147
+ d2 = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
148
+ r2 = [0.9, 0.788, 0.675, 0.675, 0.54, 0.405, 0.27, 0.27, 0.27]
149
+ t3 = [33, 28, 41, 48, 48, 25, 37, 48, 25, 43]
150
+ d3 = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
151
+ r3 = [1, 0.875, 0.75, 0.75, 0.6, 0.6, 0.6]
152
+
153
+ # https://sphweb.bumc.bu.edu/otlt/mph-modules/bs/bs704_survival/bs704_survival4.html
154
+ t4 = [24, 3, 11, 19, 24, 13, 14, 2, 18, 17,
155
+ 24, 21, 12, 1, 10, 23, 6, 5, 9, 17]
156
+ d4 = [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1]
157
+ r4 = [0.95, 0.95, 0.897, 0.844, 0.844, 0.844, 0.844, 0.844, 0.844,
158
+ 0.844, 0.76, 0.676, 0.676, 0.676, 0.676, 0.507, 0.507]
159
+
160
+ # https://www.real-statistics.com/survival-analysis/kaplan-meier-procedure/confidence-interval-for-the-survival-function/
161
+ t5 = [3, 5, 8, 10, 5, 5, 8, 12, 15, 14, 2, 11, 10, 9, 12, 5, 8, 11]
162
+ d5 = [1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1]
163
+ r5 = [0.944, 0.889, 0.722, 0.542, 0.542, 0.542, 0.361, 0.181, 0.181, 0.181]
164
+
165
+ @pytest.mark.parametrize("case", [(t1, d1, r1), (t2, d2, r2), (t3, d3, r3),
166
+ (t4, d4, r4), (t5, d5, r5)])
167
+ def test_right_censored_against_examples(self, case):
168
+ # test `ecdf` against other implementations on example problems
169
+ times, died, ref = case
170
+ sample = stats.CensoredData.right_censored(times, np.logical_not(died))
171
+ res = stats.ecdf(sample)
172
+ assert_allclose(res.sf.probabilities, ref, atol=1e-3)
173
+ assert_equal(res.sf.quantiles, np.sort(np.unique(times)))
174
+
175
+ # test reference implementation against other implementations
176
+ res = _kaplan_meier_reference(times, np.logical_not(died))
177
+ assert_equal(res[0], np.sort(np.unique(times)))
178
+ assert_allclose(res[1], ref, atol=1e-3)
179
+
180
+ @pytest.mark.parametrize('seed', [182746786639392128, 737379171436494115,
181
+ 576033618403180168, 308115465002673650])
182
+ def test_right_censored_against_reference_implementation(self, seed):
183
+ # test `ecdf` against reference implementation on random problems
184
+ rng = np.random.default_rng(seed)
185
+ n_unique = rng.integers(10, 100)
186
+ sample, times, censored = self.get_random_sample(rng, n_unique)
187
+ res = stats.ecdf(sample)
188
+ ref = _kaplan_meier_reference(times, censored)
189
+ assert_allclose(res.sf.quantiles, ref[0])
190
+ assert_allclose(res.sf.probabilities, ref[1])
191
+
192
+ # If all observations are uncensored, the KM estimate should match
193
+ # the usual estimate for uncensored data
194
+ sample = stats.CensoredData(uncensored=times)
195
+ res = _survival._ecdf_right_censored(sample) # force Kaplan-Meier
196
+ ref = stats.ecdf(times)
197
+ assert_equal(res[0], ref.sf.quantiles)
198
+ assert_allclose(res[1], ref.cdf.probabilities, rtol=1e-14)
199
+ assert_allclose(res[2], ref.sf.probabilities, rtol=1e-14)
200
+
201
+ def test_right_censored_ci(self):
202
+ # test "greenwood" confidence interval against example 4 (URL above).
203
+ times, died = self.t4, self.d4
204
+ sample = stats.CensoredData.right_censored(times, np.logical_not(died))
205
+ res = stats.ecdf(sample)
206
+ ref_allowance = [0.096, 0.096, 0.135, 0.162, 0.162, 0.162, 0.162,
207
+ 0.162, 0.162, 0.162, 0.214, 0.246, 0.246, 0.246,
208
+ 0.246, 0.341, 0.341]
209
+
210
+ sf_ci = res.sf.confidence_interval()
211
+ cdf_ci = res.cdf.confidence_interval()
212
+ allowance = res.sf.probabilities - sf_ci.low.probabilities
213
+
214
+ assert_allclose(allowance, ref_allowance, atol=1e-3)
215
+ assert_allclose(sf_ci.low.probabilities,
216
+ np.clip(res.sf.probabilities - allowance, 0, 1))
217
+ assert_allclose(sf_ci.high.probabilities,
218
+ np.clip(res.sf.probabilities + allowance, 0, 1))
219
+ assert_allclose(cdf_ci.low.probabilities,
220
+ np.clip(res.cdf.probabilities - allowance, 0, 1))
221
+ assert_allclose(cdf_ci.high.probabilities,
222
+ np.clip(res.cdf.probabilities + allowance, 0, 1))
223
+
224
+ # test "log-log" confidence interval against Mathematica
225
+ # e = {24, 3, 11, 19, 24, 13, 14, 2, 18, 17, 24, 21, 12, 1, 10, 23, 6, 5,
226
+ # 9, 17}
227
+ # ci = {1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0}
228
+ # R = EventData[e, ci]
229
+ # S = SurvivalModelFit[R]
230
+ # S["PointwiseIntervals", ConfidenceLevel->0.95,
231
+ # ConfidenceTransform->"LogLog"]
232
+
233
+ ref_low = [0.694743, 0.694743, 0.647529, 0.591142, 0.591142, 0.591142,
234
+ 0.591142, 0.591142, 0.591142, 0.591142, 0.464605, 0.370359,
235
+ 0.370359, 0.370359, 0.370359, 0.160489, 0.160489]
236
+ ref_high = [0.992802, 0.992802, 0.973299, 0.947073, 0.947073, 0.947073,
237
+ 0.947073, 0.947073, 0.947073, 0.947073, 0.906422, 0.856521,
238
+ 0.856521, 0.856521, 0.856521, 0.776724, 0.776724]
239
+ sf_ci = res.sf.confidence_interval(method='log-log')
240
+ assert_allclose(sf_ci.low.probabilities, ref_low, atol=1e-6)
241
+ assert_allclose(sf_ci.high.probabilities, ref_high, atol=1e-6)
242
+
243
+ def test_right_censored_ci_example_5(self):
244
+ # test "exponential greenwood" confidence interval against example 5
245
+ times, died = self.t5, self.d5
246
+ sample = stats.CensoredData.right_censored(times, np.logical_not(died))
247
+ res = stats.ecdf(sample)
248
+ lower = np.array([0.66639, 0.624174, 0.456179, 0.287822, 0.287822,
249
+ 0.287822, 0.128489, 0.030957, 0.030957, 0.030957])
250
+ upper = np.array([0.991983, 0.970995, 0.87378, 0.739467, 0.739467,
251
+ 0.739467, 0.603133, 0.430365, 0.430365, 0.430365])
252
+
253
+ sf_ci = res.sf.confidence_interval(method='log-log')
254
+ cdf_ci = res.cdf.confidence_interval(method='log-log')
255
+
256
+ assert_allclose(sf_ci.low.probabilities, lower, atol=1e-5)
257
+ assert_allclose(sf_ci.high.probabilities, upper, atol=1e-5)
258
+ assert_allclose(cdf_ci.low.probabilities, 1-upper, atol=1e-5)
259
+ assert_allclose(cdf_ci.high.probabilities, 1-lower, atol=1e-5)
260
+
261
+ # Test against R's `survival` library `survfit` function, 90%CI
262
+ # library(survival)
263
+ # options(digits=16)
264
+ # time = c(3, 5, 8, 10, 5, 5, 8, 12, 15, 14, 2, 11, 10, 9, 12, 5, 8, 11)
265
+ # status = c(1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1)
266
+ # res = survfit(Surv(time, status)
267
+ # ~1, conf.type = "log-log", conf.int = 0.90)
268
+ # res$time; res$lower; res$upper
269
+ low = [0.74366748406861172, 0.68582332289196246, 0.50596835651480121,
270
+ 0.32913131413336727, 0.32913131413336727, 0.32913131413336727,
271
+ 0.15986912028781664, 0.04499539918147757, 0.04499539918147757,
272
+ 0.04499539918147757]
273
+ high = [0.9890291867238429, 0.9638835422144144, 0.8560366823086629,
274
+ 0.7130167643978450, 0.7130167643978450, 0.7130167643978450,
275
+ 0.5678602982997164, 0.3887616766886558, 0.3887616766886558,
276
+ 0.3887616766886558]
277
+ sf_ci = res.sf.confidence_interval(method='log-log',
278
+ confidence_level=0.9)
279
+ assert_allclose(sf_ci.low.probabilities, low)
280
+ assert_allclose(sf_ci.high.probabilities, high)
281
+
282
+ # And with conf.type = "plain"
283
+ low = [0.8556383113628162, 0.7670478794850761, 0.5485720663578469,
284
+ 0.3441515412527123, 0.3441515412527123, 0.3441515412527123,
285
+ 0.1449184105424544, 0., 0., 0.]
286
+ high = [1., 1., 0.8958723780865975, 0.7391817920806210,
287
+ 0.7391817920806210, 0.7391817920806210, 0.5773038116797676,
288
+ 0.3642270254596720, 0.3642270254596720, 0.3642270254596720]
289
+ sf_ci = res.sf.confidence_interval(confidence_level=0.9)
290
+ assert_allclose(sf_ci.low.probabilities, low)
291
+ assert_allclose(sf_ci.high.probabilities, high)
292
+
293
+ def test_right_censored_ci_nans(self):
294
+ # test `ecdf` confidence interval on a problem that results in NaNs
295
+ times, died = self.t1, self.d1
296
+ sample = stats.CensoredData.right_censored(times, np.logical_not(died))
297
+ res = stats.ecdf(sample)
298
+
299
+ # Reference values generated with Matlab
300
+ # format long
301
+ # t = [37 43 47 56 60 62 71 77 80 81];
302
+ # d = [0 0 1 1 0 0 0 1 1 1];
303
+ # censored = ~d1;
304
+ # [f, x, flo, fup] = ecdf(t, 'Censoring', censored, 'Alpha', 0.05);
305
+ x = [37, 47, 56, 77, 80, 81]
306
+ flo = [np.nan, 0, 0, 0.052701464070711, 0.337611126231790, np.nan]
307
+ fup = [np.nan, 0.35417230377, 0.5500569798, 0.9472985359, 1.0, np.nan]
308
+ i = np.searchsorted(res.cdf.quantiles, x)
309
+
310
+ message = "The confidence interval is undefined at some observations"
311
+ with pytest.warns(RuntimeWarning, match=message):
312
+ ci = res.cdf.confidence_interval()
313
+
314
+ # Matlab gives NaN as the first element of the CIs. Mathematica agrees,
315
+ # but R's survfit does not. It makes some sense, but it's not what the
316
+ # formula gives, so skip that element.
317
+ assert_allclose(ci.low.probabilities[i][1:], flo[1:])
318
+ assert_allclose(ci.high.probabilities[i][1:], fup[1:])
319
+
320
+ # [f, x, flo, fup] = ecdf(t, 'Censoring', censored, 'Function',
321
+ # 'survivor', 'Alpha', 0.05);
322
+ flo = [np.nan, 0.64582769623, 0.449943020228, 0.05270146407, 0, np.nan]
323
+ fup = [np.nan, 1.0, 1.0, 0.947298535929289, 0.662388873768210, np.nan]
324
+ i = np.searchsorted(res.cdf.quantiles, x)
325
+
326
+ with pytest.warns(RuntimeWarning, match=message):
327
+ ci = res.sf.confidence_interval()
328
+
329
+ assert_allclose(ci.low.probabilities[i][1:], flo[1:])
330
+ assert_allclose(ci.high.probabilities[i][1:], fup[1:])
331
+
332
+ # With the same data, R's `survival` library `survfit` function
333
+ # doesn't produce the leading NaN
334
+ # library(survival)
335
+ # options(digits=16)
336
+ # time = c(37, 43, 47, 56, 60, 62, 71, 77, 80, 81)
337
+ # status = c(0, 0, 1, 1, 0, 0, 0, 1, 1, 1)
338
+ # res = survfit(Surv(time, status)
339
+ # ~1, conf.type = "plain", conf.int = 0.95)
340
+ # res$time
341
+ # res$lower
342
+ # res$upper
343
+ low = [1., 1., 0.64582769623233816, 0.44994302022779326,
344
+ 0.44994302022779326, 0.44994302022779326, 0.44994302022779326,
345
+ 0.05270146407071086, 0., np.nan]
346
+ high = [1., 1., 1., 1., 1., 1., 1., 0.9472985359292891,
347
+ 0.6623888737682101, np.nan]
348
+ assert_allclose(ci.low.probabilities, low)
349
+ assert_allclose(ci.high.probabilities, high)
350
+
351
+ # It does with conf.type="log-log", as do we
352
+ with pytest.warns(RuntimeWarning, match=message):
353
+ ci = res.sf.confidence_interval(method='log-log')
354
+ low = [np.nan, np.nan, 0.38700001403202522, 0.31480711370551911,
355
+ 0.31480711370551911, 0.31480711370551911, 0.31480711370551911,
356
+ 0.08048821148507734, 0.01049958986680601, np.nan]
357
+ high = [np.nan, np.nan, 0.9813929658789660, 0.9308983170906275,
358
+ 0.9308983170906275, 0.9308983170906275, 0.9308983170906275,
359
+ 0.8263946341076415, 0.6558775085110887, np.nan]
360
+ assert_allclose(ci.low.probabilities, low)
361
+ assert_allclose(ci.high.probabilities, high)
362
+
363
+ def test_right_censored_against_uncensored(self):
364
+ rng = np.random.default_rng(7463952748044886637)
365
+ sample = rng.integers(10, 100, size=1000)
366
+ censored = np.zeros_like(sample)
367
+ censored[np.argmax(sample)] = True
368
+ res = stats.ecdf(sample)
369
+ ref = stats.ecdf(stats.CensoredData.right_censored(sample, censored))
370
+ assert_equal(res.sf.quantiles, ref.sf.quantiles)
371
+ assert_equal(res.sf._n, ref.sf._n)
372
+ assert_equal(res.sf._d[:-1], ref.sf._d[:-1]) # difference @ [-1]
373
+ assert_allclose(res.sf._sf[:-1], ref.sf._sf[:-1], rtol=1e-14)
374
+
375
+ def test_plot_iv(self):
376
+ rng = np.random.default_rng(1769658657308472721)
377
+ n_unique = rng.integers(10, 100)
378
+ sample, _, _ = self.get_random_sample(rng, n_unique)
379
+ res = stats.ecdf(sample)
380
+
381
+ try:
382
+ import matplotlib.pyplot as plt # noqa: F401
383
+ res.sf.plot() # no other errors occur
384
+ except (ModuleNotFoundError, ImportError):
385
+ message = r"matplotlib must be installed to use method `plot`."
386
+ with pytest.raises(ModuleNotFoundError, match=message):
387
+ res.sf.plot()
388
+
389
+
390
+ class TestLogRank:
391
+
392
+ @pytest.mark.parametrize(
393
+ "x, y, statistic, pvalue",
394
+ # Results validate with R
395
+ # library(survival)
396
+ # options(digits=16)
397
+ #
398
+ # futime_1 <- c(8, 12, 26, 14, 21, 27, 8, 32, 20, 40)
399
+ # fustat_1 <- c(1, 1, 1, 1, 1, 1, 0, 0, 0, 0)
400
+ # rx_1 <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
401
+ #
402
+ # futime_2 <- c(33, 28, 41, 48, 48, 25, 37, 48, 25, 43)
403
+ # fustat_2 <- c(1, 1, 1, 0, 0, 0, 0, 0, 0, 0)
404
+ # rx_2 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
405
+ #
406
+ # futime <- c(futime_1, futime_2)
407
+ # fustat <- c(fustat_1, fustat_2)
408
+ # rx <- c(rx_1, rx_2)
409
+ #
410
+ # survdiff(formula = Surv(futime, fustat) ~ rx)
411
+ #
412
+ # Also check against another library which handle alternatives
413
+ # library(nph)
414
+ # logrank.test(futime, fustat, rx, alternative = "two.sided")
415
+ # res["test"]
416
+ [(
417
+ # https://sphweb.bumc.bu.edu/otlt/mph-modules/bs/bs704_survival/BS704_Survival5.html
418
+ # uncensored, censored
419
+ [[8, 12, 26, 14, 21, 27], [8, 32, 20, 40]],
420
+ [[33, 28, 41], [48, 48, 25, 37, 48, 25, 43]],
421
+ # chi2, ["two-sided", "less", "greater"]
422
+ 6.91598157449,
423
+ [0.008542873404, 0.9957285632979385, 0.004271436702061537]
424
+ ),
425
+ (
426
+ # https://sphweb.bumc.bu.edu/otlt/mph-modules/bs/bs704_survival/BS704_Survival5.html
427
+ [[19, 6, 5, 4], [20, 19, 17, 14]],
428
+ [[16, 21, 7], [21, 15, 18, 18, 5]],
429
+ 0.835004855038,
430
+ [0.3608293039, 0.8195853480676912, 0.1804146519323088]
431
+ ),
432
+ (
433
+ # Bland, Altman, "The logrank test", BMJ, 2004
434
+ # https://www.bmj.com/content/328/7447/1073.short
435
+ [[6, 13, 21, 30, 37, 38, 49, 50, 63, 79, 86, 98, 202, 219],
436
+ [31, 47, 80, 82, 82, 149]],
437
+ [[10, 10, 12, 13, 14, 15, 16, 17, 18, 20, 24, 24, 25, 28, 30,
438
+ 33, 35, 37, 40, 40, 46, 48, 76, 81, 82, 91, 112, 181],
439
+ [34, 40, 70]],
440
+ 7.49659416854,
441
+ [0.006181578637, 0.003090789318730882, 0.9969092106812691]
442
+ )]
443
+ )
444
+ def test_log_rank(self, x, y, statistic, pvalue):
445
+ x = stats.CensoredData(uncensored=x[0], right=x[1])
446
+ y = stats.CensoredData(uncensored=y[0], right=y[1])
447
+
448
+ for i, alternative in enumerate(["two-sided", "less", "greater"]):
449
+ res = stats.logrank(x=x, y=y, alternative=alternative)
450
+
451
+ # we return z and use the normal distribution while other framework
452
+ # return z**2. The p-value are directly comparable, but we have to
453
+ # square the statistic
454
+ assert_allclose(res.statistic**2, statistic, atol=1e-10)
455
+ assert_allclose(res.pvalue, pvalue[i], atol=1e-10)
456
+
457
+ def test_raises(self):
458
+ sample = stats.CensoredData([1, 2])
459
+
460
+ msg = r"`y` must be"
461
+ with pytest.raises(ValueError, match=msg):
462
+ stats.logrank(x=sample, y=[[1, 2]])
463
+
464
+ msg = r"`x` must be"
465
+ with pytest.raises(ValueError, match=msg):
466
+ stats.logrank(x=[[1, 2]], y=sample)
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_tukeylambda_stats.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from numpy.testing import assert_allclose, assert_equal
3
+
4
+ from scipy.stats._tukeylambda_stats import (tukeylambda_variance,
5
+ tukeylambda_kurtosis)
6
+
7
+
8
+ def test_tukeylambda_stats_known_exact():
9
+ """Compare results with some known exact formulas."""
10
+ # Some exact values of the Tukey Lambda variance and kurtosis:
11
+ # lambda var kurtosis
12
+ # 0 pi**2/3 6/5 (logistic distribution)
13
+ # 0.5 4 - pi (5/3 - pi/2)/(pi/4 - 1)**2 - 3
14
+ # 1 1/3 -6/5 (uniform distribution on (-1,1))
15
+ # 2 1/12 -6/5 (uniform distribution on (-1/2, 1/2))
16
+
17
+ # lambda = 0
18
+ var = tukeylambda_variance(0)
19
+ assert_allclose(var, np.pi**2 / 3, atol=1e-12)
20
+ kurt = tukeylambda_kurtosis(0)
21
+ assert_allclose(kurt, 1.2, atol=1e-10)
22
+
23
+ # lambda = 0.5
24
+ var = tukeylambda_variance(0.5)
25
+ assert_allclose(var, 4 - np.pi, atol=1e-12)
26
+ kurt = tukeylambda_kurtosis(0.5)
27
+ desired = (5./3 - np.pi/2) / (np.pi/4 - 1)**2 - 3
28
+ assert_allclose(kurt, desired, atol=1e-10)
29
+
30
+ # lambda = 1
31
+ var = tukeylambda_variance(1)
32
+ assert_allclose(var, 1.0 / 3, atol=1e-12)
33
+ kurt = tukeylambda_kurtosis(1)
34
+ assert_allclose(kurt, -1.2, atol=1e-10)
35
+
36
+ # lambda = 2
37
+ var = tukeylambda_variance(2)
38
+ assert_allclose(var, 1.0 / 12, atol=1e-12)
39
+ kurt = tukeylambda_kurtosis(2)
40
+ assert_allclose(kurt, -1.2, atol=1e-10)
41
+
42
+
43
+ def test_tukeylambda_stats_mpmath():
44
+ """Compare results with some values that were computed using mpmath."""
45
+ a10 = dict(atol=1e-10, rtol=0)
46
+ a12 = dict(atol=1e-12, rtol=0)
47
+ data = [
48
+ # lambda variance kurtosis
49
+ [-0.1, 4.78050217874253547, 3.78559520346454510],
50
+ [-0.0649, 4.16428023599895777, 2.52019675947435718],
51
+ [-0.05, 3.93672267890775277, 2.13129793057777277],
52
+ [-0.001, 3.30128380390964882, 1.21452460083542988],
53
+ [0.001, 3.27850775649572176, 1.18560634779287585],
54
+ [0.03125, 2.95927803254615800, 0.804487555161819980],
55
+ [0.05, 2.78281053405464501, 0.611604043886644327],
56
+ [0.0649, 2.65282386754100551, 0.476834119532774540],
57
+ [1.2, 0.242153920578588346, -1.23428047169049726],
58
+ [10.0, 0.00095237579757703597, 2.37810697355144933],
59
+ [20.0, 0.00012195121951131043, 7.37654321002709531],
60
+ ]
61
+
62
+ for lam, var_expected, kurt_expected in data:
63
+ var = tukeylambda_variance(lam)
64
+ assert_allclose(var, var_expected, **a12)
65
+ kurt = tukeylambda_kurtosis(lam)
66
+ assert_allclose(kurt, kurt_expected, **a10)
67
+
68
+ # Test with vector arguments (most of the other tests are for single
69
+ # values).
70
+ lam, var_expected, kurt_expected = zip(*data)
71
+ var = tukeylambda_variance(lam)
72
+ assert_allclose(var, var_expected, **a12)
73
+ kurt = tukeylambda_kurtosis(lam)
74
+ assert_allclose(kurt, kurt_expected, **a10)
75
+
76
+
77
+ def test_tukeylambda_stats_invalid():
78
+ """Test values of lambda outside the domains of the functions."""
79
+ lam = [-1.0, -0.5]
80
+ var = tukeylambda_variance(lam)
81
+ assert_equal(var, np.array([np.nan, np.inf]))
82
+
83
+ lam = [-1.0, -0.25]
84
+ kurt = tukeylambda_kurtosis(lam)
85
+ assert_equal(kurt, np.array([np.nan, np.inf]))
loan-advisor-agent/.venv/Lib/site-packages/scipy/stats/tests/test_variation.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import numpy as np
4
+ import pytest
5
+
6
+ from scipy.stats import variation
7
+ from scipy._lib._util import AxisError
8
+
9
+ from scipy._lib._array_api import make_xp_test_case, eager_warns
10
+ from scipy._lib._array_api_no_0d import xp_assert_equal, xp_assert_close
11
+ from scipy.stats._axis_nan_policy import (too_small_nd_omit, too_small_nd_not_omit,
12
+ SmallSampleWarning)
13
+
14
+ skip_xp_backends = pytest.mark.skip_xp_backends
15
+
16
+
17
+ @make_xp_test_case(variation)
18
+ class TestVariation:
19
+ """
20
+ Test class for scipy.stats.variation
21
+ """
22
+
23
+ def test_ddof(self, xp):
24
+ x = xp.arange(9.0)
25
+ xp_assert_close(variation(x, ddof=1), xp.asarray(math.sqrt(60/8)/4))
26
+
27
+ @pytest.mark.parametrize('sgn', [1, -1])
28
+ def test_sign(self, sgn, xp):
29
+ x = xp.asarray([1., 2., 3., 4., 5.])
30
+ v = variation(sgn*x)
31
+ expected = xp.asarray(sgn*math.sqrt(2)/3)
32
+ xp_assert_close(v, expected, rtol=1e-10)
33
+
34
+ @skip_xp_backends(np_only=True, reason="test plain python scalar input")
35
+ def test_scalar(self, xp):
36
+ # A scalar is treated like a 1-d sequence with length 1.
37
+ assert variation(4.0) == 0.0
38
+
39
+ @pytest.mark.parametrize('nan_policy, expected',
40
+ [('propagate', np.nan),
41
+ ('omit', np.sqrt(20/3)/4)])
42
+ @skip_xp_backends(np_only=True,
43
+ reason='`nan_policy` only supports NumPy backend')
44
+ def test_variation_nan(self, nan_policy, expected, xp):
45
+ x = xp.arange(10.)
46
+ x[9] = xp.nan
47
+ xp_assert_close(variation(x, nan_policy=nan_policy), expected)
48
+
49
+ @skip_xp_backends(np_only=True,
50
+ reason='`nan_policy` only supports NumPy backend')
51
+ def test_nan_policy_raise(self, xp):
52
+ x = xp.asarray([1.0, 2.0, xp.nan, 3.0])
53
+ with pytest.raises(ValueError, match='input contains nan'):
54
+ variation(x, nan_policy='raise')
55
+
56
+ @skip_xp_backends(np_only=True,
57
+ reason='`nan_policy` only supports NumPy backend')
58
+ def test_bad_nan_policy(self, xp):
59
+ with pytest.raises(ValueError, match='must be one of'):
60
+ variation([1, 2, 3], nan_policy='foobar')
61
+
62
+ @skip_xp_backends(np_only=True,
63
+ reason='`keepdims` only supports NumPy backend')
64
+ def test_keepdims(self, xp):
65
+ x = xp.reshape(xp.arange(10), (2, 5))
66
+ y = variation(x, axis=1, keepdims=True)
67
+ expected = np.array([[np.sqrt(2)/2],
68
+ [np.sqrt(2)/7]])
69
+ xp_assert_close(y, expected)
70
+
71
+ @skip_xp_backends(np_only=True,
72
+ reason='`keepdims` only supports NumPy backend')
73
+ @pytest.mark.parametrize('axis, expected',
74
+ [(0, np.empty((1, 0))),
75
+ (1, np.full((5, 1), fill_value=np.nan))])
76
+ def test_keepdims_size0(self, axis, expected, xp):
77
+ x = xp.zeros((5, 0))
78
+ if axis == 1:
79
+ with pytest.warns(SmallSampleWarning, match=too_small_nd_not_omit):
80
+ y = variation(x, axis=axis, keepdims=True)
81
+ else:
82
+ y = variation(x, axis=axis, keepdims=True)
83
+ xp_assert_equal(y, expected)
84
+
85
+ @skip_xp_backends(np_only=True,
86
+ reason='`keepdims` only supports NumPy backend')
87
+ @pytest.mark.parametrize('incr, expected_fill', [(0, np.inf), (1, np.nan)])
88
+ def test_keepdims_and_ddof_eq_len_plus_incr(self, incr, expected_fill, xp):
89
+ x = xp.asarray([[1, 1, 2, 2], [1, 2, 3, 3]])
90
+ y = variation(x, axis=1, ddof=x.shape[1] + incr, keepdims=True)
91
+ xp_assert_equal(y, xp.full((2, 1), fill_value=expected_fill))
92
+
93
+ @skip_xp_backends(np_only=True,
94
+ reason='`nan_policy` only supports NumPy backend')
95
+ def test_propagate_nan(self, xp):
96
+ # Check that the shape of the result is the same for inputs
97
+ # with and without nans, cf gh-5817
98
+ a = xp.reshape(xp.arange(8, dtype=float), (2, -1))
99
+ a[1, 0] = xp.nan
100
+ v = variation(a, axis=1, nan_policy="propagate")
101
+ xp_assert_close(v, [math.sqrt(5/4)/1.5, xp.nan], atol=1e-15)
102
+
103
+ @skip_xp_backends(np_only=True, reason='Python list input uses NumPy backend')
104
+ def test_axis_none(self, xp):
105
+ # Check that `variation` computes the result on the flattened
106
+ # input when axis is None.
107
+ y = variation([[0, 1], [2, 3]], axis=None)
108
+ xp_assert_close(y, math.sqrt(5/4)/1.5)
109
+
110
+ def test_bad_axis(self, xp):
111
+ # Check that an invalid axis raises np.exceptions.AxisError.
112
+ x = xp.asarray([[1, 2, 3], [4, 5, 6]])
113
+ with pytest.raises((AxisError, IndexError)):
114
+ variation(x, axis=10)
115
+
116
+ @pytest.mark.filterwarnings("ignore:divide by zero encountered:RuntimeWarning:dask")
117
+ def test_mean_zero(self, xp):
118
+ # Check that `variation` returns inf for a sequence that is not
119
+ # identically zero but whose mean is zero.
120
+ x = xp.asarray([10., -3., 1., -4., -4.])
121
+ y = variation(x)
122
+ xp_assert_equal(y, xp.asarray(xp.inf))
123
+
124
+ x2 = xp.stack([x, -10.*x])
125
+ y2 = variation(x2, axis=1)
126
+ xp_assert_equal(y2, xp.asarray([xp.inf, xp.inf]))
127
+
128
+ @pytest.mark.filterwarnings("ignore:invalid value encountered:RuntimeWarning:dask")
129
+ @pytest.mark.parametrize('x', [[0.]*5, [1, 2, np.inf, 9]])
130
+ def test_return_nan(self, x, xp):
131
+ x = xp.asarray(x)
132
+ # Test some cases where `variation` returns nan.
133
+ y = variation(x)
134
+ xp_assert_equal(y, xp.asarray(xp.nan, dtype=x.dtype))
135
+
136
+ @pytest.mark.filterwarnings('ignore:Invalid value encountered:RuntimeWarning:dask')
137
+ @pytest.mark.parametrize('axis, expected',
138
+ [(0, []), (1, [np.nan]*3), (None, np.nan)])
139
+ def test_2d_size_zero_with_axis(self, axis, expected, xp):
140
+ x = xp.empty((3, 0))
141
+ if axis != 0:
142
+ # specific message depends on `axis`, and `SmallSampleWarning`
143
+ # is specific enough.
144
+ with eager_warns(SmallSampleWarning, xp=xp):
145
+ y = variation(x, axis=axis)
146
+ else:
147
+ y = variation(x, axis=axis)
148
+ xp_assert_equal(y, xp.asarray(expected))
149
+
150
+ @pytest.mark.filterwarnings('ignore:divide by zero encountered:RuntimeWarning:dask')
151
+ def test_neg_inf(self, xp):
152
+ # Edge case that produces -inf: ddof equals the number of non-nan
153
+ # values, the values are not constant, and the mean is negative.
154
+ x1 = xp.asarray([-3., -5.])
155
+ xp_assert_equal(variation(x1, ddof=2), xp.asarray(-xp.inf))
156
+
157
+ @skip_xp_backends(np_only=True,
158
+ reason='`nan_policy` only supports NumPy backend')
159
+ def test_neg_inf_nan(self, xp):
160
+ x2 = xp.asarray([[xp.nan, 1, -10, xp.nan],
161
+ [-20, -3, xp.nan, xp.nan]])
162
+ xp_assert_equal(variation(x2, axis=1, ddof=2, nan_policy='omit'),
163
+ [-xp.inf, -xp.inf])
164
+
165
+ @skip_xp_backends(np_only=True,
166
+ reason='`nan_policy` only supports NumPy backend')
167
+ @pytest.mark.parametrize("nan_policy", ['propagate', 'omit'])
168
+ def test_combined_edge_cases(self, nan_policy, xp):
169
+ x = xp.asarray([[0, 10, xp.nan, 1],
170
+ [0, -5, xp.nan, 2],
171
+ [0, -5, xp.nan, 3]])
172
+ if nan_policy == 'omit':
173
+ with pytest.warns(SmallSampleWarning, match=too_small_nd_omit):
174
+ y = variation(x, axis=0, nan_policy=nan_policy)
175
+ else:
176
+ y = variation(x, axis=0, nan_policy=nan_policy)
177
+ xp_assert_close(y, [xp.nan, xp.inf, xp.nan, math.sqrt(2/3)/2])
178
+
179
+ @skip_xp_backends(np_only=True,
180
+ reason='`nan_policy` only supports NumPy backend')
181
+ @pytest.mark.parametrize(
182
+ 'ddof, expected',
183
+ [(0, [np.sqrt(1/6), np.sqrt(5/8), np.inf, 0, np.nan, 0.0, np.nan]),
184
+ (1, [0.5, np.sqrt(5/6), np.inf, 0, np.nan, 0, np.nan]),
185
+ (2, [np.sqrt(0.5), np.sqrt(5/4), np.inf, np.nan, np.nan, 0, np.nan])]
186
+ )
187
+ def test_more_nan_policy_omit_tests(self, ddof, expected, xp):
188
+ # The slightly strange formatting in the follow array is my attempt to
189
+ # maintain a clean tabular arrangement of the data while satisfying
190
+ # the demands of pycodestyle. Currently, E201 and E241 are not
191
+ # disabled by the `noqa` annotation.
192
+ nan = xp.nan
193
+ x = xp.asarray([[1.0, 2.0, nan, 3.0],
194
+ [0.0, 4.0, 3.0, 1.0],
195
+ [nan, -.5, 0.5, nan],
196
+ [nan, 9.0, 9.0, nan],
197
+ [nan, nan, nan, nan],
198
+ [3.0, 3.0, 3.0, 3.0],
199
+ [0.0, 0.0, 0.0, 0.0]])
200
+ with pytest.warns(SmallSampleWarning, match=too_small_nd_omit):
201
+ v = variation(x, axis=1, ddof=ddof, nan_policy='omit')
202
+ xp_assert_close(v, expected)
203
+
204
+ @skip_xp_backends(np_only=True,
205
+ reason='`nan_policy` only supports NumPy backend')
206
+ def test_variation_ddof(self, xp):
207
+ # test variation with delta degrees of freedom
208
+ # regression test for gh-13341
209
+ a = xp.asarray([1., 2., 3., 4., 5.])
210
+ nan_a = xp.asarray([1, 2, 3, xp.nan, 4, 5, xp.nan])
211
+ y = variation(a, ddof=1)
212
+ nan_y = variation(nan_a, nan_policy="omit", ddof=1)
213
+ xp_assert_close(y, math.sqrt(5/2)/3)
214
+ assert y == nan_y
loan-advisor-agent/.venv/Lib/site-packages/scipy/version.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ """
3
+ Module to expose more detailed version info for the installed `scipy`
4
+ """
5
+ version = "1.17.1"
6
+ full_version = version
7
+ short_version = version.split('.dev')[0]
8
+ git_revision = "527eb7fd7953a1de068f94bf8b322f249b9405ae"
9
+ release = 'dev' not in version and '+' not in version
10
+
11
+ if not release:
12
+ version = full_version